From 669858c1d1732253d9073b697449ec55056421fe Mon Sep 17 00:00:00 2001 From: cat <1716967236@qq.com> Date: Sun, 13 Sep 2026 10:21:15 +0800 Subject: [PATCH] =?UTF-8?q?test:=20=E8=A1=A5=E5=85=85=E7=BA=A0=E9=94=99?= =?UTF-8?q?=E8=8A=82=E7=82=B9=E7=8B=AC=E7=AB=8B=E6=A8=A1=E5=9E=8B=E7=8E=AF?= =?UTF-8?q?=E5=A2=83=E5=8F=98=E9=87=8F=E7=9A=84=E5=A4=B1=E8=B4=A5=E7=94=A8?= =?UTF-8?q?=E4=BE=8B=EF=BC=88=E5=BE=85=E5=AE=9E=E7=8E=B0=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增 test_纠错节点用独立环境变量不受全局模型影响,锁定期望行为: params.model > SUBTITLE_CORRECTION_MODEL > LLM_MODEL > 兜底默认。 背景:上一个提交想"让纠错节点保留旧模型",但只改了 os.getenv 的兜底常量 ——只要 .env 里存在 LLM_MODEL(生产环境必然存在),兜底值永远取不到, 该改动实际无效,真实 LLM 集成测试 test_generic_correction_generalizes_to_unseen_mishearing 失败(新模型 误听泛化实测 0/4,旧模型 4/4)。 本用例当前为红(测试代码尚缺 urllib 导入,实现也待补),按用户要求 与实现一起保留待 review 后修正。 --- tests/test_llm_default_model.py | 48 +++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/tests/test_llm_default_model.py b/tests/test_llm_default_model.py index 4e6b85d..da3d6ef 100644 --- a/tests/test_llm_default_model.py +++ b/tests/test_llm_default_model.py @@ -107,6 +107,54 @@ def test_params_model_优先于环境变量(monkeypatch, tmp_path: Path) -> None assert sent[0]["model"] == "工作流/模型" +def test_纠错节点用独立环境变量不受全局模型影响() -> None: + """纠错节点必须能用**独立**环境变量固定模型,不受全局 LLM_MODEL 控制。 + + 回归(本次提交前实测):该节点原先写的是 `os.getenv("LLM_MODEL", "旧模型")` + ——`LLM_MODEL` 存在时(生产环境必有)兜底值永远不会被取到, + 导致"有意保留旧模型"实际失效,真实 LLM 集成测试 + test_generic_correction_generalizes_to_unseen_mishearing 失败。 + + 正确行为:`params.model` > `SUBTITLE_CORRECTION_MODEL` > `LLM_MODEL` > 兜底。 + 这样既保留全局一致(不设置该变量时),又能在该节点需要时单独固定模型。 + """ + import os + from unittest import mock + + from nodes import subtitle_correction as sc + + entries = [{"start": 100.0, "end": 103.0, "text": "もっとマンゴーを舐めてください"}] + sent: list[str] = [] + + class Response: + def __enter__(self): + return self + + def __exit__(self, *args): + return False + + def read(self): + return json.dumps({"choices": [{"message": {"content": "请多舔舔我的小穴"}}]}).encode() + + def fake_open(request, *args, **kwargs): + sent.append(json.loads(request.data)["model"]) + return Response() + + env = {"LLM_MODEL": "全局/模型", "SUBTITLE_CORRECTION_MODEL": "纠错/专用模型"} + with mock.patch.object(urllib.request, "urlopen", fake_open), mock.patch.dict(os.environ, env): + sc.correct_entry(entries[0], entries, 0, {}) + assert sent[-1] == "纠错/专用模型", "独立环境变量应优先于全局 LLM_MODEL" + sc.correct_entry(entries[0], entries, 0, {"model": "参数/模型"}) + assert sent[-1] == "参数/模型", "params.model 优先级最高" + # 未设置独立变量时退回全局 LLM_MODEL(保持单一全局配置能力)。 + with mock.patch.object(urllib.request, "urlopen", fake_open), mock.patch.dict( + os.environ, {"LLM_MODEL": "全局/模型"} + ): + os.environ.pop("SUBTITLE_CORRECTION_MODEL", None) + sc.correct_entry(entries[0], entries, 0, {}) + assert sent[-1] == "全局/模型" + + def test_兜底默认模型按节点职责分离() -> None: """翻译节点跟随 .env 的 LLM_MODEL;纠错节点保留旧默认(实测更优)。