From c3411bd439d5df229b1c16e281811b26c674ecd3 Mon Sep 17 00:00:00 2001 From: Limin Wang Date: Fri, 20 Nov 2026 00:16:22 -0800 Subject: [PATCH] =?UTF-8?q?test(conftest):=20=E6=B5=8B=E8=AF=95=E9=87=8C?= =?UTF-8?q?=20Claude=20=E7=9A=84=20settings.json=20=E4=B8=80=E5=BE=8B?= =?UTF-8?q?=E6=8C=87=E5=88=B0=E4=B8=B4=E6=97=B6=E7=9B=AE=E5=BD=95=E2=80=94?= =?UTF-8?q?=E2=80=94=E7=9C=9F=E5=A3=B3=E5=85=B3=E8=81=94=E5=8A=A8=E6=97=B6?= =?UTF-8?q?=E7=9A=84=E5=8D=B8=20hooks=20=E4=B8=8D=E8=AE=B8=E7=A2=B0?= =?UTF-8?q?=E5=BC=80=E5=8F=91=E6=9C=BA=E7=9C=9F=E5=AE=9E=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 2026-11-20 凌晨跑 test_background_service_gates / test_harness_ownership 把本机 ~/.claude/settings.json 里肥鱼的 hooks 清掉了 (#84 起关联动会卸 hooks)。在既有的 autouse 总夹具里把 ClaudeCodeMonitor.get_settings_path 指到 tmp_path,不另加夹具; 加一个回归测试:真管理器把 claude 关掉,真实文件的 mtime 不变。 Co-Authored-By: Claude Fable 5.1 --- tests/conftest.py | 7 ++++- tests/test_hooks_test_isolation.py | 45 ++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 tests/test_hooks_test_isolation.py diff --git a/tests/conftest.py b/tests/conftest.py index f1d9a8f..a471ab0 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -136,7 +136,7 @@ def _no_real_usage_measurement(monkeypatch): @pytest.fixture(autouse=True) -def _no_real_session_history_scan(monkeypatch): +def _no_real_session_history_scan(monkeypatch, tmp_path): """「最近的对话」的回填在测试里绝不真的去读 ``~/.claude`` 或起 wsl.exe。 建 overlay 壳就会排一次回填;默认扫描读的是开发者 / CI 机器上真实的 Claude Code @@ -177,6 +177,11 @@ def _run_without_wsl(argv, *args, **kwargs): # 同理:管理器不许去读这台机器真实的 ~/.claude/settings.json 查 hooks 在不在 # (要测这项检查的用例自己打开,见 test_claude_hooks_missing.py) monkeypatch.setattr(agent_link.AgentLinkManager, "CHECK_HOOKS_PRESENT", False) + # 也不许写它:关联动时 2026-11-19 起会卸掉肥鱼装的 hooks(#84),真壳 / 真管理器的用例把 claude 关掉就会改写 + # 开发机上真实的 ~/.claude/settings.json(2026-11-20 凌晨跑 test_background_service_gates / test_harness_ownership + # 就这样把本机 hooks 清掉了)。settings.json 的路径一律指到临时目录;要测装卸本身的用例自己再指到自己的文件。 + monkeypatch.setattr(agent_link.ClaudeCodeMonitor, "get_settings_path", + staticmethod(lambda: tmp_path / "claude-home" / "settings.json")) @pytest.fixture(autouse=True) diff --git a/tests/test_hooks_test_isolation.py b/tests/test_hooks_test_isolation.py new file mode 100644 index 0000000..f009d50 --- /dev/null +++ b/tests/test_hooks_test_isolation.py @@ -0,0 +1,45 @@ +# -*- coding: utf-8 -*- +"""测试里绝不碰这台机器真实的 ``~/.claude/settings.json``(conftest 的总夹具把路径指到临时目录)。 + +2026-11-20 凌晨:真壳的用例把 claude 联动关掉 → ``_uninstall_hooks`` → 改写了开发机上真实的 settings.json,本机 hooks 被清光。 +""" +from __future__ import annotations + +from pathlib import Path + +from pet.agent_link import AgentLinkManager, ClaudeCodeMonitor +from pet.config import Config + + +def test_the_settings_path_points_into_the_temp_dir_under_pytest(tmp_path): + path = ClaudeCodeMonitor.get_settings_path() + assert tmp_path in path.parents, "conftest 把 settings.json 指到了每个用例自己的临时目录" + assert path != Path.home() / ".claude" / "settings.json" + + +def test_turning_the_claude_link_off_writes_only_the_temp_settings_file(tmp_path): + settings = ClaudeCodeMonitor.get_settings_path() + settings.parent.mkdir(parents=True) + settings.write_text('{"hooks": {}}', encoding="utf-8") + real = Path.home() / ".claude" / "settings.json" + before = real.stat().st_mtime_ns if real.is_file() else None + + class Win: + def isVisible(self): + return True + + def mark_activity(self): + pass + + def show_bubble(self, *_a, **_k): + pass + + config = Config(base=tmp_path) + config.set("agent_link", {**config.get("agent_link"), "claude": True}) + mgr = AgentLinkManager(Win(), config, min_interval=0.0) + mgr.apply_config() + assert mgr.monitors["claude"]._running + config.set("agent_link", {**config.get("agent_link"), "claude": False}) + mgr.apply_config() # 在跑 → 停:走卸 hooks 那条路 + assert not mgr.monitors["claude"]._running + assert (real.stat().st_mtime_ns if real.is_file() else None) == before, "真实的 settings.json 一个字节都不许动"