okami.mom/strata

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

Niko1221Merge f-020-release into f-020-int (0.2.0 release wiring and docs)477fd36bc

main
14.5 KiB311 linesraw
1"""Tester report 2026-10-18, T06 / T08 / T10 and the smaller monitor items: one seven-view model in every view and at every width,
2Connect that says what is vendor history and what is this server's live result, and a light theme the Connect view and its pop-ups follow."""
3
4from __future__ import annotations
5
6import asyncio
7import re
8import unittest
9from unittest import mock
10
11from strata_cli import nav
12from strata_cli.launch import check, harness
13from strata_cli.launch.monitor_ext import LaunchMonitor
14from strata_cli.launch.widgets import DARK, LIGHT
15from strata_cli.monitor import MonitorApp
16from tests.terminal.launch_support import Home, text_of
17from tests.terminal.test_launch_monitor import INFO
18from tests.terminal.test_overview import Source, metrics
19
20SIZES = ((120, 40), (80, 24), (60, 20), (40, 15))
21NAMES = ["Overview", "Serving", "Requests", "Engine", "Hardware", "Logs", "Connect"]
22SHORT = {"Overview": "Over", "Serving": "Serve", "Requests": "Reqs", "Engine": "Engine", "Hardware": "GPU", "Logs": "Logs", "Connect": "Conn"}
23
24
25def tab_line(shown: str) -> str:
26    """The row with the tab names: the one that has the marked tab."""
27    for line in shown.splitlines():
28        if re.search(r"\[\d [A-Za-z]+\]", line) and re.search(r"\b\d [A-Za-z]+", line.replace("[", " ")):
29            return line
30    return ""
31
32
33def marked(line: str) -> str | None:
34    m = re.search(r"\[(\d) ([A-Za-z]+)\]", line)
35    return None if m is None else m.group(2)
36
37
38class TabModel(unittest.TestCase):
39    def test_all_names_when_there_is_room(self):
40        self.assertEqual(nav.plain(1, 120, True), "1 Overview  [2 Serving]  3 Requests  4 Engine  5 Hardware  6 Logs  7 Connect")
41        self.assertEqual(nav.plain(1, 120, False), "1 Overview  [2 Serving]  3 Requests  4 Engine  5 Hardware  6 Logs")
42
43    def test_short_names_then_a_window_around_the_selected_tab_and_it_is_always_marked(self):
44        for connect in (False, True):
45            for current in range(nav.count(connect)):
46                for width in (120, 90, 80, 60, 50, 40, 30, 24):
47                    line = nav.plain(current, width, connect)
48                    self.assertLessEqual(len(line), width, (current, width, connect))
49                    name = NAMES[current]
50                    self.assertTrue(f"[{current + 1} {name}]" in line or f"[{current + 1} {SHORT[name]}]" in line, (line, current, width))
51                    if width >= 60 + (10 if connect else 0):
52                        self.assertNotIn("<", line)                       # nothing hidden when the short names fit
53        self.assertEqual(nav.plain(3, 40, True), "< 3 Reqs [4 Engine] 5 GPU 6 Logs 7 Conn")
54        self.assertEqual(nav.plain(3, 34, True), "< 3 Reqs [4 Engine] 5 GPU 6 Logs >")
55
56    def test_the_hint_matches_the_number_of_views(self):
57        self.assertEqual((nav.keys(False), nav.keys(True)), ("1-6", "1-7"))
58
59    def test_styled_marks_the_selected_tab_bold(self):
60        t = nav.styled(2, 120, True, accent="#34d399")
61        spans = [(t.plain[s.start:s.end], s.style) for s in t.spans if "bold" in str(s.style)]
62        self.assertEqual([x for x, _ in spans], ["[3 Requests]"])
63
64
65class MonitorViews(unittest.TestCase):
66    def run_app(self, cls, size, coro, **kw):
67        async def go():
68            if cls is LaunchMonitor:
69                app = LaunchMonitor(Source(metrics("idle")), None, info=dict(INFO), interval=30, **kw)
70            else:
71                app = MonitorApp(Source(metrics("idle")), None, interval=30, **kw)
72            async with app.run_test(size=size) as pilot:
73                await pilot.pause(0.4)
74                return await coro(app, pilot)
75        with Home():
76            return asyncio.run(go())
77
78    def test_arrows_visit_all_seven_views_and_wrap_both_ways(self):
79        async def case(app, pilot):
80            seen = [app.view]
81            for _ in range(7):
82                await pilot.press("right")
83                await pilot.pause(0.05)
84                seen.append(app.view)
85            self.assertEqual(seen, [0, 1, 2, 3, 4, 5, 6, 0])           # Logs -> Connect -> Overview
86            await pilot.press("left")
87            await pilot.pause(0.05)
88            self.assertEqual(app.view, 6)                               # Overview <- Connect
89            await pilot.press("left")
90            await pilot.pause(0.05)
91            self.assertEqual(app.view, 5)                               # Connect <- Logs: the previous tab, not a relative of the last ordinary view
92        self.run_app(LaunchMonitor, (120, 40), case)
93
94    def test_number_keys_and_arrows_agree(self):
95        async def case(app, pilot):
96            for n in range(1, 8):
97                await pilot.press(str(n))
98                await pilot.pause(0.05)
99                self.assertEqual(app.view, n - 1)
100        self.run_app(LaunchMonitor, (120, 40), case)
101
102    def test_every_view_at_every_size_shows_the_tabs_the_hint_and_the_selection(self):
103        async def case(app, pilot):
104            for size in SIZES:
105                await pilot.resize_terminal(*size)
106                await pilot.pause(0.15)
107                for index in range(7):
108                    await pilot.press(str(index + 1))
109                    await pilot.pause(0.08)
110                    shown = text_of(app)
111                    line = tab_line(shown)
112                    name = marked(line)
113                    self.assertIsNotNone(name, (size, index, shown[:400]))
114                    self.assertIn(name, (NAMES[index], SHORT[NAMES[index]]), (size, index, line))
115                    self.assertNotIn("1-6 views", shown, (size, index))           # the hint never disagrees with the tab line
116                    self.assertIn("Overview" if index == 0 and size[0] >= 78 else NAMES[index][:3], shown.replace("[", " "), (size, index))
117        self.run_app(LaunchMonitor, (120, 40), case)
118
119    def test_the_plain_monitor_has_six_views_and_says_1_6(self):
120        async def case(app, pilot):
121            for size in SIZES:
122                await pilot.resize_terminal(*size)
123                await pilot.pause(0.15)
124                shown = text_of(app)
125                self.assertNotIn("Connect", shown)
126                self.assertNotIn("1-7", shown)
127            await pilot.press("7")
128            await pilot.pause(0.05)
129            self.assertEqual(app.view, 0)                                      # no seventh view here
130            await pilot.press("left")
131            await pilot.pause(0.05)
132            self.assertEqual(app.view, 5)
133        self.run_app(MonitorApp, (120, 40), case)
134
135    def test_strata_monitor_is_the_same_seven_view_monitor_as_launchs(self):
136        """`strata monitor --url ...` builds the launch monitor without the quit question: seven views, key 7, Connect points at that server."""
137        from strata_cli import monitor_commands
138        seen = {}
139
140        class Fake:
141            def __init__(self, endpoint, snapshot, **kw):
142                seen.update(kw, endpoint=endpoint)
143
144            def run(self):
145                seen["ran"] = True
146
147        args = monitor_commands.parser().parse_args(["monitor", "--url", "http://127.0.0.1:18765"])
148        with mock.patch("strata_cli.launch.monitor_ext.LaunchMonitor", Fake), mock.patch("sys.stdout.isatty", return_value=True), Home():
149            self.assertEqual(monitor_commands.run(args), 0)
150        self.assertTrue(seen.get("ran"))
151        self.assertFalse(seen["ask_quit"])                                   # q detaches: this command started nothing
152        self.assertEqual(seen["info"]["url"], "http://127.0.0.1:18765")      # the Connect view's address is the --url server
153        self.assertIn("seven views", " ".join(monitor_commands.parser().format_help().split()))
154
155    def test_the_monitor_strata_monitor_opens_has_seven_views_and_connect_shows_the_server(self):
156        async def case(app, pilot):
157            shown = text_of(app)
158            self.assertIn("7 Connect", shown)
159            self.assertNotIn("1-6", shown)
160            await pilot.press("7")
161            await pilot.pause(0.2)
162            self.assertEqual(app.view, 6)
163            self.assertIn(INFO["url"], text_of(app))
164            await pilot.press("escape")
165            await pilot.pause(0.1)
166            self.assertEqual(app.view, 0)
167        async def go():
168            app = LaunchMonitor(Source(metrics("idle")), None, info=dict(INFO), interval=30, ask_quit=False)
169            async with app.run_test(size=(120, 40)) as pilot:
170                await pilot.pause(0.4)
171                return await case(app, pilot)
172        with Home():
173            asyncio.run(go())
174
175    def test_the_overview_is_titled_overview(self):
176        async def case(app, pilot):
177            self.assertIn("STRATA  Overview", text_of(app))
178            self.assertNotIn("STRATA  Serving", text_of(app))
179            await pilot.press("2")
180            await pilot.pause(0.1)
181            self.assertIn("Serving", text_of(app))
182        self.run_app(LaunchMonitor, (120, 40), case)
183
184
185class ConnectStatus(unittest.TestCase):
186    def run_app(self, coro, size=(120, 40), **kw):
187        async def go():
188            app = LaunchMonitor(Source(metrics("idle")), None, info=dict(INFO), interval=30, **kw)
189            async with app.run_test(size=size) as pilot:
190                await pilot.pause(0.4)
191                return await coro(app, pilot)
192        with Home():
193            return asyncio.run(go())
194
195    def test_vendor_history_and_this_servers_result_are_separate_columns(self):
196        async def case(app, pilot):
197            await pilot.press("7")
198            await pilot.pause(0.2)
199            shown = text_of(app)
200            header = next(ln for ln in shown.splitlines() if "Vendor tested" in ln)
201            self.assertIn("This server", header)
202            for h in harness.HARNESSES:
203                said = "built into Strata" if h.built_in else h.tested
204                row = next(ln for ln in shown.splitlines() if h.title[:16] in ln and said[:23] in ln)
205                self.assertIn("Not checked", row)                          # nothing has been tried against THIS server yet
206                self.assertNotIn("tested ok", row)
207                self.assertIn(said[:23], row)
208            self.assertNotIn("Passed", shown)
209        self.run_app(case)
210
211    def test_a_check_turns_not_checked_into_passed_or_failed_for_that_app_only(self):
212        good = [check.Check("prompt", True, "ok", 1.0), check.Check("tool call", True, "ok", 1.0)]
213        bad = [check.Check("prompt", True, "ok", 1.0), check.Check("tool call", False, "no tool call", 1.0)]
214
215        async def case(app, pilot):
216            await pilot.press("7")
217            for results, want in ((good, "Passed (2/2)"), (bad, "Failed (1/2)")):
218                with mock.patch.object(check, "run_checks", return_value=results):
219                    await pilot.press("h")
220                    for _ in range(30):
221                        await pilot.pause(0.1)
222                        if want in text_of(app):
223                            break
224                self.assertIn(want, text_of(app))
225                await pilot.press("down")
226                await pilot.pause(0.1)
227            shown = text_of(app)
228            self.assertEqual(shown.count("Not checked"), 6)                # 8 apps, 2 checked
229        self.run_app(case)
230
231    def test_narrow_widths_keep_the_this_server_column(self):
232        async def case(app, pilot):
233            for width in (80, 60, 40):
234                await pilot.resize_terminal(width, 24)
235                await pilot.pause(0.15)
236                await pilot.press("7")
237                await pilot.pause(0.1)
238                shown = text_of(app)
239                self.assertIn("This server", shown, width)
240                self.assertIn("Not checked", shown, width)
241        self.run_app(case)
242
243
244def luminance(hex_color: str) -> float:
245    r, g, b = (int(hex_color[i:i + 2], 16) / 255 for i in (1, 3, 5))
246    f = lambda c: c / 12.92 if c <= 0.03928 else ((c + 0.055) / 1.055) ** 2.4          # noqa: E731
247    return 0.2126 * f(r) + 0.7152 * f(g) + 0.0722 * f(b)
248
249
250def contrast(a: str, b: str) -> float:
251    la, lb = sorted((luminance(a), luminance(b)), reverse=True)
252    return (la + 0.05) / (lb + 0.05)
253
254
255class LightTheme(unittest.TestCase):
256    def test_the_light_palette_is_readable_on_white_and_on_its_selection(self):
257        for name in ("fg", "mute", "accent", "warn", "bad"):
258            self.assertGreaterEqual(contrast(getattr(LIGHT, name), LIGHT.panel), 4.5, name)
259        self.assertGreaterEqual(contrast(LIGHT.accent, LIGHT.sel), 4.5)
260        self.assertLess(contrast(DARK.accent, "#ffffff"), 3.0)                  # the old dark colours are what the report measured (1.92:1)
261
262    def run_app(self, coro, size=(120, 40)):
263        async def go():
264            app = LaunchMonitor(Source(metrics("idle")), None, info=dict(INFO), interval=30, theme="light", color="always")
265            async with app.run_test(size=size) as pilot:
266                await pilot.pause(0.4)
267                return await coro(app, pilot)
268        with Home():
269            return asyncio.run(go())
270
271    def test_connect_uses_the_light_colours_only(self):
272        async def case(app, pilot):
273            await pilot.press("7")
274            await pilot.pause(0.2)
275            t = app.connect_text(118)
276            used = {str(s.style) for s in t.spans}
277            joined = " ".join(used)
278            for dark_only in ("#34d399", "#8e979f", "#f28b82", "#f5c56b", "#1f2a27"):
279                self.assertNotIn(dark_only, joined)
280            self.assertIn(LIGHT.accent, joined)
281            self.assertIn(LIGHT.mute, joined)
282        self.run_app(case)
283
284    def test_the_dialog_opened_from_light_mode_is_light(self):
285        async def case(app, pilot):
286            self.assertTrue(app.has_class("theme-light"))
287            await pilot.press("7")
288            await pilot.pause(0.1)
289            await pilot.press("w")
290            await pilot.pause(0.3)
291            dialog = app.screen.query_one("#dialog")
292            self.assertEqual(str(dialog.styles.background.hex).lower()[:7], "#ffffff")
293            await pilot.press("escape")
294            await pilot.pause(0.1)
295        self.run_app(case)
296
297    def test_theme_cycling_moves_the_class_and_the_dialog_back_to_dark(self):
298        async def case(app, pilot):
299            self.assertTrue(app.has_class("theme-light"))
300            await pilot.press("t")                                           # light -> terminal
301            await pilot.pause(0.05)
302            self.assertFalse(app.has_class("theme-light"))
303            await pilot.press("t")                                           # terminal -> dark
304            await pilot.press("7", "w")
305            await pilot.pause(0.3)
306            self.assertEqual(str(app.screen.query_one("#dialog").styles.background.hex).lower()[:7], "#161a1d")
307        self.run_app(case)
308
309
310if __name__ == "__main__":
311    unittest.main()