Open History to see the last commit
1import { expect, test } from './fixtures'
2
3/**
4 * Drives `/api/compaction-usage-wire`, which runs `chat()` with a capturing
5 * `fetch` and returns the request bodies, the `onCompact` reasons, and the
6 * log records that compaction wrote.
7 */
8interface WireResult {
9 ok: boolean
10 error?: string
11 bodies: Array<unknown>
12 reasons: Array<string>
13 errors: Array<string | undefined>
14 records: Array<{ type: string; reason?: string }>
15 folded?: unknown
16}
17
18async function runCase(
19 request: { post: (url: string) => Promise<{ json: () => Promise<unknown> }> },
20 name: string,
21): Promise<WireResult> {
22 const response = await request.post(`/api/compaction-usage-wire?case=${name}`)
23 const result = (await response.json()) as WireResult
24 if (!result.ok) throw new Error(`Route failed: ${result.error}`)
25 return result
26}
27
28test.describe('withCompaction: usage, compactNext, overflow, durable, background', () => {
29 test('countTokens usage compacts when the reported usage passes maxTokens', async ({
30 request,
31 }) => {
32 const result = await runCase(request, 'usage')
33
34 // The first request is small by the estimate, so it is not compacted.
35 expect(JSON.stringify(result.bodies[0])).toContain('SECRET_ALPHA_ONE')
36 // The first call reported 500 input tokens, so the next one compacts.
37 const second = JSON.stringify(result.bodies[1])
38 expect(second).not.toContain('SECRET_ALPHA_ONE')
39 expect(second).toContain('omitted to save context')
40 expect(second).toContain('KEEP_ME_LAST')
41 expect(result.reasons).toEqual(['threshold'])
42 })
43
44 test('compactNext compacts the next call under maxTokens with auto off', async ({
45 request,
46 }) => {
47 const result = await runCase(request, 'compact-next')
48
49 const first = JSON.stringify(result.bodies[0])
50 expect(first).not.toContain('SECRET_ALPHA_ONE')
51 expect(first).toContain('omitted to save context')
52 expect(result.reasons).toEqual(['forced'])
53 })
54
55 test('an overflow compacts and calls the model again in the same run', async ({
56 request,
57 }) => {
58 const result = await runCase(request, 'overflow')
59
60 expect(result.bodies).toHaveLength(2)
61 expect(JSON.stringify(result.bodies[0])).toContain('SECRET_ALPHA_ONE')
62 const retry = JSON.stringify(result.bodies[1])
63 expect(retry).not.toContain('SECRET_ALPHA_ONE')
64 expect(retry).toContain('KEEP_ME_LAST')
65 expect(result.reasons).toEqual(['forced'])
66 // The failed call never reaches the caller.
67 expect(result.errors).toEqual([])
68 })
69
70 test('durable writes a compaction record that folds to the sent list', async ({
71 request,
72 }) => {
73 const result = await runCase(request, 'durable')
74
75 expect(result.records).toHaveLength(1)
76 expect(result.records[0]).toMatchObject({
77 type: 'tanstack.compaction',
78 reason: 'threshold',
79 })
80 const folded = JSON.stringify(result.folded)
81 expect(folded).toContain('KEEP_ME_LAST')
82 expect(folded).not.toContain('SECRET_ALPHA_ONE')
83 expect(JSON.stringify(result.bodies[0])).not.toContain('SECRET_ALPHA_ONE')
84 })
85
86 test('background prepares the summary in one run and applies it in the next', async ({
87 request,
88 }) => {
89 const result = await runCase(request, 'background')
90
91 // The first run does not wait for the summary.
92 expect(JSON.stringify(result.bodies[0])).toContain('OLD_DETAIL')
93 // The second run starts from the ready summary.
94 const second = JSON.stringify(result.bodies[1])
95 expect(second).toContain('EARLIER_SUMMARY')
96 expect(second).not.toContain('OLD_DETAIL')
97 expect(result.reasons).toEqual(['background'])
98 })
99})