aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_assistant.py
blob: f91f3cd56b1f83ba0479167183f72ed0806132f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# deck-it - tests for assistant
# Copyright (C) 2026 Danilo M. <danix@danix.xyz>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# 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 sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parent.parent))

import unittest

from deckit.modules import assistant as a


def folded(*events):
    st = a.blank()
    for e in events:
        a.fold(st, e)
    return st


class Fold(unittest.TestCase):
    def test_hello_with_a_reply_in_flight(self):
        st = folded({"type": "hello", "state": "thinking", "model": "m1", "chat_model": "c1",
                     "turns": [{"user": "hi", "reply": "yo", "tools": ["now"], "files": []}],
                     "pending": {"user": "and?", "reply": "par", "think": "", "tools": [], "files": []}})
        self.assertEqual((st["state"], st["model"], st["chatModel"]), ("thinking", "m1", "c1"))
        self.assertEqual(st["turns"][0]["tools"], ["now"])
        self.assertEqual(st["pending"]["reply"], "par")

    def test_a_turn_streams_and_settles(self):
        st = folded({"type": "user", "text": "q"}, {"type": "think", "delta": "h"},
                    {"type": "tool", "name": "web", "done": False}, {"type": "tool", "name": "web", "done": True},
                    {"type": "token", "delta": "a"}, {"type": "token", "delta": "b"},
                    {"type": "done", "id": 3})
        self.assertIsNone(st["pending"])
        self.assertEqual(st["turns"], [{"user": "q", "reply": "ab", "think": "h", "tools": ["web"],
                                        "stopped": False}])

    def test_stop_keeps_the_partial_reply_marked(self):
        st = folded({"type": "user", "text": "q"}, {"type": "token", "delta": "a"},
                    {"type": "error", "message": "stopped", "turn": True},
                    {"type": "done", "id": None, "stopped": True})
        self.assertEqual(st["turns"][-1]["error"], "stopped")
        self.assertIsNone(st["pending"])

    def test_command_error_is_a_notice(self):
        self.assertEqual(folded({"type": "error", "message": "bad id"})["notice"], "bad id")

    def test_conversation_replaces_the_transcript(self):
        st = folded({"type": "user", "text": "q"},
                    {"type": "conversation", "id": None, "turns": [], "model": None})
        self.assertEqual((st["turns"], st["pending"], st["epoch"]), ([], None, 1))

    def test_written_ends_the_op(self):
        st = a.blank()
        st["op"] = {"label": "To .md", "running": True, "ok": False, "out": [], "error": ""}
        a.fold(st, {"type": "written", "path": "/x/doc.md"})
        self.assertEqual((st["op"]["running"], st["op"]["ok"], st["op"]["out"]), (False, True, ["/x/doc.md"]))

    def test_refused_write_ends_the_op(self):
        st = a.blank()
        st["op"] = {"label": "To .md", "running": True, "ok": False, "out": [], "error": ""}
        a.fold(st, {"type": "error", "message": "bad write", "turn": True})
        self.assertEqual((st["op"]["running"], st["op"]["error"]), (False, "bad write"))


class Hears(unittest.TestCase):
    MODELS = [{"id": "deaf", "status": "loaded", "cloud": False, "inputs": ["text"]},
              {"id": "ears", "status": "unloaded", "cloud": False, "inputs": ["text", "audio"]},
              {"id": "p:big", "provider": "p", "cloud": True}]

    def test_local_pick_decides(self):
        self.assertTrue(a.hears(self.MODELS, "ears"))
        self.assertFalse(a.hears(self.MODELS, "deaf"))

    def test_cloud_or_no_pick_uses_the_loaded_model(self):
        self.assertFalse(a.hears(self.MODELS, "p:big"))
        self.assertFalse(a.hears(self.MODELS, None))

    def test_unknown_is_yes(self):
        self.assertTrue(a.hears([], None))
        self.assertTrue(a.hears([{"id": "x", "status": "loaded", "cloud": False, "inputs": None}], None))


if __name__ == "__main__":
    unittest.main()