okami.mom/strata

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

Niko1221Merge f-028-app-p13b8dc8fa3

main
4.4 KiB78 linesraw
1"""The packaged Terminal (#4188 parts 2 and 3): setup.py without a CMakeLists.txt, and `strata install` handing setup its options.
2Nothing here starts an engine, downloads anything or reads the real profile."""
3from __future__ import annotations
4
5import os
6import subprocess
7import tempfile
8import unittest
9from pathlib import Path
10from unittest import mock
11
12import setup as S
13from strata_cli import cli
14
15
16class PackagedSetupWithoutCMake(unittest.TestCase):
17    """#4188: the packaged Terminal ships VERSION and BUNDLE.json but no CMakeLists.txt."""
18
19    def test_source_version_is_zero_without_a_source_tree(self):
20        with tempfile.TemporaryDirectory() as tmp, mock.patch.object(S, "ROOT", Path(tmp)):
21            self.assertEqual(S.source_version(), "0")
22            (Path(tmp) / "CMakeLists.txt").write_text("project(strata VERSION 0.1.52 LANGUAGES C)\n", encoding="utf-8")
23            self.assertEqual(S.source_version(), "0.1.52")
24            (Path(tmp) / "CMakeLists.txt").write_bytes(b"\xff\xfe\x00 not text")
25            self.assertEqual(S.source_version(), "0")
26
27    def test_prebuilt_bases_names_the_latest_release_when_there_is_no_source_tree(self):
28        with tempfile.TemporaryDirectory() as tmp, mock.patch.object(S, "ROOT", Path(tmp)):
29            self.assertEqual(S.prebuilt_bases(S.PREBUILT_URL), [S.PREBUILT_URL])
30            (Path(tmp) / "CMakeLists.txt").write_text("project(strata VERSION 0.1.52)\n", encoding="utf-8")
31            bases = S.prebuilt_bases(S.PREBUILT_URL)
32            self.assertEqual(len(bases), 2)
33            self.assertIn("v0.1.52", bases[0])
34            self.assertEqual(S.prebuilt_bases("https://example.org/engine"), ["https://example.org/engine/"])
35
36    def test_an_installed_engine_is_not_called_stale_just_because_there_is_no_source_to_compare(self):
37        with tempfile.TemporaryDirectory() as tmp, mock.patch.object(S, "ROOT", Path(tmp)), \
38                mock.patch.object(S, "binary_engine_version", return_value="0.1.50"):
39            self.assertFalse(S.stale_binary("x"))
40            (Path(tmp) / "CMakeLists.txt").write_text("project(strata VERSION 0.1.52)\n", encoding="utf-8")
41            self.assertTrue(S.stale_binary("x"))
42
43
44class InstallForwardsSetupOptions(unittest.TestCase):
45    """#4188: `strata install --source modelscope --family qwen ...` was refused by strata's own parser."""
46
47    def test_the_options_of_setup_go_behind_a_double_dash_and_update_stays_strata_s(self):
48        self.assertEqual(cli._install_argv(["install", "--source", "modelscope", "--yes"]), ["install", "--", "--source", "modelscope", "--yes"])
49        self.assertEqual(cli._install_argv(["install", "--family", "qwen", "--update"]), ["install", "--update", "--", "--family", "qwen"])
50        self.assertEqual(cli._install_argv(["install"]), ["install", "--"])
51        self.assertEqual(cli._install_argv(["install", "-h"]), ["install", "-h"])
52        self.assertEqual(cli._install_argv(["status", "--source", "x"]), ["status", "--source", "x"])
53        self.assertEqual(cli._install_argv([]), [])
54
55    def test_the_parser_hands_all_of_it_to_setup_in_order(self):
56        ap = cli.parser()
57        for argv in (["install", "--source", "modelscope", "--family", "qwen", "--model", "IQ3_XXS", "--context", "32k", "--yes"],
58                     ["install", "--family", "qwen", "--yes"], ["install", "--yes"]):
59            a, extras = ap.parse_known_args(cli._install_argv(argv))
60            self.assertEqual(extras, [], argv)
61            self.assertEqual([x for x in a.setup_args if x != "--"], argv[1:], argv)
62
63    def test_main_runs_setup_with_every_option_and_in_order(self):
64        with tempfile.TemporaryDirectory() as tmp:
65            (Path(tmp) / "setup.py").write_text("# stand-in\n", encoding="utf-8")
66            calls = []
67            with mock.patch.dict(os.environ, {"STRATA_INSTALL_DIR": tmp}), \
68                    mock.patch.object(subprocess, "call", side_effect=lambda argv, **kw: calls.append(argv) or 0):
69                code = cli.main(["install", "--source", "modelscope", "--family", "qwen", "--model", "IQ3_XXS", "--context", "32k", "--yes"])
70                self.assertEqual(code, 0)
71                code = cli.main(["install", "--update", "--yes"])
72                self.assertEqual(code, 0)
73        self.assertEqual(calls[0][2:], ["--setup", "--source", "modelscope", "--family", "qwen", "--model", "IQ3_XXS", "--context", "32k", "--yes"])
74        self.assertEqual(calls[1][2:], ["--update", "--yes"])
75
76
77if __name__ == "__main__":
78    unittest.main()