import { describe, expect, it, vi } from 'vitest'; import { HonoSSEStream } from '../../../source/services/stream/hono-sse-stream.ts'; import { HttpRequestStream } from '../../../source/services/stream/http-request-stream.ts'; import { WSStream } from '../../../source/services/stream/ws-stream.ts'; describe('stream lifecycle observers', (): void => { it('buffers exactly one normal HTTP response', async (): Promise => { const stream = new HttpRequestStream(); await stream.send({ type: 'response', statusCode: 200, body: { ok: true }, }); expect(stream.getResponse()).toEqual({ type: 'response', statusCode: 200, body: { ok: true }, }); await expect(stream.send({ type: 'response', statusCode: 200, body: { second: true }, })).rejects.toThrow('only send one response'); }); it('notifies WebSocket observers registered after remote closure', (): void => { const stream = new WSStream({ send: vi.fn(), close: vi.fn(), readyState: 1, }); const onClose = vi.fn(); stream.markClosed(); stream.onClose(onClose); expect(onClose).toHaveBeenCalledOnce(); }); it('notifies SSE observers registered after local closure', async (): Promise => { const streamApi = { writeSSE: vi.fn(), close: vi.fn(), }; const stream = new HonoSSEStream(streamApi as unknown as ConstructorParameters[0]); const onClose = vi.fn(); await stream.close(); stream.onClose(onClose); await stream.close(); expect(streamApi.close).toHaveBeenCalledOnce(); expect(onClose).toHaveBeenCalledOnce(); }); });