Files
aetherbound-guild/runtime/tests/p10_1/first_crossing_resolution_test.gd
T

83 lines
5.1 KiB
GDScript

extends SceneTree
const SaveV7 = preload("res://p10_1/save_v7_service.gd")
const BattleService = preload("res://p10_1/first_crossing_battle_service.gd")
const ResolutionService = preload("res://p10_1/first_crossing_resolution_service.gd")
const Fixture = preload("res://tests/p10_1/first_crossing_fixture.gd")
var failures: Array[String] = []
func _initialize() -> void:
call_deferred("run_test")
func run_test() -> void:
verify_victory_and_market_2()
verify_defeat_and_recovery()
verify_atomic_boundaries()
if failures.is_empty():
print("ABG_P10_1_FIRST_CROSSING_RESOLUTION_OK victory=reward_once,market2_saved defeat=casualty_permanent,recovery_bounded duplicate=true save_v7=roundtrip atomic=true")
quit(0)
else:
for failure in failures:
push_error(failure)
quit(1)
func verify_victory_and_market_2() -> void:
var fixture = Fixture.new()
var prepared: Dictionary = fixture.prepared_snapshot(0)
var battle: Dictionary = BattleService.new(prepared).run_to_terminal("hold_line").snapshot
var resolution = ResolutionService.new(prepared)
var revision_before := int(resolution.state.revision)
var settled: Dictionary = resolution.settle("settle-victory", revision_before, battle)
expect(settled.ok and resolution.state.revision == revision_before + 1, "victory settles exactly one revision")
expect(resolution.state.current_run.phase == "market_2" and resolution.state.current_run.market_index == 2, "victory reaches saved Market 2")
expect(resolution.state.current_run.market_2_handoff.source_battle_hash == battle.snapshot_hash, "Market 2 handoff binds battle")
expect(settled.receipt.reward_coin == battle.reward_coin and resolution.state.current_run.coin == int(prepared.current_run.coin) + int(battle.reward_coin), "victory grants exact route reward once")
var after: Dictionary = resolution.state.snapshot()
var duplicate: Dictionary = resolution.settle("settle-victory", revision_before, battle)
expect(duplicate.ok and duplicate.duplicate and resolution.state.snapshot() == after, "victory retry cannot duplicate reward or Market advance")
var saved: Dictionary = SaveV7.new().encode_snapshot(after, 20)
expect(saved.ok and SaveV7.new().decode_text(String(saved.text), 20).snapshot == after, "saved Market 2 roundtrips through Save v7")
func verify_defeat_and_recovery() -> void:
var fixture = Fixture.new()
var prepared: Dictionary = fixture.prepared_snapshot(2)
var battle: Dictionary = BattleService.new(prepared).run_to_terminal("hold_line").snapshot
expect(battle.outcome == "defeat" and not battle.fallen_recruit_ids.is_empty(), "high-route hold fixture defeats with a casualty")
var resolution = ResolutionService.new(prepared)
var settled: Dictionary = resolution.settle("settle-defeat", resolution.state.revision, battle)
expect(settled.ok and resolution.state.current_run.phase == "recovery" and resolution.state.current_run.recovery_required, "defeat enters explicit recovery")
for fallen_id in battle.fallen_recruit_ids:
expect(fallen_id not in resolution.state.current_run.company and not resolution.state.current_run.recruit_records.has(fallen_id), "Fallen Recruit is permanently removed")
var survivors_before: Array = Array(resolution.state.current_run.company).duplicate()
var coin_before := int(resolution.state.current_run.coin)
var recovered: Dictionary = resolution.recover("recover-defeat", resolution.state.revision, String(battle.snapshot_hash), int(battle.recovery_coin))
expect(recovered.ok and resolution.state.current_run.phase == "market" and not resolution.state.current_run.recovery_required, "recovery returns to changed Market 1")
expect(resolution.state.current_run.company == survivors_before and resolution.state.current_run.coin == coin_before - int(battle.recovery_coin), "recovery charges exact cost without resurrection")
var after: Dictionary = resolution.state.snapshot()
var duplicate: Dictionary = resolution.recover("recover-defeat", int(settled.revision), String(battle.snapshot_hash), int(battle.recovery_coin))
expect(duplicate.ok and duplicate.duplicate and resolution.state.snapshot() == after, "recovery retry is idempotent")
func verify_atomic_boundaries() -> void:
var fixture = Fixture.new()
var prepared: Dictionary = fixture.prepared_snapshot(0)
var battle: Dictionary = BattleService.new(prepared).run_to_terminal("hold_line").snapshot
var resolution = ResolutionService.new(prepared)
var before: Dictionary = resolution.state.snapshot()
var tampered: Dictionary = battle.duplicate(true)
tampered.reward_coin = 999
var rejected: Dictionary = resolution.settle("tampered", resolution.state.revision, tampered)
expect(not rejected.ok and rejected.code == "battle_hash_mismatch" and resolution.state.snapshot() == before, "tampered battle rejects without mutation")
var stale: Dictionary = resolution.settle("stale", resolution.state.revision + 1, battle)
expect(not stale.ok and stale.code == "stale_revision" and resolution.state.snapshot() == before, "stale settlement rejects without mutation")
func expect(condition: bool, message: String) -> void:
if not condition:
failures.append("P10.1 first-crossing resolution: %s" % message)