Files
opsxshell/test/utils/terminalPipeline.test.ts

60 lines
1.6 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest'
import { TerminalPipeline } from '../../src/utils/terminalPipeline'
describe('TerminalPipeline', () => {
it('routes input and reports output bytes through one boundary', () => {
const input = vi.fn()
const write = vi.fn()
const onOutputBytes = vi.fn()
const scheduled: Array<() => void> = []
const pipeline = new TerminalPipeline({
input,
output: {
write,
schedule: (callback) => {
scheduled.push(callback)
return scheduled.length
},
cancel: vi.fn(),
},
onOutputBytes,
})
pipeline.input('date\r')
pipeline.output(Uint8Array.from([0x68, 0x69]))
scheduled.shift()?.()
expect(input).toHaveBeenCalledWith('date\r')
expect(onOutputBytes).toHaveBeenCalledWith(2)
expect(write).toHaveBeenCalledWith('hi')
})
it('keeps split UTF-8 output intact and catches async input failures', async () => {
const write = vi.fn()
const onError = vi.fn()
const scheduled: Array<() => void> = []
const pipeline = new TerminalPipeline({
input: () => Promise.reject(new Error('closed')),
output: {
write,
schedule: (callback) => {
scheduled.push(callback)
return scheduled.length
},
cancel: vi.fn(),
},
onError,
})
pipeline.input('x')
pipeline.output(Uint8Array.from([0xe4, 0xb8]))
pipeline.output(Uint8Array.from([0xad]))
scheduled.shift()?.()
await Promise.resolve()
expect(write).toHaveBeenCalledWith('中')
expect(onError).toHaveBeenCalledOnce()
})
})