173 lines
9.7 KiB
GDScript
173 lines
9.7 KiB
GDScript
extends SceneTree
|
|
|
|
const Battle = preload("res://fg4/rg04_contract2_battle_service.gd")
|
|
const Contract = preload("res://fg4/rg04_contract2_service.gd")
|
|
const Fixture = preload("res://tests/fg4/contract1_fixture.gd")
|
|
|
|
var failures: Array[String] = []
|
|
var successful_routes := 0
|
|
var counterfactual_routes := 0
|
|
var reversal_cases := 0
|
|
|
|
|
|
func _initialize() -> void:
|
|
call_deferred("run_test")
|
|
|
|
|
|
func run_test() -> void:
|
|
verify_two_viable_plans_and_state_reversals()
|
|
verify_complete_twelve_round_flow()
|
|
verify_permanent_loss_and_same_board_recovery()
|
|
verify_tamper_stale_and_duplicate_boundaries()
|
|
if failures.is_empty():
|
|
print("ABG_FG4_RG04_CONTRACT2_ROUNDS_OK routes=%d counterfactual_routes=%d reversal_cases=%d rounds=12 history=72 plans=8 clauses=3 pressure_families=4 redaction_source=visible omitted_target=visible replay_order=visible exact_once=true permanent_loss=true same_board_recovery=true resurrection=0" % [successful_routes, counterfactual_routes, reversal_cases])
|
|
quit(0)
|
|
else:
|
|
for failure in failures:
|
|
push_error(failure)
|
|
quit(1)
|
|
|
|
|
|
func verify_two_viable_plans_and_state_reversals() -> void:
|
|
for market_index in range(1, 13):
|
|
for risk in range(3):
|
|
var primary := String(Battle.PRIMARY_PLAN[market_index])
|
|
var secondary := String(Battle.SECONDARY_PLAN[market_index])
|
|
var primary_score := Battle.route_plan_score(market_index, risk, primary, "AR-011", 0, 1.0)
|
|
var secondary_score := Battle.route_plan_score(market_index, risk, secondary, "AR-011", 0, 1.0)
|
|
expect(primary_score >= 15 and secondary_score >= 15, "Market %d risk %d has two viable plans" % [market_index, risk])
|
|
if primary_score >= 15 and secondary_score >= 15:
|
|
counterfactual_routes += 1
|
|
var primary := String(Battle.PRIMARY_PLAN[market_index])
|
|
var secondary := String(Battle.SECONDARY_PLAN[market_index])
|
|
var obey_context := Battle.route_plan_score(market_index, 1, primary, "AR-023", 0, 1.0) > Battle.route_plan_score(market_index, 1, secondary, "AR-023", 0, 1.0)
|
|
var break_context := Battle.route_plan_score(market_index, 1, secondary, "AR-024", 0, 1.0) > Battle.route_plan_score(market_index, 1, primary, "AR-024", 0, 1.0)
|
|
expect(obey_context and break_context, "Market %d flips between obey and break Artifact states" % market_index)
|
|
if obey_context and break_context:
|
|
reversal_cases += 1
|
|
|
|
|
|
func verify_complete_twelve_round_flow() -> void:
|
|
var service := Contract.new(Fixture.new().rg04_contract2_market_1_snapshot())
|
|
expect(service.valid, "complete RG-04 Contract-2 fixture is valid")
|
|
if not service.valid:
|
|
return
|
|
var seen_clauses := {}
|
|
var seen_pressures := {}
|
|
for market_index in range(1, 13):
|
|
var run: Dictionary = service.state.current_run
|
|
var route: Dictionary = run.market_board.battle_offers[0]
|
|
var coin_before := int(run.coin)
|
|
seen_clauses[String(route.archive_clause.kind)] = true
|
|
seen_pressures[String(route.copy_disclosure.pressure_family)] = true
|
|
var prepared := service.commit_route(
|
|
"fg4-rg04-round-%02d-prepare" % market_index,
|
|
service.state.revision,
|
|
String(route.offer_id),
|
|
String(run.market_board.board_hash)
|
|
)
|
|
expect(prepared.ok, "Market %d route prepares" % market_index)
|
|
if not prepared.ok:
|
|
return
|
|
var plan_id := String(Battle.PRIMARY_PLAN[market_index])
|
|
var battle := Battle.new(service.state.snapshot()).run_to_terminal(plan_id)
|
|
expect(battle.ok and battle.snapshot.outcome == "victory", "Market %d safe response wins" % market_index)
|
|
expect(has_event(battle.snapshot.event_log, "archive_clause_disclosed"), "Market %d emits clause disclosure" % market_index)
|
|
expect(has_event(battle.snapshot.event_log, "copy_source_disclosed"), "Market %d emits copy-source disclosure" % market_index)
|
|
expect(has_event(battle.snapshot.event_log, "redaction_source_disclosed"), "Market %d exposes redaction source" % market_index)
|
|
expect(has_event(battle.snapshot.event_log, "target_clause_omitted"), "Market %d exposes omitted target clause" % market_index)
|
|
expect(has_event(battle.snapshot.event_log, "replay_order_disclosed"), "Market %d exposes replay order" % market_index)
|
|
expect(not String(battle.snapshot.redaction_disclosure.omitted_target_clause).is_empty(), "Market %d retains omitted target authority" % market_index)
|
|
var settle_revision := service.state.revision
|
|
var settlement_id := "fg4-rg04-round-%02d-settle" % market_index
|
|
var settled := service.settle_route(
|
|
settlement_id,
|
|
settle_revision,
|
|
battle.snapshot
|
|
)
|
|
expect(settled.ok and settled.receipt.reward_coin == route.reward_coin, "Market %d settles exact reward" % market_index)
|
|
expect(settled.ok and settled.receipt.xp_award == battle.snapshot.xp_grant, "Market %d settles exact XP" % market_index)
|
|
expect(service.state.current_run.coin == coin_before + route.reward_coin, "Market %d Coin changes once" % market_index)
|
|
if market_index == 1 and settled.ok:
|
|
var coin_after := int(service.state.current_run.coin)
|
|
var duplicate := service.settle_route(settlement_id, settle_revision, battle.snapshot)
|
|
expect(duplicate.ok and duplicate.duplicate and service.state.current_run.coin == coin_after, "duplicate settlement pays no second Coin or XP")
|
|
if settled.ok:
|
|
successful_routes += 3
|
|
expect(service.state.current_run.phase == "rg04_contract2_boss_muster" and service.state.current_run.cleared_rounds == 72, "twelve victories open Redaction Hart Muster")
|
|
expect(service.state.current_run.route_history.size() == 72, "history contains prior 60 plus new 12 routes")
|
|
expect(sorted_keys(seen_clauses) == ["alternate", "preserve", "repeat"], "all clause kinds execute")
|
|
expect(sorted_keys(seen_pressures) == ["clause", "cleanse_trap", "copy", "redirect"], "all pressure families execute")
|
|
|
|
|
|
func verify_permanent_loss_and_same_board_recovery() -> void:
|
|
var service := Contract.new(Fixture.new().rg04_contract2_market_1_snapshot())
|
|
if not service.valid:
|
|
expect(false, "RG-04 recovery fixture is valid")
|
|
return
|
|
var board: Dictionary = service.state.current_run.market_board.duplicate(true)
|
|
var route: Dictionary = board.battle_offers[2]
|
|
var company_before: int = service.state.current_run.company.size()
|
|
var history_before: int = service.state.current_run.route_history.size()
|
|
var prepared := service.commit_route("fg4-rg04-loss-prepare", service.state.revision, String(route.offer_id), String(board.board_hash))
|
|
var battle := Battle.new(service.state.snapshot()).run_to_terminal(first_wrong_plan(1))
|
|
expect(prepared.ok and battle.ok and battle.snapshot.outcome == "defeat", "wrong response causes defeat")
|
|
expect(battle.snapshot.fallen_recruit_ids.size() == 1, "defeat discloses one permanent casualty")
|
|
var fallen_id := String(battle.snapshot.fallen_recruit_ids[0])
|
|
var returned_instance := String(service.state.current_run.equipped_by_recruit.get(fallen_id, ""))
|
|
var settled := service.settle_route("fg4-rg04-loss-settle", service.state.revision, battle.snapshot)
|
|
expect(settled.ok and service.state.current_run.phase == "rg04_contract2_recovery", "defeat opens RG-04 recovery")
|
|
expect(service.state.current_run.company.size() == company_before - 1 and fallen_id not in service.state.current_run.company, "Fallen Recruit is permanently removed")
|
|
if not returned_instance.is_empty():
|
|
expect(String(service.state.current_run.inventory[returned_instance].wearer_id).is_empty(), "Fallen Recruit equipment returns to inventory")
|
|
expect(service.state.current_run.route_history.size() == history_before and settled.receipt.reward_coin == 0, "defeat pays no Coin and clears no route")
|
|
var recovered := service.recover_route(
|
|
"fg4-rg04-loss-recover",
|
|
service.state.revision,
|
|
String(service.state.current_run.last_battle_report.battle_hash),
|
|
int(service.state.current_run.recovery_cost)
|
|
)
|
|
expect(recovered.ok and service.state.current_run.phase == "rg04_contract2_market", "recovery returns to exact Market")
|
|
expect(service.state.current_run.market_board == board, "recovery preserves exact board")
|
|
expect(fallen_id not in service.state.current_run.company, "recovery never resurrects")
|
|
|
|
|
|
func verify_tamper_stale_and_duplicate_boundaries() -> void:
|
|
var service := Contract.new(Fixture.new().rg04_contract2_market_1_snapshot())
|
|
if not service.valid:
|
|
return
|
|
var route: Dictionary = service.state.current_run.market_board.battle_offers[0]
|
|
var revision_before := service.state.revision
|
|
var board_hash := String(service.state.current_run.market_board.board_hash)
|
|
expect(not service.commit_route("fg4-rg04-stale", revision_before - 1, String(route.offer_id), board_hash).ok, "stale prepare rejects")
|
|
var prepared := service.commit_route("fg4-rg04-boundary-prepare", revision_before, String(route.offer_id), board_hash)
|
|
var duplicate := service.commit_route("fg4-rg04-boundary-prepare", revision_before, String(route.offer_id), board_hash)
|
|
expect(prepared.ok and duplicate.ok and duplicate.duplicate, "duplicate prepare is inert")
|
|
var battle := Battle.new(service.state.snapshot()).run_to_terminal(String(Battle.PRIMARY_PLAN[1]))
|
|
var tampered: Dictionary = battle.snapshot.duplicate(true)
|
|
tampered.archive_clause.kind = "wrong"
|
|
expect(not service.settle_route("fg4-rg04-tampered-settle", service.state.revision, tampered).ok, "tampered clause snapshot rejects")
|
|
expect(service.state.current_run.phase == "rg04_contract2_prepared", "rejected settlement preserves prepared state")
|
|
|
|
|
|
func first_wrong_plan(market_index: int) -> String:
|
|
for plan_id in Battle.PLAN_IDS:
|
|
if plan_id not in [Battle.PRIMARY_PLAN[market_index], Battle.SECONDARY_PLAN[market_index]]:
|
|
return String(plan_id)
|
|
return ""
|
|
|
|
|
|
func has_event(events: Array, kind: String) -> bool:
|
|
return events.any(func(event): return String(event.get("kind", "")) == kind)
|
|
|
|
|
|
func sorted_keys(values: Dictionary) -> Array:
|
|
var result: Array = values.keys()
|
|
result.sort()
|
|
return result
|
|
|
|
|
|
func expect(condition: bool, message: String) -> void:
|
|
if not condition:
|
|
failures.append("FG-4 RG-04 Contract-2 rounds: %s" % message)
|