Files
aetherbound-guild/runtime/tests/fg4/rg04_contract2_activation_test.gd
T

112 lines
5.5 KiB
GDScript

extends SceneTree
const Activation = preload("res://fg4/rg04_contract2_activation_service.gd")
const Fixture = preload("res://tests/fg4/contract1_fixture.gd")
const State = preload("res://fg4/run_state.gd")
var failures: Array[String] = []
var rejected_cases := 0
func _initialize() -> void:
call_deferred("run_test")
func run_test() -> void:
verify_exact_activation_and_preservation()
verify_activation_boundaries()
if failures.is_empty():
print("ABG_FG4_RG04_CONTRACT2_ACTIVATION_OK source=CLEAR-RG04-C01 contract=CTR-R4-02 phase=rg04_contract2_market market=1 board=3,4,3 prior_routes=60 company,growth,hp,equipment,economy,artifact,records,prior_receipts=exact duplicate=true save_version=11 rejected_cases=%d" % rejected_cases)
quit(0)
else:
for failure in failures:
push_error(failure)
quit(1)
func verify_exact_activation_and_preservation() -> void:
var before := Fixture.new().rg04_contract2_handoff_snapshot()
expect(not before.is_empty(), "RG-04 Contract-2 handoff fixture exists")
var service := Activation.new(before)
expect(service.valid and service.state.current_run.phase == "rg04_contract2_handoff", "exact handoff restores")
if not service.valid:
return
var handoff_hash := String(service.state.current_run.market_board.handoff_hash)
var revision_before := service.state.revision
var preserved_before := preserved_projection(service.state.snapshot())
var prior_receipts := Dictionary(service.state.operation_receipts).duplicate(true)
var activated := service.activate_contract(
"fg4-rg04-contract2-activate", revision_before, handoff_hash
)
expect(activated.ok and not activated.duplicate, "RG-04 Contract 2 activates exactly once")
if not activated.ok:
return
var run: Dictionary = service.state.current_run
expect(run.region_id == "RG-04" and run.contract_id == "CTR-R4-02", "Region and Contract identity are exact")
expect(run.phase == "rg04_contract2_market" and run.market_index == 1 and run.round_index == 1, "Contract-2 Market 1 is active")
expect(run.cleared_rounds == 60 and run.route_history.size() == 60, "sixty prior routes remain exact")
expect(run.market_board.recruit_slots.size() == 3 and run.market_board.equipment_offers.size() == 4 and run.market_board.battle_offers.size() == 3, "Market 1 has exact 3/4/3 board")
expect(run.market_board.archive_clause.kind in ["repeat", "alternate", "preserve"], "archive clause remains disclosed")
expect(not String(run.market_board.copy_disclosure.source_rule).is_empty(), "redaction source remains disclosed")
expect(preserved_projection(service.state.snapshot()) == preserved_before, "activation preserves carried consequences byte-equivalent")
for operation_id in prior_receipts:
expect(service.state.operation_receipts.get(operation_id) == prior_receipts[operation_id], "prior receipt %s is byte-equivalent" % operation_id)
var duplicate := service.activate_contract(
"fg4-rg04-contract2-activate", revision_before, handoff_hash
)
expect(duplicate.ok and duplicate.duplicate and duplicate.revision == activated.revision, "duplicate activation is inert")
var roundtrip := State.new()
expect(roundtrip.restore_snapshot(service.state.snapshot()) and roundtrip.snapshot() == service.state.snapshot(), "activated state roundtrips exactly")
func verify_activation_boundaries() -> void:
var before := Fixture.new().rg04_contract2_handoff_snapshot()
if before.is_empty():
return
var service := Activation.new(before)
var original := service.state.snapshot()
var handoff_hash := String(service.state.current_run.market_board.handoff_hash)
expect_rejected(service.activate_contract("fg4-rg04-c2-stale", service.state.revision - 1, handoff_hash), "stale revision", "stale_revision")
expect_rejected(service.activate_contract("fg4-rg04-c2-bad-hash", service.state.revision, "0".repeat(64)), "handoff hash tamper", "handoff_hash_mismatch")
expect(service.state.snapshot() == original, "rejected activation does not mutate state")
var invalid := before.duplicate(true)
invalid.current_run.market_board.handoff_hash = "0".repeat(64)
expect(not State.new().restore_snapshot(invalid), "tampered source state rejects before activation")
rejected_cases += 1
var wrong_phase := Fixture.new().rg04_market_1_snapshot()
var wrong_service := Activation.new(wrong_phase)
expect_rejected(wrong_service.activate_contract("fg4-rg04-c2-wrong-phase", wrong_service.state.revision, "0".repeat(64)), "non-handoff source", "rg04_contract2_handoff_required")
func preserved_projection(snapshot_value: Dictionary) -> Dictionary:
var run: Dictionary = snapshot_value.current_run
return {
"run_id": run.run_id,
"run_seed": run.run_seed,
"company": run.company,
"deployed": run.deployed_recruit_ids,
"reserve": run.reserve_recruit_ids,
"recruit_records": run.recruit_records,
"inventory": run.inventory,
"equipped": run.equipped_by_recruit,
"coin": run.coin,
"credit": run.replacement_credit,
"artifacts": run.active_artifact_ids,
"history": run.route_history,
"guild_profile": snapshot_value.guild_profile,
"legacy_operation_receipts": snapshot_value.legacy_operation_receipts,
"legacy_entry_commits": snapshot_value.legacy_entry_commits,
}
func expect_rejected(result: Dictionary, label: String, code: String) -> void:
var rejected := not bool(result.get("ok", false)) and String(result.get("code", "")) == code
expect(rejected, "%s rejects with %s; got %s" % [label, code, str(result)])
if rejected:
rejected_cases += 1
func expect(condition: bool, message: String) -> void:
if not condition:
failures.append("FG-4 RG-04 Contract-2 activation: %s" % message)