extends SceneTree ## Headless smoke test for the rules and the board's public-ish operations. ## --headless --path . --script res://tools/smoke_test.gd var solved_flag := false func _init() -> void: call_deferred("_run") func _run() -> void: var levels: Array = Levels.load_all() if levels.is_empty(): print("FAIL: no levels") quit(1) return var board := Board.new() root.add_child(board) board.size = Vector2(640, 640) board.solved.connect(_on_solved) var all_ok := true for i in levels.size(): var level: Dictionary = levels[i] board.set_level(level) solved_flag = false var fixed_count: int = board.fixed.size() var seen := {} for r in board.n: var c := int(level["solution"][r]) var cell := Vector2i(r, c) if board.fixed.has(cell): continue # Check the guest in directly. board._toggle_rat(cell) seen[cell] = true var valid: bool = Rules.is_valid(board.cells, board.regions) var rats: int = Rules.count_rats(board.cells) var conflicts: int = board.conflict_cells.size() if not valid or rats != board.n or conflicts != 0 or not solved_flag: all_ok = false print("FAIL %s: valid=%s rats=%d/%d conflicts=%d solved=%s fixed=%d" % [ level["id"], valid, rats, board.n, conflicts, solved_flag, fixed_count ]) # Undo / restart / hint on the first level. var first: Dictionary = levels[0] board.set_level(first) var before: int = Rules.count_rats(board.cells) var target := _first_empty_solution(board, first) if target.x >= 0: board._toggle_rat(target) var placed: int = Rules.count_rats(board.cells) board.undo() var undone: int = Rules.count_rats(board.cells) if placed != before + 1 or undone != before: all_ok = false print("FAIL undo: before=%d placed=%d undone=%d" % [before, placed, undone]) board.set_level(first) if target.x >= 0: board._toggle_rat(target) board.restart() if Rules.count_rats(board.cells) != board.fixed.size(): all_ok = false print("FAIL restart: rats=%d fixed=%d" % [Rules.count_rats(board.cells), board.fixed.size()]) board.set_level(first) solved_flag = false var guard := 0 while guard < 12: guard += 1 if not board.hint(): break if not solved_flag: all_ok = false print("FAIL hint: board not solved after %d hints" % guard) # Coordinate mapping: the centre of a cell must map back to that cell. board.size = Vector2(640, 640) board._update_layout() var mapped: Vector2i = board._pos_to_cell(board._cell_rect(3, 4).get_center()) if mapped != Vector2i(3, 4): all_ok = false print("FAIL mapping: expected (3,4), got %s" % str(mapped)) # Tap marks X, right-click checks in, and hold checks in without a tap. board.set_level(first) board._update_layout() var tap_cell := _find_empty_cell(board) if tap_cell.x >= 0: _left_click(board, tap_cell, true) _left_click(board, tap_cell, false) if int(board.cells[tap_cell.x][tap_cell.y]) != Board.X: all_ok = false print("FAIL tap: expected X, got %d" % int(board.cells[tap_cell.x][tap_cell.y])) _right_click(board, tap_cell) if int(board.cells[tap_cell.x][tap_cell.y]) != Board.RAT: all_ok = false print("FAIL right-click: expected RAT, got %d" % int(board.cells[tap_cell.x][tap_cell.y])) board.set_level(first) board._update_layout() var hold_cell := _find_empty_cell(board) if hold_cell.x >= 0: _left_click(board, hold_cell, true) board._process(board.hold_seconds + 0.05) if int(board.cells[hold_cell.x][hold_cell.y]) != Board.RAT: all_ok = false print("FAIL hold: expected RAT, got %d" % int(board.cells[hold_cell.x][hold_cell.y])) _left_click(board, hold_cell, false) if int(board.cells[hold_cell.x][hold_cell.y]) != Board.RAT: all_ok = false print("FAIL hold release: guest was removed") print("SMOKE TEST: %s" % ("PASS" if all_ok else "FAIL")) quit(0 if all_ok else 1) func _on_solved() -> void: solved_flag = true func _find_empty_cell(board: Board) -> Vector2i: for r in board.n: for c in board.n: var cell := Vector2i(r, c) if board.fixed.has(cell): continue if int(board.cells[r][c]) == Board.EMPTY and not board.auto_x[r][c]: return cell return Vector2i(-1, -1) func _left_click(board: Board, cell: Vector2i, pressed: bool) -> void: var event := InputEventMouseButton.new() event.button_index = MOUSE_BUTTON_LEFT event.pressed = pressed event.position = board._cell_rect(cell.x, cell.y).get_center() board._gui_input(event) 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 _first_empty_solution(board: Board, level: Dictionary) -> Vector2i: for r in board.n: var cell := Vector2i(r, int(level["solution"][r])) if not board.fixed.has(cell): return cell return Vector2i(-1, -1)