-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathworkflow.e2e.test.ts
More file actions
436 lines (382 loc) · 14.9 KB
/
Copy pathworkflow.e2e.test.ts
File metadata and controls
436 lines (382 loc) · 14.9 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
import { execFileSync } from "node:child_process";
import { existsSync, readFileSync, writeFileSync } from "node:fs";
import { copyFile, mkdir, rm } from "node:fs/promises";
import { dirname, join, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { expect, it, vi } from "vitest";
import {
createSdkTestContext,
DEFAULT_GITHUB_TOKEN,
getLegacyCliPathForTests,
} from "./harness/sdkTestContext.js";
import { retry } from "./harness/sdkTestHelper.js";
const __dirname = dirname(fileURLToPath(import.meta.url));
const sdkEntryPoint = process.env.COPILOT_CLI_PATH
? new URL("../../dist/index.js", import.meta.url).href
: new URL("../../src/index.js", import.meta.url).href;
const { approveAll, RuntimeConnection } = (await import(
sdkEntryPoint
)) as typeof import("../../src/index.js");
const cliPath = process.env.COPILOT_CLI_PATH ?? (await getLegacyCliPathForTests());
const cliDistDirectory = process.env.COPILOT_EXTENSION_SDK_PATH
? dirname(process.env.COPILOT_EXTENSION_SDK_PATH)
: dirname(cliPath);
const workflowTestContext = await createSdkTestContext({
copilotClientOptions: {
connection: RuntimeConnection.forStdio({ path: cliPath }),
env: {
COPILOT_CLI_ENABLED_FEATURE_FLAGS: "EXTENSIONS,AGENT_FACTORIES",
},
extensionLaunchProvider: {
resolve: async (request) => ({
launch: {
executable: "node",
args: [join(cliDistDirectory, "preloads", "extension_bootstrap.mjs")],
env: {
COPILOT_CLI_DIST_DIR: cliDistDirectory,
EXTENSION_PATH: request.modulePath,
},
},
}),
},
},
});
async function setupWorkflowExtension(workDir: string, onPermissionRequest = approveAll) {
const { copilotClient, openAiEndpoint } = workflowTestContext;
const extensionDir = join(workDir, ".github", "extensions", "workflow-smoke");
const readyFile = join(extensionDir, "ready");
await rm(join(workDir, ".github"), { recursive: true, force: true });
await mkdir(extensionDir, { recursive: true });
await copyFile(
join(__dirname, "fixtures", "workflow-extension.mjs"),
join(extensionDir, "extension.mjs")
);
execFileSync("git", ["init", "--quiet"], { cwd: workDir });
await openAiEndpoint.setCopilotUserByToken(DEFAULT_GITHUB_TOKEN, {
login: "workflow-e2e-user",
copilot_plan: "individual_pro",
token_based_billing: true,
is_mcp_enabled: true,
endpoints: {
api: openAiEndpoint.url,
telemetry: "https://localhost:1/telemetry",
},
analytics_tracking_id: "workflow-e2e-tracking-id",
});
const session = await copilotClient.createSession({
requestExtensions: true,
extensionSdkPath: resolve(__dirname, "..", "..", "dist"),
onPermissionRequest,
onElicitationRequest: async () => ({
action: "accept",
content: { action: "approve" },
}),
});
try {
await retry(
"wait for the workflow extension to join the session",
async () => {
expect(existsSync(readyFile)).toBe(true);
},
300,
100
);
return session;
} catch (error) {
await session.disconnect();
throw error;
}
}
it("runs an extension-authored workflow across the SDK process boundary", async () => {
const { workDir } = workflowTestContext;
await using session = await setupWorkflowExtension(workDir);
const result = await session.workflow.run("argument-echo", {
args: { source: "sdk-workflow-e2e", count: 12 },
limits: { timeoutSeconds: 15 },
notifyOnComplete: false,
});
expect(result).toMatchObject({
status: "completed",
});
expect(result.result).toEqual({ source: "sdk-workflow-e2e", count: 12 });
}, 60_000);
it.skip("forwards every declared subagent option to the runtime", async () => {
const { workDir } = workflowTestContext;
await using session = await setupWorkflowExtension(workDir);
const result = await session.workflow.run("forwards-subagent-options");
expect(result).toMatchObject({
status: "completed",
result: { didThrow: false },
});
}, 60_000);
it("throws WorkflowResumeError with not_found for an unknown run", async () => {
const { workDir } = workflowTestContext;
await using session = await setupWorkflowExtension(workDir);
const error = await session.workflow
.resume("00000000-0000-0000-0000-000000000000")
.catch((caught: unknown) => caught);
expect(error).toMatchObject({
name: "WorkflowResumeError",
code: "not_found",
});
});
it("throws WorkflowResumeError with non_resumable for a completed run", async () => {
const { workDir } = workflowTestContext;
await using session = await setupWorkflowExtension(workDir);
const run = await session.workflow.run("argument-echo", { notifyOnComplete: false });
const error = await session.workflow.resume(run.runId).catch((caught: unknown) => caught);
expect(error).toMatchObject({
name: "WorkflowResumeError",
code: "non_resumable",
});
});
it("forwards workflow runtime controls across the SDK process boundary", async () => {
const { workDir } = workflowTestContext;
await using session = await setupWorkflowExtension(workDir);
const suppressed = await session.workflow.run("phased", {
notifyOnComplete: false,
logPhaseNames: false,
});
expect(suppressed).toMatchObject({
status: "completed",
result: "finished",
});
const progress = await session.workflow.getRunProgress(suppressed.runId);
expect(progress.records).toEqual(
expect.arrayContaining([
expect.objectContaining({ kind: "phase", text: "Collect" }),
expect.objectContaining({ kind: "log", text: "Collected" }),
expect.objectContaining({ kind: "phase", text: "Summarize" }),
expect.objectContaining({ kind: "log", text: "Summarized" }),
])
);
const events = await session.getEvents();
expect(
events.some(
(event) =>
event.type === "system.notification" &&
event.data.kind.type === "workflow_completed" &&
event.data.kind.runId === suppressed.runId
)
).toBe(false);
expect(
events.filter(
(event) => event.type === "session.info" && event.data.infoType === "workflow_phase"
)
).toEqual([]);
});
it("pages workflow runs and returns cursor metadata", async () => {
const { workDir } = workflowTestContext;
await using session = await setupWorkflowExtension(workDir);
const first = await session.workflow.run("argument-echo", {
args: { ordinal: 1 },
notifyOnComplete: false,
});
const second = await session.workflow.run("argument-echo", {
args: { ordinal: 2 },
notifyOnComplete: false,
});
const third = await session.workflow.run("argument-echo", {
args: { ordinal: 3 },
notifyOnComplete: false,
});
const newest = await session.workflow.listRuns({ limit: 1 });
expect(newest).toMatchObject({
runs: [expect.objectContaining({ runId: third.runId })],
hasMoreNewer: false,
omittedOlder: 2,
});
expect(newest.oldestSeq).toBe(newest.newestSeq);
expect(newest.oldestSeq).not.toBeNull();
const older = await session.workflow.listRuns({
beforeSeq: newest.oldestSeq!,
limit: 1,
});
expect(older).toMatchObject({
runs: [expect.objectContaining({ runId: second.runId })],
hasMoreNewer: true,
omittedOlder: 1,
});
const oldest = await session.workflow.listRuns({
beforeSeq: older.oldestSeq!,
limit: 1,
});
expect(oldest).toMatchObject({
runs: [expect.objectContaining({ runId: first.runId })],
hasMoreNewer: true,
omittedOlder: 0,
});
});
it("runs a workflow when its session denies every permission request", async () => {
const { workDir } = workflowTestContext;
const denyPermissions = vi.fn(() => ({ kind: "reject" as const }));
await using session = await setupWorkflowExtension(workDir, denyPermissions);
await expect(
session.workflow.run("argument-echo", { notifyOnComplete: false })
).resolves.toMatchObject({
status: "completed",
});
expect(denyPermissions).not.toHaveBeenCalled();
});
it("resumes a failed workflow when its session denies every permission request", async () => {
const { workDir } = workflowTestContext;
const denyPermissions = vi.fn(() => ({ kind: "reject" as const }));
await using session = await setupWorkflowExtension(workDir, denyPermissions);
const failedRun = await session.workflow.run("fails-once", { notifyOnComplete: false });
expect(failedRun).toMatchObject({
status: "error",
});
await expect(
session.workflow.resume(failedRun.runId, {
notifyOnComplete: false,
logPhaseNames: false,
})
).resolves.toMatchObject({
status: "completed",
result: "resumed",
});
expect(denyPermissions).not.toHaveBeenCalled();
});
it("pauses a running workflow through the session API", async () => {
const { workDir } = workflowTestContext;
const extensionDir = join(workDir, ".github", "extensions", "workflow-smoke");
await using session = await setupWorkflowExtension(workDir);
const execution = session.workflow.run("externally-paused", {
notifyOnComplete: false,
});
await retry(
"wait for the externally paused workflow to enter its body",
async () => {
expect(existsSync(join(extensionDir, "external-pause-entered"))).toBe(true);
},
100,
100
);
let runId: string | undefined;
await retry(
"find the running workflow before pausing it",
async () => {
const running = (await session.workflow.listRuns()).find(
(run) => run.workflowName === "externally-paused" && run.status === "running"
);
expect(running).toBeDefined();
runId = running?.runId;
},
100,
100
);
if (!runId) {
throw new Error("Running workflow did not expose a run ID");
}
await expect(session.workflow.pause(runId)).resolves.toMatchObject({
runId,
status: "paused",
});
await expect(execution).resolves.toMatchObject({
runId,
status: "paused",
});
});
it("pauses once at a durable checkpoint and continues after resume", async () => {
const { workDir } = workflowTestContext;
const extensionDir = join(workDir, ".github", "extensions", "workflow-smoke");
await using session = await setupWorkflowExtension(workDir);
const paused = await session.workflow.run("durable-pause-checkpoint", {
notifyOnComplete: false,
});
expect(paused).toMatchObject({ status: "paused" });
expect(readFileSync(join(extensionDir, "checkpoint-attempts"))).toHaveLength(1);
expect(readFileSync(join(extensionDir, "checkpoint-preparations"))).toHaveLength(1);
const resumed = await session.workflow.resume(paused.runId, {
notifyOnComplete: false,
});
expect(resumed).toMatchObject({
runId: paused.runId,
status: "completed",
result: { attempt: 2, prepared: 1 },
});
expect(readFileSync(join(extensionDir, "checkpoint-attempts"))).toHaveLength(2);
expect(readFileSync(join(extensionDir, "checkpoint-preparations"))).toHaveLength(1);
});
it("refuses a workflow started through the context session from a workflow body", async () => {
const { workDir } = workflowTestContext;
await using session = await setupWorkflowExtension(workDir);
const result = await session.workflow.run("starts-from-context-session", {
notifyOnComplete: false,
});
expect(result).toMatchObject({
status: "completed",
result: expect.stringContaining("workflow.run, workflow.resume, and workflow.pause"),
});
expect((result as { result: string }).result).toContain("workflow body");
});
it("refuses a workflow started through the module session from a workflow body", async () => {
const { workDir } = workflowTestContext;
await using session = await setupWorkflowExtension(workDir);
const result = await session.workflow.run("starts-from-module-session", {
notifyOnComplete: false,
});
expect(result).toMatchObject({
status: "completed",
result: expect.stringContaining("workflow.run, workflow.resume, and workflow.pause"),
});
expect((result as { result: string }).result).toContain("workflow body");
});
it("allows a module-level extension watcher to start a workflow while another body is parked", async () => {
const { workDir } = workflowTestContext;
const extensionDir = join(workDir, ".github", "extensions", "workflow-smoke");
await using session = await setupWorkflowExtension(workDir);
const parked = session.workflow.run("parked", { notifyOnComplete: false });
await retry(
"wait for the parked workflow to enter its body",
async () => {
expect(existsSync(join(extensionDir, "entered"))).toBe(true);
},
100,
100
);
writeFileSync(join(extensionDir, "start-b"), "start");
const bResultFile = join(extensionDir, "b-result");
await retry(
"wait for the module-level watcher workflow run to succeed",
async () => {
expect(existsSync(bResultFile)).toBe(true);
expect(JSON.parse(readFileSync(bResultFile, "utf8"))).toMatchObject({
status: "success",
result: {
status: "completed",
result: { source: "module-watcher" },
},
});
},
100,
100
);
writeFileSync(join(extensionDir, "release"), "release");
await expect(parked).resolves.toMatchObject({
status: "completed",
result: "released",
});
}, 60_000);
it("returns an array result from an extension-authored workflow", async () => {
const { workDir } = workflowTestContext;
await using session = await setupWorkflowExtension(workDir);
const result = await session.workflow.run("array-result", { notifyOnComplete: false });
expect(result).toMatchObject({
status: "completed",
result: [1, "two", false],
});
});
it("passes array workflow arguments across the SDK process boundary", async () => {
const { workDir } = workflowTestContext;
await using session = await setupWorkflowExtension(workDir);
const args = [1, "two", false];
const result = await session.workflow.run("argument-echo", {
args,
notifyOnComplete: false,
});
expect(result).toMatchObject({
status: "completed",
result: args,
});
});