-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cli.py
More file actions
156 lines (128 loc) · 6.4 KB
/
Copy pathtest_cli.py
File metadata and controls
156 lines (128 loc) · 6.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
from __future__ import annotations
import io
import json
import os
import tempfile
import unittest
from contextlib import redirect_stdout
from pathlib import Path
from unittest.mock import patch
from smzdm_notice import cli
class CliTests(unittest.TestCase):
def test_setup_creates_local_files_without_overwriting(self) -> None:
with tempfile.TemporaryDirectory() as tmp, patch.dict(os.environ, {}, clear=False):
root = Path(tmp)
(root / ".env").write_text("CUSTOM=1\n", encoding="utf-8")
code, output = _run_cli("--root", str(root), "setup")
self.assertEqual(code, 0)
self.assertEqual((root / ".env").read_text(encoding="utf-8"), "CUSTOM=1\n")
self.assertTrue((root / "preference.md").exists())
self.assertTrue((root / "inventory.md").exists())
self.assertTrue((root / "llm_models.json").exists())
routing_data = json.loads((root / "llm_models.json").read_text(encoding="utf-8"))
self.assertNotIn("model_id", routing_data["agents"]["filter"])
self.assertEqual(routing_data["agents"]["filter"]["request"]["temperature"], 0.3)
self.assertTrue((root / "workspace/state").is_dir())
self.assertIn("exists: .env", output)
def test_doctor_passes_with_complete_local_setup(self) -> None:
with tempfile.TemporaryDirectory() as tmp, patch.dict(os.environ, {}, clear=False):
root = Path(tmp)
_write_ready_project(root)
code, output = _run_cli("--root", str(root), "doctor")
self.assertEqual(code, 0)
self.assertIn("OK: required local files exist", output)
def test_doctor_fails_when_smzdm_config_is_missing(self) -> None:
with tempfile.TemporaryDirectory() as tmp, patch.dict(os.environ, {}, clear=False):
root = Path(tmp)
_write_ready_project(
root,
extra_env="SMZDM_CLIENT_PLATFORM=\nSMZDM_APP_VERSION=\nSMZDM_SIGN_KEY=\nSMZDM_USER_AGENT=",
)
code, output = _run_cli("--root", str(root), "doctor")
self.assertEqual(code, 1)
self.assertIn(
"FAIL: missing required .env values: "
"SMZDM_CLIENT_PLATFORM, SMZDM_APP_VERSION, SMZDM_SIGN_KEY, SMZDM_USER_AGENT",
output,
)
def test_doctor_fails_without_llm_models_json(self) -> None:
with tempfile.TemporaryDirectory() as tmp, patch.dict(os.environ, {}, clear=False):
root = Path(tmp)
_write_ready_project(root)
(root / "llm_models.json").unlink()
code, output = _run_cli("--root", str(root), "doctor")
self.assertEqual(code, 1)
self.assertIn("FAIL: llm_models.json missing", output)
def test_doctor_uses_custom_llm_models_file_path(self) -> None:
with tempfile.TemporaryDirectory() as tmp, patch.dict(os.environ, {}, clear=False):
root = Path(tmp)
_write_ready_project(root, extra_env="LLM_MODELS_FILE=config/llm_models.json")
(root / "config").mkdir()
(root / "config/llm_models.json").write_text(
(root / "llm_models.json").read_text(encoding="utf-8"),
encoding="utf-8",
)
(root / "llm_models.json").unlink()
code, output = _run_cli("--root", str(root), "doctor")
self.assertEqual(code, 0)
self.assertIn("OK: required local files exist", output)
self.assertIn("OK: llm_models.json is readable", output)
def test_doctor_uses_runtime_llm_routing_validation(self) -> None:
with tempfile.TemporaryDirectory() as tmp, patch.dict(os.environ, {}, clear=False):
root = Path(tmp)
_write_ready_project(root)
data = json.loads((root / "llm_models.json").read_text(encoding="utf-8"))
data["agents"]["filter"]["request"] = {"extra_body": "bad"}
(root / "llm_models.json").write_text(json.dumps(data), encoding="utf-8")
code, output = _run_cli("--root", str(root), "doctor")
self.assertEqual(code, 1)
self.assertIn("FAIL: llm_models.json invalid:", output)
self.assertIn("extra_body", output)
def test_save_and_diff_config(self) -> None:
with tempfile.TemporaryDirectory() as tmp, patch.dict(os.environ, {}, clear=False):
root = Path(tmp)
_write_ready_project(root)
save_code, save_output = _run_cli("--root", str(root), "save-config")
list_code, list_output = _run_cli("--root", str(root), "diff-config", "--list")
diff_code, diff_output = _run_cli("--root", str(root), "diff-config")
self.assertEqual(save_code, 0)
self.assertEqual(list_code, 0)
self.assertEqual(diff_code, 0)
self.assertIn("saved: workspace/backups/inventory.md.", save_output)
self.assertIn("inventory.md:", list_output)
self.assertIn("=== inventory.md.", diff_output)
def _run_cli(*args: str) -> tuple[int, str]:
stdout = io.StringIO()
with redirect_stdout(stdout):
code = cli.main(list(args))
return code, stdout.getvalue()
def _write_ready_project(root: Path, extra_env: str = "") -> None:
root.mkdir(parents=True, exist_ok=True)
(root / ".env").write_text(
"\n".join(
[
"FEISHU_APP_ID=cli_real_app_id",
"FEISHU_APP_SECRET=real-secret",
"SMZDM_CLIENT_PLATFORM=iphone",
"SMZDM_APP_VERSION=11.1.70",
"SMZDM_SIGN_KEY=real-smzdm-sign-key",
"SMZDM_USER_AGENT=real-smzdm-user-agent",
"LLM_DEEPSEEK_API_KEY=real-key",
"RANKING_NAMES=综合榜-全部",
extra_env.strip(),
]
).strip()
+ "\n",
encoding="utf-8",
)
(root / "preference.md").write_text("偏好\n", encoding="utf-8")
(root / "inventory.md").write_text("库存\n", encoding="utf-8")
(root / "llm_models.json").write_text(
(
'{"connections":{"deepseek":{"provider":"openai_compatible","label":"DeepSeek",'
'"base_url":"https://api.deepseek.com/v1","api_key_env":"LLM_DEEPSEEK_API_KEY"}},'
'"defaults":{"connection":"deepseek","model_id":"deepseek-chat"},'
'"agents":{"filter":{},"arbiter":{},"draft":{}}}'
),
encoding="utf-8",
)