190 lines
7.0 KiB
Python
190 lines
7.0 KiB
Python
"""Tests for the driver's orchestration logic (external tools are mocked)."""
|
|
import json
|
|
|
|
from woodshop import driver
|
|
from woodshop.cli import normalize_anchor
|
|
|
|
|
|
def test_anchor_aliases():
|
|
assert normalize_anchor("end") == "end_b"
|
|
assert normalize_anchor("the end") == "end_b" # falls through to default end_b
|
|
assert normalize_anchor("start") == "end_a"
|
|
assert normalize_anchor("NEAR") == "end_a"
|
|
assert normalize_anchor("") == "end_b"
|
|
|
|
|
|
def test_dispatch_resolves_dollar_symbols(monkeypatch):
|
|
"""$1/$2 in a multi-op turn resolve to the ids of boards placed this turn."""
|
|
seen = []
|
|
|
|
def fake_run(cmd, stdin=""):
|
|
if cmd[0] != "pa-execute-tool":
|
|
return ""
|
|
name, args = cmd[2], json.loads(cmd[4])
|
|
seen.append((name, args))
|
|
if name == "wood-place":
|
|
n = sum(1 for c in seen if c[0] == "wood-place")
|
|
return json.dumps({"success": True, "output": f"Placed p{n}: a board.", "error": ""})
|
|
return json.dumps({"success": True, "output": f"did {name}", "error": ""})
|
|
|
|
monkeypatch.setattr(driver, "_run", fake_run)
|
|
calls = [
|
|
{"tool": "wood-place", "args": {"stock": "2x4", "length": "2 ft"}},
|
|
{"tool": "wood-place", "args": {"stock": "2x4", "length": "2 ft"}},
|
|
{"tool": "wood-join", "args": {"part_b": "$2", "to": "$1", "angle": "90"}},
|
|
]
|
|
driver.dispatch(calls, verbose=False)
|
|
join_args = next(a for n, a in seen if n == "wood-join")
|
|
assert join_args["part_b"] == "p2"
|
|
assert join_args["to"] == "p1"
|
|
|
|
|
|
def test_say_pseudo_tool_does_not_dispatch(monkeypatch):
|
|
calls_made = []
|
|
monkeypatch.setattr(driver, "_run", lambda cmd, stdin="": calls_made.append(cmd) or "")
|
|
msgs = driver.dispatch([{"tool": "say", "args": {"text": "which end?"}}], verbose=False)
|
|
assert msgs == ["which end?"]
|
|
assert calls_made == [] # nothing executed
|
|
|
|
|
|
def test_interpret_tolerates_fenced_json(monkeypatch):
|
|
monkeypatch.setattr(
|
|
driver, "_run",
|
|
lambda cmd, stdin="": '```json\n[{"tool": "wood-undo", "args": {}}]\n```'
|
|
if cmd[:2] != ["pa-load-tools", "--tools"] else "[]",
|
|
)
|
|
calls = driver.interpret("undo that", schemas="[]")
|
|
assert calls == [{"tool": "wood-undo", "args": {}}]
|
|
|
|
|
|
def test_summarize_rolls_up_many_ops():
|
|
calls = ([{"tool": "wood-place", "args": {}}] * 8
|
|
+ [{"tool": "wood-join", "args": {}}] * 2
|
|
+ [{"tool": "wood-stand", "args": {}}] * 4)
|
|
summary = driver.summarize(calls, [""] * len(calls))
|
|
assert "placed 8" in summary
|
|
assert "joined 2" in summary
|
|
assert "stood up 4" in summary
|
|
assert len(summary) < 80 # short enough to speak
|
|
|
|
|
|
def test_summarize_speaks_queries_verbatim():
|
|
calls = [{"tool": "wood-cutlist", "args": {}}]
|
|
messages = ["CUT LIST\n 2 x 2x4 ..."]
|
|
assert driver.summarize(calls, messages).startswith("CUT LIST")
|
|
|
|
|
|
def test_summarize_speaks_clarification():
|
|
calls = [{"tool": "say", "args": {"text": "which end?"}}]
|
|
assert driver.summarize(calls, ["which end?"]) == "which end?"
|
|
|
|
|
|
def test_interpret_handles_garbage(monkeypatch):
|
|
monkeypatch.setattr(driver, "_run", lambda cmd, stdin="": "I'm not sure what you mean")
|
|
calls = driver.interpret("blah", schemas="[]")
|
|
assert calls[0]["tool"] == "say"
|
|
|
|
|
|
def test_extract_calls_ignores_trailing_brackets():
|
|
"""A greedy [.*] would swallow the trailing '[note]' and fail to parse."""
|
|
raw = '[{"tool": "wood-undo", "args": {}}]\n\nLet me know [if that helps].'
|
|
assert driver._extract_calls(raw) == [{"tool": "wood-undo", "args": {}}]
|
|
|
|
|
|
def test_extract_calls_strips_fences_and_handles_object():
|
|
assert driver._extract_calls('```json\n{"tool": "wood-clear", "args": {}}\n```') == \
|
|
[{"tool": "wood-clear", "args": {}}]
|
|
|
|
|
|
def test_extract_calls_returns_none_on_garbage():
|
|
assert driver._extract_calls("no json here") is None
|
|
|
|
|
|
def test_render_history_empty_and_populated():
|
|
assert driver._render_history(None) == "(no prior turns)"
|
|
assert driver._render_history([]) == "(no prior turns)"
|
|
text = driver._render_history([("build a table", "Done — placed 9.")])
|
|
assert 'User: "build a table"' in text
|
|
assert "WoodShop: Done — placed 9." in text
|
|
|
|
|
|
def test_render_history_windowed():
|
|
turns = [(f"u{i}", f"a{i}") for i in range(10)]
|
|
text = driver._render_history(turns)
|
|
assert "u9" in text and "u4" in text # last _MAX_HISTORY kept
|
|
assert "u3" not in text # older dropped
|
|
|
|
|
|
def test_interpret_includes_history_in_prompt(monkeypatch):
|
|
captured = {}
|
|
|
|
def fake_run(cmd, stdin=""):
|
|
captured["prompt"] = stdin
|
|
return "[]"
|
|
|
|
monkeypatch.setattr(driver, "_run", fake_run)
|
|
driver.interpret("yes", schemas="[]", scene_text="empty",
|
|
history=[("add tenons?", "Want me to put a tenon on each end?")])
|
|
assert "Want me to put a tenon on each end?" in captured["prompt"]
|
|
assert 'User: "add tenons?"' in captured["prompt"]
|
|
|
|
|
|
def test_handle_appends_to_history(monkeypatch):
|
|
monkeypatch.setattr(driver, "_run",
|
|
lambda cmd, stdin="": '[{"tool": "say", "args": {"text": "hi there"}}]')
|
|
history = []
|
|
driver.handle("hello", schemas="[]", voice=False, verbose=False, history=history)
|
|
assert history == [("hello", "hi there")]
|
|
|
|
|
|
def test_woodshop_cmd_prefers_path(monkeypatch):
|
|
monkeypatch.setattr(driver.shutil, "which", lambda name: "/opt/bin/woodshop")
|
|
assert driver.woodshop_cmd() == ["/opt/bin/woodshop"]
|
|
|
|
|
|
def test_woodshop_cmd_falls_back_to_module(monkeypatch):
|
|
monkeypatch.setattr(driver.shutil, "which", lambda name: None)
|
|
cmd = driver.woodshop_cmd()
|
|
assert cmd[1:] == ["-m", "woodshop"] and cmd[0] # python -m woodshop
|
|
|
|
|
|
def test_find_image_url():
|
|
assert driver.find_image_url("build like this https://x.com/chair.jpg please") \
|
|
== "https://x.com/chair.jpg"
|
|
assert driver.find_image_url("https://x.com/a.PNG") == "https://x.com/a.PNG"
|
|
assert driver.find_image_url("no image here http://x.com/page") is None
|
|
|
|
|
|
def test_interpret_includes_image_directive(monkeypatch, tmp_path):
|
|
captured = {}
|
|
|
|
def fake_run(cmd, stdin=""):
|
|
captured["prompt"] = stdin
|
|
return "[]"
|
|
|
|
img = tmp_path / "ref.jpg"
|
|
img.write_bytes(b"\xff\xd8\xff") # not a real jpeg, just a path
|
|
monkeypatch.setattr(driver, "_run", fake_run)
|
|
driver.interpret("build something like this", schemas="[]", scene_text="empty",
|
|
image_path=str(img))
|
|
assert "REFERENCE PHOTO" in captured["prompt"]
|
|
assert str(img) in captured["prompt"]
|
|
|
|
|
|
def test_fetch_image_writes_temp(monkeypatch):
|
|
import io
|
|
|
|
class FakeResp:
|
|
headers = {"Content-Type": "image/png"}
|
|
def __enter__(self): return self
|
|
def __exit__(self, *a): return False
|
|
def read(self): return b"\x89PNG\r\n\x1a\n"
|
|
|
|
monkeypatch.setattr(driver.urllib.request, "urlopen", lambda *a, **k: FakeResp())
|
|
path = driver.fetch_image("https://x.com/chair.png")
|
|
assert path.endswith(".png")
|
|
with open(path, "rb") as f:
|
|
assert f.read().startswith(b"\x89PNG")
|
|
import os as _os
|
|
_os.remove(path)
|