|
| 1 | +import type { ConsentTransport } from './consent'; |
| 2 | +import { ConsentManager } from './consent'; |
| 3 | + |
| 4 | +function createMockTransport(): ConsentTransport & { calls: any[] } { |
| 5 | + const calls: any[] = []; |
| 6 | + return { |
| 7 | + calls, |
| 8 | + async syncConsent(url, publishableKey, body) { |
| 9 | + calls.push({ url, publishableKey, body }); |
| 10 | + }, |
| 11 | + }; |
| 12 | +} |
| 13 | + |
| 14 | +describe('ConsentManager', () => { |
| 15 | + it('initialises with the provided consent level', () => { |
| 16 | + const transport = createMockTransport(); |
| 17 | + const manager = new ConsentManager('sandbox', 'pk_test', 'anonymous', 'TestSDK', transport); |
| 18 | + expect(manager.getLevel()).toBe('anonymous'); |
| 19 | + }); |
| 20 | + |
| 21 | + it('calls onPurgeQueue and onClearIdentity when downgrading to none', () => { |
| 22 | + const transport = createMockTransport(); |
| 23 | + const manager = new ConsentManager('sandbox', 'pk_test', 'full', 'TestSDK', transport); |
| 24 | + const onPurge = jest.fn(); |
| 25 | + const onClear = jest.fn(); |
| 26 | + |
| 27 | + manager.setLevel('none', 'anon-123', { |
| 28 | + onPurgeQueue: onPurge, |
| 29 | + onClearIdentity: onClear, |
| 30 | + }); |
| 31 | + |
| 32 | + expect(onPurge).toHaveBeenCalled(); |
| 33 | + expect(onClear).toHaveBeenCalled(); |
| 34 | + expect(manager.getLevel()).toBe('none'); |
| 35 | + }); |
| 36 | + |
| 37 | + it('calls onStripIdentity when downgrading from full to anonymous', () => { |
| 38 | + const transport = createMockTransport(); |
| 39 | + const manager = new ConsentManager('sandbox', 'pk_test', 'full', 'TestSDK', transport); |
| 40 | + const onStrip = jest.fn(); |
| 41 | + |
| 42 | + manager.setLevel('anonymous', 'anon-123', { onStripIdentity: onStrip }); |
| 43 | + |
| 44 | + expect(onStrip).toHaveBeenCalled(); |
| 45 | + expect(manager.getLevel()).toBe('anonymous'); |
| 46 | + }); |
| 47 | + |
| 48 | + it('does not call onStripIdentity when upgrading from anonymous to full', () => { |
| 49 | + const transport = createMockTransport(); |
| 50 | + const manager = new ConsentManager('sandbox', 'pk_test', 'anonymous', 'TestSDK', transport); |
| 51 | + const onStrip = jest.fn(); |
| 52 | + |
| 53 | + manager.setLevel('full', 'anon-123', { onStripIdentity: onStrip }); |
| 54 | + |
| 55 | + expect(onStrip).not.toHaveBeenCalled(); |
| 56 | + expect(manager.getLevel()).toBe('full'); |
| 57 | + }); |
| 58 | + |
| 59 | + it('syncs consent to server via transport on setLevel', () => { |
| 60 | + const transport = createMockTransport(); |
| 61 | + const manager = new ConsentManager('sandbox', 'pk_test', 'none', 'TestSDK', transport); |
| 62 | + manager.setLevel('full', 'anon-123'); |
| 63 | + |
| 64 | + expect(transport.calls).toHaveLength(1); |
| 65 | + expect(transport.calls[0]).toEqual({ |
| 66 | + url: 'https://api.sandbox.immutable.com/v1/audience/tracking-consent', |
| 67 | + publishableKey: 'pk_test', |
| 68 | + body: { |
| 69 | + anonymousId: 'anon-123', |
| 70 | + status: 'full', |
| 71 | + source: 'TestSDK', |
| 72 | + }, |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + it('truncates source to 128 characters', () => { |
| 77 | + const transport = createMockTransport(); |
| 78 | + const longSource = 'x'.repeat(200); |
| 79 | + const manager = new ConsentManager('sandbox', 'pk_test', 'none', longSource, transport); |
| 80 | + manager.setLevel('full', 'anon-123'); |
| 81 | + |
| 82 | + expect(transport.calls[0].body.source).toHaveLength(128); |
| 83 | + }); |
| 84 | + |
| 85 | + it('uses correct base URL per environment', () => { |
| 86 | + const devTransport = createMockTransport(); |
| 87 | + const devManager = new ConsentManager('dev', 'pk_test', 'none', 'SDK', devTransport); |
| 88 | + devManager.setLevel('full', 'anon-123'); |
| 89 | + expect(devTransport.calls[0].url).toContain('api.dev.immutable.com'); |
| 90 | + |
| 91 | + const prodTransport = createMockTransport(); |
| 92 | + const prodManager = new ConsentManager('production', 'pk_test', 'none', 'SDK', prodTransport); |
| 93 | + prodManager.setLevel('full', 'anon-123'); |
| 94 | + expect(prodTransport.calls[0].url).toContain('api.immutable.com'); |
| 95 | + }); |
| 96 | + |
| 97 | + it('does not throw when transport rejects', async () => { |
| 98 | + const transport: ConsentTransport = { |
| 99 | + async syncConsent() { |
| 100 | + throw new Error('network error'); |
| 101 | + }, |
| 102 | + }; |
| 103 | + const manager = new ConsentManager('sandbox', 'pk_test', 'none', 'SDK', transport); |
| 104 | + |
| 105 | + // Should not throw — fire-and-forget |
| 106 | + expect(() => manager.setLevel('full', 'anon-123')).not.toThrow(); |
| 107 | + |
| 108 | + // Let the rejected promise settle |
| 109 | + await Promise.resolve(); |
| 110 | + await Promise.resolve(); |
| 111 | + }); |
| 112 | +}); |
0 commit comments