Skip to content

Commit de470a4

Browse files
test(audience): add tests for DebugLogger and collectContext
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 55328bd commit de470a4

2 files changed

Lines changed: 92 additions & 0 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { collectContext } from './context';
2+
import { LIBRARY_NAME, LIBRARY_VERSION } from './config';
3+
4+
describe('collectContext', () => {
5+
it('should return context with SDK library name and version', () => {
6+
const ctx = collectContext();
7+
8+
expect(ctx.library).toBe(LIBRARY_NAME);
9+
expect(ctx.libraryVersion).toBe(LIBRARY_VERSION);
10+
});
11+
});
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import type { Message } from '@imtbl/audience-core';
2+
import { DebugLogger } from './debug';
3+
4+
describe('DebugLogger', () => {
5+
let logSpy: jest.SpyInstance;
6+
let warnSpy: jest.SpyInstance;
7+
8+
beforeEach(() => {
9+
logSpy = jest.spyOn(console, 'log').mockImplementation();
10+
warnSpy = jest.spyOn(console, 'warn').mockImplementation();
11+
});
12+
13+
afterEach(() => {
14+
logSpy.mockRestore();
15+
warnSpy.mockRestore();
16+
});
17+
18+
const stubMessage: Message = {
19+
type: 'track',
20+
messageId: 'msg-1',
21+
eventTimestamp: '2026-01-01T00:00:00Z',
22+
anonymousId: 'anon-1',
23+
surface: 'web',
24+
context: { library: 'test', libraryVersion: '0.0.0' },
25+
event: 'click',
26+
properties: {},
27+
};
28+
29+
it('should not log when disabled', () => {
30+
const logger = new DebugLogger(false);
31+
logger.logEvent('track', stubMessage);
32+
logger.logFlush(true, 1);
33+
logger.logConsent('none', 'full');
34+
logger.logWarning('test');
35+
36+
expect(logSpy).not.toHaveBeenCalled();
37+
expect(warnSpy).not.toHaveBeenCalled();
38+
});
39+
40+
it('should log events when enabled', () => {
41+
const logger = new DebugLogger(true);
42+
logger.logEvent('track', stubMessage);
43+
44+
expect(logSpy).toHaveBeenCalledWith(
45+
'[Immutable Audience] track',
46+
stubMessage,
47+
);
48+
});
49+
50+
it('should log flush status', () => {
51+
const logger = new DebugLogger(true);
52+
53+
logger.logFlush(true, 5);
54+
expect(logSpy).toHaveBeenCalledWith(
55+
'[Immutable Audience] flush ok (5 messages)',
56+
);
57+
58+
logger.logFlush(false, 3);
59+
expect(logSpy).toHaveBeenCalledWith(
60+
'[Immutable Audience] flush failed (3 messages)',
61+
);
62+
});
63+
64+
it('should log consent transitions', () => {
65+
const logger = new DebugLogger(true);
66+
logger.logConsent('none', 'full');
67+
68+
expect(logSpy).toHaveBeenCalledWith(
69+
'[Immutable Audience] consent none → full',
70+
);
71+
});
72+
73+
it('should log warnings', () => {
74+
const logger = new DebugLogger(true);
75+
logger.logWarning('something went wrong');
76+
77+
expect(warnSpy).toHaveBeenCalledWith(
78+
'[Immutable Audience] something went wrong',
79+
);
80+
});
81+
});

0 commit comments

Comments
 (0)