-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathtest_scenario_canvas_e2e.py
More file actions
230 lines (207 loc) · 8.15 KB
/
Copy pathtest_scenario_canvas_e2e.py
File metadata and controls
230 lines (207 loc) · 8.15 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
"""Scenario-parity E2Es for canvas provider callback routing."""
from __future__ import annotations
from collections.abc import Sequence
import pytest
from copilot import (
CanvasAction,
CanvasDeclaration,
CanvasError,
CanvasHandler,
OpenCanvasInstance,
)
from copilot.rpc import (
CanvasProviderCloseRequest,
CanvasProviderInvokeActionRequest,
CanvasProviderOpenRequest,
CanvasProviderOpenResult,
)
from copilot.session import PermissionHandler
from ._scenario_fake_cli import create_scenario_client, read_scenario_capture
from .testharness import E2ETestContext
pytestmark = pytest.mark.asyncio(loop_scope="module")
def _counter_canvas() -> CanvasDeclaration:
return CanvasDeclaration(
id="counter",
display_name="Counter",
description="Scenario counter canvas",
input_schema={"type": "object"},
actions=[
CanvasAction(
name="increment",
description="Increment the counter",
input_schema={"type": "object"},
)
],
)
class _ScenarioCanvasHandler(CanvasHandler):
def __init__(self, failing_operation: str | None = None, *, structured: bool = True) -> None:
self.failing_operation = failing_operation
self.structured = structured
self.open_calls: list[CanvasProviderOpenRequest] = []
self.action_calls: list[CanvasProviderInvokeActionRequest] = []
self.close_calls: list[CanvasProviderCloseRequest] = []
def _fail_if_requested(self, operation: str) -> None:
if self.failing_operation != operation:
return
if self.structured:
raise CanvasError("scenario_canvas_error", f"{operation} failed")
raise RuntimeError(f"{operation} failed unexpectedly")
async def on_open(self, ctx: CanvasProviderOpenRequest) -> CanvasProviderOpenResult:
self.open_calls.append(ctx)
self._fail_if_requested("open")
return CanvasProviderOpenResult(
status="ready",
title="Scenario Counter",
url="https://example.test/scenario-counter",
)
async def on_action(self, ctx: CanvasProviderInvokeActionRequest) -> dict[str, int]:
self.action_calls.append(ctx)
self._fail_if_requested("action")
return {"newValue": 42}
async def on_close(self, ctx: CanvasProviderCloseRequest) -> None:
self.close_calls.append(ctx)
self._fail_if_requested("close")
def _operation_calls(
handler: _ScenarioCanvasHandler,
operation: str,
) -> Sequence[
CanvasProviderOpenRequest | CanvasProviderInvokeActionRequest | CanvasProviderCloseRequest
]:
if operation == "open":
return handler.open_calls
if operation == "action":
return handler.action_calls
return handler.close_calls
class TestScenarioCanvas:
@pytest.mark.parametrize("operation", ["open", "action", "close"])
async def test_should_preserve_structured_canvas_error_envelope(
self,
ctx: E2ETestContext,
operation: str,
):
client, capture_path = create_scenario_client(ctx, f"canvas-error-{operation}")
handler = _ScenarioCanvasHandler(operation)
try:
session = await client.create_session(
session_id="scenario-session",
canvases=[_counter_canvas()],
canvas_handler=handler,
on_permission_request=PermissionHandler.approve_all,
)
try:
calls = _operation_calls(handler, operation)
assert len(calls) == 1
assert calls[0].session_id == "scenario-session"
assert calls[0].canvas_id == "counter"
assert calls[0].extension_id == "python-scenario-tests"
assert calls[0].instance_id == f"scenario-{operation}"
assert calls[0].host is not None
assert calls[0].host.capabilities is not None
assert calls[0].host.capabilities.canvases is True
capture = read_scenario_capture(capture_path)
assert capture["callbackResponses"] == [
{
"jsonrpc": "2.0",
"id": "canvas-callback",
"error": {
"code": -32603,
"message": f"{operation} failed",
"data": {
"code": "scenario_canvas_error",
"message": f"{operation} failed",
},
},
}
]
finally:
await session.disconnect()
finally:
await client.stop()
async def test_should_wrap_unexpected_canvas_handler_error(
self,
ctx: E2ETestContext,
):
client, capture_path = create_scenario_client(ctx, "canvas-error-open")
handler = _ScenarioCanvasHandler("open", structured=False)
try:
session = await client.create_session(
session_id="scenario-session",
canvases=[_counter_canvas()],
canvas_handler=handler,
on_permission_request=PermissionHandler.approve_all,
)
try:
capture = read_scenario_capture(capture_path)
assert capture["callbackResponses"][0]["error"] == {
"code": -32603,
"message": "open failed unexpectedly",
"data": {
"code": "canvas_handler_error",
"message": "open failed unexpectedly",
},
}
finally:
await session.disconnect()
finally:
await client.stop()
async def test_should_reattach_canvas_and_route_all_callbacks_before_resume_completes(
self,
ctx: E2ETestContext,
):
client, capture_path = create_scenario_client(ctx, "canvas-resume")
first = await client.create_session(
session_id="scenario-session",
canvases=[_counter_canvas()],
canvas_handler=_ScenarioCanvasHandler(),
on_permission_request=PermissionHandler.approve_all,
)
await first.disconnect()
handler = _ScenarioCanvasHandler()
resumed = await client.resume_session(
"scenario-session",
canvases=[_counter_canvas()],
canvas_handler=handler,
open_canvases=[
OpenCanvasInstance(
canvas_id="counter",
extension_id="python-scenario-tests",
instance_id="reattached-counter",
input={"startValue": 3},
status="ready",
)
],
on_permission_request=PermissionHandler.approve_all,
)
try:
assert len(handler.open_calls) == 1
assert handler.open_calls[0].input == {"startValue": 7}
assert len(handler.action_calls) == 1
assert handler.action_calls[0].action_name == "increment"
assert handler.action_calls[0].input == {"amount": 5}
assert len(handler.close_calls) == 1
assert handler.close_calls[0].instance_id == "scenario-close"
capture = read_scenario_capture(capture_path)
assert capture["callbackResponses"] == [
{
"jsonrpc": "2.0",
"id": "resume-open",
"result": {
"status": "ready",
"title": "Scenario Counter",
"url": "https://example.test/scenario-counter",
},
},
{
"jsonrpc": "2.0",
"id": "resume-action",
"result": {"newValue": 42},
},
{
"jsonrpc": "2.0",
"id": "resume-close",
"result": None,
},
]
finally:
await resumed.disconnect()
await client.stop()