diff options
| author | Elizabeth Hunt <me@liz.coffee> | 2025-12-14 22:39:18 -0800 |
|---|---|---|
| committer | Elizabeth Hunt <me@liz.coffee> | 2025-12-14 22:39:18 -0800 |
| commit | 666674327f009e9b1013218fc384f193b64c6997 (patch) | |
| tree | acebae7b425b469584eb0a5bec396899c2739501 /tst/traceables.test.ts | |
| parent | 594ce452693a71b501d3aff3f35ef3732c06c341 (diff) | |
| download | pengueno-666674327f009e9b1013218fc384f193b64c6997.tar.gz pengueno-666674327f009e9b1013218fc384f193b64c6997.zip | |
Adds unit tests
Diffstat (limited to 'tst/traceables.test.ts')
| -rw-r--r-- | tst/traceables.test.ts | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/tst/traceables.test.ts b/tst/traceables.test.ts new file mode 100644 index 0000000..cafd333 --- /dev/null +++ b/tst/traceables.test.ts @@ -0,0 +1,64 @@ +import { CollectingTrace, TestTraceable } from './test_utils'; +import { + LogLevel, + LogMetricTrace, + LogMetricTraceable, + Metric, + MetricsTrace, + MetricValueTag, + type MetricValue, +} from '../lib/index'; + +describe('trace/trace (traceables + LogMetricTrace)', () => { + beforeEach(() => { + jest.useFakeTimers(); + jest.setSystemTime(new Date('2020-01-01T00:00:00.000Z')); + }); + + afterEach(() => { + jest.useRealTimers(); + }); + + test('LogMetricTrace routes metrics to MetricsTrace', () => { + const emitted: MetricValue[] = []; + const metrics = new MetricsTrace((vals: MetricValue[]) => emitted.push(...vals)); + const logs = new CollectingTrace<any>(); + + const t = new LogMetricTrace(logs, metrics); + const m = Metric.fromName('x'); + + const scoped = t.traceScope(m); + jest.setSystemTime(new Date('2020-01-01T00:00:00.010Z')); + scoped.trace(m); + + expect(emitted).toEqual( + expect.arrayContaining([ + expect.objectContaining({ _tag: MetricValueTag, name: 'x.count', value: 1 }), + expect.objectContaining({ _tag: MetricValueTag, name: 'x.time', value: 10 }), + ]), + ); + }); + + test('LogMetricTrace routes non-metrics to LogTrace', () => { + const metrics = new MetricsTrace(() => undefined); + const logs = new CollectingTrace<any>(); + + const t = new LogMetricTrace(logs, metrics); + t.traceScope(LogLevel.INFO).trace('hello'); + + const flattened = logs.events.flatMap((e) => e); + expect(flattened).toEqual(expect.arrayContaining([LogLevel.INFO, 'hello'])); + }); + + test('LogMetricTraceable embeds emitted metrics into log trace', () => { + const logs = new CollectingTrace<any>(); + const t = TestTraceable.of('x', logs); + const withMetrics = LogMetricTraceable.ofLogTraceable(t); + + const metric = Metric.fromName('m'); + withMetrics.trace.trace(metric.count.withValue(1)); + + const flattened = logs.events.flatMap((e) => e.map((x) => (typeof x === 'function' ? x() : x))); + expect(flattened.some((x) => typeof x === 'string' && x.includes('<metrics>'))).toBe(true); + }); +}); |
