Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
45 changes: 45 additions & 0 deletions tests/test_hooks_test_isolation.py
Original file line number Diff line number Diff line change
@@ -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 一个字节都不许动"
Loading