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
9.9 KiB218 linesraw
1"""Tester report 2026-10-18 (Terminal 0.1.45), T02: `strata launch` with flags opened the screens straight into the run, which crashed
2(IndexError: pop from empty list in switch_screen) and exited 0. The run now opens on its own screen; an error of the screens ends non-zero."""
3
4from __future__ import annotations
5
6import asyncio
7import contextlib
8import json
9import io
10import unittest
11from unittest import mock
12
13from textual.app import App
14
15from strata_cli.launch import app as launch_app
16from strata_cli.launch import settings as S
17from strata_cli.launch.app import Ctx, LaunchApp
18from strata_cli.launch.widgets import Glyphs
19from tests.terminal.launch_support import HW_12, Home, catalog, text_of
20from tests.terminal.test_launch_tui import FakeRunner, SIZE, make_ctx
21
22
23def flags_settings() -> S.LaunchSettings:
24    """What `strata launch --model q2_0 --family qwen --context 128k --kv int8 --vision cpu --parallel 1 --tune skip --access local --port 8081` gives."""
25    return S.LaunchSettings(name="QA flags", family="qwen", model="Q2_0", context=131072, kv="int8", vision="cpu", parallel=1, tune="skip",
26                            access="local", port=8081)
27
28
29class FlagsOpenTheRun(unittest.TestCase):
30    def setUp(self):
31        self.home = Home().__enter__()
32        self.addCleanup(self.home.__exit__, None, None, None)
33        FakeRunner.instances.clear()
34        FakeRunner.behaviour = "ok"
35        FakeRunner.download = 0.0
36        p = mock.patch("strata_cli.launch.app.Runner", FakeRunner)
37        p.start()
38        self.addCleanup(p.stop)
39
40    def test_auto_run_shows_the_run_screen_and_finishes(self):
41        app = LaunchApp(make_ctx(settings=flags_settings()), auto_run=True)
42
43        async def go():
44            async with app.run_test(size=SIZE) as pilot:
45                for _ in range(60):
46                    await pilot.pause(0.05)
47                    if app.result is not None:
48                        break
49                return app.result
50
51        result = asyncio.run(go())
52        self.assertIsNotNone(result, "the run did not finish: the screens crashed instead of opening the run")
53        self.assertEqual(result.urls, ["http://127.0.0.1:8081"])
54
55    def test_auto_run_with_a_big_download_asks_first_and_then_runs(self):
56        FakeRunner.download = 66.0
57        app = LaunchApp(make_ctx(settings=flags_settings()), auto_run=True)
58
59        async def go():
60            async with app.run_test(size=SIZE) as pilot:
61                await pilot.pause(0.3)
62                self.assertIn("Download about 66 GB?", text_of(app))
63                await pilot.press("enter")                      # Download and start
64                for _ in range(60):
65                    await pilot.pause(0.05)
66                    if app.result is not None:
67                        break
68                return app.result
69
70        self.assertIsNotNone(asyncio.run(go()))
71
72    def headless_run(self, app):
73        return App.run(app, headless=True)
74
75    def test_run_tui_with_flags_returns_zero_and_opens_the_monitor(self):
76        calls = []
77        with mock.patch.object(LaunchApp, "run", lambda self, **kw: App.run(self, headless=True)), \
78                mock.patch.object(launch_app, "run_monitor", lambda info, **kw: calls.append(info) or 0), \
79                mock.patch.object(launch_app, "Catalog", create=True):
80            cat = catalog(HW_12)
81            code = launch_app.run_tui(flags_settings(), catalog=cat, auto_run=True)
82        self.assertEqual(code, 0)
83        self.assertEqual(len(calls), 1)
84
85    def test_an_error_of_the_screens_ends_non_zero_with_a_plain_sentence(self):
86        class Broken(FakeRunner):
87            def __init__(self, *a, **kw):
88                raise RuntimeError("boom")
89
90        err = io.StringIO()
91        with mock.patch.object(LaunchApp, "run", lambda self, **kw: App.run(self, headless=True)), \
92                mock.patch.object(launch_app, "Runner", Broken), \
93                contextlib.redirect_stderr(err), contextlib.redirect_stdout(io.StringIO()):
94            code = launch_app.run_tui(flags_settings(), catalog=catalog(HW_12), auto_run=True)
95        self.assertNotEqual(code, 0)
96        self.assertIn("nothing was started", err.getvalue())
97        self.assertIn("--no-tui", err.getvalue())
98
99    def test_the_command_turns_a_crash_into_exit_one(self):
100        from strata_cli.launch import cmd
101        err = io.StringIO()
102        with contextlib.redirect_stderr(err):
103            code = cmd._tui(lambda *a, **k: (_ for _ in ()).throw(IndexError("pop from empty list")))
104        self.assertEqual(code, 1)
105        self.assertIn("unexpected problem", err.getvalue())
106        self.assertNotIn("Traceback", err.getvalue())
107
108
109if __name__ == "__main__":
110    unittest.main()
111
112
113class NoEngineFlow(unittest.TestCase):
114    """Owner decision 2026-10-18: no usable engine is a warning, not a refusal; the model downloads and the end message names the problem."""
115
116    def setUp(self):
117        self.home = Home().__enter__()
118        self.addCleanup(self.home.__exit__, None, None, None)
119
120    def served_nothing(self):
121        from strata_cli.launch.runner import Result, no_engine_final
122        text = no_engine_final("The feed has no engine package for this PC.")
123        return Result("launch-x", [], None, None, [text], "/x", serving=False, engine_problem="The feed has no engine package for this PC.",
124                      final_text=text)
125
126    def test_the_command_ends_with_exit_zero_and_the_engine_message(self):
127        from strata_cli.launch.runner import Runner
128        from tests.terminal.launch_support import run_cli
129        with mock.patch.object(Runner, "run", return_value=self.served_nothing()):
130            code, out, err = run_cli(["launch", "--no-tui", "--model", "q2_0", "--yes"])
131        self.assertEqual(code, 0, err)
132        self.assertIn("The model is ready, but Strata can't start it yet", out)
133        self.assertIn("The feed has no engine package for this PC", out)
134        self.assertIn("Next step:", out)
135        self.assertNotIn("Strata is serving", out)
136
137    def test_json_says_not_serving(self):
138        from strata_cli.launch.runner import Runner
139        from tests.terminal.launch_support import run_cli
140        with mock.patch.object(Runner, "run", return_value=self.served_nothing()):
141            code, out, err = run_cli(["launch", "--no-tui", "--model", "q2_0", "--output", "json"])
142        data = json.loads(out)
143        self.assertEqual(code, 0)
144        self.assertTrue(data["ok"])
145        self.assertFalse(data["serving"])
146        self.assertIn("engine", data["engine_problem"])
147
148    def test_yes_is_passed_to_the_runner_and_json_never_asks(self):
149        from strata_cli.launch.runner import Runner
150        from tests.terminal.launch_support import run_cli
151        seen = []
152        real = Runner.__init__
153
154        def spy(self, *a, **kw):
155            seen.append(kw.get("assume_yes"))
156            real(self, *a, **kw)
157
158        with mock.patch.object(Runner, "__init__", spy), mock.patch.object(Runner, "run", return_value=self.served_nothing()):
159            run_cli(["launch", "--no-tui", "--model", "q2_0", "--yes"])
160            run_cli(["launch", "--no-tui", "--model", "q2_0", "--output", "json"])
161            run_cli(["launch", "--no-tui", "--model", "q2_0"])
162        self.assertEqual(seen, [True, True, False])
163
164    def test_the_question_default_is_yes(self):
165        from strata_cli.launch import cmd
166        with mock.patch("builtins.input", return_value=""), mock.patch.object(cmd.sys, "stdin") as sin, \
167                mock.patch.object(cmd.sys, "stdout") as sout:
168            sin.isatty.return_value = True
169            sout.isatty.return_value = True
170            self.assertTrue(cmd._ask_yes("t", "text"))
171        with mock.patch("builtins.input", return_value="n"), mock.patch.object(cmd.sys, "stdin") as sin, \
172                mock.patch.object(cmd.sys, "stdout") as sout:
173            sin.isatty.return_value = True
174            sout.isatty.return_value = True
175            self.assertFalse(cmd._ask_yes("t", "text"))
176        self.assertTrue(cmd._ask_yes("t", "text"))               # no keyboard: the person already named the model
177
178    def test_the_screens_ask_with_a_dialog_and_enter_means_yes(self):
179        FakeRunner.instances.clear()
180        FakeRunner.behaviour = "ok"
181        FakeRunner.download = 0.0
182        answers = []
183
184        class Asking(FakeRunner):
185            def run(self, start=True):
186                answers.append(self.confirm("No engine", "Strata couldn't find an engine for this computer (x). Download?"))
187                return super().run(start)
188
189            def __init__(self, *a, confirm=None, **kw):
190                super().__init__(*a, **kw)
191                self.confirm = confirm
192
193        app = LaunchApp(make_ctx(settings=flags_settings()), auto_run=True)
194
195        async def go():
196            with mock.patch("strata_cli.launch.app.Runner", Asking):
197                async with app.run_test(size=SIZE) as pilot:
198                    await pilot.pause(0.4)
199                    self.assertIn("Strata couldn't find an engine for this computer", text_of(app))
200                    await pilot.press("enter")                  # Download anyway
201                    for _ in range(60):
202                        await pilot.pause(0.05)
203                        if app.result is not None:
204                            break
205                    return app.result
206
207        self.assertIsNotNone(asyncio.run(go()))
208        self.assertEqual(answers, [True])
209
210    def test_the_screens_show_the_end_message_and_exit_zero(self):
211        from strata_cli.launch.app import LaunchApp as LA
212        res = self.served_nothing()
213        with mock.patch.object(LA, "run", lambda self, **kw: res), mock.patch.object(launch_app, "run_monitor", side_effect=AssertionError("no monitor")):
214            out = io.StringIO()
215            with contextlib.redirect_stdout(out):
216                code = launch_app.run_tui(flags_settings(), catalog=catalog(HW_12), auto_run=True)
217        self.assertEqual(code, 0)
218        self.assertIn("The model is ready, but Strata can't start it yet", out.getvalue())