TableTower/tests/test_roller.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

84 lines
3.2 KiB
GDScript
Raw 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
## Headless checks for the data model and roller.
## Run: tools/run_tests.sh roller
func run() -> void:
var board := SampleBoard.create()
var rng := RandomNumberGenerator.new()
rng.seed = 12345
var trinkets := board.get_table("trinkets")
_check(trinkets.entries.size() == 100, "trinkets has 100 entries")
_check(trinkets.die_size() == 100, "trinkets rolls a d100")
var unique := {}
for e in trinkets.entries:
unique[e.text] = true
_check(unique.size() == 100, "trinket entries are unique")
_check(trinkets.num_label(trinkets.entries[0], 0) == "01", "first trinket is numbered 01")
_check(trinkets.num_label(trinkets.entries[99], 99) == "00", "last trinket is numbered 00")
var wild := board.get_table("wilderness")
_check(wild.is_ranged() and wild.die_size() == 100, "wilderness is a ranged d100")
_check(wild.range_problems() == "", "wilderness ranges have no gaps or overlaps")
_check(wild.num_label(wild.entries[5], 5) == "96–00", "last range reads 96–00")
for e in wild.entries:
_check(e.pipes.size() == 1, "range %s has exactly one thread" % e.text)
# Range weighting: 01–30 should come up about 30% of the time.
var counts := {}
for i in 4000:
var n := Roller.roll(board, "wilderness", rng)
var text: String = (n["entry"] as TableEntry).text
counts[text] = counts.get(text, 0) + 1
var share := float(counts.get("Travelers on the road", 0)) / 4000.0
_check(absf(share - 0.30) < 0.02, "01–30 rolls about 30%% of the time (got %.3f)" % share)
var rare := float(counts.get("An odd find", 0)) / 4000.0
_check(absf(rare - 0.05) < 0.01, "96–00 rolls about 5%% of the time (got %.3f)" % rare)
# Chains follow pipes.
var reached := false
for i in 500:
var n := Roller.roll(board, "wilderness", rng)
if Roller.reaches(n, "trinkets"):
reached = true
_check(Roller.flatten(n, []).size() == 2, "wilderness → trinkets chain has two steps")
break
_check(reached, "wilderness can reach trinkets")
# Gaps and overlaps are reported.
wild.entries[1].range_lo = 35
_check(wild.range_problems() == "No result for 31–34.", "gap reported: %s" % wild.range_problems())
wild.entries[1].range_lo = 25
_check(wild.range_problems() == "25–30 matches more than one result.", "overlap reported: %s" % wild.range_problems())
wild.entries[1].range_lo = 31
var n_gap := RollTable.new("g", "Gappy")
var only := TableEntry.new("e1", "Low")
only.has_range = true
only.range_lo = 1
only.range_hi = 50
n_gap.entries.append(only)
var stops := 0
for i in 200:
var b2 := TowerBoard.new()
b2.tables.append(n_gap)
if Roller.roll(b2, "g", rng).has("stop"):
stops += 1
_check(stops > 50 and stops < 150, "rolls into a gap stop with a message (%d/200)" % stops)
# JSON round trip keeps everything.
var copy := TowerBoard.new()
_check(copy.load_json(board.to_json()), "spread JSON loads back")
_check(copy.to_json() == board.to_json(), "spread JSON round-trips unchanged")
_check(copy.get_table("trinkets").collapsed, "collapsed state is saved")
# Removing a table removes pipes into it.
copy.remove_table("loot")
var dangling := false
for t in copy.tables:
for e in t.entries:
if e.pipes.has("loot"):
dangling = true
_check(not dangling and copy.get_table("loot") == null, "removing a table removes threads into it")
finish()