-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathrpc_queue.e2e.test.ts
More file actions
143 lines (127 loc) · 5.96 KB
/
rpc_queue.e2e.test.ts
File metadata and controls
143 lines (127 loc) · 5.96 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
import { randomUUID } from "node:crypto";
import { describe, expect, it } from "vitest";
import { approveAll, type SessionEvent } from "../../src/index.js";
import { createSdkTestContext } from "./harness/sdkTestContext.js";
import { waitForCondition } from "./harness/sdkTestHelper.js";
describe("Session queue RPC", async () => {
const { copilotClient: client } = await createSdkTestContext();
async function expectQueueEmpty(session: Awaited<ReturnType<typeof client.createSession>>) {
const pending = await session.rpc.queue.pendingItems();
expect(pending.items).toEqual([]);
expect(pending.steeringMessages).toEqual([]);
}
function isPendingCommand(
item: { kind: string; displayText: string },
command: string
): boolean {
return (
item.kind === "command" &&
(item.displayText === command || item.displayText.includes(command.replace(/^\//, "")))
);
}
it("fresh queue is empty and empty mutations are no-ops", async () => {
const session = await client.createSession({ onPermissionRequest: approveAll });
try {
await expectQueueEmpty(session);
expect((await session.rpc.queue.removeMostRecent()).removed).toBe(false);
await expectQueueEmpty(session);
await session.rpc.queue.clear();
await expectQueueEmpty(session);
expect((await session.rpc.queue.removeMostRecent()).removed).toBe(false);
await expectQueueEmpty(session);
} finally {
await session.disconnect();
}
});
it("pendingItems reports queued command and remove and clear update queue", async () => {
const session = await client.createSession({ onPermissionRequest: approveAll });
let firstEvent: Extract<SessionEvent, { type: "command.queued" }> | undefined;
let respondedToFirst = false;
const interest = await session.rpc.eventLog.registerInterest({
eventType: "command.queued",
});
try {
const firstCommand = `/sdk-queue-first-${randomUUID()}`;
const secondCommand = `/sdk-queue-second-${randomUUID()}`;
const thirdCommand = `/sdk-queue-third-${randomUUID()}`;
const firstQueued = new Promise<Extract<SessionEvent, { type: "command.queued" }>>(
(resolve) => {
session.on((event) => {
if (
event.type === "command.queued" &&
event.data.command === firstCommand
) {
resolve(event);
}
});
}
);
expect((await session.rpc.commands.enqueue({ command: firstCommand })).queued).toBe(
true
);
firstEvent = await firstQueued;
expect((await session.rpc.commands.enqueue({ command: secondCommand })).queued).toBe(
true
);
await waitForCondition(
async () =>
(await session.rpc.queue.pendingItems()).items.some((item) =>
isPendingCommand(item, secondCommand)
),
{ timeoutMessage: `Timed out waiting for ${secondCommand} in queue.` }
);
expect((await session.rpc.queue.removeMostRecent()).removed).toBe(true);
await waitForCondition(
async () =>
!(await session.rpc.queue.pendingItems()).items.some((item) =>
isPendingCommand(item, secondCommand)
),
{ timeoutMessage: `Timed out waiting for ${secondCommand} to leave queue.` }
);
expect((await session.rpc.commands.enqueue({ command: thirdCommand })).queued).toBe(
true
);
await waitForCondition(
async () =>
(await session.rpc.queue.pendingItems()).items.some((item) =>
isPendingCommand(item, thirdCommand)
),
{ timeoutMessage: `Timed out waiting for ${thirdCommand} in queue.` }
);
await session.rpc.queue.clear();
await waitForCondition(
async () =>
!(await session.rpc.queue.pendingItems()).items.some((item) =>
isPendingCommand(item, thirdCommand)
),
{ timeoutMessage: `Timed out waiting for ${thirdCommand} to leave queue.` }
);
const completed = await session.rpc.commands.respondToQueuedCommand({
requestId: firstEvent.data.requestId,
result: { handled: true, stopProcessingQueue: true },
});
respondedToFirst = completed.success;
expect(completed.success).toBe(true);
await waitForCondition(
async () => {
const pending = await session.rpc.queue.pendingItems();
return pending.items.length === 0 && pending.steeringMessages.length === 0;
},
{ timeoutMessage: "Timed out waiting for queue to empty." }
);
} finally {
if (!respondedToFirst && firstEvent) {
await session.rpc.commands.respondToQueuedCommand({
requestId: firstEvent.data.requestId,
result: { handled: true, stopProcessingQueue: true },
});
}
await session.rpc.queue.clear();
await session.rpc.eventLog.releaseInterest({ handle: interest.handle });
await session.disconnect();
}
});
});