TableTower/tests/test_shell.gd

175 lines
8.1 KiB
GDScript3
Raw Normal View History

extends SceneTree
## Headless checks for the app-shell logic: table settings, collections, import.
## Run: godot --headless --path . -s res://tests/test_shell.gd
const ROOT := "user://test_shell_tmp"
var _failures := 0
func _init() -> void:
_wipe(ROOT)
_test_settings()
_test_ranges()
_test_wrap()
_test_collections()
_test_import()
_test_export()
_wipe(ROOT)
print("%s: %d failure(s)" % ["PASS" if _failures == 0 else "FAIL", _failures])
quit(1 if _failures else 0)
func _plain_table(n: int = 4) -> RollTable:
var t := RollTable.new("t", "Loot")
for i in n:
t.entries.append(TableEntry.new("e%d" % i, "Item %d" % i))
return t
func _test_settings() -> void:
var t := _plain_table()
var r := TableSettingsDialog.apply_settings(t, {"name": " Gems ", "details": "[b]x[/b]", "tags": "a, b ,, a",
"ranged": false, "dice": "1d100", "card_mode": true, "deck_empty": "stop"})
_check(r["ok"], "plain settings apply")
_check(t.name == "Gems" and t.details == "[b]x[/b]", "name trimmed, details kept")
_check(t.tags == ["a", "b"], "tags split and de-duplicated")
_check(t.card_mode and t.deck_empty == "stop", "card mode and deck_empty set")
_check(r["deck_reset"], "card mode change resets the deck")
var bad := TableSettingsDialog.apply_settings(t, {"ranged": true, "dice": "banana", "name": "Changed"})
_check(not bad["ok"] and t.name == "Gems" and not t.is_ranged(), "invalid dice changes nothing")
var ok := TableSettingsDialog.apply_settings(t, {"ranged": true, "dice": "2d6", "name": "Gems",
"card_mode": true, "deck_empty": "stop"})
_check(ok["ok"] and t.is_ranged() and t.dice == "2d6", "converted to ranged 2d6")
_check(t.range_problems() == "", "default ranges cover 2d6 cleanly: '%s'" % t.range_problems())
var back := TableSettingsDialog.apply_settings(t, {"ranged": false, "dice": "2d6", "name": "Gems",
"card_mode": true, "deck_empty": "stop"})
_check(back["ok"] and not t.is_ranged(), "converted back to plain drops ranges")
_check(t.to_dict().has("dice") == false, "plain table saves no dice")
var r2 := TableSettingsDialog.apply_settings(t, {"ranged": true, "dice": "1d20", "name": "Gems"})
_check(r2["ok"] and t.dice == "1d20" and t.range_problems() == "", "ranged 1d20 is clean")
var r3 := TableSettingsDialog.apply_settings(t, {"ranged": true, "dice": "1d8", "name": "Gems"})
_check(r3["ok"] and t.dice == "1d8" and t.range_problems() != "", "shrinking dice reports range problems")
func _test_ranges() -> void:
# Weighted: 1 and 3 on a d100 -> 25 and 75 values.
var t := _plain_table(2)
t.entries[0].weight = 1
t.entries[1].weight = 3
t.dice = "1d100"
TableSettingsDialog.assign_ranges(t)
_check(t.entries[0].range_lo == 1 and t.entries[0].range_hi == 25, "weight 1 of 4 gets 1-25")
_check(t.entries[1].range_lo == 26 and t.entries[1].range_hi == 100, "weight 3 of 4 gets 26-100")
# More entries than values: everything stays valid and in span.
var many := _plain_table(8)
many.dice = "1d6"
TableSettingsDialog.assign_ranges(many)
var lit := 0
for e in many.entries:
if e.has_range:
lit += 1
_check(e.range_lo >= 1 and e.range_hi <= 6 and e.range_lo <= e.range_hi, "range inside 1d6")
_check(lit == 6, "six values -> six ranges, got %d" % lit)
# Weight 0 entries get no range; extra pipes are dropped.
var z := _plain_table(3)
z.entries[1].weight = 0
z.entries[0].pipes = ["a", "b"] as Array[String]
TableSettingsDialog.assign_ranges(z)
_check(not z.entries[1].has_range and z.entries[0].pipes.size() == 1, "weight 0 skipped, extra pipe dropped")
_check(z.entries[2].range_hi == 100, "last live entry reaches the top")
func _test_wrap() -> void:
var r := TableSettingsDialog.wrap_text("hello world", Vector2i(0, 5), "[b]", "[/b]")
_check(r["text"] == "[b]hello[/b] world" and r["caret"] == 12, "bold wraps selection")
var r2 := TableSettingsDialog.wrap_text("ab", Vector2i(1, 1), "[i]", "[/i]")
_check(r2["text"] == "a[i][/i]b" and r2["caret"] == 4, "empty selection inserts a pair")
func _test_collections() -> void:
var state := StateStore.new(ROOT)
var lib := LibraryStore.new(ROOT, state)
var a := CollectionSwitcher.create_collection(lib, null, "Alpha")
a.tables.append(_plain_table())
var b := CollectionSwitcher.create_collection(lib, a, "Beta")
_check(lib.list().size() == 2, "two collections after create")
_check(lib.load_collection(a.id).tables.size() == 1, "creating saves the current collection first")
var back := CollectionSwitcher.switch_collection(lib, b, a.id)
_check(back != null and back.id == a.id and back.tables.size() == 1, "switch loads the other collection")
_check(CollectionSwitcher.switch_collection(lib, a, "missing") == null, "switch to a missing id is null")
_check(not CollectionSwitcher.rename_collection(lib, a, " "), "blank rename refused")
_check(CollectionSwitcher.rename_collection(lib, a, "Alpha Prime"), "rename works")
_check(lib.load_collection(a.id).name == "Alpha Prime" and a.id == "alpha", "rename keeps the id")
var copy := CollectionSwitcher.duplicate_collection(lib, a, "Alpha copy")
_check(copy != null and copy.id != a.id and copy.tables.size() == 1, "duplicate copies the tables")
state.set_variable(a.id, "x", "1")
var next := CollectionSwitcher.delete_collection(lib, copy)
_check(next != null and next.id != copy.id and not lib.exists(copy.id), "delete returns another collection")
_check(state.get_variables(a.id).has("x"), "delete leaves other collections' state")
for e in lib.list():
var c := lib.load_collection(str(e["id"]))
CollectionSwitcher.delete_collection(lib, c)
_check(lib.list().size() == 1, "deleting the last collection leaves a fresh empty one")
func _test_import() -> void:
var lib := LibraryStore.new(ROOT, StateStore.new(ROOT))
var before := lib.list().size()
var canvas := JSON.stringify({
"nodes": [
{"id": "g1", "type": "group", "label": "Loot [url=https://x.example]link[/url]", "x": 0, "y": 0, "width": 400, "height": 300},
{"id": "n1", "type": "text", "text": "Coins [img]res://icon.svg[/img][b]bold[/b]", "x": 10, "y": 40, "width": 300, "height": 60},
{"id": "n2", "type": "text", "text": "A [color=red]gem[/color]", "x": 10, "y": 120, "width": 300, "height": 60},
],
"edges": [],
})
var r := IoMenu.import_as_collection(lib, canvas, "loot.canvas")
_check(r["ok"], "canvas import ok: %s" % str(r["errors"]))
var b: TowerBoard = r["board"]
_check(b != null and lib.list().size() == before + 1 and lib.exists(b.id), "import adds a new collection")
var all := ""
for t in b.tables:
all += t.name + "|"
for e in t.entries:
all += e.text + "|"
_check(not all.contains("[url") and not all.contains("[img") and not all.contains("res://"), "import is sanitized: " + all)
_check(all.contains("[b]bold[/b]") and all.contains("[color=red]gem[/color]"), "safe tags survive: " + all)
var r2 := IoMenu.import_as_collection(lib, canvas, "loot.canvas")
_check(r2["ok"] and r2["board"].id != b.id, "importing twice makes two collections")
var bad := IoMenu.import_as_collection(lib, "{not json", "x.json")
_check(not bad["ok"] and not (bad["errors"] as Array).is_empty() and lib.list().size() == before + 2, "bad file adds nothing")
_check(IoMenu.detect_format("a.canvas", "") == "canvas" and IoMenu.detect_format("a", "<graphml/>") == "graphml", "format detection")
func _test_export() -> void:
var s := SampleBoard.create()
s.name = "Sample"
for f in IoMenu.EXPORTS:
var text := IoMenu.export_text(f["key"], s)
_check(text.length() > 50, "export %s has content" % f["key"])
_check(IoMenu.export_text("mermaid_md", s).contains("```mermaid"), "markdown export has a mermaid fence")
_check(IoMenu.export_text("json", s).contains("\"tables\""), "json export has tables")
_check(IoMenu.default_file_name(s, IoMenu.EXPORTS[1]) == "sample.canvas", "default file name")
func _wipe(path: String) -> void:
var abs := ProjectSettings.globalize_path(path)
if not DirAccess.dir_exists_absolute(abs):
return
var d := DirAccess.open(abs)
for sub in d.get_directories():
_wipe(path.path_join(sub))
for f in d.get_files():
DirAccess.remove_absolute(abs.path_join(f))
DirAccess.remove_absolute(abs)
func _check(cond: bool, msg: String) -> void:
if not cond:
_failures += 1
print("FAIL: ", msg)