Files
Ratdoku/tools/ui_test.gd
T
Ratdoku Builder 82c5691d2e feat(input): make double-tap placement the default
New saves start in double-tap mode. Version 1 saves migrate once to double-tap and are rewritten as version 2, while the settings toggle still persists a later choice. Smoke tests now force hold mode for their hold checks; UI tests cover the fresh default and the migration.
2026-12-02 22:10:27 -06:00

272 lines
9.6 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
_test_save_defaults(save)
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)
main.open_settings("home")
await process_frame
_check(
main.settings_sheet.toggles["double_tap_place"].mouse_filter == Control.MOUSE_FILTER_PASS,
"settings rows should pass swipes to the ScrollContainer"
)
main.close_settings()
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)
_check(main.guest_book.grid.mouse_filter == Control.MOUSE_FILTER_PASS, "guest grid should pass swipes")
var first_card = main.guest_book.grid.get_child(0)
_check(first_card.mouse_filter == Control.MOUSE_FILTER_PASS, "guest cards should pass swipes")
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:
var rule_scrolls = main.rulebook.find_children("*", "ScrollContainer", true, false)
_check(rule_scrolls.size() > 0, "Rules ScrollContainer missing")
if rule_scrolls.size() > 0:
var rules_content = rule_scrolls[0].get_child(0)
_check(rules_content.mouse_filter == Control.MOUSE_FILTER_PASS, "rules content should pass swipes")
_check(tutorial_board.mouse_filter == Control.MOUSE_FILTER_STOP, "tutorial board should keep its own input")
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", true)
print("UI TEST: ", "PASS" if failures == 0 else "FAIL (%d)" % failures)
quit(0 if failures == 0 else 1)
func _test_save_defaults(save) -> void:
# A fresh save starts in double-tap mode at version 2.
var abs_path := ProjectSettings.globalize_path(save.PATH)
if FileAccess.file_exists(save.PATH):
DirAccess.remove_absolute(abs_path)
save.load_game()
_check(int(save.data.get("version", 0)) == 2, "fresh saves should be version 2")
_check(bool(save.get_setting("double_tap_place")), "fresh saves should default to double-tap")
# A version 1 save with hold selected migrates to double-tap once.
var old: Dictionary = save._default_data()
old["version"] = 1
old["settings"]["double_tap_place"] = false
var file := FileAccess.open(save.PATH, FileAccess.WRITE)
file.store_string(JSON.stringify(old, "\t"))
file.close()
save.load_game()
_check(int(save.data.get("version", 0)) == 2, "version 1 saves should migrate to version 2")
_check(bool(save.get_setting("double_tap_place")), "version 1 saves should migrate to double-tap")
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", true)
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)