okami.mom/strata

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

Niko1221Merge f-028-app-p13b8dc8fa3

main
3.9 KiB76 linesraw
1"""#3714: the launch hint named an engine log that was never written (the start stopped before the engine existed). Nothing here starts
2an engine or reads the real profile."""
3from __future__ import annotations
4
5import json
6import unittest
7from pathlib import Path
8from unittest import mock
9
10from strata_cli.launch import runner as R, settings as LS
11from strata_control.profiles import ProfileError
12from tests.terminal.launch_support import HW_12, Home, catalog
13
14
15class LogHint(unittest.TestCase):
16    """#3714: 'The engine's log has the reason: <path>' for a log that was never written (the start stopped before the engine existed)."""
17
18    def config(self, home: Home, log: Path) -> Path:
19        path = home.base / "run.json"
20        path.write_text('{"exe": "e", "args": [], "log": %s}' % json.dumps(str(log)), encoding="utf-8")
21        return path
22
23    def test_a_busy_profile_gets_its_own_sentence_and_no_log_path(self):
24        with Home() as h:
25            log = h.base / "missing.log"
26            text = R._log_hint(self.config(h, log), ProfileError("this profile already has a live managed runtime", 6))
27            self.assertNotIn(str(log), text)
28            self.assertIn("still running or starting", text)
29            self.assertIn("strata launch --stop", text)
30
31    def test_a_log_that_does_not_exist_is_not_pointed_at(self):
32        with Home() as h:
33            log = h.base / "missing.log"
34            text = R._log_hint(self.config(h, log), RuntimeError("something else"))
35            self.assertNotIn(str(log), text)
36            self.assertIn("did not get as far as writing its log", text)
37
38    def test_a_log_that_exists_is_still_pointed_at(self):
39        with Home() as h:
40            log = h.base / "engine.log"
41            log.write_text("line\n", encoding="utf-8")
42            self.assertEqual(R._log_hint(self.config(h, log), RuntimeError("x")), f"The engine's log has the reason: {log}")
43
44    def test_without_a_config_the_general_sentence_stays(self):
45        self.assertIn("strata logs", R._log_hint(None))
46
47    def test_explain_engine_exit_does_not_name_a_missing_log_either(self):
48        with Home() as h:
49            log = h.base / "missing.log"
50            text, nxt = R.explain_engine_exit({"state": "failed", "error": "server_exited"}, self.config(h, log))
51            self.assertNotIn(str(log), nxt)
52            self.assertIn("did not get as far as writing its log", nxt)
53            log.write_text("[strata] out of memory\n", encoding="utf-8")
54            text, nxt = R.explain_engine_exit({"state": "failed", "error": "server_exited"}, self.config(h, log))
55            self.assertIn(f"The engine's log: {log}", nxt)
56
57    def test_start_reports_a_busy_profile_without_a_log_path(self):
58        with Home() as h:
59            log = h.base / "never-written.log"
60            runner = R.Runner(LS.LaunchSettings(family="qwen", model="IQ3_XXS", context=32768, vision="off"), catalog=catalog(HW_12))
61            runner.config_path = self.config(h, log)
62            busy = ProfileError("this profile already has a live managed runtime", 6)
63            with mock.patch.object(R.Runner, "stop_others"), mock.patch.object(R.Runner, "check_port_free"), \
64                    mock.patch.object(R.Runner, "log_marks", return_value={}), mock.patch.object(R.Runner, "end_failed_start"), \
65                    mock.patch.object(R.Runner, "start_wait", return_value=5), mock.patch.object(R.Runner, "emit"), \
66                    mock.patch("strata_control.runtime.status", return_value={"state": "stopped"}), \
67                    mock.patch("strata_control.runtime.start", side_effect=busy):
68                with self.assertRaises(R.LaunchError) as raised:
69                    runner.start()
70        self.assertIn("already has a live managed runtime", raised.exception.text)
71        self.assertNotIn(str(log), raised.exception.next)
72        self.assertIn("strata launch --stop", raised.exception.next)
73
74
75if __name__ == "__main__":
76    unittest.main()