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>
374 lines
16 KiB
GDScript
374 lines
16 KiB
GDScript
extends TestBase
|
||
## Headless checks for dice, weights, pipe options, card mode and JSON.
|
||
## Run: tools/run_tests.sh model
|
||
|
||
|
||
func run() -> void:
|
||
_test_dice()
|
||
_test_weights()
|
||
_test_ranged_dice()
|
||
_test_pipes()
|
||
_test_cards()
|
||
_test_cycles()
|
||
_test_seed()
|
||
_test_json()
|
||
_test_sample()
|
||
finish()
|
||
|
||
|
||
func _entry(id: String, text: String, weight: int = 1) -> TableEntry:
|
||
var e := TableEntry.new(id, text)
|
||
e.weight = weight
|
||
return e
|
||
|
||
|
||
func _ranged(id: String, dice: String, spans: Array) -> RollTable:
|
||
var t := RollTable.new(id, id)
|
||
t.dice = dice
|
||
for i in spans.size():
|
||
var e := _entry("%s%d" % [id, i], "r%d" % i)
|
||
e.has_range = true
|
||
e.range_lo = spans[i][0]
|
||
e.range_hi = spans[i][1]
|
||
t.entries.append(e)
|
||
return t
|
||
|
||
|
||
func _board(tables: Array) -> TowerBoard:
|
||
var b := TowerBoard.new()
|
||
for t in tables:
|
||
b.tables.append(t)
|
||
return b
|
||
|
||
|
||
func _rng(seed_value: int) -> RandomNumberGenerator:
|
||
var r := RandomNumberGenerator.new()
|
||
r.seed = seed_value
|
||
return r
|
||
|
||
|
||
func _test_dice() -> void:
|
||
var d := Dice.parse("2d6")
|
||
_check(d.valid and d.min_value() == 2 and d.max_value() == 12, "2d6 parses with range 2–12")
|
||
var d2 := Dice.parse(" 1D8 + 2 ")
|
||
_check(d2.valid and d2.min_value() == 3 and d2.max_value() == 10 and d2.expression() == "1d8+2", "1d8+2 parses (spaces, case)")
|
||
var d3 := Dice.parse("3d4-1")
|
||
_check(d3.valid and d3.min_value() == 2 and d3.max_value() == 11, "3d4-1 parses")
|
||
_check(Dice.parse("d20").valid and Dice.parse("d20").count == 1, "d20 means 1d20")
|
||
for bad in ["", "abc", "0d6", "21d6", "1d1", "1d1001", "2d", "2d6+", "1d6*2", "-1d6", "1d6+x", "1d6+99999"]:
|
||
_check(not Dice.parse(bad).valid, "invalid dice rejected: '%s'" % bad)
|
||
_check(Dice.parse("nope").min_value() == 0 and Dice.parse("nope").roll(_rng(1)) == 0, "invalid dice roll nothing")
|
||
_check(d.label() == "2d6" and Dice.parse("1d100").label() == "d100" and d2.label() == "1d8+2", "dice labels")
|
||
|
||
for expr in ["2d6", "1d100", "1d8+2", "20d1000", "3d4-1"]:
|
||
var dist := Dice.parse(expr).distribution()
|
||
var sum := 0.0
|
||
for v: int in dist:
|
||
sum += float(dist[v])
|
||
_check(absf(sum - 1.0) < 1e-9, "distribution of %s sums to 1 (%.12f)" % [expr, sum])
|
||
var two := d.distribution()
|
||
_check(absf(float(two[7]) - 1.0 / 6.0) < 1e-12 and absf(float(two[2]) - 1.0 / 36.0) < 1e-12, "2d6: P(7)=1/6, P(2)=1/36")
|
||
_check(not two.has(1) and not two.has(13), "2d6 has no impossible totals")
|
||
_check(d2.distribution().has(3) and not d2.distribution().has(2), "1d8+2 distribution starts at 3")
|
||
|
||
var r := _rng(7)
|
||
var seen := {}
|
||
for i in 3000:
|
||
var v := d.roll(r)
|
||
seen[v] = seen.get(v, 0) + 1
|
||
_check(seen.size() == 11, "2d6 rolls cover 2–12")
|
||
_check(absf(float(seen[7]) / 3000.0 - 1.0 / 6.0) < 0.03, "2d6 rolls land on 7 about 1/6 of the time")
|
||
|
||
|
||
func _test_weights() -> void:
|
||
var t := RollTable.new("w", "W")
|
||
t.entries.append(_entry("a", "Gold", 3))
|
||
t.entries.append(_entry("b", "Silver", 1))
|
||
t.entries.append(_entry("c", "Never", 0))
|
||
_check(absf(t.chance(t.entries[0]) - 0.75) < 1e-9 and t.chance(t.entries[2]) == 0.0, "weighted chance() is weight/total")
|
||
_check(t.die_size() == 3 and t.dice_label() == "d3", "plain table die size stays the entry count")
|
||
_check(t.num_label(t.entries[2], 2) == "3", "plain num_label stays the row number")
|
||
var b := _board([t])
|
||
var r := _rng(99)
|
||
var counts := {}
|
||
for i in 8000:
|
||
var n := Roller.roll(b, "w", r)
|
||
var text: String = (n["entry"] as TableEntry).text
|
||
counts[text] = counts.get(text, 0) + 1
|
||
_check(absf(float(counts["Gold"]) / 8000.0 - 0.75) < 0.02, "weight 3 of 4 comes up about 75%")
|
||
_check(not counts.has("Never"), "weight 0 never comes up")
|
||
t.entries[0].weight = 0
|
||
t.entries[1].weight = 0
|
||
_check(Roller.roll(b, "w", r).has("stop"), "all-zero weights stop instead of crashing")
|
||
_check(t.chance(t.entries[0]) == 0.0, "all-zero weights give 0 chance")
|
||
|
||
|
||
func _test_ranged_dice() -> void:
|
||
var t := _ranged("wx", "2d6", [[2, 4], [5, 9], [10, 12]])
|
||
_check(t.is_ranged() and t.die_size() == 12 and t.min_value() == 2, "2d6 ranged table spans 2–12")
|
||
_check(t.dice_label() == "2d6", "dice_label is 2d6")
|
||
_check(t.range_problems() == "", "2d6 ranges 2–12 are clean")
|
||
var total := 0.0
|
||
for e in t.entries:
|
||
total += t.chance(e)
|
||
_check(absf(total - 1.0) < 1e-9, "2d6 chances sum to 1")
|
||
_check(absf(t.chance(t.entries[0]) - 6.0 / 36.0) < 1e-9, "2–4 on 2d6 is 6/36")
|
||
_check(absf(t.chance(t.entries[1]) - 24.0 / 36.0) < 1e-9, "5–9 on 2d6 is 24/36")
|
||
var seven := _ranged("s", "2d6", [[2, 6], [7, 7], [8, 12]])
|
||
_check(seven.chance(seven.entries[1]) > seven.chance(seven.entries[0]) / 5.0, "7 is the most likely single value")
|
||
_check(t.fmt(5) == "5" and t.num_label(t.entries[1], 1) == "5–9", "2d6 uses plain numbers")
|
||
|
||
var gappy := _ranged("g", "2d6", [[2, 5], [7, 14]])
|
||
_check(gappy.range_problems().contains("No result for 6."), "gap inside 2–12 reported: %s" % gappy.range_problems())
|
||
_check(gappy.range_problems().contains("outside"), "out-of-span range reported: %s" % gappy.range_problems())
|
||
var one_low := _ranged("l", "2d6", [[1, 12]])
|
||
_check(one_low.range_problems().contains("outside"), "range starting below min reported")
|
||
var d100 := _ranged("h", "1d100", [[1, 50], [51, 100]])
|
||
_check(d100.fmt(100) == "00" and d100.fmt(7) == "07", "1d100 table formats 00 and two digits")
|
||
var d6 := _ranged("k", "1d8+2", [[3, 10]])
|
||
_check(d6.fmt(10) == "10" and d6.fmt(3) == "3" and d6.dice_label() == "1d8+2", "1d8+2 uses plain numbers")
|
||
|
||
var r := _rng(5)
|
||
var b := _board([t])
|
||
var low := 0
|
||
for i in 4000:
|
||
var n := Roller.roll(b, "wx", r)
|
||
_check(int(n["value"]) >= 2 and int(n["value"]) <= 12, "ranged value in span")
|
||
if int(n["index"]) == 0:
|
||
low += 1
|
||
_check(absf(float(low) / 4000.0 - 6.0 / 36.0) < 0.025, "ranged 2d6 roll frequencies follow the distribution")
|
||
|
||
|
||
func _test_pipes() -> void:
|
||
var src := RollTable.new("src", "Src")
|
||
var e := _entry("e1", "Go")
|
||
e.add_pipe("dst", 3, "foe")
|
||
e.add_pipe("other")
|
||
e.add_pipe("dst", 3, "foe")
|
||
src.entries.append(e)
|
||
_check(e.pipes.size() == 2, "thread targets stay unique")
|
||
var dst := RollTable.new("dst", "Dst")
|
||
dst.entries.append(_entry("d1", "Thing"))
|
||
var other := RollTable.new("other", "Other")
|
||
other.entries.append(_entry("o1", "Else"))
|
||
var b := _board([src, dst, other])
|
||
var n := Roller.roll(b, "src", _rng(1))
|
||
var kids: Array = n["children"]
|
||
_check(kids.size() == 4, "count 3 plus one plain thread gives 4 children (got %d)" % kids.size())
|
||
var saved := 0
|
||
for c: Dictionary in kids:
|
||
if c.get("save_as", "") == "foe":
|
||
saved += 1
|
||
_check(saved == 3, "save_as is set on the 3 counted children")
|
||
_check(not (kids[3] as Dictionary).has("save_as"), "plain thread has no save_as")
|
||
_check(not n.has("save_as"), "root node has no save_as")
|
||
_check(Roller.flatten(n, []).size() == 5, "flatten walks counted children")
|
||
var lit_e := {}
|
||
var lit_p := {}
|
||
Roller.collect_lit(n, lit_e, lit_p)
|
||
_check(lit_p.has(Roller.pipe_key("src", "e1", "dst")), "counted thread is lit")
|
||
|
||
e.remove_pipe("dst")
|
||
_check(e.pipes == ["other"] and e.pipe_count.is_empty() and e.pipe_save.is_empty(), "remove_pipe clears options")
|
||
_check(e.count_for("other") == 1 and e.save_for("other") == "", "defaults are count 1, no save")
|
||
e.set_pipe_count("other", 0)
|
||
_check(e.count_for("other") == 1, "count below 1 clamps to 1")
|
||
e.set_pipe_save("other", "@loot")
|
||
_check(e.to_dict()["pipes"] == [{"table": "other", "save_as": "@loot"}], "save-only thread serializes as object")
|
||
|
||
|
||
func _test_cards() -> void:
|
||
var t := RollTable.new("deck", "Deck")
|
||
t.card_mode = true
|
||
t.entries.append(_entry("a", "A"))
|
||
t.entries.append(_entry("b", "B", 2))
|
||
t.entries.append(_entry("c", "C"))
|
||
t.entries.append(_entry("z", "Z", 0))
|
||
var b := _board([t])
|
||
var decks := {}
|
||
var r := _rng(21)
|
||
var got := {}
|
||
for i in 4:
|
||
var n := Roller.roll(b, "deck", r, 0, decks)
|
||
var tx: String = (n["entry"] as TableEntry).text
|
||
got[tx] = got.get(tx, 0) + 1
|
||
_check(not n.has("reshuffled"), "no reshuffle mid-deck")
|
||
_check(got.get("A", 0) == 1 and got.get("B", 0) == 2 and got.get("C", 0) == 1 and not got.has("Z"), "4 draws deal each copy once: %s" % str(got))
|
||
var dk: DeckState = decks["deck"]
|
||
_check(dk.cards_left(t) == Vector2i(0, 4), "deck shows 0 of 4 left")
|
||
var n5 := Roller.roll(b, "deck", r, 0, decks)
|
||
_check(n5.get("reshuffled", false) == true and not n5.has("stop"), "empty deck reshuffles and flags the node")
|
||
_check(dk.cards_left(t) == Vector2i(3, 4), "after reshuffle one card is drawn")
|
||
|
||
# Persisting through a dict.
|
||
var restored := DeckState.from_dict(dk.to_dict())
|
||
_check(restored.cards_left(t) == dk.cards_left(t), "DeckState round-trips through a dictionary")
|
||
|
||
# Stop mode.
|
||
t.deck_empty = "stop"
|
||
var d2 := {}
|
||
for i in 4:
|
||
Roller.roll(b, "deck", r, 0, d2)
|
||
var stopped := Roller.roll(b, "deck", r, 0, d2)
|
||
_check(stopped.get("stop", "") == "Empty" and stopped.get("deck_empty", false), "a deck that waits for Shuffle gives its Empty result")
|
||
(d2["deck"] as DeckState).reshuffle()
|
||
_check(not Roller.roll(b, "deck", r, 0, d2).has("stop"), "manual reshuffle deals again")
|
||
|
||
# Edits.
|
||
t.deck_empty = "reshuffle"
|
||
var d3 := DeckState.new()
|
||
for i in 4:
|
||
d3.draw(t, r)
|
||
_check(d3.cards_left(t) == Vector2i(0, 4), "fully drawn deck")
|
||
t.entries[0].weight = 3
|
||
_check(d3.cards_left(t) == Vector2i(2, 6), "raising a weight adds copies")
|
||
t.entries.append(_entry("n", "New"))
|
||
_check(d3.cards_left(t) == Vector2i(3, 7), "new entries start undrawn")
|
||
t.entries.remove_at(1)
|
||
_check(d3.cards_left(t) == Vector2i(3, 5), "deleted entries drop out")
|
||
var first := d3.draw(t, r)
|
||
_check(first.has("index"), "can still draw after edits")
|
||
t.entries[0].weight = 1
|
||
_check(d3.cards_left(t).x == 1 and d3.cards_left(t).y == 3, "lowering a weight removes copies (%s)" % str(d3.cards_left(t)))
|
||
|
||
# Ranged deck.
|
||
var rt := _ranged("rd", "2d6", [[2, 6], [7, 7], [8, 12]])
|
||
rt.card_mode = true
|
||
var rb := _board([rt])
|
||
var rd := {}
|
||
var seen := {}
|
||
for i in 3:
|
||
var n := Roller.roll(rb, "rd", r, 0, rd)
|
||
var idx: int = n["index"]
|
||
var v: int = n["value"]
|
||
seen[idx] = true
|
||
var e: TableEntry = rt.entries[idx]
|
||
_check(v >= e.range_lo and v <= e.range_hi, "ranged card value %d is inside its range" % v)
|
||
_check(seen.size() == 3, "ranged deck deals every entry once")
|
||
_check(Roller.roll(rb, "rd", r, 0, rd).get("reshuffled", false) == true, "ranged deck reshuffles")
|
||
|
||
|
||
func _test_cycles() -> void:
|
||
var a := RollTable.new("a", "A")
|
||
var ea := _entry("ea", "to b")
|
||
ea.add_pipe("b")
|
||
a.entries.append(ea)
|
||
var bt := RollTable.new("b", "B")
|
||
var eb := _entry("eb", "to a")
|
||
eb.add_pipe("a")
|
||
bt.entries.append(eb)
|
||
var board := _board([a, bt])
|
||
_check(Roller.endless_cycles(board) == ["a", "b"], "two-table loop is endless")
|
||
bt.entries.append(_entry("eb2", "exit"))
|
||
_check(Roller.endless_cycles(board).is_empty(), "an exit entry fixes the repeat")
|
||
bt.entries[1].weight = 0
|
||
_check(Roller.endless_cycles(board).size() == 2, "a zero-weight exit does not count")
|
||
bt.entries[1].weight = 1
|
||
var self_t := RollTable.new("s", "S")
|
||
var es := _entry("es", "again")
|
||
es.add_pipe("s")
|
||
self_t.entries.append(es)
|
||
_check(Roller.endless_cycles(_board([self_t])) == ["s"], "sole self-thread is endless")
|
||
self_t.entries.append(_entry("es2", "done"))
|
||
_check(Roller.endless_cycles(_board([self_t])).is_empty(), "self-thread on some entries is fine")
|
||
# An entry that pipes into a cycle and a fine table is still stuck.
|
||
var ok := RollTable.new("ok", "Ok")
|
||
ok.entries.append(_entry("k", "fine"))
|
||
var mix := RollTable.new("mix", "Mix")
|
||
var em := _entry("em", "both")
|
||
em.add_pipe("ok")
|
||
em.add_pipe("mix")
|
||
mix.entries.append(em)
|
||
_check(Roller.endless_cycles(_board([ok, mix])) == ["mix"], "entry needing a cycle blocks its table")
|
||
_check(Roller.endless_cycles(SampleBoard.create()).is_empty(), "sample spread has no endless cycles")
|
||
|
||
|
||
func _test_seed() -> void:
|
||
var board := SampleBoard.create()
|
||
var texts: Array[String] = []
|
||
for run in 2:
|
||
var r := _rng(2024)
|
||
var decks := {}
|
||
var out := PackedStringArray()
|
||
for i in 25:
|
||
for s in Roller.flatten(Roller.roll(board, "wilderness", r, 0, decks), []):
|
||
out.append("%s=%s" % [s["table"], s["text"]])
|
||
for s in Roller.flatten(Roller.roll(board, "party_roles", r, 0, decks), []):
|
||
out.append("%s=%s" % [s["table"], s["text"]])
|
||
texts.append("|".join(out))
|
||
_check(texts[0] == texts[1] and texts[0].length() > 100, "same seed gives the same chains")
|
||
var other := _rng(2025)
|
||
var diff := PackedStringArray()
|
||
for i in 25:
|
||
for s in Roller.flatten(Roller.roll(board, "wilderness", other), []):
|
||
diff.append("%s=%s" % [s["table"], s["text"]])
|
||
_check(not texts[0].begins_with("|".join(diff)), "a different seed gives a different run")
|
||
|
||
|
||
func _test_json() -> void:
|
||
var legacy := {"tables": [{"id": "old", "name": "Old", "x": 1, "y": 2, "die": 20, "entries": [
|
||
{"id": "e1", "text": "Low", "range": [1, 10], "pipes": ["x", {"table": "y", "count": 2, "save_as": "@pal"}]},
|
||
{"id": "e2", "text": "High", "range": [11, 20], "weight": 4, "details": "[b]hi[/b]"}]}]}
|
||
var b := TowerBoard.new()
|
||
_check(b.load_json(JSON.stringify(legacy)), "legacy spread loads")
|
||
var t := b.get_table("old")
|
||
_check(t.dice == "1d20" and t.die_size() == 20 and t.dice_label() == "d20", "legacy die 20 becomes 1d20")
|
||
var e := t.entries[0]
|
||
_check(e.pipes == ["x", "y"] and e.count_for("y") == 2 and e.save_for("y") == "@pal" and e.count_for("x") == 1, "mixed string/object threads load")
|
||
_check(t.entries[1].weight == 4 and t.entries[1].details == "[b]hi[/b]", "weight and entry details load")
|
||
var d := t.to_dict()
|
||
_check(not d.has("die") and d["dice"] == "1d20", "die is no longer written")
|
||
var ed: Dictionary = d["entries"][0]
|
||
_check(ed["pipes"] == ["x", {"table": "y", "count": 2, "save_as": "@pal"}], "threads save in contract form")
|
||
_check(not (d["entries"][1] as Dictionary).has("pipes"), "empty threads omitted")
|
||
|
||
var plain := RollTable.new("p", "P")
|
||
plain.entries.append(_entry("e", "x"))
|
||
var pd := plain.to_dict()
|
||
for k in ["collapsed", "details", "tags", "dice", "card_mode", "deck_empty"]:
|
||
_check(not pd.has(k), "default %s omitted" % k)
|
||
_check(not (pd["entries"][0] as Dictionary).has("weight") and not (pd["entries"][0] as Dictionary).has("details"), "default entry fields omitted")
|
||
|
||
plain.details = "Hello"
|
||
plain.tags.append("a")
|
||
plain.tags.append("b")
|
||
plain.card_mode = true
|
||
plain.deck_empty = "stop"
|
||
var back := RollTable.from_dict(JSON.parse_string(JSON.stringify(plain.to_dict())))
|
||
_check(back.details == "Hello" and back.tags == ["a", "b"] and back.card_mode and back.deck_empty == "stop", "details, tags, deck mode round-trip")
|
||
_check(back.to_dict() == plain.to_dict(), "table dict round-trips")
|
||
|
||
var sample := SampleBoard.create()
|
||
var copy := TowerBoard.new()
|
||
_check(copy.load_json(sample.to_json()) and copy.to_json() == sample.to_json(), "sample spread JSON round-trips")
|
||
|
||
|
||
func _test_sample() -> void:
|
||
var s := SampleBoard.create()
|
||
var ids := {}
|
||
for t in s.tables:
|
||
_check(not ids.has(t.id), "unique table id %s" % t.id)
|
||
ids[t.id] = true
|
||
var eids := {}
|
||
for e in t.entries:
|
||
_check(not eids.has(e.id), "unique entry ids in %s" % t.id)
|
||
eids[e.id] = true
|
||
for p in e.pipes:
|
||
_check(s.get_table(p) != null, "thread target %s exists" % p)
|
||
var loot := s.get_table("loot")
|
||
_check(loot.entries[0].weight == 3 and absf(loot.chance(loot.entries[0]) - 3.0 / 8.0) < 1e-9, "gold weight 3 of 8")
|
||
var roles := s.get_table("party_roles")
|
||
_check(roles.card_mode and roles.entries.size() == 6 and roles.position == Vector2(1440, 330), "Party Roles is a 6-card deck")
|
||
var w := s.get_table("weather")
|
||
_check(w.dice == "2d6" and w.position == Vector2(1790, 380) and w.range_problems() == "", "Weather is a clean 2d6 table")
|
||
_check(s.get_table("monsters").entries[0].details != "" and s.get_table("wilderness").details != "", "sample has details")
|
||
_check(s.get_table("wilderness").position == Vector2(40, 180) and s.get_table("quality").position == Vector2(1790, 130), "existing positions kept")
|
||
var camp := s.get_table("make_camp")
|
||
_check(camp.kind == RollTable.KIND_CHOICE and camp.choice == "pick" and camp.entries.size() == 4, "Make Camp is a 4-option pick card")
|
||
_check(camp.entries[0].effects == "@luck += 1" and camp.entries[2].pipes == ["ruins"], "its options change Luck or go searching")
|
||
var luck := s.get_table("luck")
|
||
_check(luck.kind == RollTable.KIND_VARIABLE and luck.var_name == "luck" and luck.initial == "0", "Luck is a value card starting at 0")
|
||
var ruins := s.get_table("ruins")
|
||
_check(ruins.dice_mod == "@luck" and ruins.range_problems() == "", "Search the Ruins is 1d6 + Luck with clean ranges")
|
||
var reach: Array = ruins.entries.map(func(e: TableEntry) -> bool: return RollRange.natural_reach(ruins, e))
|
||
_check(reach == [false, true, true, true, false], "only a modifier reaches its first and last results: %s" % str(reach))
|