SSE Session and examples

This commit is contained in:
2026-09-17 07:51:48 +00:00
committed by Kuldeep
parent e96ab6457b
commit 752cda0706
16 changed files with 2306 additions and 4 deletions
+9
View File
@@ -101,6 +101,9 @@ const testEventEmitterEmitContinuesAfterListenerThrows = (): void => {
const emitter = new EventEmitter<TestEvents>();
const secondListener = vi.fn();
// Spy on the console.error method
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
emitter.on('message', (): void => {
throw new Error('listener failure');
});
@@ -110,6 +113,12 @@ const testEventEmitterEmitContinuesAfterListenerThrows = (): void => {
expect(secondListener).toHaveBeenCalledOnce();
expect(secondListener).toHaveBeenCalledWith('hello');
expect(consoleErrorSpy).toHaveBeenCalledOnce();
expect(consoleErrorSpy).toHaveBeenCalledWith(new Error('listener failure'));
// Remove the mock on console.error
consoleErrorSpy.mockRestore();
};
/**
+59
View File
@@ -0,0 +1,59 @@
import { expect, test } from 'vitest';
import { normalizeError } from '../source/misc.ts';
/** Test cases for the normalizeError function. */
const normalizeErrorTestCases = [
{
input: new Error('test error'),
expected: new Error('test error'),
},
{
input: 'test error',
expected: new Error('test error'),
},
{
input: 1,
expected: new Error('1'),
},
{
input: undefined,
expected: new Error('undefined'),
},
{
input: null,
expected: new Error('null'),
},
{
input: false,
expected: new Error('false'),
},
{
input: true,
expected: new Error('true'),
},
{
input: {},
expected: new Error('[object Object]'),
},
{
input: [],
expected: new Error(),
},
];
/**
* Tests that normalizeError converts non-Error throws to Error instances.
*/
const testNormalizeError = async (): Promise<void> => {
for (const testCase of normalizeErrorTestCases) {
const result = normalizeError(testCase.input);
expect(result).toBeInstanceOf(Error);
expect(result.message).toBe(testCase.expected.message);
}
};
const runTests = async (): Promise<void> => {
test('normalizeError: converts non-Error throws to Error instances', testNormalizeError);
};
await runTests();
+65
View File
@@ -0,0 +1,65 @@
export type SseTestStreamOptions = {
/** Milliseconds to wait before enqueueing each chunk after the first. */
chunkDelayMs?: number;
/**
* When true, closes the body as soon as all initial chunks have been sent.
* Use this to simulate a server that sends events and then ends the stream.
*/
closeWhenDone?: boolean;
};
/**
* Test double for an SSE HTTP response body.
*
* Enqueues fixture chunks in order and stays open until {@link close} is called,
* matching real servers that keep the connection alive after each event's trailing
* `\n\n` frame boundary.
*/
export class SseTestStream {
readonly stream: ReadableStream<Uint8Array>;
private controller: ReadableStreamDefaultController<Uint8Array> | null = null;
private closed = false;
/**
* @param chunks - Fixture `raw` strings, whole or split, to simulate chunk boundaries.
* @param options - Delivery timing and optional auto-close after the initial chunks.
*/
constructor(chunks: string[], options: SseTestStreamOptions = {}) {
const { chunkDelayMs = 0, closeWhenDone = false } = options;
const encoder = new TextEncoder();
this.stream = new ReadableStream({
start: async (controller): Promise<void> => {
this.controller = controller;
for (let i = 0; i < chunks.length; i++) {
if (chunkDelayMs > 0 && i > 0) {
await new Promise((resolve) => setTimeout(resolve, chunkDelayMs));
}
if (this.closed) return;
controller.enqueue(encoder.encode(chunks[i]!));
}
if (closeWhenDone) {
this.close();
}
},
});
}
/**
* Ends the HTTP body the way a server closing the SSE connection would.
*/
close(): void {
if (this.closed) return;
this.closed = true;
this.controller?.close();
}
}
File diff suppressed because it is too large Load Diff