"""Tests for the GUI controller's in-process command execution (no display).""" import pytest pytest.importorskip("PySide6") from PySide6.QtCore import QCoreApplication # noqa: E402 from woodshop import driver # noqa: E402 from woodshop.gui.controller import Controller # noqa: E402 _app = QCoreApplication.instance() or QCoreApplication([]) def _controller(tmp_path): return Controller(str(tmp_path / "scene.json")) def test_execute_calls_with_symbols(tmp_path): """The controller's executor applies wood-* calls in-process, with $N.""" c = _controller(tmp_path) calls = [ {"tool": "wood-place", "args": {"stock": "2x4", "length": "4 ft"}}, {"tool": "wood-place", "args": {"stock": "2x4", "length": "2 ft"}}, {"tool": "wood-stand", "args": {"part": "$2"}}, {"tool": "wood-join", "args": {"part_b": "$2", "to": "$1", "angle": "0"}}, ] driver.dispatch(calls, verbose=False, executor=c.execute_call) assert [p.id for p in c.scene.parts] == ["p1", "p2"] assert c.scene.get_part("p2").is_vertical assert len(c.scene.joints) == 1 def test_button_ops_and_persistence(tmp_path): c = _controller(tmp_path) c.place("2x4", 48) c.stand() # acts on selection (p1) assert c.scene.get_part("p1").is_vertical c.duplicate() # p2 assert len(c.scene.parts) == 2 c.delete() # deletes selection p2 assert [p.id for p in c.scene.parts] == ["p1"] # changes are persisted to disk from woodshop.scene import Scene assert len(Scene.load(c.scene_path).parts) == 1 def test_select_and_undo_redo(tmp_path): c = _controller(tmp_path) c.place("2x4", 24) c.place("2x4", 36) c.select("p1") assert c.selected_id == "p1" c.undo() # removes p2 assert len(c.scene.parts) == 1 c.redo() assert len(c.scene.parts) == 2 def test_toggle_multiselect(tmp_path): c = _controller(tmp_path) c.place("2x4", 24) c.place("2x4", 24) c.select("p1") c.toggle("p2") assert set(c.selected) == {"p1", "p2"} c.toggle("p1") # ctrl-click again removes it assert c.selected == ["p2"] def test_group_move_is_single_undo(tmp_path): c = _controller(tmp_path) for _ in range(3): c.place("2x4", 24) c.set_selected(["p1", "p2", "p3"]) c.move_selected(dy=4) # "move these 4 inches in +y" assert all(p.position_in[1] == 4 for p in c.scene.parts) c.undo() # one undo reverts the whole group assert all(p.position_in[1] == 0 for p in c.scene.parts) def test_unknown_tool_is_safe(tmp_path): c = _controller(tmp_path) assert "unknown" in c.execute_call("wood-bogus", {}).lower()