Files
claude-code-haha/adapters/common/__tests__/message-buffer.test.ts
T
程序员阿江(Relakkes) bde985d202 chore: drop 27 imports nothing references in src and adapters
Found by widening scripts/pr/dead-imports.ts past src/server/ws once its
lexer stopped desyncing. None of these bindings appear anywhere in their own
file; several are the tail of a move that left the import behind, and
useMergedTools still lists replBridgeEnabled in a dependency array after the
useAppState call that set it was stubbed out.

Nine statements lost their last binding. Each target was checked for a
top-level side effect first: node:fs, node:fs/promises and bun:bundle have
none, ws/events.js is a type-only import that never emits, AppState.tsx,
model/model.ts and markdown-style.ts declare only, and utils/config.ts does
register a cleanup but is imported by 157 other files in src, so
attachments.ts dropping it cannot unregister anything.

No behavior change intended and none observed: src is 2995 pass / 37 skip /
30 fail across 273 files before and after, with the 30 failures identical
test for test, and adapters is 437 pass / 0 fail both times. The 30 are
pre-existing and unrelated.
2026-08-04 20:42:27 +08:00

118 lines
3.1 KiB
TypeScript

import { describe, it, expect } from 'bun:test'
import { MessageBuffer } from '../message-buffer.js'
describe('MessageBuffer', () => {
it('accumulates text and flushes on complete', async () => {
const flushed: Array<{ text: string; isComplete: boolean }> = []
const buf = new MessageBuffer(
(text, isComplete) => { flushed.push({ text, isComplete }) },
500, // 500ms interval
1000, // 1000 char threshold
)
buf.append('Hello ')
buf.append('World')
await buf.complete()
expect(flushed.length).toBeGreaterThanOrEqual(1)
const allText = flushed.map((f) => f.text).join('')
expect(allText).toBe('Hello World')
// Last flush should be marked complete
expect(flushed[flushed.length - 1]!.isComplete).toBe(true)
})
it('flushes when character threshold is reached', async () => {
const flushed: string[] = []
const buf = new MessageBuffer(
(text) => { flushed.push(text) },
10000, // very long interval (won't trigger)
10, // 10 char threshold
)
buf.append('12345678901') // 11 chars > threshold
// Wait for microtask
await new Promise((r) => setTimeout(r, 10))
expect(flushed.length).toBeGreaterThanOrEqual(1)
buf.reset()
})
it('flushes on timer interval', async () => {
const flushed: string[] = []
const buf = new MessageBuffer(
(text) => { flushed.push(text) },
50, // 50ms interval
1000,
)
buf.append('hi')
// Wait for timer
await new Promise((r) => setTimeout(r, 80))
expect(flushed).toContain('hi')
buf.reset()
})
it('does not flush empty buffer on complete', async () => {
const flushed: string[] = []
const buf = new MessageBuffer(
(text) => { flushed.push(text) },
)
await buf.complete()
expect(flushed.length).toBe(0)
})
it('waits for an in-flight flush before complete resolves', async () => {
let releaseFlush!: () => void
let flushStarted = false
const flushed: Array<{ text: string; isComplete: boolean }> = []
const buf = new MessageBuffer(
async (text, isComplete) => {
flushStarted = true
flushed.push({ text, isComplete })
await new Promise<void>((resolve) => {
releaseFlush = resolve
})
},
10000,
3,
)
buf.append('abcd')
await new Promise((resolve) => setTimeout(resolve, 0))
expect(flushStarted).toBe(true)
let completeResolved = false
const completing = buf.complete().then(() => {
completeResolved = true
})
await new Promise((resolve) => setTimeout(resolve, 0))
expect(completeResolved).toBe(false)
releaseFlush()
await completing
expect(completeResolved).toBe(true)
expect(flushed).toEqual([{ text: 'abcd', isComplete: false }])
})
it('resets properly between messages', async () => {
const flushed: string[] = []
const buf = new MessageBuffer(
(text) => { flushed.push(text) },
500,
1000,
)
buf.append('first')
buf.reset()
buf.append('second')
await buf.complete()
const allText = flushed.map((f) => f).join('')
expect(allText).toBe('second')
})
})