|
| 1 | +import { |
| 2 | + AudienceError, TransportError, toAudienceError, |
| 3 | +} from './errors'; |
| 4 | + |
| 5 | +describe('AudienceError', () => { |
| 6 | + it('is an instance of Error', () => { |
| 7 | + const err = new AudienceError({ |
| 8 | + code: 'FLUSH_FAILED', |
| 9 | + message: 'flush failed', |
| 10 | + status: 500, |
| 11 | + endpoint: 'https://example.com', |
| 12 | + }); |
| 13 | + |
| 14 | + expect(err).toBeInstanceOf(Error); |
| 15 | + expect(err).toBeInstanceOf(AudienceError); |
| 16 | + expect(err.name).toBe('AudienceError'); |
| 17 | + }); |
| 18 | + |
| 19 | + it('exposes structured fields from init', () => { |
| 20 | + const cause = new TypeError('boom'); |
| 21 | + const err = new AudienceError({ |
| 22 | + code: 'NETWORK_ERROR', |
| 23 | + message: 'network down', |
| 24 | + status: 0, |
| 25 | + endpoint: 'https://example.com', |
| 26 | + responseBody: { detail: 'x' }, |
| 27 | + cause, |
| 28 | + }); |
| 29 | + |
| 30 | + expect(err.code).toBe('NETWORK_ERROR'); |
| 31 | + expect(err.message).toBe('network down'); |
| 32 | + expect(err.status).toBe(0); |
| 33 | + expect(err.endpoint).toBe('https://example.com'); |
| 34 | + expect(err.responseBody).toEqual({ detail: 'x' }); |
| 35 | + expect(err.cause).toBe(cause); |
| 36 | + }); |
| 37 | +}); |
| 38 | + |
| 39 | +describe('toAudienceError', () => { |
| 40 | + const httpError = new TransportError({ |
| 41 | + status: 500, |
| 42 | + endpoint: 'https://api.dev.immutable.com/v1/audience/messages', |
| 43 | + body: { code: 'INTERNAL_ERROR' }, |
| 44 | + }); |
| 45 | + |
| 46 | + const networkError = new TransportError({ |
| 47 | + status: 0, |
| 48 | + endpoint: 'https://api.dev.immutable.com/v1/audience/messages', |
| 49 | + cause: new TypeError('Failed to fetch'), |
| 50 | + }); |
| 51 | + |
| 52 | + describe('flush source', () => { |
| 53 | + it('maps HTTP error to FLUSH_FAILED with status in message', () => { |
| 54 | + const err = toAudienceError(httpError, 'flush', 5); |
| 55 | + |
| 56 | + expect(err.code).toBe('FLUSH_FAILED'); |
| 57 | + expect(err.message).toBe('Flush failed with status 500'); |
| 58 | + expect(err.status).toBe(500); |
| 59 | + expect(err.endpoint).toBe(httpError.endpoint); |
| 60 | + expect(err.responseBody).toEqual({ code: 'INTERNAL_ERROR' }); |
| 61 | + }); |
| 62 | + |
| 63 | + it('maps network error to NETWORK_ERROR with batch count in message', () => { |
| 64 | + const err = toAudienceError(networkError, 'flush', 5); |
| 65 | + |
| 66 | + expect(err.code).toBe('NETWORK_ERROR'); |
| 67 | + expect(err.message).toBe('Network error sending 5 messages'); |
| 68 | + expect(err.status).toBe(0); |
| 69 | + expect(err.cause).toBe(networkError.cause); |
| 70 | + }); |
| 71 | + |
| 72 | + it('falls back to count 0 in network message when count is undefined', () => { |
| 73 | + const err = toAudienceError(networkError, 'flush'); |
| 74 | + expect(err.message).toBe('Network error sending 0 messages'); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + describe('consent source', () => { |
| 79 | + it('maps HTTP error to CONSENT_SYNC_FAILED with status in message', () => { |
| 80 | + const err = toAudienceError( |
| 81 | + { ...httpError, endpoint: 'https://api.dev.immutable.com/v1/audience/tracking-consent' }, |
| 82 | + 'consent', |
| 83 | + ); |
| 84 | + |
| 85 | + expect(err.code).toBe('CONSENT_SYNC_FAILED'); |
| 86 | + expect(err.message).toBe('Consent sync failed with status 500'); |
| 87 | + }); |
| 88 | + |
| 89 | + it('maps network error to NETWORK_ERROR with consent-specific message', () => { |
| 90 | + const err = toAudienceError(networkError, 'consent'); |
| 91 | + |
| 92 | + expect(err.code).toBe('NETWORK_ERROR'); |
| 93 | + expect(err.message).toBe('Network error syncing consent'); |
| 94 | + expect(err.status).toBe(0); |
| 95 | + expect(err.cause).toBe(networkError.cause); |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + describe('partial-rejection (2xx with rejected > 0)', () => { |
| 100 | + it('maps to VALIDATION_REJECTED with backend body preserved', () => { |
| 101 | + const partialError = new TransportError({ |
| 102 | + status: 200, |
| 103 | + endpoint: 'https://api.dev.immutable.com/v1/audience/messages', |
| 104 | + body: { accepted: 50, rejected: 50 }, |
| 105 | + }); |
| 106 | + |
| 107 | + const err = toAudienceError(partialError, 'flush', 100); |
| 108 | + |
| 109 | + expect(err.code).toBe('VALIDATION_REJECTED'); |
| 110 | + expect(err.status).toBe(200); |
| 111 | + expect(err.message).toBe('Backend rejected 50 of 100 messages'); |
| 112 | + expect(err.responseBody).toEqual({ accepted: 50, rejected: 50 }); |
| 113 | + }); |
| 114 | + |
| 115 | + it('handles missing accepted/rejected fields gracefully', () => { |
| 116 | + const partialError = new TransportError({ |
| 117 | + status: 200, |
| 118 | + endpoint: 'https://api.dev.immutable.com/v1/audience/messages', |
| 119 | + body: {}, |
| 120 | + }); |
| 121 | + |
| 122 | + const err = toAudienceError(partialError, 'flush'); |
| 123 | + |
| 124 | + expect(err.code).toBe('VALIDATION_REJECTED'); |
| 125 | + expect(err.message).toBe('Backend rejected 0 of 0 messages'); |
| 126 | + }); |
| 127 | + }); |
| 128 | +}); |
0 commit comments