// Copyright HyperTwist, Inc. All Rights Reserved.

#include "Misc/AutomationTest.h"

#include "HyperTwistTraining/HyperTwistTrainingRuntimeLibrary.h"

#if WITH_AUTOMATION_TESTS

IMPLEMENT_SIMPLE_AUTOMATION_TEST(
	FHyperTwistMagicTilePhase6RTMacroRemappingContractTest,
	"HyperTwist.Permissive.MagicTile.Phase6R.T.MacroRemappingContract",
	EAutomationTestFlags::EditorContext | EAutomationTestFlags::EngineFilter
)

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

	TestTrue(TEXT("The MagicTile tiling bundle must stay structurally valid after Phase 6R-T."), Bundle.IsStructurallyValid());
	TestTrue(TEXT("The MagicTile tiling bundle must preserve multiple package references after the macro slice lands."), Bundle.PackageReferences.Num() >= 8);

	FHyperTwistTrainingTilingMacroRemappingContract Contract;
	TestTrue(
		TEXT("The primary tiling macro-remapping contract must resolve from the runtime library."),
		UHyperTwistTrainingRuntimeLibrary::TryGetBundledTilingMacroRemappingContract(
			Bundle.PrimaryMacroRemappingContractId,
			Contract
		)
	);
	TestEqual(TEXT("The macro-remapping contract id must stay stable."), Contract.ContractId, TEXT("tiling-macro-remapping-stack"));
	TestTrue(
		TEXT("The macro-remapping contract must preserve the Macro package as a required input."),
		Contract.RequiredPackageIds.Contains(TEXT("package/magictile-macro"))
	);
	TestTrue(
		TEXT("The macro-remapping contract must preserve the reference Mobius transform surface."),
		Contract.ReferenceSurfaceTags.Contains(TEXT("reference mobius transform"))
	);
	TestTrue(
		TEXT("The macro-remapping contract must preserve reflection-aware reverse handling."),
		Contract.TransformSurfaceTags.Contains(TEXT("reverse XOR view-reflection handling"))
	);
	TestTrue(
		TEXT("The macro-remapping contract must preserve commutator playback semantics."),
		Contract.PlaybackSurfaceTags.Contains(TEXT("commutator second-half playback"))
	);
	TestTrue(
		TEXT("The macro-remapping contract must keep the whole macro editor shell excluded."),
		Contract.ExplicitExclusions.Contains(TEXT("whole macro editor shell"))
	);
	TestEqual(TEXT("The macro-remapping contract must keep the direct donor source repo."), Contract.SourceAttribution.SourceRepo, TEXT("roice3/MagicTile"));
	return true;
}

IMPLEMENT_SIMPLE_AUTOMATION_TEST(
	FHyperTwistMagicTilePhase6RTMacroPersistenceBoundaryTest,
	"HyperTwist.Permissive.MagicTile.Phase6R.T.MacroPersistenceBoundary",
	EAutomationTestFlags::EditorContext | EAutomationTestFlags::EngineFilter
)

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

	FHyperTwistTrainingTilingMacroPersistenceBoundary Boundary;
	TestTrue(
		TEXT("The tiling macro persistence boundary must resolve from the runtime library."),
		UHyperTwistTrainingRuntimeLibrary::TryGetBundledTilingMacroPersistenceBoundary(
			Bundle.PrimaryMacroPersistenceBoundaryId,
			Boundary
		)
	);
	TestEqual(TEXT("The macro persistence boundary id must stay stable."), Boundary.BoundaryId, TEXT("tiling-macro-persistence-boundary"));
	TestTrue(
		TEXT("The macro persistence boundary must preserve the MagicTile macro XML root tag."),
		Boundary.PersistenceSurfaceTags.Contains(TEXT("MagicTileMacros XML root"))
	);
	TestTrue(
		TEXT("The macro persistence boundary must preserve active-puzzle compatibility checks."),
		Boundary.CompatibilitySurfaceTags.Contains(TEXT("active puzzle id compatibility"))
	);
	TestTrue(
		TEXT("The macro persistence boundary must preserve puzzle-specific macro caveats."),
		Boundary.ProvenanceCaveats.Contains(TEXT("macro files are puzzle-specific and are not intended to travel across differently configured puzzles"))
	);
	TestTrue(
		TEXT("The macro persistence boundary must keep canonical replay ownership excluded."),
		Boundary.ExplicitExclusions.Contains(TEXT("generic replay or canonical notation ownership"))
	);
	return true;
}

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

bool FHyperTwistMagicTilePhase6RTPackageChecklistTest::RunTest(const FString& Parameters)
{
	const FString Checklist = UHyperTwistTrainingRuntimeLibrary::BuildBundledTilingPackageChecklistTsv();
	TestTrue(TEXT("The tiling package checklist must not be empty after Phase 6R-T."), !Checklist.IsEmpty());
	TestTrue(
		TEXT("The checklist must preserve the macro package id."),
		Checklist.Contains(TEXT("package/magictile-macro"))
	);
	TestTrue(
		TEXT("The checklist must preserve the Macro export path."),
		Checklist.Contains(TEXT("./MagicTile/Puzzle/Twisting/Macro.cs"))
	);
	TestTrue(
		TEXT("The checklist must preserve the twist-handler package id."),
		Checklist.Contains(TEXT("package/magictile-twist-handler"))
	);
	TestTrue(
		TEXT("The checklist must preserve the direct permissive donor posture."),
		Checklist.Contains(TEXT("direct permissive donor"))
	);
	return true;
}

#endif
