// Copyright HyperTwist, Inc. All Rights Reserved.

#include "Misc/AutomationTest.h"

#include "HyperTwistTraining/HyperTwistTrainingRuntimeLibrary.h"

#if WITH_AUTOMATION_TESTS

IMPLEMENT_SIMPLE_AUTOMATION_TEST(
	FHyperTwistHyperspeedcubePhase6RKNotationContractTest,
	"HyperTwist.Permissive.Hyperspeedcube.Phase6R.K.NotationContract",
	EAutomationTestFlags::EditorContext | EAutomationTestFlags::EngineFilter
)

bool FHyperTwistHyperspeedcubePhase6RKNotationContractTest::RunTest(const FString& Parameters)
{
	const FHyperTwistTrainingHyperspeedcubeReferenceBundle Bundle =
		UHyperTwistTrainingRuntimeLibrary::GetBundledHyperspeedcubeReferenceBundle();

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

	FHyperTwistTrainingHyperspeedcubeNotationContract Contract;
	TestTrue(
		TEXT("The primary Hyperspeedcube notation contract must resolve from the runtime library."),
		UHyperTwistTrainingRuntimeLibrary::TryGetBundledHyperspeedcubeNotationContract(
			Bundle.PrimaryNotationContractId,
			Contract
		)
	);
	TestEqual(TEXT("The notation contract id must stay stable."), Contract.ContractId, TEXT("hyperspeedcube-notation-contract"));
	TestTrue(
		TEXT("The notation contract must require the retained notation parser package."),
		Contract.RequiredPackageIds.Contains(TEXT("package/hyperspeedcube-notation-parser"))
	);
	TestTrue(
		TEXT("The notation contract must preserve layer-prefix parsing."),
		Contract.ParsingSurfaceTags.Contains(TEXT("layer-prefix parse"))
	);
	TestTrue(
		TEXT("The notation contract must preserve node-list display round-trip."),
		Contract.CanonicalizationSurfaceTags.Contains(TEXT("node-list display round-trip"))
	);
	TestTrue(
		TEXT("The notation contract must keep puzzle-definition DSL ownership excluded."),
		Contract.ExplicitExclusions.Contains(TEXT("puzzle-definition DSL ownership"))
	);
	TestEqual(
		TEXT("The notation contract must keep the direct donor source repo."),
		Contract.SourceAttribution.SourceRepo,
		TEXT("HactarCE/Hyperspeedcube")
	);
	return true;
}

IMPLEMENT_SIMPLE_AUTOMATION_TEST(
	FHyperTwistHyperspeedcubePhase6RKSerializationBoundaryTest,
	"HyperTwist.Permissive.Hyperspeedcube.Phase6R.K.SerializationBoundary",
	EAutomationTestFlags::EditorContext | EAutomationTestFlags::EngineFilter
)

bool FHyperTwistHyperspeedcubePhase6RKSerializationBoundaryTest::RunTest(const FString& Parameters)
{
	const FHyperTwistTrainingHyperspeedcubeReferenceBundle Bundle =
		UHyperTwistTrainingRuntimeLibrary::GetBundledHyperspeedcubeReferenceBundle();

	FHyperTwistTrainingHyperspeedcubeSerializationBoundary Boundary;
	TestTrue(
		TEXT("The Hyperspeedcube serialization boundary must resolve from the runtime library."),
		UHyperTwistTrainingRuntimeLibrary::TryGetBundledHyperspeedcubeSerializationBoundary(
			Bundle.PrimarySerializationBoundaryId,
			Boundary
		)
	);
	TestEqual(
		TEXT("The serialization boundary id must stay stable."),
		Boundary.BoundaryId,
		TEXT("hyperspeedcube-log-serialization-boundary")
	);
	TestTrue(
		TEXT("The serialization boundary must preserve versioned log-file deserialize."),
		Boundary.SerializationSurfaceTags.Contains(TEXT("versioned log-file deserialize"))
	);
	TestTrue(
		TEXT("The serialization boundary must preserve migration warning collection."),
		Boundary.WarningSurfaceTags.Contains(TEXT("migration warning collection"))
	);
	TestTrue(
		TEXT("The serialization boundary must preserve the first-solve caveat."),
		Boundary.ProvenanceCaveats.Contains(TEXT("the active donor app load path only consumes the first solve from a log bundle"))
	);
	TestTrue(
		TEXT("The serialization boundary must keep canonical replay verification excluded."),
		Boundary.ExplicitExclusions.Contains(TEXT("first-party canonical replay verification ownership"))
	);
	return true;
}

IMPLEMENT_SIMPLE_AUTOMATION_TEST(
	FHyperTwistHyperspeedcubePhase6RKPackageChecklistTest,
	"HyperTwist.Permissive.Hyperspeedcube.Phase6R.K.PackageChecklist",
	EAutomationTestFlags::EditorContext | EAutomationTestFlags::EngineFilter
)

bool FHyperTwistHyperspeedcubePhase6RKPackageChecklistTest::RunTest(const FString& Parameters)
{
	const FString Checklist = UHyperTwistTrainingRuntimeLibrary::BuildBundledHyperspeedcubePackageChecklistTsv();
	TestTrue(TEXT("The Hyperspeedcube package checklist must not be empty."), !Checklist.IsEmpty());
	TestTrue(
		TEXT("The checklist must preserve the notation parser package id."),
		Checklist.Contains(TEXT("package/hyperspeedcube-notation-parser"))
	);
	TestTrue(
		TEXT("The checklist must preserve the retained hyperpuzzle_log export path."),
		Checklist.Contains(TEXT("./crates/hyperpuzzle_log/src/lib.rs"))
	);
	TestTrue(
		TEXT("The checklist must preserve the direct dual-license donor posture."),
		Checklist.Contains(TEXT("direct dual-license donor"))
	);
	return true;
}

#endif
