Files
windroost/p2_godot_sample/scripts/offline_anchor_test.gd
T
wushenghua c8074948b6 S1/S2/S8 integration candidate: schema-agnostic probes and clean CH touch teardown
S1/S2/S8 product code is already present in 529cdeb (all three candidate
commits are ancestors). Align the S1/S2 domain probes with the integrated
schema 7 by comparing to ProfileStore.SCHEMA, and make the CH systems touch
probe free its shell synchronously so the representative run exits clean.
All systems in 529cdeb (S3/S4/S5/S6/buoy) are preserved unchanged.
2026-11-29 10:16:01 +08:00

251 lines
13 KiB
GDScript

extends SceneTree
## S2 offline anchor/receipt evidence (domain layer).
##
## Every number and field name here is source-pinned to
## clicker_heroes_rebuild_ancient_limit_build25 @ 46cc8ad:
## scripts/offline_book.gd CAP_MS=28800000, PAUSE_GATE_MS=240000,
## anchor_ms/revision/pending/receipt, restore rules
## scripts/game_state.gd::settle_offline / acknowledge_offline
## WR keeps its gold rate and claim UI. CH souls/cores/relic/seed/tail have no
## WR offline consumer and are not carried (no value is invented for them).
const OUTPUT_PATH := "res://artifacts/offline_anchor_test.json"
const HOUR_MS := 3600000
const FIXED_NOW_MS := 1000000000000
var _problems: Array = []
var _steps: Array = []
func _init() -> void:
_run.call_deferred()
func _check(cond: bool, msg: String) -> void:
if not cond:
_problems.append(msg)
func _record(name: String, data: Dictionary) -> void:
var e := data.duplicate()
e["step"] = name
_steps.append(e)
func _fresh_profile(anchor_ms: int) -> Dictionary:
var p := ProfileStore.fresh_profile()
p["offline_anchor_ms"] = anchor_ms
p["last_saved_at"] = int(anchor_ms / 1000)
return p
func _run() -> void:
var rate := ProfileStore.offline_rate()
var expected_cap_gold := int(float(ProfileStore.OFFLINE_CAP_MS) / 1000.0 * 40.0 * rate)
# 1. Legacy migration: a v5 save without an anchor is restored from
# last_saved_at * 1000 (CH game_state.gd:2513-2514).
var legacy := {"schema": 5, "gold": 7, "last_saved_at": 1000000000, "seen_intro": true}
var raw := FileAccess.open(ProfileStore.save_path(), FileAccess.WRITE)
_check(raw != null, "could not open isolated save for legacy fixture")
if raw != null:
raw.store_string(JSON.stringify(legacy))
raw.close()
var migrated := ProfileStore.load_profile()
_check(int(migrated.get("schema", 0)) == ProfileStore.SCHEMA, "v5 save must migrate to the current schema")
_check(int(migrated.get("offline_anchor_ms", 0)) == 1000000000 * 1000,
"legacy anchor must be last_saved_at * 1000")
_check(int(migrated.get("offline_revision", 0)) == 0, "legacy revision must start at 0")
_check(not bool(migrated.get("offline_pending", false)), "legacy book must not be pending")
_check((migrated.get("offline_receipt", {}) as Dictionary).is_empty(), "legacy receipt must be empty")
_check(int(migrated.get("gold", 0)) == 7, "legacy gold must survive migration")
_record("legacy_migration", {"schema": migrated.get("schema"),
"anchor": migrated.get("offline_anchor_ms"), "revision": migrated.get("offline_revision"),
"gold": migrated.get("gold")})
# 2. Eight-hour cap: 30h elapsed settles only CAP_MS and flags capped.
var capped_profile := _fresh_profile(FIXED_NOW_MS - 30 * HOUR_MS)
var cap_receipt := ProfileStore.build_offline_report(capped_profile, 40.0, FIXED_NOW_MS)
_check(not cap_receipt.is_empty(), "30h settlement must produce a receipt")
_check(int(cap_receipt.get("applied_ms", -1)) == ProfileStore.OFFLINE_CAP_MS,
"applied_ms must equal the source 28800000 cap")
_check(bool(cap_receipt.get("capped", false)), "a 30h gap must be capped")
_check(int(cap_receipt.get("from_ms", 0)) == FIXED_NOW_MS - 30 * HOUR_MS, "from_ms must be the anchor")
_check(int(cap_receipt.get("to_ms", 0)) == FIXED_NOW_MS, "to_ms must be the advanced anchor")
_check(int(cap_receipt.get("id", -1)) == int(capped_profile.get("offline_revision", -1)),
"receipt id must equal the book revision")
_check(int(capped_profile.get("pending_offline_gold", 0)) == expected_cap_gold,
"capped gold must use the 8h cap and shared rate")
_check(bool(capped_profile.get("offline_pending", false)), "a payable settlement must be pending")
_check(int(capped_profile.get("offline_anchor_ms", 0)) == FIXED_NOW_MS, "anchor must advance to now")
_check(not cap_receipt.has("souls") and not cap_receipt.has("cores")
and not cap_receipt.has("relic_uid") and not cap_receipt.has("seed"),
"receipt must not invent CH soul/chest/relic/seed values")
_record("eight_hour_cap", {"applied_ms": cap_receipt.get("applied_ms"),
"capped": cap_receipt.get("capped"), "gold": cap_receipt.get("gold"),
"expected": expected_cap_gold, "revision": cap_receipt.get("id")})
# 3. Pause gate: below PAUSE_GATE_MS only the anchor moves; at/above it pays.
var short_pause := _fresh_profile(FIXED_NOW_MS - 100000)
var short_receipt := ProfileStore.build_offline_report(short_pause, 40.0, FIXED_NOW_MS, "pause")
_check(short_receipt.is_empty(), "a 100s pause must not settle")
_check(int(short_pause.get("offline_anchor_ms", 0)) == FIXED_NOW_MS,
"a gated pause must still advance the anchor")
_check(not bool(short_pause.get("offline_pending", false)), "a gated pause must not be pending")
var long_pause := _fresh_profile(FIXED_NOW_MS - 300000)
var long_receipt := ProfileStore.build_offline_report(long_pause, 40.0, FIXED_NOW_MS, "pause")
_check(str(long_receipt.get("kind", "")) == "pause", "a 300s pause must settle with kind=pause")
_check(int(long_receipt.get("applied_ms", -1)) == 300000, "a 300s pause applies 300000ms")
_record("pause_gate", {"gate_ms": ProfileStore.OFFLINE_PAUSE_GATE_MS,
"short_settled": not short_receipt.is_empty(), "long_settled": not long_receipt.is_empty(),
"long_applied_ms": long_receipt.get("applied_ms")})
# 4. Rollback: a future anchor pays nothing, is not left pending, and is
# recorded with rollback=true so the cap/rollback state stays readable.
var rollback_profile := _fresh_profile(FIXED_NOW_MS + HOUR_MS)
var rollback_receipt := ProfileStore.build_offline_report(rollback_profile, 40.0, FIXED_NOW_MS)
_check(bool(rollback_receipt.get("rollback", false)), "a future anchor must set rollback=true")
_check(int(rollback_profile.get("pending_offline_gold", 0)) == 0, "rollback must not pay gold")
_check(not bool(rollback_profile.get("offline_pending", false)), "rollback must not wedge the book")
_check(int(rollback_profile.get("offline_anchor_ms", 0)) == FIXED_NOW_MS + HOUR_MS,
"rollback must not move the high-water mark backward")
_record("rollback", {"rollback": rollback_receipt.get("rollback"),
"gold": rollback_profile.get("pending_offline_gold"), "anchor": rollback_profile.get("offline_anchor_ms")})
# 5. Ack-once: id must match revision, the first claim pays and the second
# cannot. Reuse the capped report from step 2.
var id_before := int(capped_profile.get("offline_revision", 0))
capped_profile["offline_revision"] = id_before + 1
_check(ProfileStore.claim_offline(capped_profile) == 0, "a mismatched receipt id must not pay")
capped_profile["offline_revision"] = id_before
var first := ProfileStore.claim_offline(capped_profile)
var second := ProfileStore.claim_offline(capped_profile)
_check(first == expected_cap_gold, "first acknowledged claim must pay the full report")
_check(second == 0, "a second claim must pay nothing")
_check(int(capped_profile.get("gold", 0)) == expected_cap_gold, "gold must be credited exactly once")
_check(not bool(capped_profile.get("offline_pending", false)), "claim must clear pending")
_record("ack_once", {"id": id_before, "first": first, "second": second,
"gold": capped_profile.get("gold")})
# 6. Pending preservation: a save between sessions must not destroy the
# unclaimed report, and a later build must not overwrite it.
var preserved := _fresh_profile(FIXED_NOW_MS - HOUR_MS)
ProfileStore.build_offline_report(preserved, 40.0, FIXED_NOW_MS)
var preserved_gold := int(preserved.get("pending_offline_gold", 0))
_check(saved_ok(preserved), "preservation fixture must save")
_check(int(preserved.get("pending_offline_gold", 0)) == preserved_gold,
"save_profile must keep the pending report")
_check(ProfileStore.build_offline_report(preserved, 40.0, FIXED_NOW_MS).is_empty(),
"build must not overwrite a pending report")
_check(int(preserved.get("pending_offline_gold", 0)) == preserved_gold,
"pending gold must survive a re-build")
_record("pending_preservation", {"gold": preserved_gold})
# 7. Corrupt receipt: strict restore resets the whole book and refuses to
# pay from tampered state (the rest of the profile survives).
var corrupt := _fresh_profile(FIXED_NOW_MS)
var bad_receipt := {
"id": 9, "kind": "startup", "from_ms": FIXED_NOW_MS - HOUR_MS,
"to_ms": FIXED_NOW_MS, "applied_ms": ProfileStore.OFFLINE_CAP_MS + 1,
"capped": true, "rollback": false, "gold": 99999,
}
corrupt["offline_revision"] = 9
corrupt["offline_receipt"] = bad_receipt
corrupt["offline_pending"] = true
corrupt["pending_offline_gold"] = 99999
corrupt["offline_report_key"] = "offline_9"
var sanitized := ProfileStore.normalize_for_test(corrupt)
_check(int(sanitized.get("offline_revision", 0)) == 0, "corrupt revision must reset")
_check(not bool(sanitized.get("offline_pending", false)), "corrupt pending must clear")
_check((sanitized.get("offline_receipt", {}) as Dictionary).is_empty(), "corrupt receipt must drop")
_check(int(sanitized.get("pending_offline_gold", 0)) == 0, "corrupt report must not pay")
_check(ProfileStore.claim_offline(sanitized) == 0, "corrupt book must not claim")
_record("corrupt_receipt", {"revision": sanitized.get("offline_revision"),
"pending": sanitized.get("offline_pending"), "gold": sanitized.get("pending_offline_gold")})
# 8. Save-failure/retry: a blocker at the temp path makes the atomic rename
# impossible. The failed claim leaves the pending report and wallet intact;
# once the blocker is removed the retry pays exactly once.
var retry := _fresh_profile(FIXED_NOW_MS - HOUR_MS)
ProfileStore.build_offline_report(retry, 40.0, FIXED_NOW_MS)
var retry_gold := int(retry.get("pending_offline_gold", 0))
var wallet_before := int(retry.get("gold", 0))
var made := DirAccess.make_dir_absolute(ProfileStore.temp_path())
_check(made == OK, "could not create the save-failure blocker")
_check(ProfileStore.claim_offline(retry) == 0, "a failed save must not pay")
_check(int(retry.get("pending_offline_gold", 0)) == retry_gold, "failed save must preserve pending gold")
_check(int(retry.get("gold", 0)) == wallet_before, "failed save must preserve the wallet")
_check(bool(retry.get("offline_pending", false)), "failed save must preserve pending flag")
var removed := DirAccess.remove_absolute(ProfileStore.temp_path())
_check(removed == OK, "could not remove the save-failure blocker")
var retried := ProfileStore.claim_offline(retry)
_check(retried == retry_gold, "retry after a failed save must pay once")
_check(ProfileStore.claim_offline(retry) == 0, "retry must not double-pay")
var reloaded := ProfileStore.load_profile()
_check(int(reloaded.get("gold", 0)) == wallet_before + retry_gold, "retry must persist exactly once")
_record("save_failure_retry", {"expected": retry_gold, "failed_payout": 0,
"retried": retried, "persisted_gold": reloaded.get("gold")})
# 9. Numeric bounds: a tampered anchor/revision is clamped or reset without
# a crash, and no negative value can reach a later settlement.
var bounded := _fresh_profile(FIXED_NOW_MS)
bounded["offline_anchor_ms"] = -5
bounded["offline_revision"] = -1
bounded["offline_pending"] = "yes"
var bounded_normal := ProfileStore.normalize_for_test(bounded)
_check(int(bounded_normal.get("offline_revision", 0)) == 0, "negative revision must reset")
_check(not bool(bounded_normal.get("offline_pending", false)), "non-bool pending must reset")
_check(int(bounded_normal.get("offline_anchor_ms", 0)) >= 0, "anchor must be non-negative")
_record("bounds", {"anchor": bounded_normal.get("offline_anchor_ms"),
"revision": bounded_normal.get("offline_revision"), "pending": bounded_normal.get("offline_pending")})
# 10. A pre-v6 save with a pending gold/key but no receipt must stay
# claimable; migration must not strand already-earned currency.
var legacy_pending := {"schema": 5, "gold": 3, "last_saved_at": 1000000000,
"seen_intro": true, "pending_offline_gold": 321,
"offline_report_key": "offline_1000000000"}
var legacy_raw := FileAccess.open(ProfileStore.save_path(), FileAccess.WRITE)
_check(legacy_raw != null, "could not open isolated save for legacy pending fixture")
if legacy_raw != null:
legacy_raw.store_string(JSON.stringify(legacy_pending))
legacy_raw.close()
var legacy_loaded := ProfileStore.load_profile()
var legacy_paid := ProfileStore.claim_offline(legacy_loaded)
_check(legacy_paid == 321, "a legacy pending report must remain claimable after migration")
_check(int(legacy_loaded.get("gold", 0)) == 324, "legacy claim must credit gold once")
_check(ProfileStore.claim_offline(legacy_loaded) == 0, "legacy claim must not double-pay")
_record("legacy_pending_claim", {"paid": legacy_paid, "gold": legacy_loaded.get("gold")})
_finish()
func saved_ok(p: Dictionary) -> bool:
return ProfileStore.save_profile(p)
func _finish() -> void:
var passed := _problems.is_empty()
var report := {
"schema": 1, "kind": "windroost_offline_anchor_test",
"source_revision": "46cc8ad46ac886918ee1b2c1b0e946e1f5e17c04",
"constants": {"cap_ms": ProfileStore.OFFLINE_CAP_MS,
"pause_gate_ms": ProfileStore.OFFLINE_PAUSE_GATE_MS,
"limit": ProfileStore.OFFLINE_LIMIT},
"overall": "PASS" if passed else "FAIL",
"problems": _problems, "steps": _steps,
}
var dir := ProjectSettings.globalize_path("res://artifacts")
DirAccess.make_dir_recursive_absolute(dir)
var out := FileAccess.open(dir.path_join("offline_anchor_test.json"), FileAccess.WRITE)
if out != null:
out.store_string(JSON.stringify(report, "\t", true))
out.close()
for entry in _steps:
print("OFFLINE_STEP %s" % JSON.stringify(entry))
if passed:
print("OFFLINE_ANCHOR_PASS %d cases verified" % _steps.size())
else:
print("OFFLINE_ANCHOR_FAIL %s" % "; ".join(_problems))
quit(0 if passed else 1)