|
| 1 | +import{afterEach,describe,expect,it}from'vitest'; |
| 2 | +import{ |
| 3 | +getExternalPropagationContext, |
| 4 | +getTraceContextFromScope, |
| 5 | +hasExternalPropagationContext, |
| 6 | +registerExternalPropagationContext, |
| 7 | +}from'../../src/currentScopes'; |
| 8 | +import{Scope}from'../../src/scope'; |
| 9 | + |
| 10 | +describe('External Propagation Context',()=>{ |
| 11 | +afterEach(()=>{ |
| 12 | +// Reset by registering a provider that returns undefined |
| 13 | +registerExternalPropagationContext(()=>undefined); |
| 14 | +}); |
| 15 | + |
| 16 | +describe('registerExternalPropagationContext',()=>{ |
| 17 | +it('registers a provider function',()=>{ |
| 18 | +registerExternalPropagationContext(()=>({ |
| 19 | +traceId: 'abc123', |
| 20 | +spanId: 'def456', |
| 21 | +})); |
| 22 | + |
| 23 | +expect(hasExternalPropagationContext()).toBe(true); |
| 24 | +}); |
| 25 | +}); |
| 26 | + |
| 27 | +describe('getExternalPropagationContext',()=>{ |
| 28 | +it('returns undefined when provider returns undefined',()=>{ |
| 29 | +registerExternalPropagationContext(()=>undefined); |
| 30 | +expect(getExternalPropagationContext()).toBeUndefined(); |
| 31 | +}); |
| 32 | + |
| 33 | +it('returns trace context from provider',()=>{ |
| 34 | +registerExternalPropagationContext(()=>({ |
| 35 | +traceId: '12345678901234567890123456789012', |
| 36 | +spanId: '1234567890123456', |
| 37 | +})); |
| 38 | + |
| 39 | +constresult=getExternalPropagationContext(); |
| 40 | +expect(result).toEqual({ |
| 41 | +traceId: '12345678901234567890123456789012', |
| 42 | +spanId: '1234567890123456', |
| 43 | +}); |
| 44 | +}); |
| 45 | +}); |
| 46 | + |
| 47 | +describe('hasExternalPropagationContext',()=>{ |
| 48 | +it('returns true after registration',()=>{ |
| 49 | +registerExternalPropagationContext(()=>undefined); |
| 50 | +expect(hasExternalPropagationContext()).toBe(true); |
| 51 | +}); |
| 52 | +}); |
| 53 | + |
| 54 | +describe('getTraceContextFromScope with external propagation context',()=>{ |
| 55 | +it('uses external propagation context when available',()=>{ |
| 56 | +registerExternalPropagationContext(()=>({ |
| 57 | +traceId: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa1', |
| 58 | +spanId: 'bbbbbbbbbbbbbb01', |
| 59 | +})); |
| 60 | + |
| 61 | +constscope=newScope(); |
| 62 | +scope.setPropagationContext({ |
| 63 | +traceId: 'cccccccccccccccccccccccccccccc01', |
| 64 | +sampleRand: 0.5, |
| 65 | +}); |
| 66 | + |
| 67 | +consttraceContext=getTraceContextFromScope(scope); |
| 68 | +expect(traceContext.trace_id).toBe('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa1'); |
| 69 | +expect(traceContext.span_id).toBe('bbbbbbbbbbbbbb01'); |
| 70 | +expect(traceContext.parent_span_id).toBeUndefined(); |
| 71 | +}); |
| 72 | + |
| 73 | +it('falls back to scope propagation context when provider returns undefined',()=>{ |
| 74 | +registerExternalPropagationContext(()=>undefined); |
| 75 | + |
| 76 | +constscope=newScope(); |
| 77 | +scope.setPropagationContext({ |
| 78 | +traceId: 'cccccccccccccccccccccccccccccc01', |
| 79 | +sampleRand: 0.5, |
| 80 | +}); |
| 81 | + |
| 82 | +consttraceContext=getTraceContextFromScope(scope); |
| 83 | +expect(traceContext.trace_id).toBe('cccccccccccccccccccccccccccccc01'); |
| 84 | +}); |
| 85 | +}); |
| 86 | +}); |
0 commit comments