62 lines
1.9 KiB
TypeScript
62 lines
1.9 KiB
TypeScript
import { describe, expect, it, vi } from 'vitest'
|
|
import { TerminalOutputBuffer } from '../../src/utils/terminalOutputBuffer'
|
|
|
|
function harness(options: { maxChunkChars?: number; maxPendingChars?: number } = {}) {
|
|
const callbacks: Array<() => void> = []
|
|
const write = vi.fn()
|
|
const buffer = new TerminalOutputBuffer({
|
|
write,
|
|
schedule: (callback) => {
|
|
callbacks.push(callback)
|
|
return callbacks.length
|
|
},
|
|
cancel: vi.fn(),
|
|
...options,
|
|
})
|
|
return { buffer, callbacks, write }
|
|
}
|
|
|
|
describe('TerminalOutputBuffer', () => {
|
|
it('coalesces events into one write per frame', () => {
|
|
const { buffer, callbacks, write } = harness()
|
|
buffer.enqueue('one')
|
|
buffer.enqueue('two')
|
|
|
|
expect(callbacks).toHaveLength(1)
|
|
callbacks.shift()?.()
|
|
expect(write).toHaveBeenCalledWith('onetwo')
|
|
})
|
|
|
|
it('preserves UTF-8 characters split across native events', () => {
|
|
const { buffer, callbacks, write } = harness()
|
|
const bytes = new TextEncoder().encode('中文')
|
|
buffer.enqueue(bytes.slice(0, 2))
|
|
buffer.enqueue(bytes.slice(2, 4))
|
|
buffer.enqueue(bytes.slice(4))
|
|
callbacks.shift()?.()
|
|
|
|
expect(write).toHaveBeenCalledWith('中文')
|
|
})
|
|
|
|
it('limits work per frame and schedules remaining output', () => {
|
|
const { buffer, callbacks, write } = harness({ maxChunkChars: 4 })
|
|
buffer.enqueue('abcdefgh')
|
|
callbacks.shift()?.()
|
|
expect(write).toHaveBeenLastCalledWith('abcd')
|
|
expect(callbacks).toHaveLength(1)
|
|
callbacks.shift()?.()
|
|
expect(write).toHaveBeenLastCalledWith('efgh')
|
|
})
|
|
|
|
it('bounds queued output and reports truncation', () => {
|
|
const { buffer, callbacks, write } = harness({ maxPendingChars: 4 })
|
|
buffer.enqueue('abcdefgh')
|
|
callbacks.shift()?.()
|
|
|
|
const output = write.mock.calls[0][0] as string
|
|
expect(output).toContain('output truncated')
|
|
expect(output).toContain('efgh')
|
|
expect(output).not.toContain('abcd')
|
|
})
|
|
})
|