TableTower/tests/test_simulate.gd

99 lines
5 KiB
GDScript3
Raw Normal View History

extends SceneTree
## Keeps the simulator (BoardTester, and its CLI tools/simulate_board.gd) working: runs it on a small inline board with
## a choice, a loop, a check, a bad effect, an unreachable table and a sheet.
## Run: godot --headless --path . -s res://tests/test_simulate.gd
var _failures := 0
func _init() -> void:
var b := _board()
var r: Dictionary = await BoardTester.simulate(b, "start", {"runs": 60, "seed": 3, "track": ["gold", "@name"]})
_eq(r["runs"], 60, "60 runs")
_eq(r["completed"], 60, "every run completes")
_check((r["halted"] as Dictionary).is_empty(), "nothing halted: %s" % str(r["halted"]))
var a: Dictionary = r["actions"]
_check(a["max"] >= a["p95"] and a["p95"] >= 4 and a["mean"] > 4.0, "action stats are sensible: %s" % str(a))
var errs: Array = r["errors"]
_check(errs.size() == 1 and str(errs[0]["table"]).contains("pick") and str(errs[0]["entry"]) == "Broken", "the bad effect is reported with its table and entry: %s" % str(errs))
_eq(r["unreached"], ["island"], "the island table is never reached")
var uc: Dictionary = r["unchosen"]
_check(not uc.has("check"), "an entry that can't come up (13 on 2d6) is not reported as unchosen: %s" % str(uc))
_check(not uc.has("pick"), "every pick option was chosen at some point")
var sheet := str(r["sample_sheet"])
_check(sheet.begins_with("Hero: ") and sheet.contains("Gold "), "a sample sheet is kept: %s" % sheet)
_check(not BoardTester.is_clean(r), "a run with an effect error is not clean")
# Each run starts with empty variables: gold never grows past one run's worth.
var gold := int((r["sample_vars"].get("gold", {}) as Dictionary).get("value", -1))
_check(gold <= 18, "variables start clean for every run (gold %d)" % gold)
var mk: Array = r["unset_markers"]
_check(mk.size() == 1 and mk[0]["entry"] == "(prompt)" and str(mk[0]["text"]).contains("@nothing"), "an unset variable in a prompt is reported: %s" % str(mk))
var tg: Dictionary = r["track"]["gold"]
_check(int(tg["n"]) > 0 and float(tg["max"]) <= 18.0 and float(tg["min"]) >= 0.0, "--track summarises a number variable: %s" % str(tg))
_eq(r["track"]["name"]["n"], 0, "--track skips text variables")
var lg: Dictionary = r["longest"]
_check(int(lg["actions"]) == int(a["max"]) and not (lg["top"] as Array).is_empty(), "the longest run's busiest tables are kept: %s" % str(lg))
# A preference picks the matching option every time.
var r2: Dictionary = await BoardTester.simulate(b, "start", {"runs": 20, "seed": 4, "prefer": "^Mine$"})
_check((r2["unchosen"] as Dictionary).has("pick"), "--prefer always picks the matching option")
_check(int((r2["table_hits"] as Dictionary).get("mine", 0)) == 20 * 3, "the preferred option's pipe ran on every pass")
# The action limit is reported as a halt reason.
var r3: Dictionary = await BoardTester.simulate(b, "start", {"runs": 5, "seed": 5, "max_actions": 4})
_eq((r3["halted"] as Dictionary).get("Action limit", 0), 5, "the action limit is a halt reason")
var text := BoardTester.format_report(r)
_check(text.contains("Runs: 60") and text.contains("Tables never reached: 1") and text.contains("Sample final sheet") and text.contains("@gold: min"), "the report prints:\n%s" % text)
print("%s: %d failure(s)" % ["PASS" if _failures == 0 else "FAIL", _failures])
quit(1 if _failures else 0)
func _board() -> TowerBoard:
var b := TowerBoard.new()
b.id = "sim-test"
var start := RollTable.new("start", "Start")
var e := TableEntry.new("e1", "Begin")
e.effects = "reset @gold; @name = \"Hero\""
e.add_pipe("loop")
e.add_pipe("sheet")
start.entries.append(e)
var loop := RollTable.create(RollTable.KIND_LOOP, "loop", "Three times", "times")
loop.loop_expr = "3"
loop.entries[0].add_pipe("pick")
var pick := RollTable.create(RollTable.KIND_CHOICE, "pick", "Pick", "pick")
pick.prompt = "Which? {@nothing}"
pick.entries[0].text = "Mine"
pick.entries[0].effects = "@gold += 1d6"
pick.entries[0].add_pipe("mine")
pick.entries[1].text = "Check"
pick.entries[1].add_pipe("check")
pick.entries[2].text = "Broken"
pick.entries[2].effects = "@gold += nope("
var mine := RollTable.new("mine", "Mine")
mine.entries.append(TableEntry.new("e1", "Dig"))
var check := RollTable.new("check", "Check")
check.dice = "2d6"
for spec in [[2, 7, "Fail"], [8, 12, "Pass"], [13, 13, "Impossible"]]:
var ce := TableEntry.new("c%d" % spec[0], spec[2])
ce.has_range = true
ce.range_lo = spec[0]
ce.range_hi = spec[1]
check.entries.append(ce)
var island := RollTable.new("island", "Island")
island.entries.append(TableEntry.new("e1", "Alone"))
var sheet := RollTable.create(RollTable.KIND_SHEET, "sheet", "Sheet")
sheet.entries[0].text = "{@name}: Gold {@gold}"
var gold := RollTable.create(RollTable.KIND_VARIABLE, "v_gold", "Gold", "number")
gold.var_name = "gold"
for t in [start, loop, pick, mine, check, island, sheet, gold]:
b.tables.append(t)
return b
func _eq(got: Variant, want: Variant, what: String) -> void:
_check(str(got) == str(want), "%s\n got: %s\n want: %s" % [what, str(got), str(want)])
func _check(ok: bool, what: String) -> void:
if not ok:
_failures += 1
print(" ✗ ", what)