Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 | /** * Unit tests for the "expected errors" feature. * * Covers: * - isExpectedError() matching rules (exact, array, wildcard) * - AdapterBase.error() log-level demotion for expected errors * - Gateway respects `expectedErrors` config flag * - Propagation: expect travels with $meta through the handler chain */ import assert from 'node:assert'; import {describe, it} from 'node:test'; import {isExpectedError} from './lib.ts'; // --------------------------------------------------------------------------- // isExpectedError() — matching rules // --------------------------------------------------------------------------- describe('isExpectedError — matching rules', () => { it('returns false when error type is undefined', () => { assert.strictEqual(isExpectedError(undefined, 'foo.bar'), false); }); it('returns false when expect is undefined', () => { assert.strictEqual(isExpectedError('foo.bar', undefined), false); }); it('returns false when expect is an empty array', () => { assert.strictEqual(isExpectedError('foo.bar', []), false); }); it('exact string — matches identical type', () => { assert.strictEqual(isExpectedError('parking.invalidZone', 'parking.invalidZone'), true); }); it('exact string — does not match different type', () => { assert.strictEqual(isExpectedError('parking.rateLimit', 'parking.invalidZone'), false); }); it('array — matches when type is in the list', () => { assert.strictEqual( isExpectedError('auth.unauthorized', ['parking.invalidZone', 'auth.unauthorized']), true, ); }); it('array — does not match when type is absent from the list', () => { assert.strictEqual( isExpectedError('payment.failed', ['parking.invalidZone', 'auth.unauthorized']), false, ); }); it('wildcard — matches any type with the given prefix', () => { assert.strictEqual(isExpectedError('parking.invalidZone', 'parking.*'), true); assert.strictEqual(isExpectedError('parking.rateLimit', 'parking.*'), true); }); it('wildcard — does not match a different namespace', () => { assert.strictEqual(isExpectedError('auth.unauthorized', 'parking.*'), false); }); it('wildcard — does not match the prefix segment itself (no dot)', () => { // 'parking' alone should NOT match 'parking.*' (requires the dot) assert.strictEqual(isExpectedError('parking', 'parking.*'), false); }); it('array with wildcard — matches when a wildcard entry covers the type', () => { assert.strictEqual( isExpectedError('parking.invalidZone', ['auth.*', 'parking.*']), true, ); }); it('wildcard at top of namespace — auth.*', () => { assert.strictEqual(isExpectedError('auth.unauthorized', 'auth.*'), true); assert.strictEqual(isExpectedError('auth.tokenExpired', 'auth.*'), true); assert.strictEqual(isExpectedError('gateway.jwtMissingHeader', 'auth.*'), false); }); }); // --------------------------------------------------------------------------- // AdapterBase.error() — log-level demotion // --------------------------------------------------------------------------- describe('AdapterBase.error() — log-level demotion', () => { /** * Minimal stand-in for AdapterBase that exposes only the `error()` method * under test. We copy the implementation verbatim so we are testing the * real logic without spinning up a full adapter. */ function makeAdapter(log: Record<string, (...a: unknown[]) => void>) { return { log, error( error: {type?: string; method?: string}, $meta: {method?: string; expect?: string | string[]}, ) { if ($meta) error.method = $meta.method; if (isExpectedError(error.type, $meta?.expect)) { (this.log as Record<string, (...a: unknown[]) => void>).debug?.(error); return; } (this.log as Record<string, (...a: unknown[]) => void>).error?.(error); }, }; } it('logs at error level when expect is not set', () => { const logged: {level: string; error: unknown}[] = []; const adapter = makeAdapter({ error: e => logged.push({level: 'error', error: e}), debug: e => logged.push({level: 'debug', error: e}), }); adapter.error({type: 'parking.invalidZone'}, {method: 'parkingTest'}); assert.strictEqual(logged.length, 1); assert.strictEqual(logged[0].level, 'error'); }); it('logs at debug level and does not log at error when error is expected', () => { const logged: {level: string; error: unknown}[] = []; const adapter = makeAdapter({ error: e => logged.push({level: 'error', error: e}), debug: e => logged.push({level: 'debug', error: e}), }); adapter.error( {type: 'parking.invalidZone'}, {method: 'parkingTest', expect: 'parking.invalidZone'}, ); assert.strictEqual(logged.length, 1); assert.strictEqual(logged[0].level, 'debug'); }); it('logs at debug level for wildcard match', () => { const logged: {level: string; error: unknown}[] = []; const adapter = makeAdapter({ error: e => logged.push({level: 'error', error: e}), debug: e => logged.push({level: 'debug', error: e}), }); adapter.error( {type: 'parking.rateLimit'}, {method: 'parkingTest', expect: 'parking.*'}, ); assert.strictEqual(logged.length, 1); assert.strictEqual(logged[0].level, 'debug'); }); it('logs at error level when the error type does not match expect', () => { const logged: {level: string; error: unknown}[] = []; const adapter = makeAdapter({ error: e => logged.push({level: 'error', error: e}), debug: e => logged.push({level: 'debug', error: e}), }); adapter.error( {type: 'auth.unauthorized'}, {method: 'someMethod', expect: 'parking.invalidZone'}, ); assert.strictEqual(logged.length, 1); assert.strictEqual(logged[0].level, 'error'); }); it('attaches $meta.method to the error object', () => { const captured: unknown[] = []; const adapter = makeAdapter({ error: e => captured.push(e), debug: () => {}, }); const err: {type: string; method?: string} = {type: 'foo.bar'}; adapter.error(err, {method: 'someMethod'}); assert.strictEqual((captured[0] as typeof err).method, 'someMethod'); }); it('attaches $meta.method even when the error is expected', () => { const captured: unknown[] = []; const adapter = makeAdapter({ error: () => {}, debug: e => captured.push(e), }); const err: {type: string; method?: string} = {type: 'parking.invalidZone'}; adapter.error(err, {method: 'parkingTest', expect: 'parking.invalidZone'}); assert.strictEqual((captured[0] as typeof err).method, 'parkingTest'); }); }); // --------------------------------------------------------------------------- // Gateway expectedErrors flag // --------------------------------------------------------------------------- describe('Gateway — expectedErrors config flag', () => { /** * Simulate the gateway route-handler logic that builds `resolvedExpect`. * Returns the value of `resolvedExpect` that the gateway would compute. */ function buildResolvedExpect( expectedErrorsEnabled: boolean, expectFromBody: string | string[] | undefined, ): string[] | undefined { return expectedErrorsEnabled && expectFromBody ? ([] as string[]).concat(expectFromBody) : undefined; } it('resolves expect when expectedErrors is true and expect is provided', () => { const result = buildResolvedExpect(true, 'parking.invalidZone'); assert.deepStrictEqual(result, ['parking.invalidZone']); }); it('resolves expect array when expectedErrors is true', () => { const result = buildResolvedExpect(true, ['parking.invalidZone', 'auth.unauthorized']); assert.deepStrictEqual(result, ['parking.invalidZone', 'auth.unauthorized']); }); it('returns undefined when expectedErrors is false (production)', () => { const result = buildResolvedExpect(false, 'parking.invalidZone'); assert.strictEqual(result, undefined); }); it('returns undefined when expect is not provided even if expectedErrors is true', () => { const result = buildResolvedExpect(true, undefined); assert.strictEqual(result, undefined); }); it('with resolvedExpect=undefined, isExpectedError always returns false', () => { assert.strictEqual(isExpectedError('parking.invalidZone', undefined), false); }); }); // --------------------------------------------------------------------------- // Meta propagation — $meta.expect travels through handler chain // --------------------------------------------------------------------------- describe('$meta.expect propagation', () => { /** * Simulate a two-hop dispatch: caller → first handler → second handler. * The second handler logs an error and the first handler propagates it. * Verify that $meta.expect set by the caller controls log level at every hop. */ it('expect set by caller controls log level at every handler in the chain', () => { const logged: {level: string; type: string}[] = []; function logError(error: {type: string}, $meta: {expect?: string | string[]}) { if (isExpectedError(error.type, $meta?.expect)) { logged.push({level: 'debug', type: error.type}); } else { logged.push({level: 'error', type: error.type}); } } const callerMeta = {method: 'callerMethod', expect: 'foo.someError'}; // Simulate two handler hops both receiving the same $meta logError({type: 'foo.someError'}, callerMeta); // hop 1 logError({type: 'foo.someError'}, callerMeta); // hop 2 assert.strictEqual(logged.length, 2); assert.ok(logged.every(l => l.level === 'debug'), 'all hops should log at debug'); }); it('an error type not in expect is still logged at error level on all hops', () => { const logged: {level: string; type: string}[] = []; function logError(error: {type: string}, $meta: {expect?: string | string[]}) { if (isExpectedError(error.type, $meta?.expect)) { logged.push({level: 'debug', type: error.type}); } else { logged.push({level: 'error', type: error.type}); } } const callerMeta = {method: 'callerMethod', expect: 'foo.someError'}; logError({type: 'bar.otherError'}, callerMeta); // unexpected on hop 1 logError({type: 'bar.otherError'}, callerMeta); // unexpected on hop 2 assert.strictEqual(logged.length, 2); assert.ok(logged.every(l => l.level === 'error'), 'unexpected errors stay at error level'); }); }); |