LibreChat/packages/api/src/utils/events.spec.ts
Marco Beretta 784582850a
🧯 fix: Guard SSE Writes and Interval Cleanups Against Crashes (#17462)
* fix: Harden Timer Sweeps, SSE Writes, and Violation Counters

Guard SSE writes against ended or destroyed responses through one helper, catch and single-flight the job store and TTL cache cleanup timers, serialize in-process counter updates, cap the violation log, prune stale OAuth failure records, batch the active-job lookup, and count Redis keys with DBSIZE.

* refactor: Drop the redundant violation log entry copy

* refactor: Log single-flight sweep failures from the API package

* test: Cover active generation listing and client disconnect mid-stream

* refactor: Configure violation log retention and move title timeout into the API package

* feat: Configure OAuth failure retention through mcpSettings

* fix: Prune OAuth failures promptly under short retention
2026-10-27 23:06:58 +01:00

43 lines
1.2 KiB
TypeScript

import { writeGuarded, sendEvent, handleError } from './events';
type FakeRes = {
write: jest.Mock;
end: jest.Mock;
writableEnded?: boolean;
destroyed?: boolean;
};
const makeRes = (overrides: Partial<FakeRes> = {}): FakeRes => ({
write: jest.fn(),
end: jest.fn(),
...overrides,
});
describe('writeGuarded', () => {
it('writes to a live response', () => {
const res = makeRes();
expect(writeGuarded(res, 'x')).toBe(true);
expect(res.write).toHaveBeenCalledWith('x');
});
it('skips an ended response', () => {
const res = makeRes({ writableEnded: true });
expect(writeGuarded(res, 'x')).toBe(false);
expect(res.write).not.toHaveBeenCalled();
});
it('skips a destroyed response', () => {
const res = makeRes({ destroyed: true });
expect(writeGuarded(res, 'x')).toBe(false);
expect(res.write).not.toHaveBeenCalled();
});
});
describe('sendEvent and handleError', () => {
it('does not write after the response ended', () => {
const res = makeRes({ writableEnded: true });
sendEvent(res as never, { event: 'x' } as never);
handleError(res as never, 'boom');
expect(res.write).not.toHaveBeenCalled();
});
});