UNPKG

@accounter/server

Version:
45 lines 2.17 kB
import { afterEach, describe, expect, it, vi } from 'vitest'; import { trace } from '@opentelemetry/api'; import { CORRELATION_ID_ATTRIBUTE, CORRELATION_ID_HEADER, correlationIdPlugin, } from '../correlation-id-plugin.js'; /** Minimal `onRequest` payload with only the header lookup the plugin uses. */ function onRequestArg(headerValue) { return { request: { headers: { get: (name) => (name === CORRELATION_ID_HEADER ? headerValue : null) }, }, }; } afterEach(() => { vi.restoreAllMocks(); }); describe('correlationIdPlugin', () => { it('sets the correlation id as an attribute on the active span', () => { const setAttribute = vi.fn(); vi.spyOn(trace, 'getActiveSpan').mockReturnValue({ setAttribute }); const plugin = correlationIdPlugin(); plugin.onRequest?.(onRequestArg('corr-123')); expect(setAttribute).toHaveBeenCalledWith(CORRELATION_ID_ATTRIBUTE, 'corr-123'); }); it('trims the header value', () => { const setAttribute = vi.fn(); vi.spyOn(trace, 'getActiveSpan').mockReturnValue({ setAttribute }); correlationIdPlugin().onRequest?.(onRequestArg(' corr-abc ')); expect(setAttribute).toHaveBeenCalledWith(CORRELATION_ID_ATTRIBUTE, 'corr-abc'); }); it('is a no-op when the header is absent', () => { const getActiveSpan = vi.spyOn(trace, 'getActiveSpan'); correlationIdPlugin().onRequest?.(onRequestArg(null)); // Returns before ever touching the tracer when there is nothing to record. expect(getActiveSpan).not.toHaveBeenCalled(); }); it('is a no-op when the header is blank', () => { const getActiveSpan = vi.spyOn(trace, 'getActiveSpan'); correlationIdPlugin().onRequest?.(onRequestArg(' ')); expect(getActiveSpan).not.toHaveBeenCalled(); }); it('does not throw when there is no active span (OTEL disabled)', () => { vi.spyOn(trace, 'getActiveSpan').mockReturnValue(undefined); expect(() => correlationIdPlugin().onRequest?.(onRequestArg('corr-1'))).not.toThrow(); }); }); //# sourceMappingURL=correlation-id-plugin.test.js.map