Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

compaction-wire.spec.ts

64 lines2.4 KB

1import { expect, test } from './fixtures'
2​
3/**
4 * Wire-format verification for `withCompaction`. Drives `/api/compaction-wire`,
5 * which sends a long history through `chat()` with a small `maxTokens` and
6 * captures the outgoing SDK request. The captured body must show the oldest
7 * message evicted, the compaction note injected, and the recent tail preserved.
8 */
9test.describe('withCompaction — wire format', () => {
10 test('evicts the old head, keeps the recent tail, injects a note', async ({
11 request,
12 }) => {
13 const response = await request.post('/api/compaction-wire')
14 expect(response.ok()).toBe(true)
15 const result = (await response.json()) as {
16 ok: boolean
17 error?: string
18 firstRequestBody: unknown
19 secondRequestBody: unknown
20 canonicalMessages: unknown
21 compactionCount: number
22 }
23 if (!result.ok) throw new Error(`Route failed: ${result.error}`)
24​
25 const wire = JSON.stringify(result.firstRequestBody)
26 // Recent tail is preserved verbatim.
27 expect(wire).toContain('KEEP_ME_LAST')
28 // The dropped head was replaced by the eviction note.
29 expect(wire).toContain('omitted to save context')
30 // The oldest message is gone.
31 expect(wire).not.toContain('SECRET_ALPHA_ONE')
32​
33 // Persistence keeps the canonical transcript, while a later request reuses
34 // the compacted checkpoint without compacting the same prefix again.
35 expect(JSON.stringify(result.canonicalMessages)).toContain(
36 'SECRET_ALPHA_ONE',
37 )
38 expect(JSON.stringify(result.secondRequestBody)).not.toContain(
39 'SECRET_ALPHA_ONE',
40 )
41 expect(result.compactionCount).toBe(1)
42 })
43​
44 test('clearToolResults stubs old tool output and keeps the recent one', async ({
45 request,
46 }) => {
47 const response = await request.post('/api/compaction-wire?strategy=clear')
48 expect(response.ok()).toBe(true)
49 const result = (await response.json()) as {
50 ok: boolean
51 error?: string
52 firstRequestBody: unknown
53 }
54 if (!result.ok) throw new Error(`Route failed: ${result.error}`)
55​
56 const wire = JSON.stringify(result.firstRequestBody)
57 // The most recent tool result is preserved verbatim.
58 expect(wire).toContain('KEEP_TOOL_BETA')
59 // The old tool result content is replaced by the stub.
60 expect(wire).toContain('tool output cleared')
61 // The old tool result content is gone.
62 expect(wire).not.toContain('SECRET_TOOL_ALPHA')
63 })
64})