|
| 1 | +import json |
| 2 | +from datetime import datetime, timezone |
| 3 | +from pathlib import Path |
| 4 | +from unittest.mock import patch |
| 5 | + |
| 6 | +import pytest |
| 7 | +from pygitguardian.models import AIDiscovery, MCPConfiguration, MCPServer, UserInfo |
| 8 | + |
| 9 | +from ggshield.verticals.ai.agents.copilot import Copilot |
| 10 | + |
| 11 | + |
| 12 | +def _session_start(cwd: str = "/repo", session_id: str = "sess-1") -> str: |
| 13 | + return json.dumps( |
| 14 | + { |
| 15 | + "type": "session.start", |
| 16 | + "data": { |
| 17 | + "sessionId": session_id, |
| 18 | + "context": {"cwd": cwd}, |
| 19 | + }, |
| 20 | + "id": "evt-start", |
| 21 | + "timestamp": "2026-05-18T14:09:37.678Z", |
| 22 | + } |
| 23 | + ) |
| 24 | + |
| 25 | + |
| 26 | +def _tool_execution_start( |
| 27 | + tool_call_id: str = "call-1", |
| 28 | + mcp_server_name: str = "github-mcp-server", |
| 29 | + mcp_tool_name: str = "get_file_contents", |
| 30 | + arguments=None, |
| 31 | + timestamp: str = "2026-05-18T14:10:46.052Z", |
| 32 | +) -> str: |
| 33 | + data: dict = { |
| 34 | + "toolCallId": tool_call_id, |
| 35 | + "toolName": f"{mcp_server_name}-{mcp_tool_name}", |
| 36 | + "arguments": arguments if arguments is not None else {"owner": "GitGuardian"}, |
| 37 | + "turnId": "0", |
| 38 | + } |
| 39 | + if mcp_server_name: |
| 40 | + data["mcpServerName"] = mcp_server_name |
| 41 | + if mcp_tool_name: |
| 42 | + data["mcpToolName"] = mcp_tool_name |
| 43 | + return json.dumps( |
| 44 | + { |
| 45 | + "type": "tool.execution_start", |
| 46 | + "data": data, |
| 47 | + "id": f"evt-{tool_call_id}", |
| 48 | + "timestamp": timestamp, |
| 49 | + } |
| 50 | + ) |
| 51 | + |
| 52 | + |
| 53 | +def _seed_session(tmp_path: Path, lines: list, session_id: str = "sess-1") -> Path: |
| 54 | + session_dir = tmp_path / ".copilot" / "session-state" / session_id |
| 55 | + session_dir.mkdir(parents=True) |
| 56 | + events = session_dir / "events.jsonl" |
| 57 | + events.write_text("\n".join(lines) + "\n") |
| 58 | + return events |
| 59 | + |
| 60 | + |
| 61 | +@pytest.fixture |
| 62 | +def empty_ai_config() -> AIDiscovery: |
| 63 | + return AIDiscovery( |
| 64 | + user=UserInfo(hostname="h", username="u", machine_id="m"), |
| 65 | + servers=[], |
| 66 | + discovery_duration=0.0, |
| 67 | + ) |
| 68 | + |
| 69 | + |
| 70 | +class TestCopilotIterHistoryEvents: |
| 71 | + def test_extracts_mcp_call(self, tmp_path: Path, empty_ai_config) -> None: |
| 72 | + _seed_session( |
| 73 | + tmp_path, |
| 74 | + [ |
| 75 | + _session_start(cwd="/repo"), |
| 76 | + _tool_execution_start( |
| 77 | + tool_call_id="call_abc", |
| 78 | + mcp_server_name="github-mcp-server", |
| 79 | + mcp_tool_name="get_file_contents", |
| 80 | + arguments={"owner": "GitGuardian", "repo": "ggshield"}, |
| 81 | + timestamp="2026-05-18T14:10:46.052Z", |
| 82 | + ), |
| 83 | + ], |
| 84 | + ) |
| 85 | + |
| 86 | + with patch( |
| 87 | + "ggshield.verticals.ai.agents.copilot.get_user_home_dir", |
| 88 | + return_value=tmp_path, |
| 89 | + ): |
| 90 | + events = list(Copilot().iter_history_events(empty_ai_config)) |
| 91 | + |
| 92 | + assert len(events) == 1 |
| 93 | + ev = events[0] |
| 94 | + assert ev.tool == "get_file_contents" |
| 95 | + assert ev.server == "github-mcp-server" |
| 96 | + assert ev.agent == "copilot" |
| 97 | + assert ev.cwd == "/repo" |
| 98 | + assert ev.idempotency_key == "call_abc" |
| 99 | + assert ev.timestamp == datetime( |
| 100 | + 2026, 5, 18, 14, 10, 46, 52000, tzinfo=timezone.utc |
| 101 | + ) |
| 102 | + assert ev.input == {"owner": "GitGuardian", "repo": "ggshield"} |
| 103 | + |
| 104 | + def test_skips_non_mcp_tool_calls(self, tmp_path: Path, empty_ai_config) -> None: |
| 105 | + """``tool.execution_start`` events without ``mcpServerName`` are not MCP calls.""" |
| 106 | + builtin = json.dumps( |
| 107 | + { |
| 108 | + "type": "tool.execution_start", |
| 109 | + "data": { |
| 110 | + "toolCallId": "call_builtin", |
| 111 | + "toolName": "report_intent", |
| 112 | + "arguments": {"intent": "Searching"}, |
| 113 | + "turnId": "0", |
| 114 | + }, |
| 115 | + "id": "evt-builtin", |
| 116 | + "timestamp": "2026-05-18T14:10:46.052Z", |
| 117 | + } |
| 118 | + ) |
| 119 | + _seed_session(tmp_path, [_session_start(), builtin]) |
| 120 | + |
| 121 | + with patch( |
| 122 | + "ggshield.verticals.ai.agents.copilot.get_user_home_dir", |
| 123 | + return_value=tmp_path, |
| 124 | + ): |
| 125 | + events = list(Copilot().iter_history_events(empty_ai_config)) |
| 126 | + |
| 127 | + assert events == [] |
| 128 | + |
| 129 | + def test_drops_event_with_unparseable_timestamp( |
| 130 | + self, tmp_path: Path, empty_ai_config |
| 131 | + ) -> None: |
| 132 | + _seed_session( |
| 133 | + tmp_path, |
| 134 | + [ |
| 135 | + _session_start(), |
| 136 | + _tool_execution_start(timestamp="not-a-date"), |
| 137 | + ], |
| 138 | + ) |
| 139 | + |
| 140 | + with patch( |
| 141 | + "ggshield.verticals.ai.agents.copilot.get_user_home_dir", |
| 142 | + return_value=tmp_path, |
| 143 | + ): |
| 144 | + events = list(Copilot().iter_history_events(empty_ai_config)) |
| 145 | + |
| 146 | + assert events == [] |
| 147 | + |
| 148 | + def test_resolves_server_name_via_configuration(self, tmp_path: Path) -> None: |
| 149 | + """``mcpServerName`` matches ``MCPConfiguration.name`` byte-for-byte.""" |
| 150 | + _seed_session( |
| 151 | + tmp_path, |
| 152 | + [ |
| 153 | + _session_start(), |
| 154 | + _tool_execution_start(mcp_server_name="github-mcp-server"), |
| 155 | + ], |
| 156 | + ) |
| 157 | + |
| 158 | + config = AIDiscovery( |
| 159 | + user=UserInfo(hostname="h", username="u", machine_id="m"), |
| 160 | + servers=[ |
| 161 | + MCPServer( |
| 162 | + name="GitHub", |
| 163 | + configurations=[ |
| 164 | + MCPConfiguration( |
| 165 | + name="github-mcp-server", |
| 166 | + agent="copilot", |
| 167 | + scope=MCPConfiguration.Scope.USER, |
| 168 | + transport=MCPConfiguration.Transport.STDIO, |
| 169 | + project=None, |
| 170 | + ) |
| 171 | + ], |
| 172 | + ) |
| 173 | + ], |
| 174 | + discovery_duration=0.0, |
| 175 | + ) |
| 176 | + |
| 177 | + with patch( |
| 178 | + "ggshield.verticals.ai.agents.copilot.get_user_home_dir", |
| 179 | + return_value=tmp_path, |
| 180 | + ): |
| 181 | + events = list(Copilot().iter_history_events(config)) |
| 182 | + |
| 183 | + assert events[0].server == "GitHub" |
| 184 | + |
| 185 | + def test_walks_multiple_sessions(self, tmp_path: Path, empty_ai_config) -> None: |
| 186 | + _seed_session( |
| 187 | + tmp_path, |
| 188 | + [_session_start(cwd="/repo-a"), _tool_execution_start(tool_call_id="a")], |
| 189 | + session_id="sess-a", |
| 190 | + ) |
| 191 | + _seed_session( |
| 192 | + tmp_path, |
| 193 | + [_session_start(cwd="/repo-b"), _tool_execution_start(tool_call_id="b")], |
| 194 | + session_id="sess-b", |
| 195 | + ) |
| 196 | + |
| 197 | + with patch( |
| 198 | + "ggshield.verticals.ai.agents.copilot.get_user_home_dir", |
| 199 | + return_value=tmp_path, |
| 200 | + ): |
| 201 | + events = list(Copilot().iter_history_events(empty_ai_config)) |
| 202 | + |
| 203 | + assert {ev.idempotency_key for ev in events} == {"a", "b"} |
| 204 | + assert {ev.cwd for ev in events} == {"/repo-a", "/repo-b"} |
| 205 | + |
| 206 | + def test_missing_history_root_yields_nothing( |
| 207 | + self, tmp_path: Path, empty_ai_config |
| 208 | + ) -> None: |
| 209 | + """A user who has never run Copilot CLI has no ``~/.copilot/session-state``.""" |
| 210 | + with patch( |
| 211 | + "ggshield.verticals.ai.agents.copilot.get_user_home_dir", |
| 212 | + return_value=tmp_path, |
| 213 | + ): |
| 214 | + events = list(Copilot().iter_history_events(empty_ai_config)) |
| 215 | + |
| 216 | + assert events == [] |
| 217 | + |
| 218 | + def test_session_without_events_file_is_skipped( |
| 219 | + self, tmp_path: Path, empty_ai_config |
| 220 | + ) -> None: |
| 221 | + (tmp_path / ".copilot" / "session-state" / "empty").mkdir(parents=True) |
| 222 | + |
| 223 | + with patch( |
| 224 | + "ggshield.verticals.ai.agents.copilot.get_user_home_dir", |
| 225 | + return_value=tmp_path, |
| 226 | + ): |
| 227 | + events = list(Copilot().iter_history_events(empty_ai_config)) |
| 228 | + |
| 229 | + assert events == [] |
0 commit comments