|
| 1 | +import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; |
| 2 | + |
| 3 | +import { |
| 4 | + createAgentSpan, |
| 5 | + createFunctionSpan, |
| 6 | + createGuardrailSpan, |
| 7 | + createSpeechSpan, |
| 8 | + createMCPListToolsSpan, |
| 9 | + withAgentSpan, |
| 10 | + withFunctionSpan, |
| 11 | +} from '../src/tracing/createSpans'; |
| 12 | +import { |
| 13 | + getCurrentSpan, |
| 14 | + setTraceProcessors, |
| 15 | + setTracingDisabled, |
| 16 | + withTrace, |
| 17 | +} from '../src/tracing'; |
| 18 | +import type { TraceProvider } from '../src/tracing/provider'; |
| 19 | +import type { Span } from '../src/tracing/spans'; |
| 20 | +import * as providerModule from '../src/tracing/provider'; |
| 21 | +import { defaultProcessor, TracingProcessor } from '../src/tracing/processor'; |
| 22 | +import type { Trace } from '../src/tracing/traces'; |
| 23 | + |
| 24 | +class RecordingProcessor implements TracingProcessor { |
| 25 | + tracesStarted: Trace[] = []; |
| 26 | + tracesEnded: Trace[] = []; |
| 27 | + spansStarted: Span<any>[] = []; |
| 28 | + spansEnded: Span<any>[] = []; |
| 29 | + |
| 30 | + async onTraceStart(trace: Trace): Promise<void> { |
| 31 | + this.tracesStarted.push(trace); |
| 32 | + } |
| 33 | + async onTraceEnd(trace: Trace): Promise<void> { |
| 34 | + this.tracesEnded.push(trace); |
| 35 | + } |
| 36 | + async onSpanStart(span: Span<any>): Promise<void> { |
| 37 | + this.spansStarted.push(span); |
| 38 | + } |
| 39 | + async onSpanEnd(span: Span<any>): Promise<void> { |
| 40 | + this.spansEnded.push(span); |
| 41 | + } |
| 42 | + async shutdown(): Promise<void> { |
| 43 | + /* noop */ |
| 44 | + } |
| 45 | + async forceFlush(): Promise<void> { |
| 46 | + /* noop */ |
| 47 | + } |
| 48 | + reset() { |
| 49 | + this.tracesStarted.length = 0; |
| 50 | + this.tracesEnded.length = 0; |
| 51 | + this.spansStarted.length = 0; |
| 52 | + this.spansEnded.length = 0; |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +describe('create*Span helpers', () => { |
| 57 | + const createSpanMock = vi.fn(); |
| 58 | + let providerSpy: ReturnType<typeof vi.spyOn> | undefined; |
| 59 | + const fakeSpan = { spanId: 'span', traceId: 'trace' } as Span<any>; |
| 60 | + |
| 61 | + beforeEach(() => { |
| 62 | + createSpanMock.mockReturnValue(fakeSpan); |
| 63 | + providerSpy = vi.spyOn(providerModule, 'getGlobalTraceProvider'); |
| 64 | + providerSpy.mockReturnValue({ |
| 65 | + createSpan: createSpanMock, |
| 66 | + } as unknown as TraceProvider); |
| 67 | + }); |
| 68 | + |
| 69 | + afterEach(() => { |
| 70 | + createSpanMock.mockReset(); |
| 71 | + providerSpy?.mockRestore(); |
| 72 | + }); |
| 73 | + |
| 74 | + it('createAgentSpan falls back to the default name when not provided', () => { |
| 75 | + createAgentSpan(); |
| 76 | + |
| 77 | + expect(createSpanMock).toHaveBeenCalledWith( |
| 78 | + expect.objectContaining({ |
| 79 | + data: expect.objectContaining({ type: 'agent', name: 'Agent' }), |
| 80 | + }), |
| 81 | + undefined, |
| 82 | + ); |
| 83 | + }); |
| 84 | + |
| 85 | + it('createFunctionSpan populates default input/output values', () => { |
| 86 | + createFunctionSpan({ data: { name: 'call' } }); |
| 87 | + |
| 88 | + const calls = createSpanMock.mock.calls; |
| 89 | + const [options] = calls[calls.length - 1]; |
| 90 | + expect(options.data).toMatchObject({ |
| 91 | + type: 'function', |
| 92 | + name: 'call', |
| 93 | + input: '', |
| 94 | + output: '', |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + it('createGuardrailSpan enforces a non-triggered default state', () => { |
| 99 | + createGuardrailSpan({ data: { name: 'moderation' } }); |
| 100 | + |
| 101 | + const calls = createSpanMock.mock.calls; |
| 102 | + const [options] = calls[calls.length - 1]; |
| 103 | + expect(options.data).toMatchObject({ |
| 104 | + type: 'guardrail', |
| 105 | + name: 'moderation', |
| 106 | + triggered: false, |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
| 110 | + it('createSpeechSpan forwards the provided payload with the expected type', () => { |
| 111 | + createSpeechSpan({ |
| 112 | + data: { output: { data: 'pcm-data', format: 'pcm' } }, |
| 113 | + }); |
| 114 | + |
| 115 | + const calls = createSpanMock.mock.calls; |
| 116 | + const [options] = calls[calls.length - 1]; |
| 117 | + expect(options.data).toMatchObject({ |
| 118 | + type: 'speech', |
| 119 | + output: { data: 'pcm-data', format: 'pcm' }, |
| 120 | + }); |
| 121 | + }); |
| 122 | + |
| 123 | + it('createMCPListToolsSpan stamps the span type', () => { |
| 124 | + createMCPListToolsSpan(); |
| 125 | + |
| 126 | + const calls = createSpanMock.mock.calls; |
| 127 | + const [options] = calls[calls.length - 1]; |
| 128 | + expect(options.data).toMatchObject({ type: 'mcp_tools' }); |
| 129 | + }); |
| 130 | +}); |
| 131 | + |
| 132 | +describe('with*Span helpers', () => { |
| 133 | + const processor = new RecordingProcessor(); |
| 134 | + |
| 135 | + beforeEach(() => { |
| 136 | + processor.reset(); |
| 137 | + setTraceProcessors([processor]); |
| 138 | + setTracingDisabled(false); |
| 139 | + }); |
| 140 | + |
| 141 | + afterEach(() => { |
| 142 | + setTraceProcessors([defaultProcessor()]); |
| 143 | + setTracingDisabled(true); |
| 144 | + }); |
| 145 | + |
| 146 | + it('records errors and restores the previous span when the callback throws', async () => { |
| 147 | + const failingError = Object.assign(new Error('boom'), { |
| 148 | + data: { reason: 'bad input' }, |
| 149 | + }); |
| 150 | + |
| 151 | + await withTrace('workflow', async () => { |
| 152 | + await withAgentSpan(async (outerSpan) => { |
| 153 | + await expect( |
| 154 | + withFunctionSpan( |
| 155 | + async () => { |
| 156 | + expect(getCurrentSpan()).toBeDefined(); |
| 157 | + throw failingError; |
| 158 | + }, |
| 159 | + { data: { name: 'inner' } }, |
| 160 | + ), |
| 161 | + ).rejects.toThrow('boom'); |
| 162 | + |
| 163 | + expect(getCurrentSpan()).toBe(outerSpan); |
| 164 | + }); |
| 165 | + |
| 166 | + expect(getCurrentSpan()).toBeNull(); |
| 167 | + }); |
| 168 | + |
| 169 | + const functionSpan = processor.spansEnded.find( |
| 170 | + (span) => span.spanData.type === 'function', |
| 171 | + ); |
| 172 | + expect(functionSpan?.error).toMatchObject({ |
| 173 | + message: 'boom', |
| 174 | + data: { reason: 'bad input' }, |
| 175 | + }); |
| 176 | + }); |
| 177 | +}); |
0 commit comments