From 494d4722426d7acf51bc7265e881c60d2975bb52 Mon Sep 17 00:00:00 2001 From: "Danilo M." Date: Tue, 10 Nov 2026 12:18:30 +0100 Subject: feat(assistant): presets and ops on a file picked on the PC Co-Authored-By: Claude Opus 5.5 --- tests/test_assistant.py | 87 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) (limited to 'tests/test_assistant.py') diff --git a/tests/test_assistant.py b/tests/test_assistant.py index b384a6c..a3fb2b7 100644 --- a/tests/test_assistant.py +++ b/tests/test_assistant.py @@ -16,6 +16,7 @@ import json import os import socket +import stat import sys import tempfile import threading @@ -153,6 +154,8 @@ class Client(unittest.TestCase): self.m = a.make({"assistant": {"shell": "/nonexistent"}}) self.m.start() self.assertTrue(until(lambda: self.m.state()["connected"])) + # The daemon has registered the connection (and said hello) once it hears us. + self.assertTrue(until(lambda: {"type": "models"} in self.d.got)) def tearDown(self): self.m.stop() @@ -203,5 +206,89 @@ class Client(unittest.TestCase): self.assertEqual(self.m.state()["pending"]["reply"], "a") +CATALOG = {"presets": [{"label": "Translate", "icon": "", "kinds": ["pdf", "text"], + "prompt": "Translate this into English."}], + "ops": [{"label": "To .md", "icon": "", "op": "md", "kinds": ["pdf"], "max": 1}, + {"label": "Merge", "icon": "", "op": "pdf-merge", "kinds": ["pdf"], "min": 2}, + {"label": ".zip", "icon": "", "op": "zip", "kinds": ["any"]}], + "kinds": {"pdf": [".pdf"], "text": [".md", ".txt"]}, + "fileops": "", "outDir": "/tmp/out", "mdPrompt": "As Markdown.", "preferred": ""} + + +class QarmaArgs(unittest.TestCase): + def test_filter_and_multiple(self): + args = a.qarma_args(CATALOG["presets"][0], CATALOG["kinds"]) + self.assertIn("--multiple", args) + self.assertIn("--file-filter=Translate | *.md *.pdf *.txt", args) + + def test_single_file_op_and_any_kind(self): + self.assertNotIn("--multiple", a.qarma_args(CATALOG["ops"][0], CATALOG["kinds"])) + self.assertFalse([x for x in a.qarma_args(CATALOG["ops"][2], CATALOG["kinds"]) + if x.startswith("--file-filter")]) + + +def stub(dirpath, name, body): + p = Path(dirpath) / name + p.write_text("#!/bin/sh\n" + body + "\n") + p.chmod(p.stat().st_mode | stat.S_IXUSR) + return p + + +class Pick(Client): + def setUp(self): + self.bin = tempfile.mkdtemp(dir="/tmp") + cat = dict(CATALOG, fileops=str(Path(self.bin) / "fileops")) + stub(self.bin, "qs", "cat <<'EOF'\n" + json.dumps(cat) + "\nEOF") + self.path = os.environ["PATH"] + os.environ["PATH"] = self.bin + ":" + self.path + super().setUp() + + def tearDown(self): + os.environ["PATH"] = self.path + super().tearDown() + + def test_preset_asks_with_the_picked_files(self): + stub(self.bin, "qarma", "printf '/docs/a.pdf'") + self.m.actions["pick"]({"preset": "Translate"}) + self.assertTrue(until(lambda: {"type": "ask", "text": "Translate this into English.", + "files": ["/docs/a.pdf"]} in self.d.got)) + self.assertIsNone(self.m.state()["picking"]) + + def test_cancelled_dialog_does_nothing(self): + stub(self.bin, "qarma", "exit 1") + self.m.actions["pick"]({"preset": "Translate"}) + self.assertTrue(until(lambda: self.m.state()["picking"] is None)) + self.assertFalse([c for c in self.d.got if c["type"] == "ask"]) + + def test_op_runs_fileops(self): + stub(self.bin, "qarma", "printf '/v/a.pdf\\n/v/b.pdf'") + stub(self.bin, "fileops", 'echo \'{"ok": true, "out": ["/tmp/out/a+b.pdf"]}\'') + self.m.actions["pick"]({"op": "Merge"}) + self.assertTrue(until(lambda: (self.m.state()["op"] or {}).get("ok"))) + self.assertEqual(self.m.state()["op"]["out"], ["/tmp/out/a+b.pdf"]) + + def test_op_min_is_checked(self): + stub(self.bin, "qarma", "printf '/v/a.pdf'") + self.m.actions["pick"]({"op": "Merge"}) + self.assertTrue(until(lambda: (self.m.state()["op"] or {}).get("error"))) + self.assertIn("at least 2", self.m.state()["op"]["error"]) + + def test_md_goes_to_the_daemon(self): + stub(self.bin, "qarma", "printf '/docs/scan.pdf'") + self.m.actions["pick"]({"op": "To .md"}) + self.assertTrue(until(lambda: {"type": "write", "files": ["/docs/scan.pdf"], "prompt": "As Markdown.", + "dir": "/tmp/out", "name": "scan.md"} in self.d.got)) + + def test_unknown_label_and_busy(self): + stub(self.bin, "qarma", "sleep 2") + with self.assertRaises(a.ModuleError): + self.m.actions["pick"]({"preset": "Nope"}) + self.m.actions["pick"]({"preset": "Translate"}) + with self.assertRaises(a.ModuleError): + self.m.actions["pick"]({"preset": "Translate"}) + self.m.actions["cancel"]({}) + self.assertTrue(until(lambda: self.m.state()["picking"] is None, 3)) + + if __name__ == "__main__": unittest.main() -- cgit v1.2.3