-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathchat-ui.test.ts
More file actions
278 lines (244 loc) · 8.99 KB
/
chat-ui.test.ts
File metadata and controls
278 lines (244 loc) · 8.99 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
/* Copyright 2026 Marimo. All rights reserved. */
import type { UIMessageChunk } from "ai";
import { describe, expect, it, vi } from "vitest";
import { routeIncomingChatChunk } from "../chat-ui";
/**
* The stale-chunk filter prevents chunks from an aborted run from being enqueued into a new run's stream.
*
* It triggers when:
* 1. User sends a prompt (request_id = OLD), kernel starts emitting chunks
* 2. User clicks Stop — frontend tears down its controller, fires cancel_prompt
* 3. Kernel hasn't received the cancel yet and is still emitting chunks
* 4. User sends a new prompt (request_id = NEW), new controller opens
* 5. Late chunks tagged OLD arrive after NEW's controller is in place
*/
const makeChunk = (opts: {
messageId: string;
content: unknown;
isFinal?: boolean;
}): Parameters<typeof routeIncomingChatChunk>[0] => ({
type: "stream_chunk",
message_id: opts.messageId,
content: opts.content as UIMessageChunk | null,
is_final: opts.isFinal ?? false,
});
const makeRefs = () => ({
controllerRef: {
current: null as ReadableStreamDefaultController<UIMessageChunk> | null,
},
activeRequestIdRef: { current: null as string | null },
});
const makeMockController = () => {
return {
enqueue: vi.fn(),
close: vi.fn(),
error: vi.fn(),
desiredSize: 0,
} as unknown as ReadableStreamDefaultController<UIMessageChunk> & {
enqueue: ReturnType<typeof vi.fn>;
close: ReturnType<typeof vi.fn>;
};
};
describe("routeIncomingChatChunk", () => {
it("drops chunks when there is no active controller", () => {
const refs = makeRefs();
const result = routeIncomingChatChunk(
makeChunk({
messageId: "req-A",
content: { type: "text-delta", id: "t1", delta: "hi" },
}),
refs,
);
expect(result).toBe("dropped-no-controller");
});
it("enqueues chunks that match the active request_id", () => {
const refs = makeRefs();
const controller = makeMockController();
refs.controllerRef.current = controller;
refs.activeRequestIdRef.current = "req-A";
const chunk = { type: "text-delta", id: "t1", delta: "hi" } as const;
const result = routeIncomingChatChunk(
makeChunk({ messageId: "req-A", content: chunk }),
refs,
);
expect(result).toBe("enqueued");
expect(controller.enqueue).toHaveBeenCalledWith(chunk);
expect(controller.close).not.toHaveBeenCalled();
});
it("closes the controller and clears refs on is_final", () => {
const refs = makeRefs();
const controller = makeMockController();
refs.controllerRef.current = controller;
refs.activeRequestIdRef.current = "req-A";
const result = routeIncomingChatChunk(
makeChunk({ messageId: "req-A", content: null, isFinal: true }),
refs,
);
expect(result).toBe("closed");
expect(controller.close).toHaveBeenCalledTimes(1);
expect(refs.controllerRef.current).toBeNull();
expect(refs.activeRequestIdRef.current).toBeNull();
});
it("drops chunks whose message_id does not match the active run", () => {
// Simulates the bug: kernel hasn't received cancel for OLD yet but the
// user has already started a NEW run. A reasoning-delta for OLD arrives
// here; it must not be enqueued into NEW's stream.
const refs = makeRefs();
const controller = makeMockController();
refs.controllerRef.current = controller;
refs.activeRequestIdRef.current = "req-NEW";
const staleChunk = {
type: "reasoning-delta",
id: "r-old",
delta: "...",
} as const;
const result = routeIncomingChatChunk(
makeChunk({ messageId: "req-OLD", content: staleChunk }),
refs,
);
expect(result).toBe("dropped-stale");
expect(controller.enqueue).not.toHaveBeenCalled();
expect(controller.close).not.toHaveBeenCalled();
expect(refs.activeRequestIdRef.current).toBe("req-NEW");
});
it("drops is_final from a stale run without closing the active stream", () => {
// Belt-and-suspenders: an `is_final` for OLD that races in after NEW
// started must not tear down NEW's controller.
const refs = makeRefs();
const controller = makeMockController();
refs.controllerRef.current = controller;
refs.activeRequestIdRef.current = "req-NEW";
const result = routeIncomingChatChunk(
makeChunk({ messageId: "req-OLD", content: null, isFinal: true }),
refs,
);
expect(result).toBe("dropped-stale");
expect(controller.close).not.toHaveBeenCalled();
expect(refs.controllerRef.current).toBe(controller);
expect(refs.activeRequestIdRef.current).toBe("req-NEW");
});
it("forwards reasoning-start/delta/end sequences when ids match", () => {
// Walks the canonical happy path for a reasoning stream end-to-end.
const refs = makeRefs();
const controller = makeMockController();
refs.controllerRef.current = controller;
refs.activeRequestIdRef.current = "req-A";
const sequence = [
{ type: "reasoning-start", id: "r1" },
{ type: "reasoning-delta", id: "r1", delta: "thinking" },
{ type: "reasoning-end", id: "r1" },
] as const;
for (const chunk of sequence) {
const result = routeIncomingChatChunk(
makeChunk({ messageId: "req-A", content: chunk }),
refs,
);
expect(result).toBe("enqueued");
}
expect(controller.enqueue).toHaveBeenCalledTimes(3);
expect(controller.enqueue).toHaveBeenNthCalledWith(1, sequence[0]);
expect(controller.enqueue).toHaveBeenNthCalledWith(2, sequence[1]);
expect(controller.enqueue).toHaveBeenNthCalledWith(3, sequence[2]);
});
it(
"drops stale reasoning-delta after Stop → new run sequence " +
"(regression for missing reasoning part error)",
() => {
// Full scenario: A runs, A is stopped, B starts, A's late chunk arrives.
const refs = makeRefs();
// 1. Run A starts: controller A active.
const controllerA = makeMockController();
refs.controllerRef.current = controllerA;
refs.activeRequestIdRef.current = "req-A";
// First reasoning chunks for A flow through.
routeIncomingChatChunk(
makeChunk({
messageId: "req-A",
content: { type: "reasoning-start", id: "rA" },
}),
refs,
);
routeIncomingChatChunk(
makeChunk({
messageId: "req-A",
content: {
type: "reasoning-delta",
id: "rA",
delta: "thinking",
},
}),
refs,
);
expect(controllerA.enqueue).toHaveBeenCalledTimes(2);
// 2. User clicks Stop: abort handler clears refs (simulated).
refs.controllerRef.current = null;
refs.activeRequestIdRef.current = null;
// A late chunk for A arrives in this window — must be a no-op.
const between = routeIncomingChatChunk(
makeChunk({
messageId: "req-A",
content: {
type: "reasoning-delta",
id: "rA",
delta: "leftover",
},
}),
refs,
);
expect(between).toBe("dropped-no-controller");
// 3. User sends Run B: new controller, new active id.
const controllerB = makeMockController();
refs.controllerRef.current = controllerB;
refs.activeRequestIdRef.current = "req-B";
// 4. Another late chunk for A arrives AFTER B opened. This is the
// case that previously threw `Received reasoning-delta for missing
// reasoning part with ID "rA"` in the SDK parser.
const stale = routeIncomingChatChunk(
makeChunk({
messageId: "req-A",
content: {
type: "reasoning-delta",
id: "rA",
delta: "still leaking",
},
}),
refs,
);
expect(stale).toBe("dropped-stale");
expect(controllerB.enqueue).not.toHaveBeenCalled();
// 5. B's own chunks flow normally.
routeIncomingChatChunk(
makeChunk({
messageId: "req-B",
content: { type: "reasoning-start", id: "rB" },
}),
refs,
);
routeIncomingChatChunk(
makeChunk({
messageId: "req-B",
content: { type: "reasoning-delta", id: "rB", delta: "fresh" },
}),
refs,
);
expect(controllerB.enqueue).toHaveBeenCalledTimes(2);
},
);
it("enqueues content alongside is_final and then closes", () => {
// Sanity: a single chunk that carries both `content` and `is_final` (rare
// but legal — backend may bundle final content with the terminator)
// should enqueue then close.
const refs = makeRefs();
const controller = makeMockController();
refs.controllerRef.current = controller;
refs.activeRequestIdRef.current = "req-A";
const chunk = { type: "text-delta", id: "t1", delta: "bye" } as const;
const result = routeIncomingChatChunk(
makeChunk({ messageId: "req-A", content: chunk, isFinal: true }),
refs,
);
expect(result).toBe("closed");
expect(controller.enqueue).toHaveBeenCalledWith(chunk);
expect(controller.close).toHaveBeenCalledTimes(1);
});
});