okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-split-tuiqa (95f380769) into f-split-int: TUI QA fixes (launch with flags, discovery, monitor menu, connect checks, layouts, no browser, warnings), picture reader CPU under 24 GB / GPU from 24 GBfd0e3766a

main
8.4 KiB203 linesraw
1"""The monitor as `strata launch` opens it: the Connect view (key 7), its keys, the check, and the quit question."""
2
3from __future__ import annotations
4
5import asyncio
6import json
7import os
8import unittest
9from pathlib import Path
10from unittest import mock
11
12from strata_cli.launch import check
13from strata_cli.launch.monitor_ext import LaunchMonitor
14from tests.terminal.launch_support import Home, text_of
15from tests.terminal.test_overview import Source, metrics
16
17INFO = {"url": "http://127.0.0.1:8080", "urls": ["http://127.0.0.1:8080", "http://192.168.1.5:8080"], "key": "sk-strata-abcdefghijklmnop",
18        "model": "qwen3.8-flash-next-iq3_xxs", "context": 32768, "profile": "launch-x", "name": "Demo"}
19
20
21def flat(app) -> str:
22    return " ".join(text_of(app).split())
23
24
25class Connect(unittest.TestCase):
26    def run_app(self, coro, info=None):
27        async def go():
28            app = LaunchMonitor(Source(metrics("idle")), None, info=info or dict(INFO), interval=30)
29            async with app.run_test(size=(120, 40)) as pilot:
30                await pilot.pause(0.5)
31                return await coro(app, pilot)
32        return asyncio.run(go())
33
34    def test_overview_first_and_connect_on_7(self):
35        async def case(app, pilot):
36            t = text_of(app)
37            self.assertIn("Model state", t)
38            self.assertIn("7 Connect", t)
39            self.assertIn("q asks: keep serving or stop", t)
40            await pilot.press("7")
41            await pilot.pause(0.2)
42            t = text_of(app)
43            self.assertIn("Connect an app", t)
44            self.assertIn("URL http://127.0.0.1:8080/v1", t)
45            self.assertIn("also reachable at http://192.168.1.5:8080/v1", t)
46            self.assertIn("key sk-strata-", t)
47            self.assertNotIn("abcdefghijklmnop", t)                      # masked until k
48            for name in ("OpenCode", "Codex CLI", "Claude Code", "oh-my-pi", "pi (pi.dev)", "Hermes Agent", "OpenClaw", "DeepSeek Harness"):
49                self.assertIn(name, t)
50            self.assertIn("Vendor tested", t)
51            self.assertIn("This server", t)
52            self.assertIn("Not checked", t)
53            self.assertIn("opencode 1.19.3", t)
54            self.assertIn("OpenCode setup", t)
55            self.assertIn("~/.config/opencode/opencode.json", t)
56            await pilot.press("k")
57            await pilot.pause(0.2)
58            self.assertIn("abcdefghijklmnop", flat(app).replace("▒", ""))
59            await pilot.press("7")
60            await pilot.pause(0.2)
61            self.assertIn("Connect an app", text_of(app))                 # 7 is a view like the others: pressing it again stays
62            await pilot.press("escape")
63            await pilot.pause(0.2)
64            self.assertIn("Model state", text_of(app))                    # Esc goes back to the view you came from
65        self.run_app(case)
66
67    def test_views_one_to_six_leave_connect(self):
68        async def case(app, pilot):
69            await pilot.press("7")
70            await pilot.press("2")
71            await pilot.pause(0.2)
72            self.assertNotIn("Connect an app", text_of(app))
73        self.run_app(case)
74
75    def test_choosing_an_app_shows_its_limits_in_words(self):
76        async def case(app, pilot):
77            await pilot.press("7")
78            for _ in range(5):
79                await pilot.press("down")
80            await pilot.pause(0.2)
81            t = flat(app)
82            self.assertIn("Hermes Agent setup", t)
83            self.assertIn("provider: lmstudio", t)
84            self.assertIn("WARNING: Hermes is built for 64K or more", t)
85            await pilot.press("down")
86            await pilot.pause(0.2)
87            self.assertIn("Node 24.16", flat(app))
88            await pilot.press("down")
89            await pilot.pause(0.2)
90            self.assertIn("built into the Strata app", flat(app))
91            await pilot.press("up", "up", "up", "up", "up", "up", "up")
92            await pilot.pause(0.2)
93            self.assertIn("OpenCode setup", flat(app))
94        self.run_app(case)
95
96    def test_check_reports_each_step_and_the_row(self):
97        results = [check.Check("prompt", True, "It answered: 'pong'.", 1.0), check.Check("tool call", False, "The model answered in words and did not call the tool.", 2.0)]
98        with mock.patch.object(check, "run_checks", return_value=results):
99            async def case(app, pilot):
100                await pilot.press("7", "h")
101                for _ in range(30):
102                    await pilot.pause(0.1)
103                    if "Failed (1/2)" in text_of(app):
104                        break
105                t = flat(app)
106                self.assertIn("Failed (1/2)", t)
107                self.assertIn("prompt: It answered: 'pong'.", t)
108                self.assertIn("did not call the tool", t)
109            self.run_app(case)
110
111    def test_write_asks_then_writes_with_a_backup(self):
112        with Home() as home:
113            target = Path(os.environ["HOME"]) / ".config" / "opencode" / "opencode.json"
114            target.parent.mkdir(parents=True)
115            target.write_text(json.dumps({"theme": "dark"}))
116
117            async def case(app, pilot):
118                await pilot.press("7", "w")
119                await pilot.pause(0.2)
120                self.assertIn("Write OpenCode's settings?", flat(app))
121                self.assertIn("copied to a backup first", flat(app))
122                await pilot.press("enter")
123                await pilot.pause(0.3)
124            self.run_app(case)
125            data = json.loads(target.read_text())
126            self.assertEqual(data["theme"], "dark")
127            self.assertIn("strata", data["provider"])
128            self.assertEqual(len(list(target.parent.glob("opencode.json.strata-bak-*"))), 1)
129
130    def test_write_can_be_declined(self):
131        with Home():
132            target = Path(os.environ["HOME"]) / ".config" / "opencode" / "opencode.json"
133
134            async def case(app, pilot):
135                await pilot.press("7", "w", "down", "enter")
136                await pilot.pause(0.2)
137            self.run_app(case)
138            self.assertFalse(target.exists())
139
140    def test_copy_does_not_crash(self):
141        async def case(app, pilot):
142            await pilot.press("7", "c")
143            await pilot.pause(0.2)
144            self.assertTrue("Copied" in flat(app) or "does not allow copying" in flat(app))
145        self.run_app(case)
146
147
148class Quit(unittest.TestCase):
149    def go(self, keys):
150        holder = {}
151
152        async def go():
153            app = LaunchMonitor(Source(metrics("idle")), None, info=dict(INFO), interval=30)
154            async with app.run_test(size=(120, 40)) as pilot:
155                await pilot.pause(0.4)
156                for k in keys:
157                    await pilot.press(k)
158                    await pilot.pause(0.2)
159                holder["app"] = app
160                holder["running"] = app.is_running
161                holder["stop"] = app.stop_requested
162        asyncio.run(go())
163        return holder
164
165    def test_q_asks_with_three_choices(self):
166        async def go():
167            app = LaunchMonitor(Source(metrics("idle")), None, info=dict(INFO), interval=30)
168            async with app.run_test(size=(120, 40)) as pilot:
169                await pilot.pause(0.4)
170                await pilot.press("q")
171                await pilot.pause(0.2)
172                t = flat(app)
173                self.assertIn("Leave the monitor?", t)
174                self.assertIn("Keep serving in the background", t)
175                self.assertIn("Stop serving and free the memory", t)
176                self.assertIn("Stay in the monitor", t)
177                await pilot.press("down", "down", "enter")                # Stay
178                await pilot.pause(0.2)
179                self.assertTrue(app.is_running)
180                self.assertFalse(app.stop_requested)
181        asyncio.run(go())
182
183    def test_keep_serving_leaves_without_stopping(self):
184        h = self.go(["q", "enter"])
185        self.assertFalse(h["stop"])
186
187    def test_stop_asks_the_caller_to_stop(self):
188        h = self.go(["q", "down", "enter"])
189        self.assertTrue(h["stop"])
190
191    def test_escape_cancels_the_question(self):
192        async def go():
193            app = LaunchMonitor(Source(metrics("idle")), None, info=dict(INFO), interval=30)
194            async with app.run_test(size=(120, 40)) as pilot:
195                await pilot.pause(0.4)
196                await pilot.press("q", "escape")
197                await pilot.pause(0.2)
198                self.assertTrue(app.is_running)
199        asyncio.run(go())
200
201
202if __name__ == "__main__":
203    unittest.main()