Files
windroost/p2_godot_sample/scripts/transcendence_test.gd
T
wushenghua 34989d898d S3: port CH transcendence/outsider slice into WR new sky
Source-bound Ancient Soul wallet codec, outsider definitions/levels/bonuses,
TP and sacrifice math from clicker_heroes_rebuild @46cc8ad. Additive schema v7
fields with provenance; WR sky_marks/edicts left untouched. Save-first,
exactly-once claim, strict import, migration and real ScreenTouch probes.
2026-11-29 08:23:12 +08:00

341 lines
16 KiB
GDScript

extends SceneTree
## S3 focused evidence: CH transcendence/outsider slice ported source-bound.
## Covers wallet codec parity (raw-bit vectors captured from CH @46cc8ad),
## TP / bonus / cost families, sacrifice inputs, outsider strict import,
## schema v7 migration, exactly-once claim and save-first atomic adoption.
## Real mobile-touch evidence lives in transcendence_touch_test.gd.
const OUTPUT_PATH := "res://artifacts/transcendence_test.json"
const Wallet = preload("res://scripts/ancient_soul_wallet.gd")
const Outsiders = preload("res://scripts/outsider_book.gd")
const Transcendence = preload("res://scripts/transcendence.gd")
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 _run() -> void:
_wallet_codec_parity()
_outsider_family_parity()
_tp_parity()
_cost_and_respec_parity()
_strict_import()
_sacrifice_and_earned()
_primal_parity()
_preview_and_apply_once()
_save_first_failure()
_migration()
_finish()
func _wallet_codec_parity() -> void:
# Raw-bit vectors produced by running the unmodified CH @46cc8ad
# ancient_soul_wallet.gd in an isolated probe project (see PRODUCTION doc).
var cases: Array = [
{"in": [10.25, 50.75, 60.0, false], "balance": "f64le:0000000000803340",
"total": "f64le:0000000000004e40", "gain": "f64le:0000000000802240",
"delta0": "f64le:0000000000802440", "delta1": "f64le:000000000000f0bf"},
{"in": [10.0, 50.0, 60.0, false], "balance": "f64le:0000000000003440",
"total": "f64le:0000000000004e40", "gain": "f64le:0000000000002440",
"delta0": "f64le:0000000000002440", "delta1": "f64le:0000000000000000"},
{"in": [10.25, 50.75, 60.0, true], "balance": "f64le:0000000000004e40",
"total": "f64le:0000000000004e40", "gain": "f64le:0000000000802240",
"refund": "f64le:0000000000404440"},
]
var rows: Array = []
for c in cases:
var vals: Array = c["in"]
var r: Dictionary = Wallet.settlement(float(vals[0]), float(vals[1]), float(vals[2]), bool(vals[3]))
_check(not r.is_empty(), "wallet settlement must be valid for %s" % JSON.stringify(vals))
_check(Wallet.raw(float(r["balance"])) == str(c["balance"]), "balance raw-bit parity for %s" % JSON.stringify(vals))
_check(Wallet.raw(float(r["new_total"])) == str(c["total"]), "total raw-bit parity for %s" % JSON.stringify(vals))
_check(Wallet.raw(float(r["gain"])) == str(c["gain"]), "gain raw-bit parity for %s" % JSON.stringify(vals))
var entries: Array = r["entries"]
if c.has("delta0"):
_check(Wallet.raw(float(entries[0]["delta"])) == str(c["delta0"]), "first entry delta parity")
_check(Wallet.raw(float(entries[1]["delta"])) == str(c["delta1"]), "second entry delta parity")
if c.has("refund"):
_check(Wallet.raw(float(r["refund"])) == str(c["refund"]), "respec refund raw-bit parity")
rows.append({"input": vals, "balance": Wallet.raw(float(r["balance"])),
"total": Wallet.raw(float(r["new_total"]))})
var rf: Dictionary = Wallet.refund(10.25, 50.75)
_check(Wallet.raw(float(rf["refund"])) == "f64le:0000000000404440", "standalone refund 40.5 raw bits")
_check(bool(rf["clears_levels"]), "positive refund clears levels")
# Malformed codec / non-canonical raw strings must be rejected.
_check(Wallet.read("f64le:000000000080334A", false) == null, "uppercase hex raw string rejected (codec is lowercase)")
_check(Wallet.read("f64le:zzzzzzzzzzzzzzzz", false) == null, "non-hex raw string rejected")
_check(Wallet.read(7, false) == null, "numeric shadow without legacy flag rejected")
_record("wallet_codec", {"vectors": rows, "codec": Wallet.CODEC,
"source": "CH@46cc8ad scripts/ancient_soul_wallet.gd"})
func _outsider_family_parity() -> void:
var book := Outsiders.new()
_check(book.definitions.size() == 10, "ten CH outsider definitions")
_check(book.listed_ids() == [1, 2, 3, 5, 6, 7, 8, 9, 10], "listed ids match CH (hidden id4 excluded)")
_check(not book.buy(4, 1) and not book.buy(1, 0), "hidden/zero buys rejected")
var expected := {
1: [0.0, 50.0, 125.0, 237.5], 2: [0.0, 5.0, 9.75, 14.2625],
3: [0.0, 100.0, 200.0, 300.0], 4: [0.0, 50.0, 125.0, 237.5],
5: [0.0, 1000.0, 4000.0, 9000.0], 6: [0.0, 12.5, 25.0, 37.5],
7: [0.0, 25.0, 50.0, 75.0], 8: [0.0, 50.0, 100.0, 150.0],
9: [0.0, 75.0, 150.0, 225.0], 10: [0.0, 100.0, 200.0, 300.0],
}
var rows: Array = []
for id: int in expected.keys():
var levels: Array = expected[id]
var values: Array = []
for lv: int in [0, 1, 2, 3]:
book.levels[id] = lv
var value := book.bonus(id).to_float()
values.append(value)
_check(is_equal_approx(value, float(levels[lv])),
"bonus id%d L%d = %f expected %f" % [id, lv, value, float(levels[lv])])
rows.append({"id": id, "bonus": values})
_record("bonus_families", {"rows": rows, "source": "CH@46cc8ad outsider_book.gd"})
func _tp_parity() -> void:
var book := Outsiders.new()
var vectors := [[0.0, 0.02], [100.0, 0.0267975272838431],
[1000.0, 0.0796118092432049], [10000.0, 0.238548974275391]]
var rows: Array = []
for pair in vectors:
book.total = float(pair[0])
var got := book.transcendent_power()
var want := (1.0 - exp(-0.0003 * float(pair[0]))) * 0.23 + 0.02
_check(is_equal_approx(got, float(pair[1])) and is_equal_approx(got, want),
"TP for total %f = %.15f expected %.15f" % [float(pair[0]), got, float(pair[1])])
rows.append({"total": float(pair[0]), "tp": got})
_record("tp", {"rows": rows, "formula": "(1-exp(-0.0003*total))*0.23+0.02",
"pins": ["PR009", "PR010", "PR011"]})
func _cost_and_respec_parity() -> void:
var q := Outsiders.new()
q.balance = 6.0
q.total = 6.0
var quote := q.quote(1, 3)
_check(int(quote["count"]) == 3 and int(quote["cost"]) == 6 and bool(quote["affordable"]),
"CH quote(1,3) = 3 levels for 6 AS")
_check(q.buy(1, 3) and int(q.levels[1]) == 3, "buy id1 three levels")
_check(q.balance == 0.0 and q.total == 6.0, "buy debits balance only, not total")
_check(is_equal_approx(q.respec(), 6.0) and int(q.levels[1]) == 0,
"respec refunds 6 AS and clears levels")
var cap := Outsiders.new()
cap.levels[2] = 149
cap.balance = 1000000.0
cap.total = 1000000.0
_check(cap.buy(2, 10) and int(cap.levels[2]) == 150, "Chor'gorloth cap 150")
_check(cap.balance == 999850.0 and not cap.buy(2, 1), "cap-level purchase exact debit and reject")
_record("cost_respec", {"quote": quote, "cap_balance": cap.balance, "cap_level": int(cap.levels[2])})
func _strict_import() -> void:
var s := Outsiders.new()
s.balance = 10.25
s.total = 50.75
s.levels[1] = 3
var snap := s.snapshot()
_check(str(snap["balance"]) == "f64le:0000000000802440", "snapshot balance raw bits")
_check(str(snap["total"]) == "f64le:0000000000604940", "snapshot total raw bits")
_check(int(snap["wallet_codec"]) == Wallet.CODEC, "snapshot codec")
var restored := Outsiders.new()
_check(restored.restore(snap, false), "strict restore of canonical snapshot")
_check(restored.balance == 10.25 and restored.total == 50.75 and int(restored.levels[1]) == 3,
"restored wallet values")
var tampered := snap.duplicate(true)
tampered["balance"] = "f64le:000000000080334A"
_check(not restored.restore(tampered, false), "strict restore rejects tampered raw bit string")
tampered = snap.duplicate(true)
tampered["levels"]["10"] = 1.5
_check(not restored.restore(tampered, false), "strict restore rejects out-of-cap level")
_record("strict_import", {"snapshot": snap, "tampered_rejected": true})
func _sacrifice_and_earned() -> void:
var profile := ProfileStore.fresh_profile()
profile["hero_souls_sacrificed"] = 3.0
profile["oath_souls_spent"] = 6.0
profile["hero_souls"] = 4.0
var sac := Transcendence.sacrifice_inputs(profile)
# CH folds the POST-ascension hero soul balance into sacrifice:
# 4 (balance) + floor(0/2000) + 0 primal = 4; 3 + 6 + 4 = 13.
_check(int(float(sac["hero_souls_after_ascension"])) == 4, "post-ascension balance from S1 preview")
_check(int(float(sac["total"])) == 13, "sacrifice = carried + oath spent + post-ascension balance")
var earned := [0.0, 9.0, 10.0, 100.0, 1000000000000.0]
var want := [0, 4, 5, 10, 60]
var rows: Array = []
for i in earned.size():
var got := Transcendence.earned_total(earned[i])
_check(got == want[i], "earned_total(%f)=%d expected %d" % [earned[i], got, want[i]])
rows.append({"sacrifice": earned[i], "earned": got})
_record("sacrifice", {"inputs": sac, "earned": rows, "k": Transcendence.K_COEFFICIENT,
"pin": "PR008"})
func _primal_parity() -> void:
var book := Outsiders.new()
var expected := {100: 1, 105: 21, 110: 21, 200: 36}
var rows: Array = []
for zone: int in expected.keys():
var got := Transcendence.transcendent_primal_reward(zone, book)
_check(got == int(expected[zone]), "transcendent primal zone %d = %d expected %d" % [zone, got, int(expected[zone])])
rows.append({"zone": zone, "reward": got})
record_primal(rows)
func record_primal(rows: Array) -> void:
_record("primal", {"rows": rows, "source": "CH@46cc8ad game_state.gd:1405-1413"})
func _preview_and_apply_once() -> void:
var profile := ProfileStore.fresh_profile()
profile["completed_nodes"] = ["R1-C01-N10"]
profile["completed_regions"] = 5 # WR gate = CH zone>=300 adaptation
profile["hero_levels"] = {"H01": 3000, "H02": 2000}
profile["hero_souls"] = 4
profile["primal_souls"] = 2
profile["oath_souls_spent"] = 6
var pv := Transcendence.preview(profile)
_check(int(pv["earned_total"]) == 5, "preview earns floor(5*log10(14))=5")
_check(bool(pv["can_transcend"]), "unlocked new-sky profile can transcend")
_check(float(pv["sacrifice"]["total"]) == 14.0, "sacrifice folds level reward + primal souls")
var first := Transcendence.apply_transcendence(profile, false, 0)
_check(bool(first.get("ok")), "first transcendence must succeed")
_check(float(pv["balance"]) == 5.0, "settled balance 5")
var wallet: Dictionary = profile["ancient_souls"]
_check(str(wallet["balance"]) == Wallet.raw(5.0), "wallet balance credited once")
_check(str(wallet["total"]) == Wallet.raw(5.0), "wallet total credited once")
_check(int(profile["transcensions"]) == 1, "transcensions increments")
_check(float(profile["hero_souls_sacrificed"]) == 14.0, "sacrifice carried into next sky")
_check(float(profile["oath_souls_spent"]) == 0.0, "oath spend ledger cleared after fold")
_check(int(profile["hero_souls"]) == 0 and int(profile["primal_souls"]) == 0, "first-tier soul wallet cleared")
var receipt: Dictionary = profile["last_transcendence"]
_check(str(receipt.get("kind", "")) == "transcendence", "receipt kind")
var decoded: Variant = Wallet.decode_receipt(receipt, false)
_check(decoded is Dictionary, "receipt survives strict codec decode")
var claims: Array = profile["claimed_rewards"]
_check(claims.has("transcendence:0"), "exactly-once claim key persisted")
_check((profile["transcendence_history"] as Array).size() == 1, "one bounded receipt")
var stale := Transcendence.apply_transcendence(profile, false, 0)
_check(not bool(stale.get("ok")), "stale confirmation refused")
_check(str(stale.get("reason", "")) == "stale_run", "stale reason (CH expected_run adaptation)")
# Replay against a stale in-memory profile that still reports count 0: the
# claim key from the first reset must block a second payment at that sky.
var stale_profile := profile.duplicate(true)
stale_profile["new_sky_count"] = 0
var replay := Transcendence.apply_transcendence(stale_profile)
_check(not bool(replay.get("ok")), "replayed claim refused")
_check(str(replay.get("reason", "")) == "already_claimed", "replay reason")
_check(int(profile["transcensions"]) == 1, "duplicate must not increment counter")
_check(str((profile["ancient_souls"] as Dictionary)["balance"]) == Wallet.raw(5.0),
"duplicate must not credit wallet twice")
_record("apply_once", {"earned": int(pv["earned_total"]), "balance": profile["ancient_souls"],
"transcensions": int(profile["transcensions"]), "stale": stale.get("reason"),
"replay": replay.get("reason")})
var locked := ProfileStore.fresh_profile()
var blocked := Transcendence.apply_transcendence(locked)
_check(not bool(blocked.get("ok")), "locked profile cannot transcend")
_check(str(blocked.get("reason", "")) == "new_sky_locked", "locked reason")
_record("locked", {"reason": blocked.get("reason")})
func _save_first_failure() -> void:
var original := OS.get_environment("WINDROOST_SAVE_DIR")
var dir := ProjectSettings.globalize_path("res://artifacts")
DirAccess.make_dir_recursive_absolute(dir)
var blocker := dir.path_join("s3-blocked-save-dir")
var f := FileAccess.open(blocker, FileAccess.WRITE)
if f != null:
f.store_string("not a directory")
f.close()
OS.set_environment("WINDROOST_SAVE_DIR", blocker)
var profile := ProfileStore.fresh_profile()
profile["completed_nodes"] = ["R1-C01-N10"]
profile["completed_regions"] = 5
profile["hero_souls"] = 40
var before := JSON.stringify(profile)
var result := Transcendence.apply_transcendence(profile)
_check(not bool(result.get("ok")), "failed save must refuse transcendence")
_check(str(result.get("reason", "")) == "save_failed", "save_failed reason")
_check(JSON.stringify(profile) == before, "save-first leaves caller profile untouched on failure")
OS.set_environment("WINDROOST_SAVE_DIR", original)
DirAccess.remove_absolute(blocker)
_record("save_first", {"reason": result.get("reason"), "caller_unchanged": JSON.stringify(profile) == before})
func _migration() -> void:
var dir := ProjectSettings.globalize_path("res://artifacts")
DirAccess.make_dir_recursive_absolute(dir)
var legacy := {"schema": 6, "gold": 777, "essence": 11, "sky_marks": 5,
"hero_souls": 3, "completed_nodes": ["R1-C01-N01"]}
var f := FileAccess.open(ProfileStore.save_path(), FileAccess.WRITE)
if f != null:
f.store_string(JSON.stringify(legacy))
f.close()
var migrated := ProfileStore.load_profile()
_check(int(migrated.get("schema", -1)) == 7, "v6 save migrates to schema 7")
_check(int(migrated.get("gold", -1)) == 777, "v6 gold survives")
_check(int(migrated.get("sky_marks", -1)) == 5, "v6 sky marks survive")
_check(int(migrated.get("hero_souls", -1)) == 3, "v6 hero souls survive")
_check((migrated.get("ancient_souls") as Dictionary).is_empty(), "new wallet starts empty")
_check(int(migrated.get("transcensions", -1)) == 0, "transcensions starts at 0")
_check(float(migrated.get("hero_souls_sacrificed", -1)) == 0.0, "sacrifice ledger starts at 0")
_record("legacy_v6", {"schema": migrated.get("schema"), "gold": migrated.get("gold"),
"ancient_souls": migrated.get("ancient_souls")})
var tampered := {"schema": 7, "gold": 50, "ancient_souls": {"wallet_codec": 1,
"balance": "f64le:000000000080334A", "total": "f64le:0000000000000040", "levels": {}},
"hero_souls_sacrificed": -9, "transcendence_history": 7}
var f2 := FileAccess.open(ProfileStore.save_path(), FileAccess.WRITE)
if f2 != null:
f2.store_string(JSON.stringify(tampered))
f2.close()
var safe := ProfileStore.load_profile()
_check(int(safe.get("gold", -1)) == 50, "tampered v7 keeps valid gold")
_check((safe.get("ancient_souls") as Dictionary).is_empty(), "tampered wallet dropped")
_check(float(safe.get("hero_souls_sacrificed", -1)) == 0.0, "negative sacrifice clamped")
_check(safe.get("transcendence_history") is Array, "malformed receipt history normalized")
_record("tampered_v7", {"gold": safe.get("gold"), "ancient_souls": safe.get("ancient_souls")})
DirAccess.remove_absolute(ProfileStore.save_path())
func _finish() -> void:
var passed := _problems.is_empty()
var report := {"schema": 1, "kind": "windroost_transcendence_test",
"ch_source_revision": "46cc8ad46ac886918ee1b2c1b0e946e1f5e17c04",
"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("transcendence_test.json"), FileAccess.WRITE)
if out != null:
out.store_string(JSON.stringify(report, "\t", true))
out.close()
for e in _steps:
print("TRANSCEND_STEP %s" % JSON.stringify(e))
if passed:
print("TRANSCENDENCE_PASS")
else:
print("TRANSCENDENCE_FAIL %s" % "; ".join(_problems))
quit(0 if passed else 1)