|
| 1 | +/* eslint-disable @typescript-eslint/unbound-method */ |
| 2 | +import type { IoHelper, SpanDefinition } from '../../../src/api/io/private'; |
| 3 | +import { SpanMaker } from '../../../src/api/io/private'; |
| 4 | +import * as maker from '../../../src/api/io/private/message-maker'; |
| 5 | + |
| 6 | +describe('SpanMaker', () => { |
| 7 | + let ioHelper: jest.Mocked<IoHelper>; |
| 8 | + |
| 9 | + beforeEach(() => { |
| 10 | + ioHelper = { |
| 11 | + notify: jest.fn(), |
| 12 | + } as any; |
| 13 | + }); |
| 14 | + |
| 15 | + test('begin creates span with unique id and sends start message', async () => { |
| 16 | + // GIVEN |
| 17 | + const definition: SpanDefinition<{}, { duration: number }> = { |
| 18 | + name: 'Test Span', |
| 19 | + start: maker.info<{}>({ |
| 20 | + code: 'CDK_TOOLKIT_I1234', |
| 21 | + description: 'test', |
| 22 | + interface: 'stuff', |
| 23 | + }), |
| 24 | + end: maker.info<{ duration: number }>({ |
| 25 | + code: 'CDK_TOOLKIT_I1234', |
| 26 | + description: 'test', |
| 27 | + interface: 'stuff', |
| 28 | + }), |
| 29 | + }; |
| 30 | + const spanMaker = new SpanMaker(ioHelper, definition); |
| 31 | + |
| 32 | + // WHEN |
| 33 | + await spanMaker.begin('Test span', {}); |
| 34 | + |
| 35 | + // THEN |
| 36 | + expect(ioHelper.notify).toHaveBeenCalledTimes(1); |
| 37 | + const notifyCall = ioHelper.notify.mock.calls[0][0]; |
| 38 | + expect(notifyCall.message).toBe('Test span'); |
| 39 | + expect(notifyCall.span).toBeDefined(); // UUID should be set |
| 40 | + }); |
| 41 | + |
| 42 | + test('end returns elapsed time and sends end message', async () => { |
| 43 | + // GIVEN |
| 44 | + const definition: SpanDefinition<{}, { duration: number }> = { |
| 45 | + name: 'Test Span', |
| 46 | + start: maker.info<{}>({ |
| 47 | + code: 'CDK_TOOLKIT_I1234', |
| 48 | + description: 'Starting span', |
| 49 | + interface: 'stuff', |
| 50 | + }), |
| 51 | + end: maker.info<{ duration: number }>({ |
| 52 | + code: 'CDK_TOOLKIT_I1234', |
| 53 | + description: 'Ending span', |
| 54 | + interface: 'stuff', |
| 55 | + }), |
| 56 | + }; |
| 57 | + const spanMaker = new SpanMaker(ioHelper, definition); |
| 58 | + |
| 59 | + // WHEN |
| 60 | + const messageSpan = await spanMaker.begin('Test span', {}); |
| 61 | + const result = await messageSpan.end(); |
| 62 | + |
| 63 | + // THEN |
| 64 | + expect(result.asMs).toBeGreaterThanOrEqual(0); |
| 65 | + expect(ioHelper.notify).toHaveBeenCalledTimes(2); |
| 66 | + const endCall = ioHelper.notify.mock.calls[1][0] as any; |
| 67 | + expect(endCall.message).toContain('Test Span time'); |
| 68 | + expect(endCall.span).toBeDefined(); |
| 69 | + expect(endCall.data.duration).toBeGreaterThanOrEqual(0); |
| 70 | + }); |
| 71 | + |
| 72 | + test('intermediate messages are sent with same span id', async () => { |
| 73 | + // GIVEN |
| 74 | + const definition: SpanDefinition<{}, { duration: number }> = { |
| 75 | + name: 'Test Span', |
| 76 | + start: maker.info<{}>({ |
| 77 | + code: 'CDK_TOOLKIT_I1234', |
| 78 | + description: 'Starting span', |
| 79 | + interface: 'stuff', |
| 80 | + }), |
| 81 | + end: maker.info<{ duration: number }>({ |
| 82 | + code: 'CDK_TOOLKIT_I1234', |
| 83 | + description: 'Ending span', |
| 84 | + interface: 'stuff', |
| 85 | + }), |
| 86 | + }; |
| 87 | + const spanMaker = new SpanMaker(ioHelper, definition); |
| 88 | + |
| 89 | + // WHEN |
| 90 | + const messageSpan = await spanMaker.begin('Test span', {}); |
| 91 | + await messageSpan.notify({ |
| 92 | + message: 'Intermediate message', |
| 93 | + code: 'CDK_TOOLKIT_I1234', |
| 94 | + time: new Date(), |
| 95 | + level: 'error', |
| 96 | + data: undefined, |
| 97 | + }); |
| 98 | + await messageSpan.end(); |
| 99 | + |
| 100 | + // THEN |
| 101 | + expect(ioHelper.notify).toHaveBeenCalledTimes(3); |
| 102 | + const spanId = ioHelper.notify.mock.calls[0][0].span; |
| 103 | + expect(ioHelper.notify.mock.calls[1][0].span).toBe(spanId); |
| 104 | + expect(ioHelper.notify.mock.calls[2][0].span).toBe(spanId); |
| 105 | + }); |
| 106 | + |
| 107 | + test('end with payload overrides default elapsed time payload', async () => { |
| 108 | + // GIVEN |
| 109 | + const definition: SpanDefinition<{}, { customField: string; duration: number }> = { |
| 110 | + name: 'Test Span', |
| 111 | + start: maker.info<{}>({ |
| 112 | + code: 'CDK_TOOLKIT_I1234', |
| 113 | + description: 'Starting span', |
| 114 | + interface: 'stuff', |
| 115 | + }), |
| 116 | + end: maker.info<{ customField: string; duration: number }>({ |
| 117 | + code: 'CDK_TOOLKIT_I1234', |
| 118 | + description: 'Ending span', |
| 119 | + interface: 'stuff', |
| 120 | + }), |
| 121 | + }; |
| 122 | + const spanMaker = new SpanMaker(ioHelper, definition); |
| 123 | + |
| 124 | + // WHEN |
| 125 | + const messageSpan = await spanMaker.begin('Test span', {}); |
| 126 | + const result = await messageSpan.end({ customField: 'test value' }); |
| 127 | + |
| 128 | + // THEN |
| 129 | + expect(result).toEqual({ asMs: expect.any(Number), asSec: expect.any(Number) }); |
| 130 | + const endCall = ioHelper.notify.mock.calls[1][0]; |
| 131 | + expect(endCall.data).toEqual(expect.objectContaining({ customField: 'test value' })); |
| 132 | + }); |
| 133 | + |
| 134 | + test('begin with payload includes payload in start message', async () => { |
| 135 | + // GIVEN |
| 136 | + const definition: SpanDefinition<{ startField: string }, { duration: number }> = { |
| 137 | + name: 'Test Span', |
| 138 | + start: maker.info<{ startField: string }>({ |
| 139 | + code: 'CDK_TOOLKIT_I1234', |
| 140 | + description: 'Starting span', |
| 141 | + interface: 'stuff', |
| 142 | + }), |
| 143 | + end: maker.info<{ duration: number }>({ |
| 144 | + code: 'CDK_TOOLKIT_I1234', |
| 145 | + description: 'Ending span', |
| 146 | + interface: 'stuff', |
| 147 | + }), |
| 148 | + }; |
| 149 | + const spanMaker = new SpanMaker(ioHelper, definition); |
| 150 | + |
| 151 | + // WHEN |
| 152 | + await spanMaker.begin('Test span', { startField: 'test value' }); |
| 153 | + |
| 154 | + // THEN |
| 155 | + const startCall = ioHelper.notify.mock.calls[0][0]; |
| 156 | + expect(startCall.data).toEqual({ startField: 'test value' }); |
| 157 | + }); |
| 158 | + |
| 159 | + test('endWithMessage allows custom end message', async () => { |
| 160 | + // GIVEN |
| 161 | + const definition: SpanDefinition<{}, { duration: number }> = { |
| 162 | + name: 'Test Span', |
| 163 | + start: maker.info<{}>({ |
| 164 | + code: 'CDK_TOOLKIT_I1234', |
| 165 | + description: 'Starting span', |
| 166 | + interface: 'stuff', |
| 167 | + }), |
| 168 | + end: maker.info<{ duration: number }>({ |
| 169 | + code: 'CDK_TOOLKIT_I1234', |
| 170 | + description: 'Ending span', |
| 171 | + interface: 'stuff', |
| 172 | + }), |
| 173 | + }; |
| 174 | + const spanMaker = new SpanMaker(ioHelper, definition); |
| 175 | + |
| 176 | + // WHEN |
| 177 | + const messageSpan =await spanMaker.begin('Test span', {}); |
| 178 | + await messageSpan.end('Custom end message'); |
| 179 | + |
| 180 | + // THEN |
| 181 | + const endCall = ioHelper.notify.mock.calls[1][0]; |
| 182 | + expect(endCall.message).toBe('Custom end message'); |
| 183 | + }); |
| 184 | + |
| 185 | + test('timing sends timing message with elapsed time', async () => { |
| 186 | + // GIVEN |
| 187 | + const definition: SpanDefinition<{}, { duration: number }> = { |
| 188 | + name: 'Test Span', |
| 189 | + start: maker.info<{}>({ |
| 190 | + code: 'CDK_TOOLKIT_I1234', |
| 191 | + description: 'Starting span', |
| 192 | + interface: 'stuff', |
| 193 | + }), |
| 194 | + end: maker.info<{ duration: number }>({ |
| 195 | + code: 'CDK_TOOLKIT_I1234', |
| 196 | + description: 'Ending span', |
| 197 | + interface: 'stuff', |
| 198 | + }), |
| 199 | + }; |
| 200 | + const spanMaker = new SpanMaker(ioHelper, definition); |
| 201 | + |
| 202 | + // WHEN |
| 203 | + const messageSpan = await spanMaker.begin('Test span', {}); |
| 204 | + const elapsedTime = await messageSpan.timing(maker.info({ |
| 205 | + code: 'CDK_TOOLKIT_I1234', |
| 206 | + description: 'Timing message', |
| 207 | + interface: 'stuff', |
| 208 | + }), 'Custom timing message'); |
| 209 | + |
| 210 | + // THEN |
| 211 | + expect(elapsedTime.asMs).toBeGreaterThanOrEqual(0); |
| 212 | + expect(ioHelper.notify).toHaveBeenCalledTimes(2); |
| 213 | + const timingCall = ioHelper.notify.mock.calls[1][0] as any; |
| 214 | + expect(timingCall.message).toBe('Custom timing message'); |
| 215 | + expect(timingCall.span).toBeDefined(); |
| 216 | + expect(timingCall.data.duration).toBeGreaterThanOrEqual(0); |
| 217 | + }); |
| 218 | +}); |
0 commit comments