-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathcanvas.e2e.test.ts
More file actions
181 lines (156 loc) · 5.76 KB
/
Copy pathcanvas.e2e.test.ts
File metadata and controls
181 lines (156 loc) · 5.76 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
import { describe, expect, it } from "vitest";
import { approveAll, createCanvas } from "../../src/index.js";
import { createSdkTestContext } from "./harness/sdkTestContext.js";
describe("Canvas RPC", async () => {
const openCalls: Array<{ canvasId: string; instanceId: string; input?: unknown }> = [];
const closeCalls: Array<{ canvasId: string; instanceId: string }> = [];
const actionCalls: Array<{
canvasId: string;
instanceId: string;
actionName: string;
input?: unknown;
}> = [];
const counter = createCanvas({
id: "counter",
displayName: "Counter",
description: "A simple counter canvas for e2e testing",
inputSchema: {
type: "object",
properties: { startValue: { type: "number" } },
},
actions: [
{
name: "increment",
description: "Increment the counter",
inputSchema: {
type: "object",
properties: { amount: { type: "number" } },
},
handler: (ctx) => {
actionCalls.push({
canvasId: ctx.canvasId,
instanceId: ctx.instanceId,
actionName: ctx.actionName,
input: ctx.input,
});
return { newValue: 42 };
},
},
],
open: (ctx) => {
openCalls.push({
canvasId: ctx.canvasId,
instanceId: ctx.instanceId,
input: ctx.input,
});
return {
url: "https://example.test/counter",
title: "Counter Canvas",
status: "ready",
};
},
onClose: (ctx) => {
closeCalls.push({
canvasId: ctx.canvasId,
instanceId: ctx.instanceId,
});
},
});
const { copilotClient: client } = await createSdkTestContext();
it("discovers declared canvases via session.canvas.list", async () => {
const session = await client.createSession({
onPermissionRequest: approveAll,
canvases: [counter],
});
const result = await session.rpc.canvas.list();
expect(result.canvases).toHaveLength(1);
expect(result.canvases[0]).toMatchObject({
canvasId: "counter",
displayName: "Counter",
description: "A simple counter canvas for e2e testing",
});
await session.disconnect();
});
it("opens a canvas instance via session.canvas.open round-trip", async () => {
openCalls.length = 0;
const session = await client.createSession({
onPermissionRequest: approveAll,
canvases: [counter],
});
const result = await session.rpc.canvas.open({
canvasId: "counter",
instanceId: "counter-1",
input: { startValue: 10 },
});
expect(result.url).toBe("https://example.test/counter");
expect(result.title).toBe("Counter Canvas");
expect(openCalls).toHaveLength(1);
expect(openCalls[0]).toMatchObject({
canvasId: "counter",
instanceId: "counter-1",
input: { startValue: 10 },
});
// Verify it appears in the open list
const openList = await session.rpc.canvas.listOpen();
expect(openList.openCanvases).toHaveLength(1);
expect(openList.openCanvases[0]).toMatchObject({
canvasId: "counter",
instanceId: "counter-1",
});
await session.disconnect();
});
it("invokes an action on an open canvas instance", async () => {
openCalls.length = 0;
actionCalls.length = 0;
const session = await client.createSession({
onPermissionRequest: approveAll,
canvases: [counter],
});
await session.rpc.canvas.open({
canvasId: "counter",
instanceId: "counter-2",
input: {},
});
const result = await session.rpc.canvas.action.invoke({
instanceId: "counter-2",
actionName: "increment",
input: { amount: 5 },
});
expect(result.result).toEqual({ newValue: 42 });
expect(actionCalls).toHaveLength(1);
expect(actionCalls[0]).toMatchObject({
canvasId: "counter",
instanceId: "counter-2",
actionName: "increment",
input: { amount: 5 },
});
await session.disconnect();
});
it("closes an open canvas instance via session.canvas.close", async () => {
openCalls.length = 0;
closeCalls.length = 0;
const session = await client.createSession({
onPermissionRequest: approveAll,
canvases: [counter],
});
await session.rpc.canvas.open({
canvasId: "counter",
instanceId: "counter-3",
input: {},
});
expect(closeCalls).toHaveLength(0);
await session.rpc.canvas.close({ instanceId: "counter-3" });
expect(closeCalls).toHaveLength(1);
expect(closeCalls[0]).toMatchObject({
canvasId: "counter",
instanceId: "counter-3",
});
// Verify it's no longer in the open list
const openList = await session.rpc.canvas.listOpen();
expect(openList.openCanvases).toHaveLength(0);
await session.disconnect();
});
});