-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathevent_fidelity.test.ts
More file actions
134 lines (104 loc) · 5.02 KB
/
event_fidelity.test.ts
File metadata and controls
134 lines (104 loc) · 5.02 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
/*---------------------------------------------------------------------------------------------
* 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();
});
});