|
| 1 | +"""Regression coverage for Cursor host attribution.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import json |
| 6 | +from pathlib import Path |
| 7 | +from typing import Any |
| 8 | + |
| 9 | +import pytest |
| 10 | +from claude_smart import hook, hook_log, runtime, state |
| 11 | + |
| 12 | + |
| 13 | +@pytest.fixture(autouse=True) |
| 14 | +def _clear_cursor_detection_env(monkeypatch: pytest.MonkeyPatch) -> None: |
| 15 | + """Keep Cursor host signals local to each test.""" |
| 16 | + for name in ( |
| 17 | + "CLAUDE_CODE_ENTRYPOINT", |
| 18 | + "CURSOR_PROJECT_DIR", |
| 19 | + "CURSOR_TRANSCRIPT_PATH", |
| 20 | + "CURSOR_VERSION", |
| 21 | + ): |
| 22 | + monkeypatch.delenv(name, raising=False) |
| 23 | + |
| 24 | + |
| 25 | +@pytest.fixture |
| 26 | +def cursor_hook_log(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path: |
| 27 | + """Redirect hook telemetry away from the user's real state.""" |
| 28 | + path = tmp_path / "hook.log" |
| 29 | + monkeypatch.setattr(hook_log, "_LOG_PATH", path) |
| 30 | + monkeypatch.delenv("CLAUDE_SMART_HOOK_LOG", raising=False) |
| 31 | + return path |
| 32 | + |
| 33 | + |
| 34 | +def _log_records(path: Path) -> list[dict[str, Any]]: |
| 35 | + return [json.loads(line) for line in path.read_text().splitlines() if line] |
| 36 | + |
| 37 | + |
| 38 | +def test_cursor_hook_records_cursor_in_session_and_hook_log( |
| 39 | + session_dir: Path, |
| 40 | + cursor_hook_log: Path, |
| 41 | + tmp_path: Path, |
| 42 | + monkeypatch: pytest.MonkeyPatch, |
| 43 | +) -> None: |
| 44 | + """A Cursor-fired Claude Code plugin hook keeps Cursor provenance.""" |
| 45 | + monkeypatch.setenv("CURSOR_VERSION", "3.11.0") |
| 46 | + monkeypatch.setenv("CURSOR_PROJECT_DIR", str(tmp_path)) |
| 47 | + monkeypatch.setattr( |
| 48 | + hook, |
| 49 | + "_read_stdin_json", |
| 50 | + lambda: { |
| 51 | + "session_id": "cursor-session", |
| 52 | + "tool_name": "Read", |
| 53 | + "tool_input": {"file_path": "/tmp/example.py"}, |
| 54 | + "tool_response": {"ok": True}, |
| 55 | + }, |
| 56 | + ) |
| 57 | + |
| 58 | + assert hook.main(["claude-code", "post-tool"]) == 0 |
| 59 | + |
| 60 | + assert state.read_all("cursor-session")[0]["host"] == "cursor" |
| 61 | + log_record = _log_records(cursor_hook_log)[0] |
| 62 | + assert log_record["host"] == "cursor" |
| 63 | + assert log_record["cwd"] == str(tmp_path) |
| 64 | + assert log_record["project_id"] == tmp_path.name |
| 65 | + |
| 66 | + |
| 67 | +def test_claude_code_entrypoint_wins_over_cursor_environment( |
| 68 | + session_dir: Path, |
| 69 | + cursor_hook_log: Path, |
| 70 | + tmp_path: Path, |
| 71 | + monkeypatch: pytest.MonkeyPatch, |
| 72 | +) -> None: |
| 73 | + """Claude Code launched inside Cursor stays attributed to Claude Code.""" |
| 74 | + monkeypatch.setenv("CURSOR_VERSION", "3.11.0") |
| 75 | + monkeypatch.setenv("CURSOR_PROJECT_DIR", str(tmp_path)) |
| 76 | + monkeypatch.setenv("CLAUDE_CODE_ENTRYPOINT", "cli") |
| 77 | + monkeypatch.setattr( |
| 78 | + hook, |
| 79 | + "_read_stdin_json", |
| 80 | + lambda: { |
| 81 | + "session_id": "claude-session", |
| 82 | + "cwd": str(tmp_path), |
| 83 | + "tool_name": "Read", |
| 84 | + "tool_response": {"ok": True}, |
| 85 | + }, |
| 86 | + ) |
| 87 | + |
| 88 | + assert hook.main(["claude-code", "post-tool"]) == 0 |
| 89 | + |
| 90 | + assert state.read_all("claude-session")[0]["host"] == "claude-code" |
| 91 | + assert _log_records(cursor_hook_log)[0]["host"] == "claude-code" |
| 92 | + |
| 93 | + |
| 94 | +@pytest.mark.parametrize( |
| 95 | + ("env", "payload", "expected"), |
| 96 | + [ |
| 97 | + ({"CURSOR_TRANSCRIPT_PATH": "/tmp/cursor.jsonl"}, {}, "cursor"), |
| 98 | + ( |
| 99 | + {"CURSOR_VERSION": "3.11", "CURSOR_PROJECT_DIR": "/tmp/project"}, |
| 100 | + {}, |
| 101 | + "cursor", |
| 102 | + ), |
| 103 | + ({"CURSOR_VERSION": "3.11"}, {}, "claude-code"), |
| 104 | + ( |
| 105 | + { |
| 106 | + "CURSOR_TRANSCRIPT_PATH": "/tmp/cursor.jsonl", |
| 107 | + "CLAUDE_CODE_ENTRYPOINT": "cli", |
| 108 | + }, |
| 109 | + {}, |
| 110 | + "claude-code", |
| 111 | + ), |
| 112 | + ( |
| 113 | + {}, |
| 114 | + { |
| 115 | + "transcript_path": str( |
| 116 | + Path.home() / ".cursor" / "projects" / "session.jsonl" |
| 117 | + ) |
| 118 | + }, |
| 119 | + "cursor", |
| 120 | + ), |
| 121 | + ( |
| 122 | + {}, |
| 123 | + { |
| 124 | + "transcript_path": str( |
| 125 | + Path.home() / ".claude" / "projects" / "session.jsonl" |
| 126 | + ) |
| 127 | + }, |
| 128 | + "claude-code", |
| 129 | + ), |
| 130 | + ], |
| 131 | +) |
| 132 | +def test_resolve_hook_host_uses_layered_cursor_signals( |
| 133 | + env: dict[str, str], |
| 134 | + payload: dict[str, str], |
| 135 | + expected: str, |
| 136 | + monkeypatch: pytest.MonkeyPatch, |
| 137 | +) -> None: |
| 138 | + """Layered Cursor signals refine only ambiguous Claude Code hooks.""" |
| 139 | + for name, value in env.items(): |
| 140 | + monkeypatch.setenv(name, value) |
| 141 | + |
| 142 | + assert runtime.resolve_hook_host("claude-code", payload) == expected |
| 143 | + |
| 144 | + |
| 145 | +@pytest.mark.parametrize("host", ["codex", "opencode"]) |
| 146 | +def test_explicit_non_claude_hosts_are_not_reclassified_as_cursor( |
| 147 | + host: str, |
| 148 | + tmp_path: Path, |
| 149 | + monkeypatch: pytest.MonkeyPatch, |
| 150 | +) -> None: |
| 151 | + """Explicit non-Claude hosts remain authoritative under Cursor signals.""" |
| 152 | + monkeypatch.setenv("CURSOR_TRANSCRIPT_PATH", str(tmp_path / "cursor.jsonl")) |
| 153 | + |
| 154 | + assert runtime.resolve_hook_host(host, {}) == host |
| 155 | + |
| 156 | + |
| 157 | +def test_cursor_shares_learnings_without_losing_attribution() -> None: |
| 158 | + """Cursor keeps local attribution while sharing the learning namespace.""" |
| 159 | + assert runtime.set_host("cursor") == "cursor" |
| 160 | + assert runtime.host() == "cursor" |
| 161 | + assert runtime.attribution_host() == "cursor" |
| 162 | + assert runtime.agent_version() == "claude-code" |
0 commit comments