65 lines
1.4 KiB
TypeScript
65 lines
1.4 KiB
TypeScript
import { describe, expect, test } from "bun:test"
|
|
import { terminalWriter } from "./terminal-writer"
|
|
|
|
describe("terminalWriter", () => {
|
|
test("buffers and flushes once per schedule", () => {
|
|
const calls: string[] = []
|
|
const scheduled: VoidFunction[] = []
|
|
const writer = terminalWriter(
|
|
(data, done) => {
|
|
calls.push(data)
|
|
done?.()
|
|
},
|
|
(flush) => scheduled.push(flush),
|
|
)
|
|
|
|
writer.push("a")
|
|
writer.push("b")
|
|
writer.push("c")
|
|
|
|
expect(calls).toEqual([])
|
|
expect(scheduled).toHaveLength(1)
|
|
|
|
scheduled[0]?.()
|
|
expect(calls).toEqual(["abc"])
|
|
})
|
|
|
|
test("flush is a no-op when empty", () => {
|
|
const calls: string[] = []
|
|
const writer = terminalWriter(
|
|
(data, done) => {
|
|
calls.push(data)
|
|
done?.()
|
|
},
|
|
(flush) => flush(),
|
|
)
|
|
writer.flush()
|
|
expect(calls).toEqual([])
|
|
})
|
|
|
|
test("flush waits for pending write completion", () => {
|
|
const calls: string[] = []
|
|
let done: VoidFunction | undefined
|
|
const writer = terminalWriter(
|
|
(data, finish) => {
|
|
calls.push(data)
|
|
done = finish
|
|
},
|
|
(flush) => flush(),
|
|
)
|
|
|
|
writer.push("a")
|
|
|
|
let settled = false
|
|
writer.flush(() => {
|
|
settled = true
|
|
})
|
|
|
|
expect(calls).toEqual(["a"])
|
|
expect(settled).toBe(false)
|
|
|
|
done?.()
|
|
expect(settled).toBe(true)
|
|
})
|
|
})
|