Repository navigation
Expand file tree
/
Copy pathtest_scenario_sends_e2e.py
More file actions
177 lines (160 loc) · 7.2 KB
/
Copy pathtest_scenario_sends_e2e.py
File metadata and controls
177 lines (160 loc) · 7.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
"""Scenario-parity E2Es for send serialization and cancellation boundaries."""
from __future__ import annotations
import asyncio
from typing import Literal
import pytest
from copilot.session import AgentMessageSource, PermissionHandler
from ._scenario_fake_cli import create_scenario_client, read_scenario_capture
from .testharness import E2ETestContext
pytestmark = pytest.mark.asyncio(loop_scope="module")
def _send_requests(capture_path) -> list[dict]:
return [
request
for request in read_scenario_capture(capture_path)["requests"]
if request["method"] == "session.send"
]
class TestScenarioSends:
async def test_should_send_complete_message_wire_shape(
self,
ctx: E2ETestContext,
):
client, capture_path = create_scenario_client(ctx, "send")
try:
session = await client.create_session(
session_id="scenario-session",
on_permission_request=PermissionHandler.approve_all,
)
try:
message_id = await session.send(
"Run the extension workflow.",
attachments=[
{
"type": "selection",
"filePath": "src/example.py",
"displayName": "example.py:4-6",
"selection": {
"start": {"line": 4, "character": 2},
"end": {"line": 6, "character": 8},
},
"text": "selected text",
},
{
"type": "extension_context",
"capturedAt": "2026-01-02T03:04:05.000Z",
"extensionId": "scenario-extension",
"title": "Scenario context",
"canvasId": "scenario-canvas",
"instanceId": "scenario-instance",
"payload": {
"metadata": {
"source": "scenario",
"priority": 7,
}
},
},
],
source=AgentMessageSource("extension-agent"),
mode="immediate",
agent_mode="plan",
request_headers={"X-Scenario": "complete-wire-shape"},
display_prompt="Visible extension prompt",
)
assert message_id == "user-message"
assert _send_requests(capture_path) == [
{
"method": "session.send",
"params": {
"sessionId": "scenario-session",
"prompt": "Run the extension workflow.",
"attachments": [
{
"type": "selection",
"filePath": "src/example.py",
"displayName": "example.py:4-6",
"selection": {
"start": {"line": 4, "character": 2},
"end": {"line": 6, "character": 8},
},
"text": "selected text",
},
{
"type": "extension_context",
"capturedAt": "2026-01-02T03:04:05.000Z",
"extensionId": "scenario-extension",
"title": "Scenario context",
"canvasId": "scenario-canvas",
"instanceId": "scenario-instance",
"payload": {
"metadata": {
"source": "scenario",
"priority": 7,
}
},
},
],
"source": "agent-extension-agent",
"mode": "immediate",
"agentMode": "plan",
"requestHeaders": {
"X-Scenario": "complete-wire-shape",
},
"displayPrompt": "Visible extension prompt",
},
}
]
finally:
await session.disconnect()
finally:
await client.stop()
@pytest.mark.parametrize("mode", [None, "enqueue", "immediate"])
async def test_should_not_dispatch_pre_cancelled_send(
self,
ctx: E2ETestContext,
mode: Literal["enqueue", "immediate"] | None,
):
client, capture_path = create_scenario_client(ctx, "send")
try:
session = await client.create_session(
session_id="scenario-session",
on_permission_request=PermissionHandler.approve_all,
)
try:
send_task = asyncio.create_task(
session.send("This must not be dispatched.", mode=mode)
)
assert send_task.cancel()
with pytest.raises(asyncio.CancelledError):
await send_task
assert _send_requests(capture_path) == []
finally:
await session.disconnect()
finally:
await client.stop()
@pytest.mark.parametrize("mode", [None, "enqueue", "immediate"])
async def test_should_not_replay_send_after_ambiguous_transport_loss(
self,
ctx: E2ETestContext,
mode: Literal["enqueue", "immediate"] | None,
):
client, capture_path = create_scenario_client(ctx, "send-fail")
session = await client.create_session(
session_id="scenario-session",
on_permission_request=PermissionHandler.approve_all,
)
try:
with pytest.raises(Exception) as exc_info:
await session.send("Lose the transport after accepting this.", mode=mode)
assert type(exc_info.value).__name__ == "ProcessExitedError"
requests = _send_requests(capture_path)
assert len(requests) == 1
assert requests[0]["params"]["prompt"] == ("Lose the transport after accepting this.")
if mode is None:
assert "mode" not in requests[0]["params"]
else:
assert requests[0]["params"]["mode"] == mode
with pytest.raises(Exception) as retry_exc_info:
await session.send("Fail immediately after transport loss.")
assert type(retry_exc_info.value).__name__ == "ProcessExitedError"
assert len(_send_requests(capture_path)) == 1
finally:
await client.force_stop()