返回 DeepSeek-Reasonix
chat-content-loader.test.ts
根目录 / desktop / frontend / src / __tests__ / chat-content-loader.test.ts
1 import assert from "node:assert/strict";
2 import { ChatContentLoader } from "../lib/chatContentLoader";
3 import type { Item } from "../lib/useController";
4 const waits: Array<(text: string) => void> = [];
5 let count = 0, peak = 0;
6 const loader = new ChatContentLoader(undefined, async () => {
7 peak = Math.max(peak, ++count);
8 const result = await new Promise<string>(resolve => waits.push(resolve)); count--; return result;
9 });
10 const item = (index: number): Item => ({ kind: "assistant", id: `a${index}`, text: "preview", reasoning: "thought", streaming: false });
11 const requests = Array.from({ length: 8 }, (_, index) => loader.load(item(index), "content"));
12 assert.equal(loader.load(item(0), "content"), requests[0], "identical requests share a promise");
13 assert.equal(waits.length, 2, "only two requests start for one session");
14 for (let index = 0; index < 8; index++) { waits[index](String(index)); await requests[index]; await new Promise(resolve => setImmediate(resolve)); }
15 assert.equal(peak, 2);
16 const stale = loader.load(item(0), "reasoning");
17 const rejection = assert.rejects(stale, /closed/);
18 loader.dispose(); loader.activate();
19 const fresh = loader.load(item(0), "reasoning");
20 waits[8]("old"); await rejection;
21 assert.equal(loader.load(item(0), "reasoning"), fresh, "old cleanup cannot remove a new generation request");
22 waits[9]("new"); assert.equal(await fresh, "new");
23 const oldRevision = loader.load(item(1), "content");
24 const newRevision = loader.load({ ...item(1), text: "changed" } as Item, "content");
25 assert.notEqual(newRevision, oldRevision, "different source revisions cannot reuse the old content request");
26 waits[10]("old content");
27 assert.equal(await oldRevision, "old content");
28 await new Promise(resolve => setImmediate(resolve));
29 waits[11]("new content");
30 assert.equal(await newRevision, "new content");
31 loader.dispose();
32 await new Promise(resolve => setImmediate(resolve));
33
34 const crossWaits: Array<() => void> = [];
35 let crossStarted = 0, globalActive = 0, globalPeak = 0, leftActive = 0, leftPeak = 0, rightActive = 0, rightPeak = 0;
36 const crossResolver = (side: "left" | "right") => async () => {
37 crossStarted += 1;
38 globalPeak = Math.max(globalPeak, ++globalActive);
39 if (side === "left") leftPeak = Math.max(leftPeak, ++leftActive); else rightPeak = Math.max(rightPeak, ++rightActive);
40 await new Promise<void>(resolve => crossWaits.push(resolve));
41 globalActive -= 1;
42 if (side === "left") leftActive -= 1; else rightActive -= 1;
43 return side;
44 };
45 const left = new ChatContentLoader("left", crossResolver("left"));
46 const right = new ChatContentLoader("right", crossResolver("right"));
47 const crossRequests = [
48 ...Array.from({ length: 4 }, (_, index) => left.load(item(20 + index), "content")),
49 ...Array.from({ length: 4 }, (_, index) => right.load(item(30 + index), "content")),
50 ];
51 assert.equal(crossWaits.length, 4, "the application starts at most four body reads");
52 assert.equal(leftPeak, 2, "left session stays at its two-read cap");
53 assert.equal(rightPeak, 2, "right session stays at its two-read cap");
54 while (crossStarted < crossRequests.length) {
55 crossWaits.shift()?.();
56 await new Promise(resolve => setImmediate(resolve));
57 }
58 for (const release of crossWaits.splice(0)) release();
59 await Promise.all(crossRequests);
60 assert.equal(globalPeak, 4, "body reads share the four-request application cap");
61 left.dispose(); right.dispose();
62 console.log("chat content: global/session concurrency, deduplication, generation and cleanup races passed");
63
63 lines TYPESCRIPT