diff options
| author | Danilo M. <danix@danix.xyz> | 2026-11-10 12:17:03 +0100 |
|---|---|---|
| committer | Danilo M. <danix@danix.xyz> | 2026-11-10 12:17:03 +0100 |
| commit | eeb39c082be2ebb81a8205d26a3639ff000ebf23 (patch) | |
| tree | deef03efd017c665773b79e8a546525ed4c05017 /tests | |
| parent | a9a737c7fd0efd8f82db78ce19e7858803e0f6b2 (diff) | |
| download | deck-it-eeb39c082be2ebb81a8205d26a3639ff000ebf23.tar.gz deck-it-eeb39c082be2ebb81a8205d26a3639ff000ebf23.zip | |
feat(assistant): daemon client with chat, model and voice actions
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/test_assistant.py | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/tests/test_assistant.py b/tests/test_assistant.py index f91f3cd..b384a6c 100644 --- a/tests/test_assistant.py +++ b/tests/test_assistant.py @@ -13,7 +13,13 @@ # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., +import json +import os +import socket import sys +import tempfile +import threading +import time from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) @@ -94,5 +100,108 @@ class Hears(unittest.TestCase): self.assertTrue(a.hears([{"id": "x", "status": "loaded", "cloud": False, "inputs": None}], None)) + +class FakeDaemon: + """A desktop-assistant stand-in: says hello, records commands, can push events.""" + + def __init__(self, path): + self.got, self.conns = [], [] + self.srv = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + self.srv.bind(path) + self.srv.listen() + threading.Thread(target=self.serve, daemon=True).start() + + def serve(self): + while True: + try: + c, _ = self.srv.accept() + except OSError: + return + self.conns.append(c) + c.sendall(b'{"type":"hello","state":"idle","model":"m1","turns":[],"pending":null}\n') + threading.Thread(target=self.read, args=(c,), daemon=True).start() + + def read(self, c): + for line in c.makefile("r"): + self.got.append(json.loads(line)) + + def push(self, e): + for c in self.conns: + c.sendall((json.dumps(e) + "\n").encode()) + + def close(self): + for c in self.conns: + c.close() + self.srv.close() + + +def until(fn, secs=2): + end = time.monotonic() + secs + while time.monotonic() < end: + if fn(): + return True + time.sleep(0.02) + return False + + +class Client(unittest.TestCase): + def setUp(self): + self.dir = tempfile.mkdtemp(dir="/tmp") + a.SOCK = Path(self.dir) / "d.sock" + a.VOICE = Path(self.dir) / "deck-it" / "voice.wav" + self.d = FakeDaemon(str(a.SOCK)) + self.m = a.make({"assistant": {"shell": "/nonexistent"}}) + self.m.start() + self.assertTrue(until(lambda: self.m.state()["connected"])) + + def tearDown(self): + self.m.stop() + self.d.close() + + def test_connects_and_asks_for_models(self): + self.assertTrue(until(lambda: {"type": "models"} in self.d.got)) + self.assertEqual(self.m.state()["model"], "m1") + + def test_ask_carries_the_picked_model(self): + self.d.push({"type": "models", "items": [{"id": "ears", "status": "loaded", "cloud": False, + "inputs": ["audio"]}]}) + self.assertTrue(until(lambda: self.m.state()["models"])) + self.m.actions["model"]({"id": "ears"}) + self.m.actions["ask"]({"text": "hi"}) + self.assertTrue(until(lambda: {"type": "ask", "text": "hi", "model": "ears"} in self.d.got)) + self.assertTrue(self.m.state()["hears"]) + + def test_bad_input_is_refused(self): + for bad in ({}, {"text": ""}, {"text": 3}): + with self.assertRaises(a.ModuleError): + self.m.actions["ask"](bad) + with self.assertRaises(a.ModuleError): + self.m.actions["model"]({"id": "nope"}) + + def test_cloud_pick_with_turns_warns_once(self): + self.d.push({"type": "models", "items": [{"id": "p:big", "provider": "p", "cloud": True}]}) + self.d.push({"type": "user", "text": "q"}) + self.d.push({"type": "done", "id": 1}) + self.assertTrue(until(lambda: self.m.state()["models"] and self.m.state()["turns"])) + self.m.actions["model"]({"id": "p:big"}) + self.assertIn("goes to p", self.m.state()["notice"]) + + def test_voice_writes_the_clip_and_asks(self): + wav = b"RIFF\x24\x00\x00\x00WAVE" + b"\x00" * 32 + self.m.actions["voice"](wav) + self.assertEqual(a.VOICE.read_bytes(), wav) + self.assertTrue(until(lambda: {"type": "ask", "text": "", "audio": str(a.VOICE)} in self.d.got)) + with self.assertRaises(a.ModuleError): + self.m.actions["voice"](b"not a wav") + + def test_streamed_state_is_a_copy(self): + self.d.push({"type": "user", "text": "q"}) + self.d.push({"type": "token", "delta": "a"}) + self.assertTrue(until(lambda: (self.m.state()["pending"] or {}).get("reply") == "a")) + s = self.m.state() + s["pending"]["reply"] = "changed" + self.assertEqual(self.m.state()["pending"]["reply"], "a") + + if __name__ == "__main__": unittest.main() |
