TableTower/tests/test_book_collect.gd
RobinFiredrake d41a6e40aa Tests: 15 s budget, shared Godot workers; a freed Main cancels its roll
tools/run_tests.sh stops every run at 15 s and lists what was not run.
Suites are nodes run by tests/run_suites.gd, several per Godot process;
TestBase no longer compiles the app (AppTestBase does). Audit: fewer
repeats and less setup; 51 suites in about 12 s (was 45 s).

Fix a crash when a roll was still animating as its Main was freed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 16:19:30 -06:00

180 lines
7.3 KiB
GDScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

extends TestBase
## Book-table features for multi-part and repeated rolls: columns read as one
## phrase (column_join), "roll twice and combine" rows (TableEntry.repeat),
## saving results into a list value, and a pick with one option answering
## itself.
## Run: tools/run_tests.sh book_collect
func run() -> void:
await _test_join_and_repeat()
await _test_repeat_reroll()
await _test_repeat_one_column()
await _test_list_save()
await _test_only_option()
_test_save_load()
_test_lint()
finish()
func _ctx(b: TowerBoard, seed_value: int = 1) -> RollContext:
var rng := RandomNumberGenerator.new()
rng.seed = seed_value
return Roller.new_context(b, rng, {}, {})
func _ranged(t: RollTable, eid: String, lo: int, hi: int, text: String, cells: Array = []) -> TableEntry:
var e := TableEntry.new(eid, text)
e.has_range = true
e.range_lo = lo
e.range_hi = hi
e.cells.assign(cells)
t.entries.append(e)
return e
func _list_card(name: String) -> RollTable:
var v := RollTable.create("variable", "v_" + name, name.capitalize(), "list")
v.var_name = name
return v
## "Surface": 1d4, 1st Part | 2nd Part read as one phrase; 3–4 roll twice and
## combine. "Start" rolls it 3 times into the list @surface.
func _surface_board() -> TowerBoard:
var b := TowerBoard.new()
var t := RollTable.new("surface", "Surface")
t.dice = "1d4"
t.column_rolls = RollTable.COLUMNS_EACH
t.column_join = " "
t.columns = [{"name": "1st Part"}, {"name": "2nd Part"}]
_ranged(t, "a", 1, 1, "Ash", ["Basins"])
_ranged(t, "b", 2, 2, "Black", ["Canyons"])
_ranged(t, "r", 3, 4, "Roll twice and combine", ["Roll twice and combine"]).repeat = {"count": 2}
var start := RollTable.new("start", "Start")
var e := TableEntry.new("s", "Go")
e.add_pipe("surface", 3, "@surface")
start.entries.append(e)
b.tables.append_array([start, t, _list_card("surface")])
return b
func _test_join_and_repeat() -> void:
var b := _surface_board()
var combined := false
for s in 40:
var ctx := _ctx(b, s)
var tree := await Roller.roll_async(b, "start", ctx)
for kid: Dictionary in tree["children"]:
var text := Formatter.strip_tags(str(kid["summary"]))
_check(not text.contains(":") and not text.contains("Roll twice"), "a joined result reads as one phrase: %s" % text)
if text.contains(" & "):
combined = true
_check((kid["why"] as Array).any(func(f: Dictionary) -> bool: return f["type"] == "repeat"), "a repeat leaves a why fact")
var items := Expr.list_items(ctx.get_value("@surface"))
_check(items.size() >= 1 and items.size() <= 3, "the thread's saves collect in the list: %s" % str(items))
_check(combined, "a roll-twice row combines two rolls of its column")
var fact := {"type": "repeat", "column": "1st Part", "rolls": [{"value": 1, "text": "Ash"}, {"value": 2, "text": "Black"}]}
_eq(Describe.why(fact, b), "1st Part: Rolled it 2 times and combined them: Ash (1), Black (2)", "the repeat fact as a sentence")
func _test_repeat_reroll() -> void:
var b := _surface_board()
var r := b.get_table("surface").find_entry("r")
r.repeat = {"count": 2, "reroll": ["r"]}
for s in 40:
var tree := await Roller.roll_async(b, "start", _ctx(b, s))
for kid: Dictionary in tree["children"]:
for col: Dictionary in kid["columns"]:
_check(str(col["text"]).count(" & ") <= 1, "rolling again on the repeat row keeps it to two: %s" % col["text"])
## A combine row set to the 1st column only: the 2nd column just shows its
## own cell when the row comes up there.
func _test_repeat_one_column() -> void:
var b := _surface_board()
var r := b.get_table("surface").find_entry("r")
r.repeat = TableEntry.clean_repeat({"count": 2, "reroll": ["r"], "columns": [0]})
_eq(r.repeat.get("columns"), [0], "the columns list is kept")
var plain_second := false
for s in 60:
var tree := await Roller.roll_async(b, "start", _ctx(b, s))
for kid: Dictionary in tree["children"]:
var second: Dictionary = kid["columns"][1]
_check(not str(second["text"]).contains(" & "), "the 2nd column doesn't roll again: %s" % second["text"])
plain_second = plain_second or str(second["text"]) == "Roll twice and combine"
_check(plain_second, "it shows the row's own cell there instead")
_check(RollColumns.repeats_in(r, 0) and not RollColumns.repeats_in(r, 1), "repeats_in follows the list")
func _test_list_save() -> void:
var b := TowerBoard.new()
var res := RollTable.new("res", "Resource")
for k in 3:
res.entries.append(TableEntry.new("e%d" % k, ["Silicates", "Water ice", "Helium-3"][k]))
var start := RollTable.new("start", "Start")
var e := TableEntry.new("s", "Go")
e.effects = "@found = \"\""
e.add_pipe("res", 10, "@found")
e.add_pipe("res2", 3, "@last")
start.entries.append(e)
var res2 := RollTable.new("res2", "Other")
for k in 3:
res2.entries.append(TableEntry.new("o%d" % k, "Item %d" % k))
b.tables.append_array([start, res, res2, _list_card("found")])
var ctx := _ctx(b)
await Roller.roll_async(b, "start", ctx)
var items := Expr.list_items(ctx.get_value("@found"))
_check(items.size() >= 2 and items.size() <= 3, "ten rolls into a list keep each result once: %s" % str(items))
_check(not str(ctx.get_value("@last")).contains(","), "an undeclared value is still replaced: %s" % ctx.get_value("@last"))
await Roller.roll_async(b, "start", ctx)
_check(Expr.list_items(ctx.get_value("@found")).size() <= 3, "an effect that sets the list empties it first")
_eq(DescribeCards.pipe(e, "res", b), "×10 · adds to Found", "the thread label says it adds to the list")
func _test_only_option() -> void:
var b := TowerBoard.new()
var pick := RollTable.create("choice", "which", "Which type?")
pick.choice = "pick"
pick.options_from = "@types"
pick.save_as = "@page"
pick.entries.assign([TableEntry.new("c", RollChoice.CHOSEN)])
b.tables.append_array([pick, _list_card("types")])
var asked := [0]
var ctx := _ctx(b)
ctx.prompt = func(_req: Dictionary) -> Variant:
asked[0] += 1
return -1
ctx.save("@types", "Barren")
var node := await Roller.roll_async(b, "which", ctx)
_eq(asked[0], 0, "one option: no question")
_eq(ctx.get_value("@page"), "Barren", "and it's picked")
_check(bool(node.get("only_option", false)), "the node says so")
ctx.save("@types", "Barren, Frozen")
await Roller.roll_async(b, "which", ctx)
_eq(asked[0], 1, "two options: the player is asked")
func _test_save_load() -> void:
var b := _surface_board()
b.get_table("surface").find_entry("r").repeat = {"count": 3, "reroll": ["r"]}
var text := JSON.stringify(b.to_dict())
for d: Dictionary in [JSON.parse_string(text), JsonIO.canonical(JSON.parse_string(text))]:
var back := TowerBoard.new()
back.load_dict(d)
var t := back.get_table("surface")
_eq(t.column_join, " ", "column_join round-trips (a space)")
_eq(t.find_entry("r").repeat, {"count": 3, "reroll": ["r"]}, "repeat round-trips")
_eq(t.find_entry("a").repeat, {}, "other rows have none")
func _test_lint() -> void:
var b := _surface_board()
var t := b.get_table("surface")
for e in t.entries:
e.repeat = {"count": 2}
var msgs := Roller.lint_pipes(b).map(func(i: Dictionary) -> String: return str(i["message"]))
_check(msgs.any(func(m: String) -> bool: return m.contains("can never finish")), "lint: every row repeats")
t.columns = []
msgs = Roller.lint_pipes(b).map(func(i: Dictionary) -> String: return str(i["message"]))
_check(msgs.any(func(m: String) -> bool: return m.contains("no columns")), "lint: repeat without columns")