TableTower/tests/test_ui_threads.gd

141 lines
5.6 KiB
GDScript3
Raw Permalink Normal View History

extends AppTestBase
## The thread inspector's "Roll it with … instead" field and "Roll again if
## it comes up" list: shown for a card with numbered ranges, edits land on the
## result, undo puts them back.
## Run: tools/run_tests.sh ui_threads
func run() -> void:
if not await start_main():
return
await _test_ranged_target()
await _test_plain_target()
await _test_ports_under_cards()
await _test_card_thread_port()
finish()
## A card dragged over another hides the lower card's thread nodes it covers.
func _test_ports_under_cards() -> void:
var low := main.card_for("road")
var top := main.card_for("monsters")
top.position = low.position + Vector2(-20, 0)
top.get_parent().move_child(top, -1)
await _frames()
var ports := main.board.ports
ports._note_stack()
_check(ports._covered(low.inlet_point(), low.get_index()), "the covered card's inlet is hidden")
_check(not ports._covered(top.inlet_point(), top.get_index()), "the top card's inlet still shows")
low.get_parent().move_child(low, -1)
await _frames()
ports._note_stack()
_check(not ports._covered(low.inlet_point(), low.get_index()), "raised again, its inlet shows")
## Build mode: each card has a port on its bottom edge; a thread laid from it
## is a card thread, drawn from there, listed on the card and undoable.
func _test_card_thread_port() -> void:
await _frames(40)
var road := main.data.get_table("road")
var card := main.card_for("road")
main.board.set_zoom(1.0)
main.board.ports.queue_redraw()
await _frames(3)
var grabs: Array = main.board.ports._grab.filter(func(g: Dictionary) -> bool:
return g["tid"] == "road" and g["eid"] == RollTable.OUTLET_ID)
var bottom := card.position + Vector2(card.size.x / 2.0, card.size.y)
_check(grabs.size() == 1 and (grabs[0]["pos"] as Vector2).is_equal_approx(bottom), "a card-thread port on the card's bottom edge")
var target := main.card_for("monsters")
main.start_link("road", RollTable.OUTLET_ID)
main.editing._card_under_mouse_override = target
main.finish_link()
main.editing._card_under_mouse_override = null
_check(road.outlet.pipes.has("monsters"), "the drag made a card thread")
await _frames(3)
var key := Roller.pipe_key("road", RollTable.OUTLET_ID, "monsters")
_check(main.board.pipes._paths.has(key), "it is drawn")
main.inspector.select({"kind": "card", "tid": "road"})
await _frames(2)
_check(_find_text(main.inspector, "After any result → "), "the card section lists it")
main.undo_redo.undo()
await _frames(2)
_check(main.data.get_table("road").outlet.pipes.is_empty(), "undo takes it away")
func _find_text(n: Node, prefix: String) -> bool:
if n is Button and (n as Button).text.begins_with(prefix):
return true
for c in n.get_children():
if _find_text(c, prefix):
return true
return false
func _parts(to: String) -> Dictionary:
return main.inspector.pipe_parts.get(to, {})
func _road() -> TableEntry:
return main.data.get_table("road").entries[3]
func _test_ranged_target() -> void:
print("Ranged target")
main.set_mode(Main.Mode.BUILD)
var e := _road()
e.add_pipe("wilderness")
main.mark_dirty()
main.undo_redo._commit_edits()
main.inspecting.select_pipe(Roller.pipe_key("road", e.id, "wilderness"))
await _frames(3)
var p := _parts("wilderness")
_check(p.has("dice"), "a ranged card gets the Roll it with field")
var dice := p.get("dice") as LineEdit
_eq(dice.placeholder_text, "1d100", "its placeholder is the card's own dice")
var toggle := p.get("reroll_toggle") as Button
_check(toggle != null and toggle.text.contains("never"), "the roll-again list starts closed, saying never: %s" % (toggle.text if toggle else ""))
var boxes: Dictionary = p.get("reroll", {})
var w := main.data.get_table("wilderness")
_eq(boxes.size(), w.entries.size(), "one tick box per result")
var last := w.entries[w.entries.size() - 1]
_check((boxes[last.id] as CheckBox).text.begins_with("96–00"), "a box shows the number and the text: %s" % (boxes[last.id] as CheckBox).text)
dice.text = "3d10+"
dice.text_changed.emit("3d10+")
_eq(e.dice_for("wilderness"), "", "dice that can't be rolled aren't kept")
dice.text = "3d10+70"
dice.text_changed.emit("3d10+70")
_eq(e.dice_for("wilderness"), "3d10+70", "the dice land on the thread")
main.undo_redo._commit_edits()
toggle.pressed.emit()
_check((boxes[last.id] as CheckBox).is_visible_in_tree(), "the list opens")
(boxes[last.id] as CheckBox).button_pressed = true
_eq(e.reroll_for("wilderness"), [last.id] as Array[String], "ticking adds the result")
_check(toggle.text.contains("96–00"), "the heading names it: %s" % toggle.text)
main.undo_redo._commit_edits()
# Quick edits to one card merge into one undo step (EditHistory.COALESCE_SECS).
main.undo_redo.undo()
await _frames(2)
e = _road()
_check(e.reroll_for("wilderness").is_empty(), "undo takes the tick back")
_eq(e.dice_for("wilderness"), "", "and the dice")
main.undo_redo.redo()
await _frames(2)
e = _road()
_eq(e.dice_for("wilderness"), "3d10+70", "redo brings the dice back")
_eq(e.reroll_for("wilderness"), [last.id] as Array[String], "and the tick")
var boxes2: Dictionary = _parts("wilderness").get("reroll", {})
_check(boxes2.has(last.id) and (boxes2[last.id] as CheckBox).button_pressed, "the inspector shows the tick again")
_check(DescribeCards.pipe(e, "wilderness", main.data).contains("again on 96–00"), "the thread's label says so")
func _test_plain_target() -> void:
print("Card without ranges")
var e := main.data.get_table("wilderness").entries[0]
main.inspecting.select_pipe(Roller.pipe_key("wilderness", e.id, "road"))
await _frames(3)
var p := _parts("road")
_check(not p.has("dice"), "no Roll it with field for a card without ranges")
_check(p.has("reroll"), "but the roll-again list is there")