-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathevent_fidelity.e2e.test.ts
More file actions
239 lines (186 loc) · 9.36 KB
/
Copy pathevent_fidelity.e2e.test.ts
File metadata and controls
239 lines (186 loc) · 9.36 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
import { writeFile } from "fs/promises";
import { join } from "path";
import { describe, expect, it } from "vitest";
import { SessionEvent, approveAll } from "../../src/index.js";
import { createSdkTestContext } from "./harness/sdkTestContext";
describe("Event Fidelity", async () => {
const { copilotClient: client, workDir } = await createSdkTestContext();
it("should emit events in correct order for tool-using conversation", async () => {
await writeFile(join(workDir, "hello.txt"), "Hello World");
const session = await client.createSession({ onPermissionRequest: approveAll });
const events: SessionEvent[] = [];
session.on((event) => {
events.push(event);
});
await session.sendAndWait({
prompt: "Read the file 'hello.txt' and tell me its contents.",
});
const types = events.map((e) => e.type);
// Must have user message, tool execution, assistant message, and idle
expect(types).toContain("user.message");
expect(types).toContain("assistant.message");
// user.message should come before assistant.message
const userIdx = types.indexOf("user.message");
const assistantIdx = types.lastIndexOf("assistant.message");
expect(userIdx).toBeLessThan(assistantIdx);
// session.idle should be last
const idleIdx = types.lastIndexOf("session.idle");
expect(idleIdx).toBe(types.length - 1);
await session.disconnect();
});
it("should include valid fields on all events", async () => {
const session = await client.createSession({ onPermissionRequest: approveAll });
const events: SessionEvent[] = [];
session.on((event) => {
events.push(event);
});
await session.sendAndWait({
prompt: "What is 5+5? Reply with just the number.",
});
// All events must have id and timestamp
for (const event of events) {
expect(event.id).toBeDefined();
expect(typeof event.id).toBe("string");
expect(event.id.length).toBeGreaterThan(0);
expect(event.timestamp).toBeDefined();
expect(typeof event.timestamp).toBe("string");
}
// user.message should have content
const userEvent = events.find((e) => e.type === "user.message");
expect(userEvent).toBeDefined();
expect(userEvent?.data.content).toBeDefined();
// assistant.message should have messageId and content
const assistantEvent = events.find((e) => e.type === "assistant.message");
expect(assistantEvent).toBeDefined();
expect(assistantEvent?.data.messageId).toBeDefined();
expect(assistantEvent?.data.content).toBeDefined();
await session.disconnect();
});
it("should emit tool execution events with correct fields", async () => {
await writeFile(join(workDir, "data.txt"), "test data");
const session = await client.createSession({ onPermissionRequest: approveAll });
const events: SessionEvent[] = [];
session.on((event) => {
events.push(event);
});
await session.sendAndWait({
prompt: "Read the file 'data.txt'.",
});
// Should have tool.execution_start and tool.execution_complete
const toolStarts = events.filter((e) => e.type === "tool.execution_start");
const toolCompletes = events.filter((e) => e.type === "tool.execution_complete");
expect(toolStarts.length).toBeGreaterThanOrEqual(1);
expect(toolCompletes.length).toBeGreaterThanOrEqual(1);
// Tool start should have toolCallId and toolName
const firstStart = toolStarts[0]!;
expect(firstStart.data.toolCallId).toBeDefined();
expect(firstStart.data.toolName).toBeDefined();
// Tool complete should have toolCallId
const firstComplete = toolCompletes[0]!;
expect(firstComplete.data.toolCallId).toBeDefined();
await session.disconnect();
});
it("should emit assistant.message with messageId", async () => {
const session = await client.createSession({ onPermissionRequest: approveAll });
const events: SessionEvent[] = [];
session.on((event) => {
events.push(event);
});
await session.sendAndWait({
prompt: "Say 'pong'.",
});
const assistantEvents = events.filter((e) => e.type === "assistant.message");
expect(assistantEvents.length).toBeGreaterThanOrEqual(1);
// messageId should be present
const msg = assistantEvents[0]!;
expect(msg.data.messageId).toBeDefined();
expect(typeof msg.data.messageId).toBe("string");
expect(msg.data.content).toContain("pong");
await session.disconnect();
});
it("should emit assistant usage event after model call", async () => {
const session = await client.createSession({ onPermissionRequest: approveAll });
const events: SessionEvent[] = [];
session.on((event) => {
events.push(event);
});
await session.sendAndWait({
prompt: "What is 5+5? Reply with just the number.",
});
const usageEvent = [...events].reverse().find((e) => e.type === "assistant.usage");
expect(usageEvent).toBeDefined();
expect(typeof usageEvent!.data.model).toBe("string");
expect((usageEvent!.data.model as string).length).toBeGreaterThan(0);
expect(usageEvent!.id).toBeDefined();
expect(typeof usageEvent!.id).toBe("string");
expect(usageEvent!.timestamp).toBeDefined();
expect(typeof usageEvent!.timestamp).toBe("string");
await session.disconnect();
});
it("should emit session usage info event after model call", async () => {
const session = await client.createSession({ onPermissionRequest: approveAll });
const events: SessionEvent[] = [];
session.on((event) => {
events.push(event);
});
await session.sendAndWait({
prompt: "What is 5+5? Reply with just the number.",
});
const usageInfoEvent = [...events].reverse().find((e) => e.type === "session.usage_info");
expect(usageInfoEvent).toBeDefined();
expect(usageInfoEvent!.data.currentTokens).toBeGreaterThan(0);
expect(usageInfoEvent!.data.messagesLength).toBeGreaterThan(0);
expect(usageInfoEvent!.data.tokenLimit).toBeGreaterThan(0);
await session.disconnect();
});
it("should emit pending messages modified event when message queue changes", async () => {
const session = await client.createSession({ onPermissionRequest: approveAll });
const events: SessionEvent[] = [];
session.on((event) => {
events.push(event);
});
// sendAndWait collects everything in one round trip and matches the
// pattern of every other test in this file (and the Rust E2E equivalent),
// avoiding the split fire-and-forget + helper pattern that previously
// made this test prone to flakes.
const answer = await session.sendAndWait({
prompt: "What is 9+9? Reply with just the number.",
});
const pendingEvent = events.find((e) => e.type === "pending_messages.modified");
expect(pendingEvent).toBeDefined();
expect(answer?.data.content).toContain("18");
await session.disconnect();
});
it("should preserve message order in getMessages after tool use", async () => {
await writeFile(join(workDir, "order.txt"), "ORDER_CONTENT_42");
const session = await client.createSession({ onPermissionRequest: approveAll });
await session.sendAndWait({
prompt: "Read the file 'order.txt' and tell me what the number is.",
});
const messages = await session.getEvents();
const types = messages.map((m) => m.type);
const sessionStartIdx = types.indexOf("session.start");
const userMsgIdx = types.indexOf("user.message");
const toolStartIdx = types.indexOf("tool.execution_start");
const toolCompleteIdx = types.indexOf("tool.execution_complete");
const assistantMsgIdx = types.lastIndexOf("assistant.message");
expect(sessionStartIdx).toBeGreaterThanOrEqual(0);
expect(userMsgIdx).toBeGreaterThanOrEqual(0);
expect(toolStartIdx).toBeGreaterThanOrEqual(0);
expect(toolCompleteIdx).toBeGreaterThanOrEqual(0);
expect(assistantMsgIdx).toBeGreaterThanOrEqual(0);
expect(sessionStartIdx).toBeLessThan(userMsgIdx);
expect(userMsgIdx).toBeLessThan(toolStartIdx);
expect(toolStartIdx).toBeLessThan(toolCompleteIdx);
expect(toolCompleteIdx).toBeLessThan(assistantMsgIdx);
const userEvent = messages.find((m) => m.type === "user.message");
expect(userEvent?.data.content).toContain("order.txt");
const assistantEvents = messages.filter((m) => m.type === "assistant.message");
const lastAssistant = assistantEvents[assistantEvents.length - 1]!;
expect(lastAssistant.data.content).toContain("42");
await session.disconnect();
});
});