light-sentry-sdk/tests/field-encoder.test.ts

240 lines
7.4 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import {
FIELD_ENCODE_MAP,
FIELD_DECODE_MAP,
encodeFields,
decodeFields,
} from '../src/utils/field-encoder';
describe('field-encoder', () => {
describe('FIELD_ENCODE_MAP', () => {
it('should contain mappings for all basic fields', () => {
expect(FIELD_ENCODE_MAP.event_id).toBe('eid');
expect(FIELD_ENCODE_MAP.timestamp).toBe('ts');
expect(FIELD_ENCODE_MAP.start_timestamp).toBe('sts');
expect(FIELD_ENCODE_MAP.type).toBe('t');
expect(FIELD_ENCODE_MAP.level).toBe('l');
expect(FIELD_ENCODE_MAP.message).toBe('m');
expect(FIELD_ENCODE_MAP.platform).toBe('p');
expect(FIELD_ENCODE_MAP.release).toBe('r');
expect(FIELD_ENCODE_MAP.environment).toBe('e');
expect(FIELD_ENCODE_MAP.fingerprint).toBe('fp');
expect(FIELD_ENCODE_MAP.context_id).toBe('cid');
});
it('should contain mappings for user fields', () => {
expect(FIELD_ENCODE_MAP.user).toBe('u');
});
it('should contain mappings for tags', () => {
expect(FIELD_ENCODE_MAP.tags).toBe('tg');
});
it('should contain mappings for exception', () => {
expect(FIELD_ENCODE_MAP.exception).toBe('ex');
});
it('should contain mappings for request', () => {
expect(FIELD_ENCODE_MAP.request).toBe('req');
});
it('should contain mappings for contexts', () => {
expect(FIELD_ENCODE_MAP.contexts).toBe('ctx');
});
it('should contain mappings for breadcrumbs', () => {
expect(FIELD_ENCODE_MAP.breadcrumbs).toBe('bc');
});
it('should contain mappings for transaction', () => {
expect(FIELD_ENCODE_MAP.transaction).toBe('tx');
expect(FIELD_ENCODE_MAP.duration).toBe('d');
expect(FIELD_ENCODE_MAP.spans).toBe('sp');
});
it('should contain mappings for extra', () => {
expect(FIELD_ENCODE_MAP.extra).toBe('xt');
});
});
describe('FIELD_DECODE_MAP', () => {
it('should be reverse of FIELD_ENCODE_MAP', () => {
for (const [key, value] of Object.entries(FIELD_ENCODE_MAP)) {
expect(FIELD_DECODE_MAP[value]).toBe(key);
}
});
it('should decode short codes back to original field names', () => {
expect(FIELD_DECODE_MAP['eid']).toBe('event_id');
expect(FIELD_DECODE_MAP['ts']).toBe('timestamp');
expect(FIELD_DECODE_MAP['t']).toBe('type');
expect(FIELD_DECODE_MAP['l']).toBe('level');
expect(FIELD_DECODE_MAP['m']).toBe('message');
});
});
describe('encodeFields', () => {
it('should encode basic fields to short codes', () => {
const event = {
event_id: 'abc123',
timestamp: '2024-01-01T00:00:00.000Z',
type: 'error',
level: 'error',
message: 'Test error',
};
const encoded = encodeFields(event);
expect(encoded.eid).toBe('abc123');
expect(encoded.ts).toBe('2024-01-01T00:00:00.000Z');
expect(encoded.t).toBe('error');
expect(encoded.l).toBe('error');
expect(encoded.m).toBe('Test error');
});
it('should keep unknown fields unchanged', () => {
const event = {
event_id: 'abc123',
custom_field: 'custom_value',
another_field: 123,
};
const encoded = encodeFields(event);
expect(encoded.eid).toBe('abc123');
expect(encoded.custom_field).toBe('custom_value');
expect(encoded.another_field).toBe(123);
});
it('should return non-object input unchanged', () => {
expect(encodeFields(null as any)).toBe(null);
expect(encodeFields(undefined as any)).toBe(undefined);
expect(encodeFields(123 as any)).toBe(123);
expect(encodeFields('string' as any)).toBe('string');
});
it('should handle empty object', () => {
const encoded = encodeFields({});
expect(encoded).toEqual({});
});
it('should encode nested objects without modification', () => {
const event = {
event_id: 'abc123',
user: { id: 'user1', name: 'Test' },
contexts: { trace: { trace_id: 'xyz' } },
};
const encoded = encodeFields(event);
expect(encoded.eid).toBe('abc123');
// user is encoded to 'u', contexts is encoded to 'ctx'
expect(encoded.u).toEqual({ id: 'user1', name: 'Test' });
expect(encoded.ctx).toEqual({ trace: { trace_id: 'xyz' } });
});
it('should encode transaction fields', () => {
const transaction = {
type: 'transaction',
transaction: 'test-transaction',
duration: 1000,
spans: [],
start_timestamp: '2024-01-01T00:00:00.000Z',
};
const encoded = encodeFields(transaction);
expect(encoded.t).toBe('transaction');
expect(encoded.tx).toBe('test-transaction');
expect(encoded.d).toBe(1000);
expect(encoded.sp).toEqual([]);
expect(encoded.sts).toBe('2024-01-01T00:00:00.000Z');
});
});
describe('decodeFields', () => {
it('should decode short codes back to original field names', () => {
const event = {
eid: 'abc123',
ts: '2024-01-01T00:00:00.000Z',
t: 'error',
l: 'error',
m: 'Test error',
};
const decoded = decodeFields(event);
expect(decoded.event_id).toBe('abc123');
expect(decoded.timestamp).toBe('2024-01-01T00:00:00.000Z');
expect(decoded.type).toBe('error');
expect(decoded.level).toBe('error');
expect(decoded.message).toBe('Test error');
});
it('should keep unknown fields unchanged', () => {
const event = {
eid: 'abc123',
custom_field: 'custom_value',
another_field: 123,
};
const decoded = decodeFields(event);
expect(decoded.event_id).toBe('abc123');
expect(decoded.custom_field).toBe('custom_value');
expect(decoded.another_field).toBe(123);
});
it('should return non-object input unchanged', () => {
expect(decodeFields(null as any)).toBe(null);
expect(decodeFields(undefined as any)).toBe(undefined);
expect(decodeFields(123 as any)).toBe(123);
expect(decodeFields('string' as any)).toBe('string');
});
it('should handle empty object', () => {
const decoded = decodeFields({});
expect(decoded).toEqual({});
});
it('should decode nested objects without modification', () => {
const event = {
eid: 'abc123',
u: { id: 'user1', name: 'Test' },
ctx: { trace: { trace_id: 'xyz' } },
};
const decoded = decodeFields(event);
expect(decoded.event_id).toBe('abc123');
expect(decoded.user).toEqual({ id: 'user1', name: 'Test' });
expect(decoded.contexts).toEqual({ trace: { trace_id: 'xyz' } });
});
});
describe('encode/decode round-trip', () => {
it('should preserve data through encode/decode cycle', () => {
const original = {
event_id: 'abc123',
timestamp: '2024-01-01T00:00:00.000Z',
type: 'error',
level: 'error',
message: 'Test error',
user: { id: 'user1' },
tags: { env: 'prod' },
};
const encoded = encodeFields(original);
const decoded = decodeFields(encoded);
expect(decoded.event_id).toBe(original.event_id);
expect(decoded.timestamp).toBe(original.timestamp);
expect(decoded.type).toBe(original.type);
expect(decoded.level).toBe(original.level);
expect(decoded.message).toBe(original.message);
expect(decoded.user).toEqual(original.user);
expect(decoded.tags).toEqual(original.tags);
});
});
});