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

214 lines
8.1 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")
const Save = preload("res://fg4/save_v11_service.gd")
var failures: Array[String] = []
var phase_count := 0
func _initialize() -> void:
call_deferred("run_test")
func run_test() -> void:
var snapshots := representative_snapshots()
verify_exact_roundtrip_chain(snapshots)
verify_corruption_rollback_and_interruption(snapshots)
if failures.is_empty():
print("ABG_FG4_RG04_CONTRACT2_SAVE_V11_OK version=11 phases=%d chain=true exact_roundtrip=true offline=0 rollback=true checksum=true state_tamper=true active_backup=true dual_corruption=true interrupted_candidate=true" % phase_count)
quit(0)
else:
for failure in failures:
push_error(failure)
quit(1)
func representative_snapshots() -> Array[Dictionary]:
var snapshots: Array[Dictionary] = []
var market := Fixture.new().rg04_contract2_market_1_snapshot()
if market.is_empty():
expect(false, "RG-04 Market fixture exists")
return snapshots
snapshots.append(market)
var route_service := Contract.new(market)
var route: Dictionary = route_service.state.current_run.market_board.battle_offers[2]
var prepared := route_service.commit_route(
"fg4-rg04-save-route-prepare",
route_service.state.revision,
String(route.offer_id),
String(route_service.state.current_run.market_board.board_hash)
)
if prepared.ok:
snapshots.append(route_service.state.snapshot())
var wrong_plan := first_wrong_plan(1)
var route_battle := Battle.new(route_service.state.snapshot()).run_to_terminal(wrong_plan)
var settled := route_service.settle_route(
"fg4-rg04-save-route-settle",
route_service.state.revision,
route_battle.snapshot
)
if settled.ok:
snapshots.append(route_service.state.snapshot())
var boss_muster := Fixture.new().rg04_contract2_boss_muster_snapshot()
if boss_muster.is_empty():
expect(false, "RG-04 Boss Muster fixture exists")
return snapshots
snapshots.append(boss_muster)
var boss_service := Contract.new(boss_muster)
var boss_prepared := boss_service.commit_boss_plan(
"fg4-rg04-save-boss-prepare",
boss_service.state.revision,
String(boss_service.state.current_run.boss_muster.muster_hash),
"safe_rule_seed"
)
if boss_prepared.ok:
snapshots.append(boss_service.state.snapshot())
var boss_battle := Battle.new(boss_service.state.snapshot()).run_to_terminal("safe_rule_seed")
var boss_settled := boss_service.settle_boss(
"fg4-rg04-save-boss-settle",
boss_service.state.revision,
boss_battle.snapshot
)
if boss_settled.ok:
snapshots.append(boss_service.state.snapshot())
var closed := boss_service.close_region(
"fg4-rg04-save-contract-close",
boss_service.state.revision,
String(boss_service.state.current_run.pending_boss_result.close_id)
)
if closed.ok:
snapshots.append(boss_service.state.snapshot())
var low_hp := boss_muster.duplicate(true)
for recruit_id in low_hp.current_run.company:
low_hp.current_run.recruit_records[recruit_id].current_hp = 1
low_hp.current_run.active_artifact_ids = ["AR-024"]
var recovery_service := Contract.new(low_hp)
var recovery_prepared := recovery_service.commit_boss_plan(
"fg4-rg04-save-recovery-prepare",
recovery_service.state.revision,
String(recovery_service.state.current_run.boss_muster.muster_hash),
"safe_rule_seed"
)
var loss := Battle.new(recovery_service.state.snapshot()).run_to_terminal("safe_rule_seed")
var recovery_settled := recovery_service.settle_boss(
"fg4-rg04-save-recovery-settle",
recovery_service.state.revision,
loss.snapshot
)
if recovery_prepared.ok and recovery_settled.ok:
snapshots.append(recovery_service.state.snapshot())
return snapshots
func verify_exact_roundtrip_chain(snapshots: Array[Dictionary]) -> void:
expect(snapshots.size() == 8, "eight representative RG-04 phases exist; got %d" % snapshots.size())
var service := Save.new()
var previous_hash := ""
var sequence := 200
for snapshot_value in snapshots:
var encoded := service.encode_snapshot(snapshot_value, sequence, previous_hash)
expect(encoded.ok, "%s encodes" % String(snapshot_value.current_run.phase))
if not encoded.ok:
continue
var decoded := service.decode_text(String(encoded.text), sequence)
expect(decoded.ok and decoded.snapshot == snapshot_value, "%s roundtrips exactly" % String(snapshot_value.current_run.phase))
expect(decoded.offline_delta.values().all(func(value): return int(value) == 0), "%s creates no offline value" % String(snapshot_value.current_run.phase))
expect(String(encoded.envelope.previous_commit_hash) == previous_hash, "%s binds previous commit" % String(snapshot_value.current_run.phase))
previous_hash = String(encoded.envelope.commit_hash)
sequence += 1
phase_count += 1
func verify_corruption_rollback_and_interruption(snapshots: Array[Dictionary]) -> void:
if snapshots.size() != 8:
return
var service := Save.new()
var active := service.encode_snapshot(snapshots[0], 300)
var backup := service.encode_snapshot(snapshots[-1], 299)
expect(active.ok and backup.ok, "active and backup fixtures encode")
if not active.ok or not backup.ok:
return
expect(not service.decode_text(String(backup.text), 300).ok, "older Save is rejected as rollback")
var checksum_drift := String(active.text).replace("\"sequence\":300", "\"sequence\":301")
expect(service.decode_text(checksum_drift).code == "checksum_mismatch", "checksum drift rejects")
var tampered: Dictionary = JSON.parse_string(String(active.text))
tampered.state.current_run.market_board.copy_disclosure.source_rule = "tampered"
rehash_envelope(tampered)
expect(service.decode_text(JSON.stringify(tampered)).code == "state_invalid", "source-bound board tamper rejects")
var backup_loaded := service.load_with_backup("{bad", String(backup.text), 299)
expect(backup_loaded.ok and backup_loaded.source == "backup", "valid backup replaces corrupt active")
var dual := service.load_with_backup("{bad", "{also-bad", 0)
expect(not dual.ok and dual.code == "no_valid_save", "dual corruption rejects")
var slot := "user://fg4-rg04-contract2-save-test.json"
remove_if_exists(slot)
remove_if_exists("%s.bak" % slot)
remove_if_exists("%s.candidate" % slot)
var written := service.write_slot(slot, snapshots[0], 400)
expect(written.ok, "active Save writes atomically")
var candidate := FileAccess.open("%s.candidate" % slot, FileAccess.WRITE)
if candidate != null:
candidate.store_string("{interrupted")
candidate.close()
var loaded := service.load_slot(slot, 400)
expect(loaded.ok and loaded.snapshot == snapshots[0], "interrupted candidate never replaces active Save")
remove_if_exists(slot)
remove_if_exists("%s.bak" % slot)
remove_if_exists("%s.candidate" % slot)
func rehash_envelope(envelope: Dictionary) -> void:
envelope.state_hash = hash_value(envelope.state)
envelope.commit_hash = hash_value({
"sequence": int(envelope.sequence),
"previous_commit_hash": String(envelope.previous_commit_hash),
"state_hash": String(envelope.state_hash),
})
var checksum_input := envelope.duplicate(true)
checksum_input.erase("checksum")
envelope.checksum = hash_value(checksum_input)
func remove_if_exists(path: String) -> void:
if FileAccess.file_exists(path):
DirAccess.remove_absolute(ProjectSettings.globalize_path(path))
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 hash_value(value) -> String:
return JSON.stringify(canonical(value)).sha256_text()
func canonical(value):
if value is float and is_equal_approx(value, round(value)):
return int(round(value))
if value is Dictionary:
var result := {}
var keys: Array = value.keys()
keys.sort_custom(func(left, right): return String(left) < String(right))
for key in keys:
result[key] = canonical(value[key])
return result
if value is Array:
var result: Array = []
for item in value:
result.append(canonical(item))
return result
return value
func expect(condition: bool, message: String) -> void:
if not condition:
failures.append("FG-4 RG-04 Contract-2 Save v11: %s" % message)