-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathmessage-source.test.ts
More file actions
163 lines (154 loc) · 6.26 KB
/
Copy pathmessage-source.test.ts
File metadata and controls
163 lines (154 loc) · 6.26 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
import { PassThrough } from "node:stream";
import { describe, expect, it, onTestFinished } from "vitest";
import {
createMessageConnection,
ResponseError,
StreamMessageReader,
StreamMessageWriter,
} from "vscode-jsonrpc/node.js";
import type {
ExtensionContextAttachment,
MessageOptions,
MessageSource,
SessionEvent,
} from "../src/index.js";
import { CopilotSession } from "../src/session.js";
function sessionPair(traceContextProvider?: ConstructorParameters<typeof CopilotSession>[3]) {
const clientToServer = new PassThrough();
const serverToClient = new PassThrough();
const client = createMessageConnection(
new StreamMessageReader(serverToClient),
new StreamMessageWriter(clientToServer)
);
const server = createMessageConnection(
new StreamMessageReader(clientToServer),
new StreamMessageWriter(serverToClient)
);
onTestFinished(() => {
client.dispose();
server.dispose();
clientToServer.destroy();
serverToClient.destroy();
});
client.listen();
server.listen();
return {
session: new CopilotSession("session-1", client, undefined, traceContextProvider),
server,
};
}
const sources: (MessageSource | undefined)[] = [undefined, "user", "system", "agent-sender-id"];
const modes: MessageOptions["mode"][] = [undefined, "enqueue", "immediate"];
it("omits source when sending a plain human prompt", async () => {
const { session, server } = sessionPair();
server.onRequest("session.send", (params: unknown) => {
expect(params).toEqual({ sessionId: "session-1", prompt: "hello" });
return { messageId: "message-1" };
});
await expect(session.send("hello")).resolves.toBe("message-1");
});
describe.each(sources)("message source %s", (source) => {
it.each(modes)("preserves the wire payload with delivery mode %s", async (mode) => {
const { session, server } = sessionPair();
const expected = {
sessionId: "session-1",
prompt: "hello",
...(source === undefined ? {} : { source }),
...(mode === undefined ? {} : { mode }),
};
server.onRequest("session.send", (params: unknown) => {
expect(params).toEqual(expected);
return { messageId: "message-1" };
});
await expect(session.send({ prompt: "hello", source, mode })).resolves.toBe("message-1");
});
it("preserves attachments, display text, headers, agent mode and tracing", async () => {
const trace = {
traceparent: "00-fedcba0987654321fedcba0987654321-abcdef1234567890-01",
tracestate: "vendor=source",
};
const { session, server } = sessionPair(() => trace);
const extensionContext = {
type: "extension_context",
capturedAt: "2026-09-18T20:00:00Z",
extensionId: "scenario-extension",
title: "Selected change",
canvasId: "diff",
instanceId: "diff-17",
payload: { selection: "active" },
} satisfies ExtensionContextAttachment;
const options: MessageOptions = {
prompt: "context updated",
source,
mode: "immediate",
agentMode: "plan",
attachments: [{ type: "blob", data: "aGk=", mimeType: "text/plain" }, extensionContext],
displayPrompt: "Context updated",
requestHeaders: { "X-Tag": "context" },
};
const expected = { sessionId: "session-1", ...trace, ...options };
if (source === undefined) {
delete expected.source;
}
server.onRequest("session.send", (params: unknown) => {
expect(params).toEqual(expected);
return { messageId: "message-1" };
});
await expect(session.send(options)).resolves.toBe("message-1");
});
it.each(modes)(
"allows sendAndWait to finish without assistant output in mode %s",
async (mode) => {
const { session, server } = sessionPair();
server.onRequest("session.send", (params: unknown) => {
expect(params).toEqual({
sessionId: "session-1",
prompt: "context updated",
...(source === undefined ? {} : { source }),
...(mode === undefined ? {} : { mode }),
});
session._dispatchEvent({
type: "session.idle",
id: "idle-1",
timestamp: new Date().toISOString(),
parentId: null,
ephemeral: true,
data: {},
});
return { messageId: "message-1" };
});
await expect(
session.sendAndWait({ prompt: "context updated", source, mode }, 1000)
).resolves.toBeUndefined();
}
);
});
describe.each(["system", "agent-sender-id"] as const)("%s message errors", (source) => {
it("propagates an RPC failure from sendAndWait", async () => {
const { session, server } = sessionPair();
server.onRequest("session.send", () => new ResponseError(-32603, "send failed"));
await expect(
session.sendAndWait({ prompt: "context updated", source }, 1000)
).rejects.toThrow("send failed");
});
it("propagates a session error from sendAndWait", async () => {
const { session, server } = sessionPair();
server.onRequest("session.send", () => {
const event: SessionEvent = {
type: "session.error",
id: "error-1",
timestamp: new Date().toISOString(),
parentId: null,
data: { errorType: "notification", message: "agent failed" },
};
session._dispatchEvent(event);
return { messageId: "message-1" };
});
await expect(
session.sendAndWait({ prompt: "context updated", source }, 1000)
).rejects.toThrow("agent failed");
});
});