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
|
# deck-it - tests for llm
# 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.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
import unittest
import deckit.modules.llm as llm
class Down(unittest.TestCase):
def test_offline_when_dashboard_down(self):
def boom(url, timeout=2):
raise OSError("refused")
st = llm.make({"llm": {"url": "http://x"}, "fetch": boom}).state()
self.assertTrue(st["offline"])
class Up(unittest.TestCase):
def test_aggregates_opencode_and_ignores_other_programs(self):
data = {"/api/claude_limits": [{"context_pct": 12}],
"/api/claude_session": {"session": {"models": [{"model": "claude-opus-4"}]}},
"/api/summary": [
{"program": "opencode", "provider": "deepseek", "model": "v4",
"total_cost_usd": 1.5, "tokens": 1000, "calls": 3},
{"program": "opencode", "provider": "together", "model": "qwen",
"total_cost_usd": 0.5, "tokens": 500, "calls": 1},
{"program": "assistant", "provider": "x", "model": "y",
"total_cost_usd": 9.0, "tokens": 9999, "calls": 9},
],
"/api/last_session": {"sessions": [
{"program": "opencode", "rows": [
{"provider": "together", "model": "qwen", "calls": 1,
"cost_usd": 0.2, "tokens": 200},
{"provider": "deepseek", "model": "v4", "calls": 3,
"cost_usd": 0.3, "tokens": 300},
]},
]}}
st = llm.make({"llm": {"url": "http://x"},
"fetch": lambda u, timeout=2: data[u]}).state()
self.assertFalse(st["offline"])
self.assertEqual(st["claude"]["context_pct"], 12)
self.assertEqual(st["claude"]["model"], "claude-opus-4")
self.assertEqual(st["opencode"]["cost"], 2.0)
self.assertEqual(st["opencode"]["tokens"], 1500)
self.assertEqual(st["opencode"]["calls"], 4)
self.assertEqual(st["opencode"]["model"], "v4")
self.assertEqual(st["opencode"]["session_cost"], 0.5)
self.assertEqual(st["opencode"]["session_calls"], 4)
self.assertEqual(st["opencode"]["session_tokens"], 500)
def test_handles_the_real_latest_shape(self):
data = {"/api/claude_limits": {"latest": {"context_pct": 42, "five_hour_pct": 10}},
"/api/claude_session": {"session": None},
"/api/summary": [],
"/api/last_session": {"sessions": []}}
st = llm.make({"llm": {"url": "http://x"},
"fetch": lambda u, timeout=2: data[u]}).state()
self.assertEqual(st["claude"]["context_pct"], 42)
self.assertEqual(st["claude"]["five_hour_pct"], 10)
self.assertIsNone(st["claude"]["model"])
self.assertIsNone(st["opencode"]["model"])
if __name__ == "__main__":
unittest.main()
|