okami.mom/strata

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

Niko1221Merge f-0213-tuicfg (final c6dda74bf) into f-0213-int049ae1c67

main
3.2 KiB80 linesraw
1"""Round 0213 TUICFG, step 2: `strata launch --stop` exit status and message (#4398).
2Four cases scripted against a runtime double that tracks which fake PIDs are still alive: empty, successful, failed, mixed."""
3from __future__ import annotations
4
5import unittest
6from unittest import mock
7
8from strata_cli.launch import access
9from tests.terminal.launch_support import run_cli
10
11NOTHING = "Nothing from `strata launch` is running."
12
13
14class Runtime:
15    """profile id -> (alive?, stop succeeds?)"""
16
17    def __init__(self, profiles):
18        self.alive = {p: ok is not None for p, ok in profiles.items()}
19        self.stops_ok = {p: bool(ok) for p, ok in profiles.items()}
20
21    def list_profiles(self):
22        return [{"id": p} for p in self.alive]
23
24    def status(self, prof):
25        return {"state": "ready" if self.alive.get(prof) else "stopped"}
26
27    def stop(self, prof, timeout=None):
28        if self.stops_ok.get(prof):
29            self.alive[prof] = False
30            return {"ok": True}
31        return {"ok": False, "error": "leftover_process_not_ended"}
32
33    def live(self):
34        return sorted(p for p, a in self.alive.items() if a)
35
36
37class LaunchStopExit(unittest.TestCase):
38    def _run(self, profiles, *args):
39        rt = Runtime(profiles)
40        with mock.patch("strata_control.profiles.list_profiles", rt.list_profiles), \
41             mock.patch("strata_control.runtime.status", rt.status), \
42             mock.patch("strata_control.runtime.stop", rt.stop), \
43             mock.patch.object(access, "stop_tunnel"):
44            code, out, err = run_cli(["launch", "--stop", *args])
45        return rt, code, out, err
46
47    def test_empty_says_nothing_and_exits_zero(self):
48        rt, code, out, _ = self._run({})
49        self.assertEqual(code, 0)
50        self.assertIn(NOTHING, out)
51        rt, code, out, _ = self._run({"launch-office": None})          # a profile exists but nothing runs
52        self.assertEqual((code, rt.live()), (0, []))
53        self.assertIn(NOTHING, out)
54
55    def test_successful_stop_exits_zero_and_leaves_nothing_alive(self):
56        rt, code, out, _ = self._run({"launch-office": True, "launch-home": True})
57        self.assertEqual((code, rt.live()), (0, []))
58        self.assertIn("Stopped office.", out)
59        self.assertNotIn(NOTHING, out)
60
61    def test_failed_stop_exits_nonzero_and_does_not_claim_nothing_runs(self):
62        for extra in ([], ["--preset", "Office"]):
63            with self.subTest(extra=extra):
64                rt, code, out, _ = self._run({"launch-office": False}, *extra)
65                self.assertEqual(code, 1)
66                self.assertEqual(rt.live(), ["launch-office"])
67                self.assertIn("Could not stop office: leftover_process_not_ended.", out)
68                self.assertNotIn(NOTHING, out)
69
70    def test_mixed_exits_nonzero_reports_both_and_only_the_failed_one_stays(self):
71        rt, code, out, _ = self._run({"launch-office": False, "launch-home": True})
72        self.assertEqual(code, 1)
73        self.assertEqual(rt.live(), ["launch-office"])
74        self.assertIn("Could not stop office", out)
75        self.assertIn("Stopped home.", out)
76        self.assertNotIn(NOTHING, out)
77
78
79if __name__ == "__main__":
80    unittest.main()