-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathanalytics.service.resilience.unit.tests.js
More file actions
191 lines (150 loc) · 7.84 KB
/
Copy pathanalytics.service.resilience.unit.tests.js
File metadata and controls
191 lines (150 loc) · 7.84 KB
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
/**
* Module dependencies.
*/
import { jest, beforeEach, afterEach, describe, test, expect } from '@jest/globals';
/**
* Unit tests for analytics service error resilience, shutdown safety,
* and edge cases not covered by the primary service unit tests.
*/
describe('Analytics service resilience tests:', () => {
let AnalyticsService;
let mockPostHogInstance;
beforeEach(async () => {
jest.resetModules();
mockPostHogInstance = {
capture: jest.fn(),
identify: jest.fn(),
groupIdentify: jest.fn(),
getFeatureFlag: jest.fn().mockResolvedValue('variant-a'),
isFeatureEnabled: jest.fn().mockResolvedValue(true),
shutdown: jest.fn().mockResolvedValue(undefined),
};
jest.unstable_mockModule('posthog-node', () => ({
PostHog: jest.fn().mockImplementation(() => mockPostHogInstance),
}));
jest.unstable_mockModule('../../../config/index.js', () => ({
default: { posthog: { apiKey: 'phc_key', host: 'https://test.posthog.com' } },
}));
const mod = await import('../services/analytics.service.js');
AnalyticsService = mod.default;
await AnalyticsService.init();
});
afterEach(() => {
jest.restoreAllMocks();
});
// ─────────────────────────────────────────────────────────────────────
// Error propagation — synchronous SDK failures
// ─────────────────────────────────────────────────────────────────────
describe('synchronous SDK error propagation:', () => {
test('track should propagate when client.capture throws', () => {
mockPostHogInstance.capture.mockImplementation(() => {
throw new Error('Capture error');
});
expect(() => AnalyticsService.track('u1', 'evt')).toThrow('Capture error');
});
test('identify should propagate when client.identify throws', () => {
mockPostHogInstance.identify.mockImplementation(() => {
throw new Error('Identify error');
});
expect(() => AnalyticsService.identify('u1', {})).toThrow('Identify error');
});
test('groupIdentify should propagate when client.groupIdentify throws', () => {
mockPostHogInstance.groupIdentify.mockImplementation(() => {
throw new Error('Group error');
});
expect(() => AnalyticsService.groupIdentify('company', 'org', {})).toThrow('Group error');
});
});
// ─────────────────────────────────────────────────────────────────────
// Error propagation — async SDK failures
// ─────────────────────────────────────────────────────────────────────
describe('async SDK error propagation:', () => {
test('getFeatureFlag should propagate rejection from SDK', async () => {
mockPostHogInstance.getFeatureFlag.mockRejectedValue(new Error('Timeout'));
await expect(AnalyticsService.getFeatureFlag('flag', 'u1')).rejects.toThrow('Timeout');
});
test('isFeatureEnabled should propagate rejection from SDK', async () => {
mockPostHogInstance.isFeatureEnabled.mockRejectedValue(new Error('Timeout'));
await expect(AnalyticsService.isFeatureEnabled('flag', 'u1')).rejects.toThrow('Timeout');
});
test('shutdown should propagate rejection from SDK', async () => {
mockPostHogInstance.shutdown.mockRejectedValue(new Error('Flush failed'));
await expect(AnalyticsService.shutdown()).rejects.toThrow('Flush failed');
});
});
// ─────────────────────────────────────────────────────────────────────
// Post-shutdown safety
// ─────────────────────────────────────────────────────────────────────
describe('post-shutdown safety:', () => {
test('double shutdown should be safe (second call is no-op)', async () => {
await AnalyticsService.shutdown();
// Second call — client is null, returns undefined synchronously
AnalyticsService.shutdown();
expect(mockPostHogInstance.shutdown).toHaveBeenCalledTimes(1);
});
test('track after shutdown should be a silent no-op', async () => {
await AnalyticsService.shutdown();
expect(() => AnalyticsService.track('u1', 'evt')).not.toThrow();
expect(mockPostHogInstance.capture).not.toHaveBeenCalled();
});
test('identify after shutdown should be a silent no-op', async () => {
await AnalyticsService.shutdown();
expect(() => AnalyticsService.identify('u1', { name: 'a' })).not.toThrow();
expect(mockPostHogInstance.identify).not.toHaveBeenCalled();
});
test('groupIdentify after shutdown should be a silent no-op', async () => {
await AnalyticsService.shutdown();
expect(() => AnalyticsService.groupIdentify('company', 'org', {})).not.toThrow();
expect(mockPostHogInstance.groupIdentify).not.toHaveBeenCalled();
});
test('getFeatureFlag after shutdown returns undefined', async () => {
await AnalyticsService.shutdown();
const result = await AnalyticsService.getFeatureFlag('flag', 'u1');
expect(result).toBeUndefined();
});
test('isFeatureEnabled after shutdown returns undefined', async () => {
await AnalyticsService.shutdown();
const result = await AnalyticsService.isFeatureEnabled('flag', 'u1');
expect(result).toBeUndefined();
});
});
// ─────────────────────────────────────────────────────────────────────
// Minimal / optional argument handling
// ─────────────────────────────────────────────────────────────────────
describe('optional argument handling:', () => {
test('track with only required args passes undefined for optional params', () => {
AnalyticsService.track('u1', 'evt');
expect(mockPostHogInstance.capture).toHaveBeenCalledWith({
distinctId: 'u1',
event: 'evt',
properties: undefined,
groups: undefined,
});
});
test('identify with no properties passes undefined', () => {
AnalyticsService.identify('u1');
expect(mockPostHogInstance.identify).toHaveBeenCalledWith({
distinctId: 'u1',
properties: undefined,
});
});
test('groupIdentify with no properties passes undefined', () => {
AnalyticsService.groupIdentify('company', 'org-1');
expect(mockPostHogInstance.groupIdentify).toHaveBeenCalledWith({
groupType: 'company',
groupKey: 'org-1',
properties: undefined,
});
});
test('getFeatureFlag forwards options to SDK', async () => {
const opts = { groups: { company: 'org-1' } };
await AnalyticsService.getFeatureFlag('flag', 'u1', opts);
expect(mockPostHogInstance.getFeatureFlag).toHaveBeenCalledWith('flag', 'u1', opts);
});
test('isFeatureEnabled forwards options to SDK', async () => {
const opts = { personProperties: { plan: 'pro' } };
await AnalyticsService.isFeatureEnabled('flag', 'u1', opts);
expect(mockPostHogInstance.isFeatureEnabled).toHaveBeenCalledWith('flag', 'u1', opts);
});
});
});