// Copyright HyperTwist, Inc. All Rights Reserved.

#include "Misc/AutomationTest.h"

#include "HyperTwistTraining/HyperTwistTrainingRuntimeLibrary.h"

#if WITH_AUTOMATION_TESTS

IMPLEMENT_SIMPLE_AUTOMATION_TEST(
	FHyperTwistMagic120CellPhase6RIRuntimeProfileTest,
	"HyperTwist.Permissive.Magic120Cell.Phase6R.I.RuntimeProfile",
	EAutomationTestFlags::EditorContext | EAutomationTestFlags::EngineFilter
)

bool FHyperTwistMagic120CellPhase6RIRuntimeProfileTest::RunTest(const FString& Parameters)
{
	const FHyperTwistTrainingMagic120CellReferenceBundle Bundle =
		UHyperTwistTrainingRuntimeLibrary::GetBundledMagic120CellReferenceBundle();

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

	FHyperTwistTrainingMagic120CellRuntimeProfileContract Contract;
	TestTrue(
		TEXT("The primary Magic120Cell runtime profile must resolve from the runtime library."),
		UHyperTwistTrainingRuntimeLibrary::TryGetBundledMagic120CellRuntimeProfileContract(
			Bundle.PrimaryRuntimeProfileContractId,
			Contract
		)
	);
	TestEqual(TEXT("The runtime profile id must stay stable."), Contract.ContractId, TEXT("magic120cell-runtime-profile"));
	TestTrue(
		TEXT("The runtime profile must keep the family-core package as a required input."),
		Contract.RequiredPackageIds.Contains(TEXT("package/magic120cell-family-core"))
	);
	TestTrue(
		TEXT("The runtime profile must preserve hypercube visibility groups."),
		Contract.SymmetrySurfaceTags.Contains(TEXT("hypercube visibility groups"))
	);
	TestTrue(
		TEXT("The runtime profile must preserve logical layer-depth visibility."),
		Contract.VisibilitySurfaceTags.Contains(TEXT("logical layer-depth visibility"))
	);
	TestTrue(
		TEXT("The runtime profile must keep generic non-Euclidean topology ownership excluded."),
		Contract.ExplicitExclusions.Contains(TEXT("generic non-Euclidean topology ownership"))
	);
	TestEqual(TEXT("The runtime profile must keep the direct donor source repo."), Contract.SourceAttribution.SourceRepo, TEXT("roice3/Magic120Cell"));
	return true;
}

IMPLEMENT_SIMPLE_AUTOMATION_TEST(
	FHyperTwistMagic120CellPhase6RIPersistenceBoundaryTest,
	"HyperTwist.Permissive.Magic120Cell.Phase6R.I.PersistenceBoundary",
	EAutomationTestFlags::EditorContext | EAutomationTestFlags::EngineFilter
)

bool FHyperTwistMagic120CellPhase6RIPersistenceBoundaryTest::RunTest(const FString& Parameters)
{
	const FHyperTwistTrainingMagic120CellReferenceBundle Bundle =
		UHyperTwistTrainingRuntimeLibrary::GetBundledMagic120CellReferenceBundle();

	FHyperTwistTrainingMagic120CellPersistenceBoundary Boundary;
	TestTrue(
		TEXT("The Magic120Cell persistence boundary must resolve from the runtime library."),
		UHyperTwistTrainingRuntimeLibrary::TryGetBundledMagic120CellPersistenceBoundary(
			Bundle.PrimaryPersistenceBoundaryId,
			Boundary
		)
	);
	TestEqual(TEXT("The persistence boundary id must stay stable."), Boundary.BoundaryId, TEXT("magic120cell-persistence-boundary"));
	TestTrue(
		TEXT("The persistence boundary must preserve the Magic120Cell Version1 header tag."),
		Boundary.PersistenceSurfaceTags.Contains(TEXT("Magic120Cell Version1 header"))
	);
	TestTrue(
		TEXT("The persistence boundary must preserve twist-hash serialization."),
		Boundary.TwistHistorySurfaceTags.Contains(TEXT("twist-hash serialization"))
	);
	TestTrue(
		TEXT("The persistence boundary must preserve the slicemask provenance caveat."),
		Boundary.ProvenanceCaveats.Contains(TEXT("slice mask is not persisted in twist hash"))
	);
	TestTrue(
		TEXT("The persistence boundary must keep canonical replay ownership excluded."),
		Boundary.ExplicitExclusions.Contains(TEXT("first-party canonical replay or verification ownership"))
	);
	return true;
}

IMPLEMENT_SIMPLE_AUTOMATION_TEST(
	FHyperTwistMagic120CellPhase6RIPackageChecklistTest,
	"HyperTwist.Permissive.Magic120Cell.Phase6R.I.PackageChecklist",
	EAutomationTestFlags::EditorContext | EAutomationTestFlags::EngineFilter
)

bool FHyperTwistMagic120CellPhase6RIPackageChecklistTest::RunTest(const FString& Parameters)
{
	const FString Checklist = UHyperTwistTrainingRuntimeLibrary::BuildBundledMagic120CellPackageChecklistTsv();
	TestTrue(TEXT("The Magic120Cell package checklist must not be empty."), !Checklist.IsEmpty());
	TestTrue(
		TEXT("The checklist must preserve the family-core package id."),
		Checklist.Contains(TEXT("package/magic120cell-family-core"))
	);
	TestTrue(
		TEXT("The checklist must preserve the loader export path."),
		Checklist.Contains(TEXT("./Magic120Cell/workFiles/loader.cpp"))
	);
	TestTrue(
		TEXT("The checklist must preserve the direct permissive donor posture."),
		Checklist.Contains(TEXT("direct permissive donor"))
	);
	return true;
}

#endif
