|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + *--------------------------------------------------------------------------------------------*/ |
| 4 | + |
| 5 | +import { writeFile } from "fs/promises"; |
| 6 | +import { join } from "path"; |
| 7 | +import { describe, expect, it } from "vitest"; |
| 8 | +import { SessionEvent } from "../../src/index.js"; |
| 9 | +import { createSdkTestContext } from "./harness/sdkTestContext"; |
| 10 | + |
| 11 | +describe("Event Fidelity", async () => { |
| 12 | + const { copilotClient: client, workDir } = await createSdkTestContext(); |
| 13 | + |
| 14 | + it("should emit events in correct order for tool-using conversation", async () => { |
| 15 | + await writeFile(join(workDir, "hello.txt"), "Hello World"); |
| 16 | + |
| 17 | + const session = await client.createSession(); |
| 18 | + const events: SessionEvent[] = []; |
| 19 | + session.on((event) => { |
| 20 | + events.push(event); |
| 21 | + }); |
| 22 | + |
| 23 | + await session.sendAndWait({ |
| 24 | + prompt: "Read the file 'hello.txt' and tell me its contents.", |
| 25 | + }); |
| 26 | + |
| 27 | + const types = events.map((e) => e.type); |
| 28 | + |
| 29 | + // Must have user message, tool execution, assistant message, and idle |
| 30 | + expect(types).toContain("user.message"); |
| 31 | + expect(types).toContain("assistant.message"); |
| 32 | + |
| 33 | + // user.message should come before assistant.message |
| 34 | + const userIdx = types.indexOf("user.message"); |
| 35 | + const assistantIdx = types.lastIndexOf("assistant.message"); |
| 36 | + expect(userIdx).toBeLessThan(assistantIdx); |
| 37 | + |
| 38 | + // session.idle should be last |
| 39 | + const idleIdx = types.lastIndexOf("session.idle"); |
| 40 | + expect(idleIdx).toBe(types.length - 1); |
| 41 | + |
| 42 | + await session.destroy(); |
| 43 | + }); |
| 44 | + |
| 45 | + it("should include valid fields on all events", async () => { |
| 46 | + const session = await client.createSession(); |
| 47 | + const events: SessionEvent[] = []; |
| 48 | + session.on((event) => { |
| 49 | + events.push(event); |
| 50 | + }); |
| 51 | + |
| 52 | + await session.sendAndWait({ |
| 53 | + prompt: "What is 5+5? Reply with just the number.", |
| 54 | + }); |
| 55 | + |
| 56 | + // All events must have id and timestamp |
| 57 | + for (const event of events) { |
| 58 | + expect(event.id).toBeDefined(); |
| 59 | + expect(typeof event.id).toBe("string"); |
| 60 | + expect(event.id.length).toBeGreaterThan(0); |
| 61 | + |
| 62 | + expect(event.timestamp).toBeDefined(); |
| 63 | + expect(typeof event.timestamp).toBe("string"); |
| 64 | + } |
| 65 | + |
| 66 | + // user.message should have content |
| 67 | + const userEvent = events.find((e) => e.type === "user.message"); |
| 68 | + expect(userEvent).toBeDefined(); |
| 69 | + expect(userEvent?.data.content).toBeDefined(); |
| 70 | + |
| 71 | + // assistant.message should have messageId and content |
| 72 | + const assistantEvent = events.find((e) => e.type === "assistant.message"); |
| 73 | + expect(assistantEvent).toBeDefined(); |
| 74 | + expect(assistantEvent?.data.messageId).toBeDefined(); |
| 75 | + expect(assistantEvent?.data.content).toBeDefined(); |
| 76 | + |
| 77 | + await session.destroy(); |
| 78 | + }); |
| 79 | + |
| 80 | + it("should emit tool execution events with correct fields", async () => { |
| 81 | + await writeFile(join(workDir, "data.txt"), "test data"); |
| 82 | + |
| 83 | + const session = await client.createSession(); |
| 84 | + const events: SessionEvent[] = []; |
| 85 | + session.on((event) => { |
| 86 | + events.push(event); |
| 87 | + }); |
| 88 | + |
| 89 | + await session.sendAndWait({ |
| 90 | + prompt: "Read the file 'data.txt'.", |
| 91 | + }); |
| 92 | + |
| 93 | + // Should have tool.execution_start and tool.execution_complete |
| 94 | + const toolStarts = events.filter( |
| 95 | + (e) => e.type === "tool.execution_start" |
| 96 | + ); |
| 97 | + const toolCompletes = events.filter( |
| 98 | + (e) => e.type === "tool.execution_complete" |
| 99 | + ); |
| 100 | + |
| 101 | + expect(toolStarts.length).toBeGreaterThanOrEqual(1); |
| 102 | + expect(toolCompletes.length).toBeGreaterThanOrEqual(1); |
| 103 | + |
| 104 | + // Tool start should have toolCallId and toolName |
| 105 | + const firstStart = toolStarts[0]!; |
| 106 | + expect(firstStart.data.toolCallId).toBeDefined(); |
| 107 | + expect(firstStart.data.toolName).toBeDefined(); |
| 108 | + |
| 109 | + // Tool complete should have toolCallId |
| 110 | + const firstComplete = toolCompletes[0]!; |
| 111 | + expect(firstComplete.data.toolCallId).toBeDefined(); |
| 112 | + |
| 113 | + await session.destroy(); |
| 114 | + }); |
| 115 | + |
| 116 | + it("should emit assistant.message with messageId", async () => { |
| 117 | + const session = await client.createSession(); |
| 118 | + const events: SessionEvent[] = []; |
| 119 | + session.on((event) => { |
| 120 | + events.push(event); |
| 121 | + }); |
| 122 | + |
| 123 | + await session.sendAndWait({ |
| 124 | + prompt: "Say 'pong'.", |
| 125 | + }); |
| 126 | + |
| 127 | + const assistantEvents = events.filter( |
| 128 | + (e) => e.type === "assistant.message" |
| 129 | + ); |
| 130 | + expect(assistantEvents.length).toBeGreaterThanOrEqual(1); |
| 131 | + |
| 132 | + // messageId should be present |
| 133 | + const msg = assistantEvents[0]!; |
| 134 | + expect(msg.data.messageId).toBeDefined(); |
| 135 | + expect(typeof msg.data.messageId).toBe("string"); |
| 136 | + expect(msg.data.content).toContain("pong"); |
| 137 | + |
| 138 | + await session.destroy(); |
| 139 | + }); |
| 140 | +}); |
0 commit comments