Files
TREK/server/tests/unit/nest/common/request-correlation.test.ts
Maurice 724c88c201 style: format server and shared with prettier
A mechanical prettier --write over server/src, server/tests, server/scripts
and shared/src with the workspaces' .prettierrc; nothing but layout and
import order changes, and every file keeps the same set of imports. shared
is formatted throughout and its format baseline is empty. The server
baseline keeps the 35 files that Prettier would grow past their lint:size
limit (they need a split first, which is not a formatting change), and the
generated entity files stay as their generator writes them.
2026-10-10 21:23:37 +02:00

69 lines
2.3 KiB
TypeScript

/**
* The correlation store (nest/common/request-correlation.ts): which incoming
* ids are adopted, how a unit of work nests inside another, and how the tag
* reads in a log line.
*/
import {
acceptRequestId,
childCorrelation,
correlationTag,
currentCorrelation,
newCorrelationId,
runWithCorrelation,
} from '../../../../src/nest/common/request-correlation';
import { describe, it, expect } from 'vitest';
describe('acceptRequestId', () => {
it('CORR-001: adopts the ids proxies and tracers send', () => {
for (const id of [
'abc123',
'7f9c2ba4-e88f-11ec-8ea0-0242ac120002',
'Root=1-5759e988-bd862e3fe1be46a994272793',
'a.b_c:d',
]) {
expect(acceptRequestId(id)).toBe(id);
}
});
it('CORR-002: refuses anything that could forge or bloat a log line', () => {
expect(acceptRequestId('')).toBeNull();
expect(acceptRequestId('x'.repeat(129))).toBeNull();
expect(acceptRequestId('has space')).toBeNull();
expect(acceptRequestId('line\nbreak')).toBeNull();
expect(acceptRequestId('<script>')).toBeNull();
expect(acceptRequestId(['a', 'b'])).toBeNull();
expect(acceptRequestId(undefined)).toBeNull();
});
});
describe('the correlation store', () => {
it('CORR-003: nothing is current outside a unit of work', () => {
expect(currentCorrelation()).toBeUndefined();
expect(correlationTag(undefined)).toBe('');
});
it('CORR-004: a correlation stays current across awaits inside its run', async () => {
const id = newCorrelationId();
const seen = await runWithCorrelation({ id, kind: 'http' }, async () => {
await new Promise((resolve) => setTimeout(resolve, 1));
return currentCorrelation();
});
expect(seen).toEqual({ id, kind: 'http' });
expect(currentCorrelation()).toBeUndefined();
});
it('CORR-005: a child gets its own id and remembers the one it started inside', () => {
const top = childCorrelation('cron');
expect(top.parentId).toBeUndefined();
const nested = runWithCorrelation({ id: 'outer', kind: 'http' }, () => childCorrelation('mcp'));
expect(nested.kind).toBe('mcp');
expect(nested.parentId).toBe('outer');
expect(nested.id).not.toBe('outer');
});
it('CORR-006: the tag names the kind and the id', () => {
expect(correlationTag({ id: 'abc', kind: 'ws' })).toBe('[ws abc] ');
});
});