Files
aetherbound-guild/runtime/tests/p10_0/boss_contract_close_test.gd
T

198 lines
14 KiB
GDScript

extends SceneTree
const MarketService = preload("res://p10_0/market_service.gd")
const ContractService = preload("res://p10_0/contract_service.gd")
const BossBattleService = preload("res://p10_0/boss_battle_service.gd")
var failures: Array[String] = []
func _initialize() -> void:
call_deferred("run_test")
func run_test() -> void:
verify_both_bosses_and_plan_counterfactuals()
verify_boss_recovery_and_muster_identity()
verify_two_contract_closes()
verify_atomic_boundaries()
if failures.is_empty():
print("ABG_P10_0_BOSS_CONTRACT_CLOSE_OK bosses=BO-R1-01,BO-R1-02 plans=2_each deterministic=true muster=stable casualty_recovery=true clears=C01,C02 renown=14,18 records=2 rg02=visible duplicate=true atomic=true")
quit(0)
else:
for failure in failures:
push_error(failure)
quit(1)
func verify_both_bosses_and_plan_counterfactuals() -> void:
for contract_id in ["CTR-R1-01", "CTR-R1-02"]:
var service = boss_ready_service(contract_id, true, false)
var contract = ContractService.new(service.state.snapshot())
var entered = contract.enter_boss_muster("muster-%s" % contract_id, contract.state.revision)
expect(entered.ok and entered.receipt.plans.size() == 2, "%s opens two disclosed boss plans" % contract_id)
var results: Array[Dictionary] = []
for plan_id in entered.receipt.plans:
var branch = ContractService.new(contract.state.snapshot())
expect(branch.commit_boss_plan("plan-%s-%s" % [contract_id, plan_id], branch.state.revision, String(branch.state.current_run.boss_muster.muster_hash), String(plan_id)).ok, "%s commits plan %s" % [contract_id, plan_id])
var first = BossBattleService.new(branch.state.snapshot())
var second = BossBattleService.new(branch.state.snapshot())
var first_result: Dictionary = first.run_to_terminal().snapshot
var second_result: Dictionary = second.run_to_terminal().snapshot
expect(first_result == second_result, "%s %s is byte deterministic" % [contract_id, plan_id])
expect(first_result.terminal and first_result.outcome == "victory" and int(first_result.tick) <= 1500, "%s %s reaches bounded victory" % [contract_id, plan_id])
expect(not first_result.first_decisive_event.is_empty(), "%s %s retains a decisive event" % [contract_id, plan_id])
results.append(first_result)
expect(String(results[0].snapshot_hash) != String(results[1].snapshot_hash), "%s plan choice changes authoritative events/results" % contract_id)
if contract_id == "CTR-R1-01":
expect(results.any(func(result): return int(result.pane_rotations) > 0 and int(result.sweep_count) > 0), "Stag resolves pane rotation and antler sweep")
else:
expect(results.any(func(result): return int(result.gust_count) > 0) and results.any(func(result): return result.event_log.any(func(event): return event.kind == "spindle_reversed")), "Gale Loom resolves gust and spindle reversal")
func verify_boss_recovery_and_muster_identity() -> void:
var service = boss_ready_service("CTR-R1-01", false, true)
var contract = ContractService.new(service.state.snapshot())
expect(contract.enter_boss_muster("recovery-muster", contract.state.revision).ok, "recovery fixture enters Muster")
var muster_id := String(contract.state.current_run.boss_muster.muster_id)
var plan_id := String(contract.state.current_run.boss_muster.plans[0])
expect(contract.commit_boss_plan("recovery-plan", contract.state.revision, String(contract.state.current_run.boss_muster.muster_hash), plan_id).ok, "recovery fixture commits boss plan")
var terminal: Dictionary = BossBattleService.new(contract.state.snapshot()).run_to_terminal().snapshot
expect(terminal.outcome == "defeat" and not terminal.fallen_recruit_ids.is_empty(), "fragile deployed line loses with permanent casualties")
var settled = contract.settle_boss("recovery-settle", contract.state.revision, String(terminal.snapshot_hash), terminal)
expect(settled.ok and String(contract.state.current_run.phase) == "market", "casualty defeat reopens preparation Market")
expect(contract.state.current_run.recovery_required and not String(contract.state.current_run.protected_recovery_offer_id).is_empty(), "boss casualties expose protected recovery hire")
expect(String(contract.state.current_run.boss_muster.muster_id) == muster_id, "Boss Muster identity survives defeat")
var market = MarketService.new(contract.state.snapshot())
var recovery_id := String(market.state.current_run.protected_recovery_offer_id)
var recovery_offer: Dictionary = market.inspect_recruit_offer(recovery_id).receipt
var hired = market.hire("boss-recovery-hire", market.state.revision, recovery_id, int(recovery_offer.list_price), true)
expect(hired.ok and not market.state.current_run.recovery_required, "protected replacement uses Credit and clears recovery gate")
var recovery_line = market.automatic_formation_candidate()
expect(market.commit_deployment("boss-recovery-deploy", market.state.revision, recovery_line).ok, "recovered Company explicitly recommits its living line")
var prepared = ContractService.new(market.state.snapshot())
expect(prepared.enter_boss_muster("reenter-muster", prepared.state.revision).ok, "recovered Company re-enters boss Muster")
expect(String(prepared.state.current_run.boss_muster.muster_id) == muster_id, "re-entry preserves fixed boss/Muster identity without reroll")
for casualty in settled.receipt.casualties:
expect(not prepared.state.current_run.recruit_records.has(casualty.recruit_id), "Boss recovery never resurrects a Fallen Recruit")
func verify_two_contract_closes() -> void:
var first_market = boss_ready_service("CTR-R1-01", true, false)
var first = complete_contract(first_market.state.snapshot(), "first")
expect(first.ok and first.receipt.clear_id == "CLEAR-RG01-C01", "first Contract closes exact clear fact")
expect(first.receipt.renown_grant == 14 and first.snapshot.guild_profile.persistent_renown == 14, "first Contract grants exact 14 Renown once")
expect(first.snapshot.guild_profile.company_records.size() == 1 and first.snapshot.current_run.is_empty(), "first clear archives one non-playable Company Record and clears Run")
var second_market = MarketService.new(first.snapshot)
expect(second_market.begin_run_creation("second-begin", second_market.state.revision, "CTR-R1-02", "standard").ok, "first clear unlocks second Contract Run creation")
finish_run_creation(second_market, "second")
prepare_boss_market(second_market, true, false)
var second = complete_contract(second_market.state.snapshot(), "second")
expect(second.ok and second.receipt.clear_id == "CLEAR-RG01-C02", "second Contract closes exact clear fact")
expect(second.snapshot.guild_profile.persistent_renown == 32 and second.snapshot.guild_profile.company_records.size() == 2, "two first clears persist 32 Renown and two Records")
expect("RG-02" in second.snapshot.guild_profile.unlocked_region_ids, "second Windglass clear makes RG-02 visible")
var duplicate_service = ContractService.new(second.snapshot)
var duplicate = duplicate_service.close_contract("close-second", int(second.receipt.get("expected_revision_before", -1)), String(second.receipt.close_id))
expect(duplicate.ok and duplicate.duplicate and duplicate_service.state.snapshot() == second.snapshot, "close retry cannot duplicate Renown, Record or unlock")
func verify_atomic_boundaries() -> void:
var service = boss_ready_service("CTR-R1-02", true, false)
var contract = ContractService.new(service.state.snapshot())
var before = contract.state.snapshot()
expect(not contract.enter_boss_muster("stale-muster", contract.state.revision + 1).ok and contract.state.snapshot() == before, "stale Muster entry rejects atomically")
expect(contract.enter_boss_muster("atomic-muster", contract.state.revision).ok, "atomic fixture enters Muster")
var muster_before = contract.state.snapshot()
expect(not contract.commit_boss_plan("bad-plan", contract.state.revision, String(contract.state.current_run.boss_muster.muster_hash), "unknown").ok and contract.state.snapshot() == muster_before, "unknown boss plan rejects atomically")
var plan_id := String(contract.state.current_run.boss_muster.plans[0])
expect(contract.commit_boss_plan("atomic-plan", contract.state.revision, String(contract.state.current_run.boss_muster.muster_hash), plan_id).ok, "atomic fixture commits legal plan")
var terminal: Dictionary = BossBattleService.new(contract.state.snapshot()).run_to_terminal().snapshot
var tampered := terminal.duplicate(true)
tampered.boss_hp = 999
var encounter_before = contract.state.snapshot()
expect(not contract.settle_boss("tampered-boss", contract.state.revision, String(terminal.snapshot_hash), tampered).ok and contract.state.snapshot() == encounter_before, "tampered boss snapshot rejects without mutation")
func complete_contract(run_snapshot: Dictionary, label: String) -> Dictionary:
var contract = ContractService.new(run_snapshot)
expect(contract.enter_boss_muster("%s-muster" % label, contract.state.revision).ok, "%s Contract enters Muster" % label)
var plan_id := String(contract.state.current_run.boss_muster.plans[0])
expect(contract.commit_boss_plan("%s-plan" % label, contract.state.revision, String(contract.state.current_run.boss_muster.muster_hash), plan_id).ok, "%s Contract commits plan" % label)
var terminal: Dictionary = BossBattleService.new(contract.state.snapshot()).run_to_terminal().snapshot
expect(terminal.outcome == "victory", "%s Contract boss wins" % label)
expect(contract.settle_boss("%s-settle" % label, contract.state.revision, String(terminal.snapshot_hash), terminal).ok, "%s Contract settles boss" % label)
var revision_before := int(contract.state.revision)
var close_id := String(contract.state.current_run.pending_close.close_id)
var result := contract.close_contract("close-%s" % label, revision_before, close_id)
if result.ok:
result.receipt.expected_revision_before = revision_before
result.receipt.close_id = close_id
return result
func boss_ready_service(contract_id: String, strong: bool, fragile_three: bool):
var service = MarketService.new()
if contract_id == "CTR-R1-02":
var unlock = service.state.snapshot()
unlock.guild_profile.contract_clear_ids = ["CLEAR-RG01-C01"]
expect(service.restore_snapshot(unlock), "second Contract unlock fixture validates")
expect(service.begin_run_creation("begin-%s" % contract_id, service.state.revision, contract_id, "standard").ok, "%s begin" % contract_id)
finish_run_creation(service, contract_id)
prepare_boss_market(service, strong, fragile_three)
return service
func finish_run_creation(service, label: String) -> void:
var draft_id := String(service.state.run_creation_draft.draft_id)
expect(service.materialize_opening_offers("materialize-%s" % label, service.state.revision, draft_id).ok, "%s materialize" % label)
var ids: Array[String] = []
for offer in service.state.run_creation_draft.opening_offers:
ids.append(String(offer.source_offer_id))
expect(service.set_opening_selection("select-%s" % label, service.state.revision, [ids[0], ids[1]]).ok, "%s select" % label)
expect(service.commit_run_creation("commit-%s" % label, service.state.revision, draft_id, int(service.state.run_creation_draft.selection_revision), Array(service.state.run_creation_draft.selected_offer_ids).duplicate(), String(service.state.run_creation_draft.opening_offer_set_hash)).ok, "%s Run commit" % label)
func prepare_boss_market(service, strong: bool, fragile_three: bool) -> void:
var raw = service.state.snapshot()
raw.current_run.coin = 300
expect(service.restore_snapshot(raw), "boss fixture funds validate")
for index in range(3):
var offer: Dictionary = service.state.current_run.market_board.recruit_offers[index]
expect(service.hire("boss-hire-%s-%d" % [service.state.current_run.contract_id, index], service.state.revision, String(offer.source_offer_id), int(offer.list_price), false).ok, "boss fixture hires %d" % index)
var generated: Dictionary = service.generator.market_board(String(service.state.current_run.run_seed), String(service.state.current_run.contract_id), 12, 0)
var board: Dictionary = service._priced_board(generated.board)
raw = service.state.snapshot()
raw.current_run.cleared_rounds = 12
raw.current_run.market_index = 12
raw.current_run.line_capacity = 10
raw.current_run.market_board = board
raw.current_run.refresh_count = 0
raw.current_run.locks = {}
raw.current_run.available_recruit_offer_ids = service._offer_ids(board.recruit_offers, "source_offer_id")
raw.current_run.available_equipment_offer_ids = service._offer_ids(board.equipment_offers, "offer_id")
raw.current_run.available_battle_offer_ids = service._offer_ids(board.battle_offers, "offer_id")
var deployed: Array = Array(raw.current_run.company).duplicate()
if fragile_three:
deployed = deployed.slice(0, 3)
raw.current_run.deployed_recruit_ids = deployed
raw.current_run.reserve_recruit_ids = Array(raw.current_run.company).filter(func(recruit_id): return recruit_id not in deployed)
raw.current_run.formation_proposal = deployed.duplicate()
for recruit_id in raw.current_run.company:
if strong:
raw.current_run.recruit_records[recruit_id].level = 10
raw.current_run.recruit_records[recruit_id].max_hp = 700
raw.current_run.recruit_records[recruit_id].current_hp = 700
for key in raw.current_run.recruit_records[recruit_id].attribute_rolls:
raw.current_run.recruit_records[recruit_id].attribute_rolls[key] = "S"
elif recruit_id in deployed:
raw.current_run.recruit_records[recruit_id].current_hp = 1
raw.current_run.recruit_records[recruit_id].original_list_price = 22
raw.current_run.recruit_records[recruit_id].coin_price_paid = 18
expect(service.restore_snapshot(raw), "Market 12 Boss fixture validates")
func expect(condition: bool, message: String) -> void:
if not condition:
failures.append("P10.0 boss/contract: %s" % message)