Files
Ratdoku/tools/ui_test.gd
T
Ratdoku Builder af4951baf8 feat(input): guard Guest Book taps and add double-tap placement
Guest Book: only open a card on a short stationary tap, ignore drags and flings, and guard the detail Close button after opening. Board: add a persisted double-tap placement mode with deferred single taps, while hold stays the default and right-click works in both modes.
2026-12-02 20:10:44 -06:00

232 lines
7.7 KiB
GDScript

extends SceneTree
## Headless UI/save integration test.
## <godot> --headless --path . --script res://tools/ui_test.gd
var failures := 0
func _init() -> void:
call_deferred("_run")
func _check(condition: bool, message: String) -> void:
if not condition:
failures += 1
print("FAIL: ", message)
func _run() -> void:
var err := change_scene_to_file("res://scenes/Main.tscn")
if err != OK:
print("FAIL: could not load Main")
quit(1)
return
await process_frame
await process_frame
var main = current_scene
var save = root.get_node_or_null("SaveGame")
_check(save != null, "SaveGame autoload missing")
if save == null:
quit(1)
return
save.reset_progress()
main.game.start_level(0)
await process_frame
save.set_setting("patterns", false)
_check(not main.game.board.show_patterns, "patterns setting did not reach Board")
save.set_setting("patterns", true)
_check(main.game.board.show_patterns, "patterns setting did not restore")
save.set_setting("show_timer", false)
main.show_screen("game")
await process_frame
_check(not main.game.timer_label.visible, "show_timer setting did not hide timer")
save.set_setting("show_timer", true)
save.set_setting("hold_seconds", 0.50)
_check(abs(main.game.board.hold_seconds - 0.50) < 0.001, "hold_seconds did not reach Board")
save.set_setting("hold_seconds", 0.35)
_test_placement_modes(save, main.game.board)
var unlocked: String = save.complete_room("room-01", 1)
_check(unlocked == "pepper", "room 1 should unlock Pepper")
_check(save.is_guest_unlocked("pepper"), "Pepper should be unlocked")
_check(save.is_guest_unlocked("miso"), "Miso should be unlocked as a resident")
main.show_screen("guest_book")
await process_frame
await process_frame
_check(main.guest_book != null, "Guest Book missing")
if main.guest_book != null and main.guest_book.progress_label != null:
_check(main.guest_book.progress_label.text.find("6/25") >= 0, "Guest Book progress should be 6/25")
_test_guest_book_input(main.guest_book)
main.show_screen("rules")
await process_frame
var tutorial_board = main.rulebook.tutorial_board
_check(tutorial_board != null, "Tutorial board missing")
if tutorial_board != null:
for i in range(8):
tutorial_board.hint()
await process_frame
_check(Rules.is_valid(tutorial_board.cells, tutorial_board.regions), "Tutorial puzzle did not solve")
save.reset_progress()
save.set_setting("patterns", true)
save.set_setting("show_timer", true)
save.set_setting("reduce_motion", false)
save.set_setting("hold_seconds", 0.35)
save.set_setting("double_tap_place", false)
print("UI TEST: ", "PASS" if failures == 0 else "FAIL (%d)" % failures)
quit(0 if failures == 0 else 1)
func _find_empty(board: Board) -> Vector2i:
for r in board.n:
for c in board.n:
if (
int(board.cells[r][c]) == Board.EMPTY
and not board.fixed.has(Vector2i(r, c))
and not board.auto_x[r][c]
):
return Vector2i(r, c)
return Vector2i(-1, -1)
func _test_placement_modes(save, board: Board) -> void:
board.restart()
save.set_setting("double_tap_place", true)
_check(board.double_tap_place, "double_tap_place did not reach Board")
# A first tap waits; a second tap on the same cell checks a guest in.
var cell := _find_empty(board)
board._handle_tap(cell)
_check(int(board.cells[cell.x][cell.y]) == Board.EMPTY, "first tap should wait in double-tap mode")
board._handle_tap(cell)
_check(int(board.cells[cell.x][cell.y]) == Board.RAT, "double tap should check a guest in")
board._handle_tap(cell)
board._handle_tap(cell)
_check(int(board.cells[cell.x][cell.y]) == Board.EMPTY, "double tap on a guest should check them out")
# A lone tap still marks an X once the double-tap window passes.
var cell2 := _find_empty(board)
board._handle_tap(cell2)
board.pending_tap_at -= Board.DOUBLE_TAP_MS + 1
board._process(0.0)
_check(int(board.cells[cell2.x][cell2.y]) == Board.X, "single tap should mark X after the window")
# Holding must not place a guest while double-tap mode is on.
board.press_cell = cell2
board.press_elapsed = 0.0
board.hold_fired = false
board._process(1.0)
_check(int(board.cells[cell2.x][cell2.y]) == Board.X, "hold should not place in double-tap mode")
board.press_cell = Vector2i(-1, -1)
board.hold_fired = false
# Right-click still checks a guest in and out in double-tap mode.
board.size = Vector2(320, 320)
board._update_layout()
var cell3 := _find_empty(board)
_right_click(board, cell3)
_check(int(board.cells[cell3.x][cell3.y]) == Board.RAT, "right-click should check in during double-tap mode")
_right_click(board, cell3)
_check(int(board.cells[cell3.x][cell3.y]) == Board.EMPTY, "right-click should check out during double-tap mode")
# Hold mode still works when the setting is off.
save.set_setting("double_tap_place", false)
_check(not board.double_tap_place, "double_tap_place did not turn off")
board.restart()
var cell4 := _find_empty(board)
board.press_cell = cell4
board.press_elapsed = 0.0
board.hold_fired = false
board._process(0.60)
_check(int(board.cells[cell4.x][cell4.y]) == Board.RAT, "hold should place in hold mode")
board.press_cell = Vector2i(-1, -1)
board.hold_fired = false
# Single tap marks X in hold mode.
var cell5 := _find_empty(board)
board.size = Vector2(320, 320)
board._update_layout()
_left_tap(board, cell5)
_check(int(board.cells[cell5.x][cell5.y]) == Board.X, "single tap should mark X in hold mode")
# The setting survives a save/load round trip.
save.set_setting("double_tap_place", true)
save.load_game()
_check(bool(save.get_setting("double_tap_place")), "double_tap_place should persist")
save.set_setting("double_tap_place", false)
board.restart()
func _left_tap(board: Board, cell: Vector2i) -> void:
var pos := board._cell_rect(cell.x, cell.y).get_center()
var down := InputEventMouseButton.new()
down.button_index = MOUSE_BUTTON_LEFT
down.pressed = true
down.position = pos
board._gui_input(down)
var up := InputEventMouseButton.new()
up.button_index = MOUSE_BUTTON_LEFT
up.pressed = false
up.position = pos
board._gui_input(up)
func _right_click(board: Board, cell: Vector2i) -> void:
var event := InputEventMouseButton.new()
event.button_index = MOUSE_BUTTON_RIGHT
event.pressed = true
event.position = board._cell_rect(cell.x, cell.y).get_center()
board._gui_input(event)
func _test_guest_book_input(book) -> void:
var guest: Dictionary = Guests.all()[0]
book.detail_layer.visible = false
# Dragging across a card is a scroll, not a tap.
_mouse(book, guest, Vector2(100, 300), true)
var motion := InputEventMouseMotion.new()
motion.global_position = Vector2(170, 300)
book._on_card_input(motion, guest, false)
_mouse(book, guest, Vector2(170, 300), false)
_check(not book.detail_layer.visible, "dragging should not open a guest card")
# A fling settling must not turn into a tap either.
book._suppress_until = Time.get_ticks_msec() + 1000
_mouse(book, guest, Vector2(100, 300), true)
_mouse(book, guest, Vector2(100, 300), false)
_check(not book.detail_layer.visible, "post-scroll tap should be ignored")
# A short, stationary tap opens the card.
book._suppress_until = 0
_mouse(book, guest, Vector2(100, 300), true)
_mouse(book, guest, Vector2(100, 300), false)
_check(book.detail_layer.visible, "a clean tap should open a guest card")
# The opening tap must not immediately close the sheet.
book._try_close_detail()
_check(book.detail_layer.visible, "close should be guarded right after opening")
book._detail_opened_at -= 300
book._try_close_detail()
_check(not book.detail_layer.visible, "close should work after the guard")
func _mouse(book, guest: Dictionary, pos: Vector2, pressed: bool) -> void:
var event := InputEventMouseButton.new()
event.button_index = MOUSE_BUTTON_LEFT
event.pressed = pressed
event.global_position = pos
book._on_card_input(event, guest, false)