// Copyright HyperTwist, Inc. All Rights Reserved.

#include "Misc/AutomationTest.h"

#include "HyperTwistTraining/HyperTwistTrainingRuntimeLibrary.h"

#if WITH_AUTOMATION_TESTS

IMPLEMENT_SIMPLE_AUTOMATION_TEST(
	FHyperTwistMagicTilePhase6RDTopologyContractTest,
	"HyperTwist.Permissive.MagicTile.Phase6R.D.TopologyContract",
	EAutomationTestFlags::EditorContext | EAutomationTestFlags::EngineFilter
)

bool FHyperTwistMagicTilePhase6RDTopologyContractTest::RunTest(const FString& Parameters)
{
	const FHyperTwistTrainingTilingReferenceBundle Bundle =
		UHyperTwistTrainingRuntimeLibrary::GetBundledTilingReferenceBundle();

	TestTrue(TEXT("The MagicTile tiling bundle must be structurally valid."), Bundle.IsStructurallyValid());
	TestTrue(TEXT("The MagicTile tiling bundle must preserve multiple package references."), Bundle.PackageReferences.Num() >= 5);

	FHyperTwistTrainingTilingTopologyContract Contract;
	TestTrue(
		TEXT("The primary tiling topology contract must resolve from the runtime library."),
		UHyperTwistTrainingRuntimeLibrary::TryGetBundledTilingTopologyContract(
			Bundle.PrimaryTopologyContractId,
			Contract
		)
	);
	TestEqual(TEXT("The topology contract id must stay stable."), Contract.ContractId, TEXT("tiling-topology-stack"));
	TestTrue(
		TEXT("The topology contract must preserve the topology-analyzer package as a required input."),
		Contract.RequiredPackageIds.Contains(TEXT("package/magictile-topology-analyzer"))
	);
	TestTrue(
		TEXT("The topology contract must preserve hyperbolic-family support as a geometry surface."),
		Contract.GeometrySurfaceTags.Contains(TEXT("hyperbolic honeycomb family tags"))
	);
	TestTrue(
		TEXT("The topology contract must keep Hyperspeedcube replacement explicitly excluded."),
		Contract.ExplicitExclusions.Contains(TEXT("Hyperspeedcube runtime-base replacement"))
	);
	TestEqual(TEXT("The topology contract must keep the direct donor source repo."), Contract.SourceAttribution.SourceRepo, TEXT("roice3/MagicTile"));
	return true;
}

IMPLEMENT_SIMPLE_AUTOMATION_TEST(
	FHyperTwistMagicTilePhase6RDLoaderBoundaryTest,
	"HyperTwist.Permissive.MagicTile.Phase6R.D.LoaderBoundary",
	EAutomationTestFlags::EditorContext | EAutomationTestFlags::EngineFilter
)

bool FHyperTwistMagicTilePhase6RDLoaderBoundaryTest::RunTest(const FString& Parameters)
{
	const FHyperTwistTrainingTilingReferenceBundle Bundle =
		UHyperTwistTrainingRuntimeLibrary::GetBundledTilingReferenceBundle();

	FHyperTwistTrainingTilingLoaderBoundary Boundary;
	TestTrue(
		TEXT("The tiling loader boundary must resolve from the runtime library."),
		UHyperTwistTrainingRuntimeLibrary::TryGetBundledTilingLoaderBoundary(
			Bundle.PrimaryLoaderBoundaryId,
			Boundary
		)
	);
	TestEqual(TEXT("The loader boundary id must stay stable."), Boundary.BoundaryId, TEXT("tiling-loader-boundary"));
	TestTrue(
		TEXT("The loader boundary must preserve generalized puzzle loading as a loader surface."),
		Boundary.LoaderSurfaceTags.Contains(TEXT("generalized puzzle loading"))
	);
	TestTrue(
		TEXT("The loader boundary must preserve the Hyperspeedcube runtime-anchor separation."),
		Boundary.RuntimeBoundaryTags.Contains(
			TEXT("Hyperspeedcube runtime anchor remains primary for runtime catalog notation replay and verification")
		)
	);
	TestTrue(
		TEXT("The loader boundary must keep legacy host-shell ownership explicitly excluded."),
		Boundary.ExplicitExclusions.Contains(TEXT("whole WinForms/OpenTK shell ownership"))
	);
	return true;
}

IMPLEMENT_SIMPLE_AUTOMATION_TEST(
	FHyperTwistMagicTilePhase6RDPackageChecklistTest,
	"HyperTwist.Permissive.MagicTile.Phase6R.D.PackageChecklist",
	EAutomationTestFlags::EditorContext | EAutomationTestFlags::EngineFilter
)

bool FHyperTwistMagicTilePhase6RDPackageChecklistTest::RunTest(const FString& Parameters)
{
	const FString Checklist = UHyperTwistTrainingRuntimeLibrary::BuildBundledTilingPackageChecklistTsv();
	TestTrue(TEXT("The tiling package checklist must not be empty."), !Checklist.IsEmpty());
	TestTrue(
		TEXT("The checklist must preserve the topology-analyzer package id."),
		Checklist.Contains(TEXT("package/magictile-topology-analyzer"))
	);
	TestTrue(
		TEXT("The checklist must preserve the TopologyAnalyzer export path."),
		Checklist.Contains(TEXT("./MagicTile/Puzzle/TopologyAnalyzer.cs"))
	);
	TestTrue(
		TEXT("The checklist must preserve the direct permissive donor posture."),
		Checklist.Contains(TEXT("direct permissive donor"))
	);
	return true;
}

#endif
