TableTower/tests/test_small_ui.gd
RobinFiredrake 8fd2b2b8c9 Sheets: a line-per-value helper and Save as a file (Text, Markdown, web page)
The Sheet card's inspector adds a 'Label: {@value}' line for any value
picked. The tray's Copy ▾ gets Save as a file: the roll's sheet (or one
line per result) as Text, Markdown, or an HTML page a browser can print
to PDF, with bold, italics and colours kept where the format allows
(SheetExport). The test runner prints a compile error shared by many
suites once, with the suite names.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 19:17:27 -06:00

181 lines
7.6 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 SceneTree
## Small UI items: backwards ranges warn and block rolling (no auto-swap), the
## inspector's thread lists, replacing a template, the tray chip's bold source
## name, and the export menu's Other formats.
## Run: godot --headless --path . -s res://tests/test_small_ui.gd -- --sample
var _failures := 0
var main: Main
func _init() -> void:
if not OS.get_cmdline_user_args().has("--sample"):
print("Run with -- --sample so the test can't touch saved data.")
quit(2)
return
main = load("res://ui/main.tscn").instantiate()
root.add_child(main)
await _frames(5)
await _test_backwards_range()
await _test_thread_lists()
_test_template_replace()
await _test_chip_and_export()
await _test_deck_mode()
await _test_batch()
await _test_pause_aside()
_test_sheet_export()
print("%s: %d failure(s)" % ["PASS" if _failures == 0 else "FAIL", _failures])
quit(1 if _failures else 0)
func _test_backwards_range() -> void:
var t := main.data.get_table("wilderness")
var e := t.entries[0]
e.range_lo = 40
e.range_hi = 10
var card := main.card_for("wilderness")
card.update_warning()
_check(e.range_lo == 40 and e.range_hi == 10, "the ends aren't swapped")
_check(RollRange.backwards(t) != "" and t.range_problems().contains("runs backwards"), "a warning says the range runs backwards")
_check(card.header_ui.roll_button.disabled, "the die button is greyed out")
main.rolling.start_roll("wilderness")
await _frames()
_check(not str(main.current).contains("wilderness") or main.current.is_empty() or main.current.get("table_id", "") != "wilderness", "it can't be rolled")
e.range_lo = 1
e.range_hi = 30
card.update_warning()
_check(not card.header_ui.roll_button.disabled, "fixing it enables the die again")
func _test_thread_lists() -> void:
main.set_mode(Main.Mode.BUILD)
await _frames()
main.select_card("road")
await _frames(3)
var texts: Array[String] = []
for b in main.inspector.find_children("*", "Button", true, false):
texts.append((b as Button).text)
var heads: Array[String] = []
for l in main.inspector.find_children("*", "Label", true, false):
heads.append((l as Label).text)
_check(heads.any(func(s: String) -> bool: return s.begins_with("THREADS IN (")), "the card inspector lists threads in")
_check(texts.any(func(s: String) -> bool: return s.begins_with("Wilderness Travel · ")), "a thread in names its card and result: %s" % str(texts.slice(0, 6)))
_check(texts.any(func(s: String) -> bool: return s.contains(" → ")), "threads out name where they go")
func _test_template_replace() -> void:
var lib := main.templating.templates()
var tpl := TemplateKit.make(main.data, ["road"], "card", "Road")
var id := lib.add(tpl)
var changed := tpl.duplicate(true)
changed["name"] = "Road, revised"
changed["replace_id"] = id
main.templating.store_template(changed)
_check(lib.get_template(id).get("name") == "Road, revised", "Replace saves over the same template")
_check(not lib.get_template(id).has("replace_id"), "without the replace marker")
var count := lib.list().size()
main.templating.store_template(tpl.duplicate(true))
_check(lib.list().size() == count + 1, "a plain save still adds a new one")
func _test_chip_and_export() -> void:
main.set_mode(Main.Mode.PLAY)
main.rolling.start_roll("road", true)
await _frames(4)
var bold := false
for l in main.tray.find_children("*", "Label", true, false):
var lab := l as Label
if lab.text == "Road Encounter" and lab.get_theme_font("font") == Style.body_bold():
bold = true
_check(bold, "the chip names its card in bold")
var pm := main.top_bar._board_menu.get_popup()
var subs := pm.find_children("*", "PopupMenu", false, false)
_check(subs.size() == 1 and (subs[0] as PopupMenu).item_count == IoMenu.EXPORTS.size() - 1, "rare formats sit under Other formats")
## Deck mode: drawn results fade, an empty deck that waits for Shuffle gives
## its Empty result, and Shuffle all puts everything back.
func _test_deck_mode() -> void:
var t := main.data.get_table("party_roles")
_check(t != null and t.card_mode, "the sample's Party Roles is a deck")
t.deck_empty = "stop"
t.deck_empty_text = "No roles left"
for i in t.entries.size():
main.rolling.start_roll("party_roles", true)
await _frames(2)
var card := main.card_for("party_roles")
var faded := 0
for e in t.entries:
if card.rows.has(e.id) and (card.rows[e.id] as Control).modulate.a < 0.5:
faded += 1
_check(faded == t.entries.size(), "every drawn result is faded (%d of %d)" % [faded, t.entries.size()])
main.rolling.start_roll("party_roles", true)
await _frames(2)
var steps: Array = main.current.get("steps", [])
var last: Dictionary = steps[steps.size() - 1] if not steps.is_empty() else {}
_check(str(last.get("text", "")) == "No roles left" and last.get("deck_empty", false), "an empty deck gives its Empty result: %s" % str(last.get("text", "")))
main.local_state.reshuffle_all()
await _frames()
_check((card.rows[t.entries[0].id] as Control).modulate.a == 1.0, "Shuffle all brings them back")
func _test_batch() -> void:
var texts: Array[String] = await BatchRoll.run(main, "road", 4)
_check(texts.size() == 4 and texts[0].contains("Road Encounter"), "a batch gives every result: %d" % texts.size())
var dialogs := main.find_children("*", "ModalCard", true, false)
_check(not dialogs.is_empty() and (dialogs[dialogs.size() - 1] as ModalCard).title_label.text == "Road Encounter × 4", "and shows them together")
(dialogs[dialogs.size() - 1] as ModalCard).close()
## A paused roll waits in the tray; another roll sets it aside; Continue
## brings it back and finishes it.
func _test_pause_aside() -> void:
var road := main.data.get_table("road")
for e in road.entries:
for to in e.pipes:
e.pipe_pause[to] = true
var bandit := road.entries[0]
var tid := bandit.pipes[0] if not bandit.pipes.is_empty() else ""
var weights := []
for e in road.entries:
weights.append(e.weight)
e.weight = 0
bandit.weight = 1
main.rolling.start_roll("road", true)
await _frames(3)
var pauses: RollPauses = main.rolling.pauses
_check(pauses.has_waiting() and main.tray._pause_box.get_child_count() > 0, "the roll waits with Continue in the tray")
main.rolling.start_roll("weather", true)
await _frames(3)
_check(pauses.has_waiting() and str(main.current.get("root", "")) == "weather", "another roll sets the paused one aside")
pauses.answer(0, true)
await _frames(4)
_check(not pauses.has_waiting() and str(main.current.get("root", "")) == "road", "Continue makes it current again")
var reached := (main.current.get("all", []) as Array).any(func(s: Dictionary) -> bool: return str(s.get("table_id", "")) == tid)
_check(tid == "" or reached, "and it goes on past the pause")
for i in road.entries.size():
road.entries[i].weight = weights[i]
road.entries[i].pipe_pause.clear()
func _test_sheet_export() -> void:
var b := "[b]Name:[/b] [color=#aa0000]Ash[/color] & co\n[i]Brave[/i]"
var md := SheetExport.as_format(b, "Hero", "md")
_check(md.begins_with("# Hero") and md.contains("**Name:**") and md.contains("*Brave*") and not md.contains("[color"), "Markdown: " + md)
var html := SheetExport.as_format(b, "Hero", "html")
_check(html.contains("<b>Name:</b>") and html.contains("color:#aa0000") and html.contains("&amp; co") and html.contains("<br>"), "HTML keeps bold and colour")
var txt := SheetExport.as_format(b, "Hero", "txt")
_check(txt.contains("Name: Ash & co") and not txt.contains("["), "Text strips the formatting")
main.rolling.start_roll("road", true)
_check(SheetExport.document(main, "txt").contains("Road Encounter"), "the current roll becomes a document")
func _frames(n: int = 2) -> void:
for i in n:
await process_frame
func _check(ok: bool, what: String) -> void:
if not ok:
_failures += 1
print("FAIL ", what)