|
| 1 | +import assert from 'node:assert'; |
| 2 | +import { EventEmitter } from 'node:events'; |
| 3 | + |
| 4 | +import { describe, it, beforeEach } from 'vitest'; |
| 5 | + |
| 6 | +import { HttpSSEWriter } from '../src/HttpSSEWriter.ts'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Minimal mock of Node.js ServerResponse for testing HttpSSEWriter. |
| 10 | + * Captures writeHead/write/end calls and emits 'close' on demand. |
| 11 | + */ |
| 12 | +class MockServerResponse extends EventEmitter { |
| 13 | + writtenHead: { statusCode: number; headers: Record<string, string> } | null = null; |
| 14 | + chunks: string[] = []; |
| 15 | + ended = false; |
| 16 | + |
| 17 | + writeHead(statusCode: number, headers: Record<string, string>): void { |
| 18 | + this.writtenHead = { statusCode, headers }; |
| 19 | + } |
| 20 | + |
| 21 | + write(chunk: string): boolean { |
| 22 | + this.chunks.push(chunk); |
| 23 | + return true; |
| 24 | + } |
| 25 | + |
| 26 | + end(): void { |
| 27 | + this.ended = true; |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +describe('test/HttpSSEWriter.test.ts', () => { |
| 32 | + let res: MockServerResponse; |
| 33 | + |
| 34 | + beforeEach(() => { |
| 35 | + res = new MockServerResponse(); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should delay headers until first writeEvent', () => { |
| 39 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 40 | + const writer = new HttpSSEWriter(res as any); |
| 41 | + |
| 42 | + // Headers not sent yet after construction |
| 43 | + assert.equal(res.writtenHead, null); |
| 44 | + assert.equal(res.chunks.length, 0); |
| 45 | + |
| 46 | + writer.writeEvent('test', { foo: 'bar' }); |
| 47 | + |
| 48 | + // Now headers should be sent |
| 49 | + assert.ok(res.writtenHead); |
| 50 | + assert.equal(res.writtenHead.statusCode, 200); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should use lowercase header keys', () => { |
| 54 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 55 | + const writer = new HttpSSEWriter(res as any); |
| 56 | + writer.writeEvent('ping', {}); |
| 57 | + |
| 58 | + assert.ok(res.writtenHead); |
| 59 | + assert.equal(res.writtenHead.headers['content-type'], 'text/event-stream'); |
| 60 | + assert.equal(res.writtenHead.headers['cache-control'], 'no-cache'); |
| 61 | + assert.equal(res.writtenHead.headers['connection'], 'keep-alive'); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should format SSE events correctly', () => { |
| 65 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 66 | + const writer = new HttpSSEWriter(res as any); |
| 67 | + writer.writeEvent('message', { text: 'hello' }); |
| 68 | + |
| 69 | + assert.equal(res.chunks.length, 1); |
| 70 | + assert.equal(res.chunks[0], 'event: message\ndata: {"text":"hello"}\n\n'); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should not write after connection closes', () => { |
| 74 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 75 | + const writer = new HttpSSEWriter(res as any); |
| 76 | + |
| 77 | + // Simulate client disconnect |
| 78 | + res.emit('close'); |
| 79 | + |
| 80 | + assert.equal(writer.closed, true); |
| 81 | + writer.writeEvent('late', { data: 'ignored' }); |
| 82 | + |
| 83 | + // No headers sent, no chunks written |
| 84 | + assert.equal(res.writtenHead, null); |
| 85 | + assert.equal(res.chunks.length, 0); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should trigger onClose callbacks when connection closes', () => { |
| 89 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 90 | + const writer = new HttpSSEWriter(res as any); |
| 91 | + const calls: number[] = []; |
| 92 | + |
| 93 | + writer.onClose(() => calls.push(1)); |
| 94 | + writer.onClose(() => calls.push(2)); |
| 95 | + |
| 96 | + res.emit('close'); |
| 97 | + |
| 98 | + assert.deepStrictEqual(calls, [1, 2]); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should handle end() idempotently', () => { |
| 102 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 103 | + const writer = new HttpSSEWriter(res as any); |
| 104 | + |
| 105 | + assert.equal(writer.closed, false); |
| 106 | + |
| 107 | + writer.end(); |
| 108 | + assert.equal(writer.closed, true); |
| 109 | + assert.equal(res.ended, true); |
| 110 | + |
| 111 | + // Reset flag to verify second end() doesn't call res.end() again |
| 112 | + res.ended = false; |
| 113 | + writer.end(); |
| 114 | + assert.equal(res.ended, false); // Not called again |
| 115 | + }); |
| 116 | + |
| 117 | + it('should write multiple events sequentially', () => { |
| 118 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 119 | + const writer = new HttpSSEWriter(res as any); |
| 120 | + |
| 121 | + writer.writeEvent('event1', { n: 1 }); |
| 122 | + writer.writeEvent('event2', { n: 2 }); |
| 123 | + writer.writeEvent('event3', { n: 3 }); |
| 124 | + |
| 125 | + assert.equal(res.chunks.length, 3); |
| 126 | + assert.equal(res.chunks[0], 'event: event1\ndata: {"n":1}\n\n'); |
| 127 | + assert.equal(res.chunks[1], 'event: event2\ndata: {"n":2}\n\n'); |
| 128 | + assert.equal(res.chunks[2], 'event: event3\ndata: {"n":3}\n\n'); |
| 129 | + |
| 130 | + // Headers sent only once |
| 131 | + assert.ok(res.writtenHead); |
| 132 | + }); |
| 133 | + |
| 134 | + it('should start with closed=false', () => { |
| 135 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 136 | + const writer = new HttpSSEWriter(res as any); |
| 137 | + assert.equal(writer.closed, false); |
| 138 | + }); |
| 139 | +}); |
0 commit comments