Skip to content
Myr

stream.test.ts

This file

Path

src/utils/stream.test.ts

121 lines · 4.1 KiB · TypeScript

Last changed by

Not known.

History of this file

Take it

The walk stopped after 200 commits: lines older than 82bdc4d point at it.

4.9.0Yusuke Wadabefore adoption82bdc4d

lines 1–121why, one step back
1import { StreamingApi } from './stream'
23describe('StreamingApi', () => {4 it('write(string)', async () => {5 const { readable, writable } = new TransformStream()6 const api = new StreamingApi(writable, readable)7 const reader = api.responseReadable.getReader()8 api.write('foo')9 expect((await reader.read()).value).toEqual(new TextEncoder().encode('foo'))10 api.write('bar')11 expect((await reader.read()).value).toEqual(new TextEncoder().encode('bar'))12 })1314 it('write(Uint8Array)', async () => {15 const { readable, writable } = new TransformStream()16 const api = new StreamingApi(writable, readable)17 const reader = api.responseReadable.getReader()18 api.write(new Uint8Array([1, 2, 3]))19 expect((await reader.read()).value).toEqual(new Uint8Array([1, 2, 3]))20 api.write(new Uint8Array([4, 5, 6]))21 expect((await reader.read()).value).toEqual(new Uint8Array([4, 5, 6]))22 })2324 it('writeln(string)', async () => {25 const { readable, writable } = new TransformStream()26 const api = new StreamingApi(writable, readable)27 const reader = api.responseReadable.getReader()28 api.writeln('foo')29 expect((await reader.read()).value).toEqual(new TextEncoder().encode('foo\n'))30 api.writeln('bar')31 expect((await reader.read()).value).toEqual(new TextEncoder().encode('bar\n'))32 })3334 it('pipe()', async () => {35 const { readable: senderReadable, writable: senderWritable } = new TransformStream()3637 // send data to readable in other scope38 ;(async () => {39 const writer = senderWritable.getWriter()40 await writer.write(new TextEncoder().encode('foo'))41 await writer.write(new TextEncoder().encode('bar'))42 // await writer.close()43 })()4445 const { readable: receiverReadable, writable: receiverWritable } = new TransformStream()4647 const api = new StreamingApi(receiverWritable, receiverReadable)4849 // pipe readable to api in other scope50 ;(async () => {51 await api.pipe(senderReadable)52 })()5354 // read data from api55 const reader = api.responseReadable.getReader()56 expect((await reader.read()).value).toEqual(new TextEncoder().encode('foo'))57 expect((await reader.read()).value).toEqual(new TextEncoder().encode('bar'))58 })5960 it('close()', async () => {61 const { readable, writable } = new TransformStream()62 const api = new StreamingApi(writable, readable)63 const reader = api.responseReadable.getReader()64 await api.close()65 expect((await reader.read()).done).toBe(true)66 })6768 it('should not throw an error in write()', async () => {69 const { readable, writable } = new TransformStream()70 const api = new StreamingApi(writable, readable)71 await api.close()72 const write = () => api.write('foo')73 expect(write).not.toThrow()74 })7576 it('should not throw an error in close()', async () => {77 const { readable, writable } = new TransformStream()78 const api = new StreamingApi(writable, readable)79 const close = async () => {80 await api.close()81 await api.close()82 }83 expect(close).not.toThrow()84 })8586 it('onAbort()', async () => {87 const { readable, writable } = new TransformStream()88 const handleAbort1 = vi.fn()89 const handleAbort2 = vi.fn()90 const api = new StreamingApi(writable, readable)91 api.onAbort(handleAbort1)92 api.onAbort(handleAbort2)93 expect(handleAbort1).not.toBeCalled()94 expect(handleAbort2).not.toBeCalled()95 await api.responseReadable.cancel()96 expect(handleAbort1).toBeCalled()97 expect(handleAbort2).toBeCalled()98 })99100 it('abort()', async () => {101 const { readable, writable } = new TransformStream()102 const handleAbort1 = vi.fn()103 const handleAbort2 = vi.fn()104 const api = new StreamingApi(writable, readable)105 api.onAbort(handleAbort1)106 api.onAbort(handleAbort2)107 expect(handleAbort1).not.toBeCalled()108 expect(handleAbort2).not.toBeCalled()109 expect(api.aborted).toBe(false)110111 api.abort()112 expect(handleAbort1).toHaveBeenCalledOnce()113 expect(handleAbort2).toHaveBeenCalledOnce()114 expect(api.aborted).toBe(true)115116 api.abort()117 expect(handleAbort1).toHaveBeenCalledOnce()118 expect(handleAbort2).toHaveBeenCalledOnce()119 expect(api.aborted).toBe(true)120 })121})