// Copyright HyperTwist, Inc. All Rights Reserved.

#include "Misc/AutomationTest.h"

#include "HyperTwistTraining/HyperTwistTrainingRuntimeLibrary.h"

#if WITH_AUTOMATION_TESTS

IMPLEMENT_SIMPLE_AUTOMATION_TEST(
	FHyperTwistHyperspeedcubePhase6RLReplayVerificationContractTest,
	"HyperTwist.Permissive.Hyperspeedcube.Phase6R.L.ReplayVerificationContract",
	EAutomationTestFlags::EditorContext | EAutomationTestFlags::EngineFilter
)

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

	TestTrue(TEXT("The Hyperspeedcube bundle must stay structurally valid."), Bundle.IsStructurallyValid());
	TestTrue(TEXT("The Hyperspeedcube bundle must preserve the expanded donor package set."), Bundle.PackageReferences.Num() >= 9);

	FHyperTwistTrainingHyperspeedcubeReplayVerificationContract Contract;
	TestTrue(
		TEXT("The primary Hyperspeedcube replay verification contract must resolve from the runtime library."),
		UHyperTwistTrainingRuntimeLibrary::TryGetBundledHyperspeedcubeReplayVerificationContract(
			Bundle.PrimaryReplayVerificationContractId,
			Contract
		)
	);
	TestEqual(
		TEXT("The replay verification contract id must stay stable."),
		Contract.ContractId,
		TEXT("hyperspeedcube-replay-verification-contract")
	);
	TestTrue(
		TEXT("The replay verification contract must require the retained verifier package."),
		Contract.RequiredPackageIds.Contains(TEXT("package/hyperspeedcube-log-verifier"))
	);
	TestTrue(
		TEXT("The replay verification contract must preserve scramble seed verification."),
		Contract.VerificationSurfaceTags.Contains(TEXT("scramble seed verification"))
	);
	TestTrue(
		TEXT("The replay verification contract must preserve replay event reconstruction."),
		Contract.ReplaySurfaceTags.Contains(TEXT("replay event reconstruction"))
	);
	TestTrue(
		TEXT("The replay verification contract must preserve verified completion timestamps."),
		Contract.TimestampSurfaceTags.Contains(TEXT("verified completion timestamp"))
	);
	TestTrue(
		TEXT("The replay verification contract must keep broader stats-shape ownership excluded."),
		Contract.ExplicitExclusions.Contains(TEXT("broader stats-shape ownership"))
	);
	TestEqual(
		TEXT("The replay verification contract must keep the direct donor source repo."),
		Contract.SourceAttribution.SourceRepo,
		TEXT("HactarCE/Hyperspeedcube")
	);
	return true;
}

IMPLEMENT_SIMPLE_AUTOMATION_TEST(
	FHyperTwistHyperspeedcubePhase6RLReplayVerificationBoundaryTest,
	"HyperTwist.Permissive.Hyperspeedcube.Phase6R.L.ReplayVerificationBoundary",
	EAutomationTestFlags::EditorContext | EAutomationTestFlags::EngineFilter
)

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

	FHyperTwistTrainingHyperspeedcubeReplayVerificationBoundary Boundary;
	TestTrue(
		TEXT("The Hyperspeedcube replay verification boundary must resolve from the runtime library."),
		UHyperTwistTrainingRuntimeLibrary::TryGetBundledHyperspeedcubeReplayVerificationBoundary(
			Bundle.PrimaryReplayVerificationBoundaryId,
			Boundary
		)
	);
	TestEqual(
		TEXT("The replay verification boundary id must stay stable."),
		Boundary.BoundaryId,
		TEXT("hyperspeedcube-replay-verification-boundary")
	);
	TestTrue(
		TEXT("The replay verification boundary must preserve session-end append on replay export."),
		Boundary.ReplayEventSurfaceTags.Contains(TEXT("session-end append on replay export"))
	);
	TestTrue(
		TEXT("The replay verification boundary must preserve the used_macros diagnostic."),
		Boundary.DiagnosticsSurfaceTags.Contains(TEXT("used_macros flag"))
	);
	TestTrue(
		TEXT("The replay verification boundary must preserve the replay export caveat."),
		Boundary.ProvenanceCaveats.Contains(TEXT("replay export appends an EndSession marker on the replay path"))
	);
	TestTrue(
		TEXT("The replay verification boundary must keep the broader review shell excluded."),
		Boundary.ExplicitExclusions.Contains(TEXT("broader review-shell ownership"))
	);
	return true;
}

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

bool FHyperTwistHyperspeedcubePhase6RLPackageChecklistTest::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 replay verifier package id."),
		Checklist.Contains(TEXT("package/hyperspeedcube-log-verifier"))
	);
	TestTrue(
		TEXT("The checklist must preserve the replay simulation export path."),
		Checklist.Contains(TEXT("./crates/hyperpuzzle_view/src/simulation/mod.rs"))
	);
	TestTrue(
		TEXT("The checklist must preserve the direct dual-license donor posture."),
		Checklist.Contains(TEXT("direct dual-license donor"))
	);
	return true;
}

#endif
