openreader/compute/core/tests/control-plane/sse.test.ts
Richard R a284ee852e feat(worker): throttle EventSource reconnects after idle disconnect
Add SSE `retry:` directive to suggest a 2-minute reconnect delay when the worker
enters idle sleep and drops silent EventSource streams. This prevents clients
from immediately reconnecting and re-waking the container, reducing unnecessary
resource usage. Update SSE encoding to support the `retry` field and add tests
for correct behavior. Improve idle disconnect logging for better observability.
2026-06-10 07:05:34 -06:00

36 lines
1.2 KiB
TypeScript

import { describe, expect, test } from 'vitest';
import { encodeSseFrame, parseSseEventId, parseSsePayload } from '../../src/control-plane/sse';
describe('sse codec', () => {
test('encodes event id and payload and decodes both reliably', () => {
const frame = encodeSseFrame({
id: 42,
event: 'snapshot',
data: { ok: true, opId: 'op-1' },
});
expect(frame).toContain('event: snapshot');
expect(parseSseEventId(frame)).toBe(42);
expect(parseSsePayload(frame)).toBe('{"ok":true,"opId":"op-1"}');
});
test('supports multiline data payload', () => {
const frame = encodeSseFrame({
id: 5,
data: 'line1\nline2',
});
expect(parseSseEventId(frame)).toBe(5);
expect(parseSsePayload(frame)).toBe('line1\nline2');
});
test('emits a retry directive when provided', () => {
const frame = encodeSseFrame({ retry: 120_000 });
expect(frame).toContain('retry: 120000');
});
test('omits retry when not finite and floors fractional values', () => {
expect(encodeSseFrame({ retry: Number.NaN })).not.toContain('retry:');
expect(encodeSseFrame({ retry: 1500.9 })).toContain('retry: 1500');
});
});