-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathchat-handle.test.ts
More file actions
349 lines (320 loc) · 13.1 KB
/
Copy pathchat-handle.test.ts
File metadata and controls
349 lines (320 loc) · 13.1 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/**
* Unit tests for `ChatHandle` — exercise the parts that are easy to get subtly
* wrong with no live harness:
*
* - Usage aggregation: tokens always SUM; cost respects `costSemantic`
* (cumulative → MAX, delta → SUM, mixed → prefer cumulative).
* - The dual interface: `for await` yields raw events; `await handle`
* resolves to a ChatResult. Both consume the same stream once.
* - Permission callback contract: `onPermissionRequest` is called with the
* exact args from the event, and the resolved decision is POSTed back to
* the harness. Default decision is "allow".
* - Lifecycle: `onComplete` fires after `ca_session_ended`; `drain()`
* throws when the stream closes without an end event; `result()` is
* memoized.
*
* Everything is mocked — no network, no harness — so the suite runs in <50ms.
*/
import { describe, it, expect, vi } from "vitest";
import type { HarnessEvent } from "@open-gitagent/protocol";
import { ChatHandle } from "./chat-handle.js";
import type { PermissionDecision } from "./types.js";
const SESS = "sess_test_01";
const HARNESS = "http://localhost:9999";
/** Tiny helper: turn a fixed list of events into an AsyncIterable<HarnessEvent>. */
async function* iter(events: HarnessEvent[]): AsyncIterable<HarnessEvent> {
for (const ev of events) yield ev;
}
/** Build a ChatHandle wired to a deterministic event stream + a fetch mock. */
function makeHandle(
events: HarnessEvent[],
extra: {
onPermissionRequest?: (
callId: string,
toolName: string,
input: unknown,
risk?: "low" | "medium" | "high" | "destructive",
) => Promise<PermissionDecision> | PermissionDecision;
onComplete?: () => Promise<void> | void;
} = {},
): { handle: ChatHandle; fetchImpl: ReturnType<typeof vi.fn> } {
const fetchImpl = vi.fn(async () =>
new Response("", { status: 200, headers: { "content-type": "application/json" } }),
);
const handle = new ChatHandle({
sessionIdPromise: Promise.resolve(SESS),
events: iter(events),
harnessUrlPromise: Promise.resolve(HARNESS),
fetchImpl: fetchImpl as unknown as typeof fetch,
...extra,
});
return { handle, fetchImpl };
}
const endedEvent = (): HarnessEvent =>
({ kind: "ca_session_ended", sessionId: SESS, reason: "complete" } as HarnessEvent);
describe("ChatHandle — usage aggregation", () => {
it("sums input/output tokens across snapshots", async () => {
const { handle } = makeHandle([
{ kind: "ca_usage_snapshot", sessionId: SESS, inputTokens: 100, outputTokens: 30 } as HarnessEvent,
{ kind: "ca_usage_snapshot", sessionId: SESS, inputTokens: 50, outputTokens: 12 } as HarnessEvent,
endedEvent(),
]);
const result = await handle;
expect(result.usage.inputTokens).toBe(150);
expect(result.usage.outputTokens).toBe(42);
});
it("sums cache creation + cache read tokens", async () => {
const { handle } = makeHandle([
{ kind: "ca_usage_snapshot", sessionId: SESS, cacheCreationInputTokens: 200, cacheReadInputTokens: 800 } as HarnessEvent,
{ kind: "ca_usage_snapshot", sessionId: SESS, cacheCreationInputTokens: 50, cacheReadInputTokens: 100 } as HarnessEvent,
endedEvent(),
]);
const result = await handle;
expect(result.usage.cacheCreationInputTokens).toBe(250);
expect(result.usage.cacheReadInputTokens).toBe(900);
});
it("cost: cumulative semantic → takes the MAX across snapshots", async () => {
// claude-agent-sdk emits a running cumulative total; we should latch onto
// the largest value seen (typically the last one).
const { handle } = makeHandle([
{ kind: "ca_usage_snapshot", sessionId: SESS, costUsd: 0.01, costSemantic: "cumulative" } as HarnessEvent,
{ kind: "ca_usage_snapshot", sessionId: SESS, costUsd: 0.04, costSemantic: "cumulative" } as HarnessEvent,
{ kind: "ca_usage_snapshot", sessionId: SESS, costUsd: 0.03, costSemantic: "cumulative" } as HarnessEvent,
endedEvent(),
]);
const result = await handle;
expect(result.usage.costUsd).toBe(0.04);
});
it("cost: delta semantic → SUMs per-message values", async () => {
// gitclaw emits per-message deltas; we should add them up.
const { handle } = makeHandle([
{ kind: "ca_usage_snapshot", sessionId: SESS, costUsd: 0.01, costSemantic: "delta" } as HarnessEvent,
{ kind: "ca_usage_snapshot", sessionId: SESS, costUsd: 0.02, costSemantic: "delta" } as HarnessEvent,
{ kind: "ca_usage_snapshot", sessionId: SESS, costUsd: 0.005, costSemantic: "delta" } as HarnessEvent,
endedEvent(),
]);
const result = await handle;
expect(result.usage.costUsd).toBeCloseTo(0.035, 5);
});
it("cost: undefined semantic is treated as cumulative (defensive)", async () => {
const { handle } = makeHandle([
{ kind: "ca_usage_snapshot", sessionId: SESS, costUsd: 0.07 } as HarnessEvent,
{ kind: "ca_usage_snapshot", sessionId: SESS, costUsd: 0.04 } as HarnessEvent,
endedEvent(),
]);
const result = await handle;
// MAX, not SUM
expect(result.usage.costUsd).toBe(0.07);
});
it("cost: mixed cumulative + delta in one turn → prefer cumulative (no double-count)", async () => {
// Defensive case — shouldn't happen in practice but the SDK has explicit
// handling and a documented preference. Pin the behavior.
const { handle } = makeHandle([
{ kind: "ca_usage_snapshot", sessionId: SESS, costUsd: 0.10, costSemantic: "cumulative" } as HarnessEvent,
{ kind: "ca_usage_snapshot", sessionId: SESS, costUsd: 0.02, costSemantic: "delta" } as HarnessEvent,
endedEvent(),
]);
const result = await handle;
expect(result.usage.costUsd).toBe(0.10);
});
it("no cost snapshots → costUsd is undefined", async () => {
const { handle } = makeHandle([
{ kind: "ca_usage_snapshot", sessionId: SESS, inputTokens: 100, outputTokens: 30 } as HarnessEvent,
endedEvent(),
]);
const result = await handle;
expect(result.usage.costUsd).toBeUndefined();
expect(result.usage.inputTokens).toBe(100);
});
it("getUsage() returns the same rollup mid-stream and after drain", async () => {
const { handle } = makeHandle([
{ kind: "ca_usage_snapshot", sessionId: SESS, inputTokens: 10, costUsd: 0.01, costSemantic: "cumulative" } as HarnessEvent,
{ kind: "sdk_message", sessionId: SESS, payload: {} } as HarnessEvent,
{ kind: "ca_usage_snapshot", sessionId: SESS, inputTokens: 5, costUsd: 0.05, costSemantic: "cumulative" } as HarnessEvent,
endedEvent(),
]);
let midSnapshotInput = 0;
for await (const ev of handle) {
if (ev.kind === "sdk_message") {
midSnapshotInput = handle.getUsage().inputTokens;
}
}
// After the first snapshot, before the second.
expect(midSnapshotInput).toBe(10);
// After drain — second snapshot has been folded in.
expect(handle.getUsage().inputTokens).toBe(15);
expect(handle.getUsage().costUsd).toBe(0.05);
});
});
describe("ChatHandle — dual iteration interface", () => {
it("`for await` yields every event in order", async () => {
const { handle } = makeHandle([
{ kind: "sdk_message", sessionId: SESS, payload: "a" } as HarnessEvent,
{ kind: "sdk_message", sessionId: SESS, payload: "b" } as HarnessEvent,
endedEvent(),
]);
const kinds: string[] = [];
for await (const ev of handle) kinds.push(ev.kind);
expect(kinds).toEqual(["sdk_message", "sdk_message", "ca_session_ended"]);
});
it("`await handle` drains to a ChatResult", async () => {
const { handle } = makeHandle([
{ kind: "sdk_message", sessionId: SESS, payload: { role: "assistant" } } as HarnessEvent,
endedEvent(),
]);
const result = await handle;
expect(result.sessionId).toBe(SESS);
expect(result.messages).toHaveLength(1);
expect(result.ended.reason).toBe("complete");
});
it("result() is memoized — calling twice doesn't re-drain", async () => {
// Build a generator that only yields once; if drain() re-iterated, the
// second call would hang or throw. Memoization should give us the
// cached promise on call #2.
let yields = 0;
async function* once(): AsyncIterable<HarnessEvent> {
if (yields > 0) throw new Error("re-iteration would dead-lock");
yields++;
yield endedEvent();
}
const handle = new ChatHandle({
sessionIdPromise: Promise.resolve(SESS),
events: once(),
harnessUrlPromise: Promise.resolve(HARNESS),
fetchImpl: (async () => new Response("", { status: 200 })) as unknown as typeof fetch,
});
const a = await handle.result();
const b = await handle.result();
expect(a).toBe(b);
});
it("drain() throws if the stream closes without ca_session_ended", async () => {
const { handle } = makeHandle([
{ kind: "sdk_message", sessionId: SESS, payload: "stranded" } as HarnessEvent,
// no ca_session_ended
]);
await expect(handle.result()).rejects.toThrow(/ca_session_ended/);
});
});
describe("ChatHandle — permission callback", () => {
it("calls onPermissionRequest with event args + POSTs the decision", async () => {
const onPermissionRequest = vi.fn(async () => ({ decision: "allow" as const }));
const { handle, fetchImpl } = makeHandle(
[
{
kind: "ca_permission_request",
sessionId: SESS,
callId: "call_42",
toolName: "Bash",
input: { command: "ls" },
risk: "low",
} as HarnessEvent,
endedEvent(),
],
{ onPermissionRequest },
);
await handle.result();
// Hook fired exactly once with the exact event payload (minus discriminator).
expect(onPermissionRequest).toHaveBeenCalledOnce();
expect(onPermissionRequest).toHaveBeenCalledWith(
"call_42",
"Bash",
{ command: "ls" },
"low",
);
// Decision was POSTed back to the harness at the right URL shape.
expect(fetchImpl).toHaveBeenCalledWith(
`${HARNESS}/v1/sessions/${SESS}/permission/call_42`,
expect.objectContaining({
method: "POST",
headers: expect.objectContaining({ "Content-Type": "application/json" }),
}),
);
});
it("default decision is `allow` when no onPermissionRequest is provided", async () => {
const { handle, fetchImpl } = makeHandle([
{
kind: "ca_permission_request",
sessionId: SESS,
callId: "call_1",
toolName: "Read",
input: { path: "/etc/hostname" },
} as HarnessEvent,
endedEvent(),
]);
await handle.result();
// The first call should be the permission POST. Body says allow.
const firstCall = fetchImpl.mock.calls[0];
expect(firstCall[0]).toBe(`${HARNESS}/v1/sessions/${SESS}/permission/call_1`);
const body = JSON.parse(firstCall[1].body);
expect(body.decision).toBe("allow");
});
it("honors a custom deny decision", async () => {
const onPermissionRequest = vi.fn(async () => ({
decision: "deny" as const,
reason: "test refusal",
}));
const { handle, fetchImpl } = makeHandle(
[
{
kind: "ca_permission_request",
sessionId: SESS,
callId: "call_9",
toolName: "Bash",
input: { command: "rm -rf /" },
risk: "destructive",
} as HarnessEvent,
endedEvent(),
],
{ onPermissionRequest },
);
await handle.result();
const body = JSON.parse(fetchImpl.mock.calls[0][1].body);
expect(body.decision).toBe("deny");
expect(body.reason).toBe("test refusal");
});
});
describe("ChatHandle — lifecycle", () => {
it("fires onComplete after ca_session_ended (once)", async () => {
const onComplete = vi.fn();
const { handle } = makeHandle(
[
{ kind: "sdk_message", sessionId: SESS, payload: "x" } as HarnessEvent,
endedEvent(),
],
{ onComplete },
);
await handle.result();
expect(onComplete).toHaveBeenCalledOnce();
});
it("does NOT fire onComplete if stream never ends", async () => {
const onComplete = vi.fn();
const { handle } = makeHandle(
[{ kind: "sdk_message", sessionId: SESS, payload: "x" } as HarnessEvent],
{ onComplete },
);
// Drain throws (no end), but we still verify the hook didn't fire.
await expect(handle.result()).rejects.toThrow();
expect(onComplete).not.toHaveBeenCalled();
});
it("collects every sdk_message payload into result.messages, in order", async () => {
const payloads = [
{ type: "assistant", text: "hi" },
{ type: "tool_use", name: "Bash" },
{ type: "assistant", text: "done" },
];
const { handle } = makeHandle([
...payloads.map((p) => ({ kind: "sdk_message", sessionId: SESS, payload: p }) as HarnessEvent),
endedEvent(),
]);
const result = await handle.result();
expect(result.messages).toEqual(payloads);
});
it("cancel() POSTs /cancel for the right session", async () => {
const { handle, fetchImpl } = makeHandle([endedEvent()]);
await handle.cancel();
expect(fetchImpl).toHaveBeenCalledWith(
`${HARNESS}/v1/sessions/${SESS}/cancel`,
expect.objectContaining({ method: "POST" }),
);
});
});