| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- 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)
- })
- })
|