okami.mom/strata

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

Niko1221Merge f-027-gh-ctl: GitHub setup, Docker, docs and bench items for 0.2.7538acd098

main
3.4 KiB74 linesraw
1"""#3570: `strata launch --show-key` printed a key only for a launch (a running one or a preset). A model set up by setup.py keeps its key in
2strata-<model>.json, so there the command said "no key" and people read the JSON by hand. It now reads that file when no launch has one."""
3from __future__ import annotations
4
5import contextlib
6import io
7import json
8import tempfile
9import unittest
10from pathlib import Path
11from unittest import mock
12
13from strata_cli import cli
14from strata_cli.launch import cmd as launch_cmd
15
16
17class ShowKeyOfSetupConfigs(unittest.TestCase):
18    def setUp(self):
19        tmp = tempfile.TemporaryDirectory()
20        self.addCleanup(tmp.cleanup)
21        self.root = Path(tmp.name)
22
23    def config(self, model_id, key="__none__", **extra):
24        path = self.root / f"strata-{model_id}.json"
25        body = {"exe": "x", "args": [], **extra}
26        if key != "__none__":
27            body["api_key"] = key
28        path.write_text(json.dumps(body), encoding="utf-8")
29        return {"id": model_id, "source_config": str(path)}
30
31    def run_cli(self, entries, *argv, launch=None):
32        out, err = io.StringIO(), io.StringIO()
33        with contextlib.redirect_stdout(out), contextlib.redirect_stderr(err), \
34                mock.patch.object(launch_cmd, "launch_info", return_value=launch), \
35                mock.patch("strata_control.models.list_installed", return_value={"models": entries}):
36            code = cli.main(["launch", "--show-key", *argv])
37        return code, out.getvalue().strip(), err.getvalue()
38
39    def test_the_only_model_with_a_key_is_shown(self):
40        code, out, err = self.run_cli([self.config("coder-iq1_m", "abc123-secret"), self.config("qwen-q2_0")])
41        self.assertEqual((code, out), (0, "abc123-secret"))
42        self.assertIn("coder-iq1_m", err)                      # the hint is on stderr: stdout stays the key alone, for scripts
43
44    def test_a_launch_that_has_a_key_comes_first(self):
45        code, out, _ = self.run_cli([self.config("coder-iq1_m", "from-the-file")], launch={"key": "from-the-launch"})
46        self.assertEqual((code, out), (0, "from-the-launch"))
47
48    def test_several_models_need_a_name_and_the_name_picks_one_in_any_case(self):
49        entries = [self.config("coder-iq1_m", "key-one"), self.config("qwen-iq3_xxs", "key-two")]
50        code, out, err = self.run_cli(entries)
51        self.assertEqual(code, 5)
52        self.assertEqual(out, "")
53        self.assertIn("coder-iq1_m", err)
54        self.assertIn("--model NAME", err)
55        self.assertNotIn("key-one", err + out)                  # no key is printed while the choice is open
56        code, out, _ = self.run_cli(entries, "--model", "IQ3_XXS")
57        self.assertEqual((code, out), (0, "key-two"))
58
59    def test_no_key_anywhere_and_a_name_that_matches_nothing_are_told_plainly(self):
60        code, out, err = self.run_cli([self.config("qwen-q2_0")])
61        self.assertEqual((code, out), (5, ""))
62        self.assertIn("There is no API key", err)
63        code, out, err = self.run_cli([self.config("qwen-q2_0", "none")], "--model", "iq1_m")
64        self.assertEqual(code, 5)
65        self.assertIn("iq1_m", err)
66
67    def test_a_preset_name_never_falls_back_to_a_setup_config(self):
68        code, out, err = self.run_cli([self.config("coder-iq1_m", "abc123-secret")], "--preset", "mine")
69        self.assertEqual((code, out), (5, ""))
70        self.assertNotIn("abc123", err)
71
72
73if __name__ == "__main__":
74    unittest.main()