1
0

output-stream.spec.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import { PassThrough } from 'node:stream'
  2. import { afterEach, expect, it, vi } from 'vitest'
  3. import { drainOutput } from '../src/output-stream.ts'
  4. afterEach(() => vi.useRealTimers())
  5. it('waits for queued output and accepts an already ended or absent stream', async () => {
  6. const stream = new PassThrough()
  7. const chunks: string[] = []
  8. stream.on('data', chunk => chunks.push(String(chunk)))
  9. const pending = drainOutput(stream, 1000)
  10. stream.end('last output')
  11. expect(await pending).toBe(true)
  12. expect(chunks).toEqual(['last output'])
  13. expect(await drainOutput(stream, 1000)).toBe(true)
  14. expect(await drainOutput(undefined, 1000)).toBe(true)
  15. })
  16. it('bounds an output descriptor retained after the process exits', async () => {
  17. vi.useFakeTimers()
  18. const stream = new PassThrough()
  19. const pending = drainOutput(stream, 100)
  20. await vi.advanceTimersByTimeAsync(100)
  21. expect(await pending).toBe(false)
  22. stream.destroy()
  23. expect(await drainOutput(stream, 100)).toBe(false)
  24. })
  25. it.each(['close', 'error'])('reports incomplete output on %s', async (event) => {
  26. const stream = new PassThrough()
  27. const pending = drainOutput(stream, 1000)
  28. stream.emit(event, new Error('pipe failed'))
  29. expect(await pending).toBe(false)
  30. stream.destroy()
  31. })