@amplytools/react-native-amply-sdk 0.15.0 → 0.15.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/dist/cjs/campaignTouch.js +44 -0
  2. package/dist/cjs/campaignTouch.js.map +1 -0
  3. package/dist/cjs/hooks/useAmplySystemEvents.js +60 -0
  4. package/dist/cjs/hooks/useAmplySystemEvents.js.map +1 -0
  5. package/dist/cjs/index.js +723 -0
  6. package/dist/cjs/index.js.map +1 -0
  7. package/dist/cjs/nativeModule.js +55 -0
  8. package/dist/cjs/nativeModule.js.map +1 -0
  9. package/dist/cjs/nativeSpecs/NativeAmplyModule.js +5 -0
  10. package/dist/cjs/nativeSpecs/NativeAmplyModule.js.map +1 -0
  11. package/dist/cjs/systemEventUtils.js +47 -0
  12. package/dist/cjs/systemEventUtils.js.map +1 -0
  13. package/dist/cjs/systemEvents.js +27 -0
  14. package/dist/cjs/systemEvents.js.map +1 -0
  15. package/docs/ARCHITECTURE.md +26 -19
  16. package/package.json +12 -6
  17. package/scripts/check-package.js +159 -0
  18. package/dist/index.js +0 -272
  19. package/dist/index.js.map +0 -1
  20. package/dist/index.mjs +0 -234
  21. package/dist/index.mjs.map +0 -1
  22. package/dist/plugin/index.d.ts +0 -6
  23. package/dist/plugin/index.d.ts.map +0 -1
  24. package/dist/plugin/index.js +0 -186
  25. package/dist/plugin/index.js.map +0 -1
  26. package/dist/plugin/index.mjs +0 -169
  27. package/dist/plugin/index.mjs.map +0 -1
  28. package/dist/plugin/withAmply.d.ts +0 -12
  29. package/dist/plugin/withAmply.d.ts.map +0 -1
  30. package/dist/src/__tests__/appLanguage.test.d.ts +0 -2
  31. package/dist/src/__tests__/appLanguage.test.d.ts.map +0 -1
  32. package/dist/src/__tests__/appLanguage.test.js +0 -211
  33. package/dist/src/__tests__/campaignResolved.test.d.ts +0 -2
  34. package/dist/src/__tests__/campaignResolved.test.d.ts.map +0 -1
  35. package/dist/src/__tests__/campaignResolved.test.js +0 -208
  36. package/dist/src/__tests__/gateRegistrationLifecycle.test.d.ts +0 -2
  37. package/dist/src/__tests__/gateRegistrationLifecycle.test.d.ts.map +0 -1
  38. package/dist/src/__tests__/gateRegistrationLifecycle.test.js +0 -215
  39. package/dist/src/__tests__/handleUrl.test.d.ts +0 -2
  40. package/dist/src/__tests__/handleUrl.test.d.ts.map +0 -1
  41. package/dist/src/__tests__/handleUrl.test.js +0 -114
  42. package/dist/src/__tests__/index.test.d.ts +0 -2
  43. package/dist/src/__tests__/index.test.d.ts.map +0 -1
  44. package/dist/src/__tests__/index.test.js +0 -204
  45. package/dist/src/__tests__/lastCampaignTouch.test.d.ts +0 -2
  46. package/dist/src/__tests__/lastCampaignTouch.test.d.ts.map +0 -1
  47. package/dist/src/__tests__/lastCampaignTouch.test.js +0 -119
  48. package/dist/src/__tests__/systemEventUtils.test.d.ts +0 -2
  49. package/dist/src/__tests__/systemEventUtils.test.d.ts.map +0 -1
  50. package/dist/src/__tests__/systemEventUtils.test.js +0 -72
  51. package/dist/tsconfig.tsbuildinfo +0 -1
  52. package/plugin/tsconfig.tsbuildinfo +0 -1
  53. package/src/__tests__/appLanguage.test.ts +0 -298
  54. package/src/__tests__/campaignResolved.test.ts +0 -294
  55. package/src/__tests__/gateRegistrationLifecycle.test.ts +0 -281
  56. package/src/__tests__/handleUrl.test.ts +0 -155
  57. package/src/__tests__/index.test.ts +0 -258
  58. package/src/__tests__/lastCampaignTouch.test.ts +0 -151
  59. package/src/__tests__/systemEventUtils.test.ts +0 -97
@@ -1,155 +0,0 @@
1
- /**
2
- * `handleUrl` hands an incoming link to the native SDK and reports whether the SDK recognised it.
3
- * It is synchronous, and it never throws: an older native build without the method answers false.
4
- */
5
- type SdkModule = typeof import('../index');
6
-
7
- function loadWith(native: Record<string, unknown>): SdkModule {
8
- // Absent isInitialized means "SDK up" for the plain pass-through tests below.
9
- const withState = {isInitialized: () => true, ...native};
10
- let Amply!: SdkModule;
11
- jest.isolateModules(() => {
12
- // eslint-disable-next-line @typescript-eslint/no-var-requires
13
- require('../nativeModule').__setNativeModule(withState as never);
14
- // eslint-disable-next-line @typescript-eslint/no-var-requires
15
- Amply = require('../index');
16
- });
17
- return Amply;
18
- }
19
-
20
- const DEBUG_URL = 'myapp://amply-debug/campaign?t=abc';
21
-
22
- describe('handleUrl', () => {
23
- it('passes the DEBUG_URL to the native module and returns its answer', () => {
24
- const handleUrl = jest.fn(() => true);
25
- const Amply = loadWith({handleUrl});
26
-
27
- expect(Amply.handleUrl(DEBUG_URL)).toBe(true);
28
- expect(handleUrl).toHaveBeenCalledWith(DEBUG_URL);
29
- });
30
-
31
- it('returns false when the native module does not recognise the DEBUG_URL', () => {
32
- const Amply = loadWith({handleUrl: jest.fn(() => false)});
33
-
34
- expect(Amply.handleUrl('myapp://settings')).toBe(false);
35
- });
36
-
37
- it('returns false without throwing when the native module has no handleUrl', () => {
38
- const Amply = loadWith({});
39
-
40
- expect(Amply.handleUrl(DEBUG_URL)).toBe(false);
41
- });
42
-
43
- it('returns false without throwing when the native call throws', () => {
44
- const Amply = loadWith({
45
- handleUrl: jest.fn(() => {
46
- throw new Error('boom');
47
- }),
48
- });
49
-
50
- expect(Amply.handleUrl(DEBUG_URL)).toBe(false);
51
- });
52
-
53
- it('returns false for a non-string DEBUG_URL', () => {
54
- const handleUrl = jest.fn(() => true);
55
- const Amply = loadWith({handleUrl});
56
-
57
- expect(Amply.handleUrl(null as never)).toBe(false);
58
- expect(handleUrl).not.toHaveBeenCalled();
59
- });
60
-
61
- it('is on the default export', () => {
62
- const Amply = loadWith({handleUrl: jest.fn(() => true)});
63
-
64
- expect(Amply.default.handleUrl(DEBUG_URL)).toBe(true);
65
- });
66
- });
67
-
68
- describe('handleUrl before initialize', () => {
69
- function loadUninitialised(initialize?: () => Promise<void>) {
70
- let up = false;
71
- const handleUrl = jest.fn(() => true);
72
- const native = {
73
- handleUrl,
74
- isInitialized: jest.fn(() => up),
75
- initialize: jest.fn(
76
- initialize ??
77
- (async () => {
78
- up = true;
79
- }),
80
- ),
81
- addListener: jest.fn(),
82
- removeListeners: jest.fn(),
83
- };
84
- return {Amply: loadWith(native), handleUrl};
85
- }
86
- const KEYS = {appId: 'id', apiKeyPublic: 'public', apiKeySecret: 'secret'};
87
-
88
- it('parks a debug link, returns true and does not call native', () => {
89
- const {Amply, handleUrl} = loadUninitialised();
90
-
91
- expect(Amply.handleUrl(DEBUG_URL)).toBe(true);
92
- expect(handleUrl).not.toHaveBeenCalled();
93
- });
94
-
95
- it('replays the parked link to native once after initialize', async () => {
96
- const {Amply, handleUrl} = loadUninitialised();
97
- Amply.handleUrl(DEBUG_URL);
98
-
99
- await Amply.initialize(KEYS);
100
-
101
- expect(handleUrl).toHaveBeenCalledTimes(1);
102
- expect(handleUrl).toHaveBeenCalledWith(DEBUG_URL);
103
- });
104
-
105
- it('replays only the newer of two parked links', async () => {
106
- const {Amply, handleUrl} = loadUninitialised();
107
- Amply.handleUrl('myapp://amply-debug/campaign?t=old');
108
- Amply.handleUrl('myapp://amply-debug/campaign?t=new');
109
-
110
- await Amply.initialize(KEYS);
111
-
112
- expect(handleUrl).toHaveBeenCalledTimes(1);
113
- expect(handleUrl).toHaveBeenCalledWith('myapp://amply-debug/campaign?t=new');
114
- });
115
-
116
- it('does not park a non-debug link', async () => {
117
- const {Amply, handleUrl} = loadUninitialised();
118
-
119
- expect(Amply.handleUrl('myapp://settings')).toBe(false);
120
- await Amply.initialize(KEYS);
121
-
122
- expect(handleUrl).not.toHaveBeenCalled();
123
- });
124
-
125
- it('drops the parked link when initialize fails', async () => {
126
- const {Amply, handleUrl} = loadUninitialised(async () => {
127
- throw new Error('init failed');
128
- });
129
- Amply.handleUrl(DEBUG_URL);
130
-
131
- await expect(Amply.initialize(KEYS)).rejects.toThrow('init failed');
132
-
133
- expect(handleUrl).not.toHaveBeenCalled();
134
- });
135
-
136
- it.each([
137
- '',
138
- 'not a url',
139
- 'myapp://amply-debug/campaign',
140
- 'myapp://amply-debug/campaign?t=',
141
- 'myapp://amply-debug/other?t=abc',
142
- 'myapp://other/campaign?t=abc',
143
- 'amply-debug/campaign?t=abc',
144
- '://amply-debug/campaign?t=abc',
145
- ])('returns false without throwing for %p', async url => {
146
- const {Amply, handleUrl} = loadUninitialised();
147
-
148
- expect(Amply.handleUrl(url)).toBe(false);
149
- await Amply.initialize(KEYS);
150
-
151
- expect(handleUrl).not.toHaveBeenCalled();
152
- });
153
- });
154
-
155
- export {};
@@ -1,258 +0,0 @@
1
- import * as Amply from '../index';
2
- import {__setNativeModule} from '../nativeModule';
3
-
4
- const mockNativeModule = {
5
- initialize: jest.fn(),
6
- isInitialized: jest.fn(() => true),
7
- track: jest.fn(),
8
- getRecentEvents: jest.fn(),
9
- getDataSetSnapshot: jest.fn(),
10
- registerDeepLinkListener: jest.fn(),
11
- addListener: jest.fn(),
12
- removeListeners: jest.fn(),
13
- onSystemEvent: jest.fn(),
14
- onDeepLink: jest.fn(),
15
- setCustomProperties: jest.fn(),
16
- getCustomProperty: jest.fn(),
17
- removeCustomProperty: jest.fn(),
18
- clearCustomProperties: jest.fn(),
19
- };
20
-
21
- describe('Amply JS API', () => {
22
- afterEach(() => {
23
- jest.clearAllMocks();
24
- __setNativeModule(null);
25
- });
26
-
27
- beforeEach(() => {
28
- __setNativeModule(mockNativeModule as never);
29
- });
30
-
31
- it('initializes the native module', async () => {
32
- await Amply.initialize({ appId: 'id', apiKeyPublic: 'public', apiKeySecret: 'secret' });
33
-
34
- expect(mockNativeModule.initialize).toHaveBeenCalledWith(
35
- expect.objectContaining({ appId: 'id', apiKeyPublic: 'public' })
36
- );
37
- });
38
-
39
- // A missing key used to reach the native SDK, whose config builder threw an exception that
40
- // crossed into Objective-C uncaught and aborted the host app — no JS try/catch could stop it.
41
- // Bad keys must cost the app its Amply, never its process.
42
- describe.each([
43
- ['apiKeySecret', { appId: 'id', apiKeyPublic: 'public' }],
44
- ['apiKeySecret', { appId: 'id', apiKeyPublic: 'public', apiKeySecret: '' }],
45
- ['apiKeySecret', { appId: 'id', apiKeyPublic: 'public', apiKeySecret: ' ' }],
46
- ['apiKeySecret', { appId: 'id', apiKeyPublic: 'public', apiKeySecret: null }],
47
- ['apiKeyPublic', { appId: 'id', apiKeyPublic: '', apiKeySecret: 'secret' }],
48
- ['appId', { appId: '', apiKeyPublic: 'public', apiKeySecret: 'secret' }],
49
- ])('when %s is missing (%j)', (field, config) => {
50
- it('rejects with AMP_INVALID_CONFIG, names the field and never calls the native module', async () => {
51
- const error = jest.spyOn(console, 'error').mockImplementation(() => {});
52
-
53
- await expect(Amply.initialize(config as never)).rejects.toMatchObject({
54
- code: 'AMP_INVALID_CONFIG',
55
- message: expect.stringContaining(field),
56
- });
57
- expect(mockNativeModule.initialize).not.toHaveBeenCalled();
58
- expect(error).toHaveBeenCalledWith(expect.stringContaining(field));
59
-
60
- error.mockRestore();
61
- });
62
- });
63
-
64
- it('passes the base-URL overrides through to the native layer', async () => {
65
- await Amply.initialize({
66
- appId: 'id',
67
- apiKeyPublic: 'public',
68
- apiKeySecret: 'secret',
69
- configBaseUrl: 'https://config-staging.example.com',
70
- backendBaseUrl: 'https://api-staging.example.com',
71
- });
72
-
73
- expect(mockNativeModule.initialize).toHaveBeenCalledWith(
74
- expect.objectContaining({
75
- configBaseUrl: 'https://config-staging.example.com',
76
- backendBaseUrl: 'https://api-staging.example.com',
77
- })
78
- );
79
- });
80
-
81
- // `endpoint` is the older single-URL spelling. It stays accepted so existing
82
- // callers keep working, but it only ever meant the backend host — the campaign
83
- // manifest is served from a different one.
84
- it('still accepts the legacy endpoint spelling', async () => {
85
- await Amply.initialize({
86
- appId: 'id',
87
- apiKeyPublic: 'public',
88
- apiKeySecret: 'secret',
89
- endpoint: 'https://api-staging.example.com',
90
- });
91
-
92
- expect(mockNativeModule.initialize).toHaveBeenCalledWith(
93
- expect.objectContaining({ endpoint: 'https://api-staging.example.com' })
94
- );
95
- });
96
-
97
- it('tracks events through the native layer', async () => {
98
- await Amply.track({ name: 'Test Event', properties: { foo: 'bar' } });
99
-
100
- expect(mockNativeModule.track).toHaveBeenCalledWith({
101
- name: 'Test Event',
102
- properties: { foo: 'bar' },
103
- });
104
- });
105
-
106
- it('reads recent events', async () => {
107
- const events = [{ name: 'e1' }];
108
- mockNativeModule.getRecentEvents.mockResolvedValueOnce(events);
109
-
110
- const result = await Amply.getRecentEvents(3);
111
-
112
- expect(mockNativeModule.getRecentEvents).toHaveBeenCalledWith(3);
113
- expect(result).toBe(events);
114
- });
115
-
116
- it('requests dataset snapshots', async () => {
117
- const snapshot = { foo: 'bar' };
118
- mockNativeModule.getDataSetSnapshot.mockResolvedValueOnce(snapshot);
119
-
120
- const type = { kind: '@device' as const };
121
- const result = await Amply.getDataSetSnapshot(type);
122
-
123
- expect(mockNativeModule.getDataSetSnapshot).toHaveBeenCalledWith(type);
124
- expect(result).toBe(snapshot);
125
- });
126
-
127
- it.each([
128
- ['singleton @device', { kind: '@device' as const }],
129
- ['singleton @user', { kind: '@user' as const }],
130
- ['singleton @custom', { kind: '@custom' as const }],
131
- ['singleton @session', { kind: '@session' as const }],
132
- [
133
- '@triggeredEvent with global strategy',
134
- {
135
- kind: '@triggeredEvent' as const,
136
- data: {
137
- countStrategy: 'global' as const,
138
- params: [{ name: 'level', value: 5 }],
139
- eventName: 'LevelComplete',
140
- },
141
- },
142
- ],
143
- [
144
- '@triggeredEvent with session strategy and no eventName',
145
- {
146
- kind: '@triggeredEvent' as const,
147
- data: {
148
- countStrategy: 'session' as const,
149
- params: [],
150
- },
151
- },
152
- ],
153
- [
154
- '@events with mixed event types',
155
- {
156
- kind: '@events' as const,
157
- data: [
158
- { name: 'Purchase', type: 'custom' as const, params: [{ name: 'sku', value: 'ABC' }] },
159
- { name: 'SessionStarted', type: 'system' as const, params: [] },
160
- ],
161
- },
162
- ],
163
- ])('passes %s to the native bridge unchanged', async (_label, type) => {
164
- mockNativeModule.getDataSetSnapshot.mockResolvedValueOnce({});
165
-
166
- await Amply.getDataSetSnapshot(type);
167
-
168
- expect(mockNativeModule.getDataSetSnapshot).toHaveBeenCalledWith(type);
169
- });
170
-
171
- it('subscribes to system events through the TurboModule emitter', async () => {
172
- const remove = jest.fn();
173
- mockNativeModule.onSystemEvent.mockReturnValueOnce({ remove });
174
-
175
- const listener = jest.fn();
176
- const unsubscribe = await Amply.addSystemEventListener(listener);
177
-
178
- expect(mockNativeModule.onSystemEvent).toHaveBeenCalledTimes(1);
179
-
180
- // The native listener is a wrapper that filters DebugLog events
181
- const nativeListener = mockNativeModule.onSystemEvent.mock.calls[0][0];
182
-
183
- // DebugLog events should be filtered out
184
- nativeListener({ name: 'DebugLog', type: 'log', timestamp: 1, properties: {} });
185
- expect(listener).not.toHaveBeenCalled();
186
-
187
- // Other events should pass through
188
- const systemEvent = { name: 'SessionStart', type: 'system', timestamp: 2, properties: {} };
189
- nativeListener(systemEvent);
190
- expect(listener).toHaveBeenCalledWith(systemEvent);
191
-
192
- unsubscribe();
193
- expect(remove).toHaveBeenCalledTimes(1);
194
- });
195
-
196
- it('sets a single custom property via setCustomProperties', () => {
197
- Amply.setCustomProperty('plan', 'premium');
198
-
199
- expect(mockNativeModule.setCustomProperties).toHaveBeenCalledWith({
200
- plan: 'premium',
201
- });
202
- });
203
-
204
- it('sets multiple custom properties at once', () => {
205
- Amply.setCustomProperties({ plan: 'premium', age: 25, active: true });
206
-
207
- expect(mockNativeModule.setCustomProperties).toHaveBeenCalledWith({
208
- plan: 'premium',
209
- age: 25,
210
- active: true,
211
- });
212
- });
213
-
214
- it('gets a custom property value', async () => {
215
- mockNativeModule.getCustomProperty.mockResolvedValueOnce({ value: 'premium' });
216
-
217
- const result = await Amply.getCustomProperty('plan');
218
-
219
- expect(mockNativeModule.getCustomProperty).toHaveBeenCalledWith('plan');
220
- expect(result).toBe('premium');
221
- });
222
-
223
- it('returns null for a missing custom property', async () => {
224
- mockNativeModule.getCustomProperty.mockResolvedValueOnce({});
225
-
226
- const result = await Amply.getCustomProperty('nonexistent');
227
-
228
- expect(result).toBeNull();
229
- });
230
-
231
- it('removes a custom property', () => {
232
- Amply.removeCustomProperty('plan');
233
-
234
- expect(mockNativeModule.removeCustomProperty).toHaveBeenCalledWith('plan');
235
- });
236
-
237
- it('clears all custom properties', () => {
238
- Amply.clearCustomProperties();
239
-
240
- expect(mockNativeModule.clearCustomProperties).toHaveBeenCalled();
241
- });
242
-
243
- it('registers deep links once and subscribes via the TurboModule emitter', async () => {
244
- const remove = jest.fn();
245
- mockNativeModule.onDeepLink.mockReturnValue({ remove });
246
-
247
- const listener = jest.fn();
248
- const unsubscribe = await Amply.addDeepLinkListener(listener);
249
- expect(mockNativeModule.registerDeepLinkListener).toHaveBeenCalledTimes(1);
250
- expect(mockNativeModule.onDeepLink).toHaveBeenCalledWith(listener);
251
-
252
- await Amply.addDeepLinkListener(() => {});
253
- expect(mockNativeModule.registerDeepLinkListener).toHaveBeenCalledTimes(1);
254
-
255
- unsubscribe();
256
- expect(remove).toHaveBeenCalledTimes(1);
257
- });
258
- });
@@ -1,151 +0,0 @@
1
- import * as Amply from '../index';
2
- import {__setNativeModule} from '../nativeModule';
3
-
4
- const mockNativeModule = {
5
- isInitialized: jest.fn(() => true),
6
- getLastCampaignTouch: jest.fn(),
7
- };
8
-
9
- const ONE_HOUR_MS = 60 * 60 * 1000;
10
-
11
- describe('getLastCampaignTouch', () => {
12
- let warn: jest.SpyInstance;
13
-
14
- beforeEach(() => {
15
- __setNativeModule(mockNativeModule as never);
16
- warn = jest.spyOn(console, 'warn').mockImplementation(() => {});
17
- });
18
-
19
- afterEach(() => {
20
- warn.mockRestore();
21
- jest.clearAllMocks();
22
- __setNativeModule(null);
23
- });
24
-
25
- it('passes withinMs through to the native module and returns the touch', async () => {
26
- mockNativeModule.getLastCampaignTouch.mockResolvedValueOnce({
27
- campaignId: 'cmp_1',
28
- campaignName: 'Spring offer',
29
- kind: 'shown',
30
- atMs: 1_760_000_000_000,
31
- });
32
-
33
- const touch = await Amply.getLastCampaignTouch(ONE_HOUR_MS);
34
-
35
- expect(mockNativeModule.getLastCampaignTouch).toHaveBeenCalledWith(ONE_HOUR_MS);
36
- expect(touch).toEqual({
37
- campaignId: 'cmp_1',
38
- campaignName: 'Spring offer',
39
- kind: 'shown',
40
- atMs: 1_760_000_000_000,
41
- });
42
- });
43
-
44
- it('is exposed on the default export too', async () => {
45
- mockNativeModule.getLastCampaignTouch.mockResolvedValueOnce(null);
46
-
47
- await expect(Amply.default.getLastCampaignTouch(0)).resolves.toBeNull();
48
- });
49
-
50
- it('resolves null when the native side has no touch', async () => {
51
- mockNativeModule.getLastCampaignTouch.mockResolvedValueOnce(null);
52
-
53
- await expect(Amply.getLastCampaignTouch(ONE_HOUR_MS)).resolves.toBeNull();
54
- });
55
-
56
- it('resolves null when the native side resolves undefined', async () => {
57
- mockNativeModule.getLastCampaignTouch.mockResolvedValueOnce(undefined);
58
-
59
- await expect(Amply.getLastCampaignTouch(ONE_HOUR_MS)).resolves.toBeNull();
60
- });
61
-
62
- it.each([
63
- ['shown', 'shown'],
64
- ['engaged', 'engaged'],
65
- ['SHOWN', 'shown'],
66
- ['ENGAGED', 'engaged'],
67
- ])('maps native kind %s to %s', async (nativeKind, expected) => {
68
- mockNativeModule.getLastCampaignTouch.mockResolvedValueOnce({
69
- campaignId: 'cmp_1',
70
- campaignName: 'Spring offer',
71
- kind: nativeKind,
72
- atMs: 1,
73
- });
74
-
75
- const touch = await Amply.getLastCampaignTouch(ONE_HOUR_MS);
76
-
77
- expect(touch?.kind).toBe(expected);
78
- });
79
-
80
- it('resolves null and warns for a kind this bridge does not know', async () => {
81
- mockNativeModule.getLastCampaignTouch.mockResolvedValueOnce({
82
- campaignId: 'cmp_1',
83
- campaignName: 'Spring offer',
84
- kind: 'clicked',
85
- atMs: 1,
86
- });
87
-
88
- await expect(Amply.getLastCampaignTouch(ONE_HOUR_MS)).resolves.toBeNull();
89
- expect(warn).toHaveBeenCalledWith(expect.stringContaining('getLastCampaignTouch'), expect.anything());
90
- });
91
-
92
- it.each([
93
- ['a missing campaignId', {campaignName: 'x', kind: 'shown', atMs: 1}],
94
- ['a non-numeric atMs', {campaignId: 'c', campaignName: 'x', kind: 'shown', atMs: '1'}],
95
- ])('resolves null for a payload with %s', async (_label, payload) => {
96
- mockNativeModule.getLastCampaignTouch.mockResolvedValueOnce(payload);
97
-
98
- await expect(Amply.getLastCampaignTouch(ONE_HOUR_MS)).resolves.toBeNull();
99
- });
100
-
101
- it('keeps an empty campaign name rather than dropping the touch', async () => {
102
- mockNativeModule.getLastCampaignTouch.mockResolvedValueOnce({
103
- campaignId: 'cmp_1',
104
- campaignName: '',
105
- kind: 'engaged',
106
- atMs: 5,
107
- });
108
-
109
- await expect(Amply.getLastCampaignTouch(ONE_HOUR_MS)).resolves.toEqual({
110
- campaignId: 'cmp_1',
111
- campaignName: '',
112
- kind: 'engaged',
113
- atMs: 5,
114
- });
115
- });
116
-
117
- it('resolves null instead of rejecting when the native call fails', async () => {
118
- mockNativeModule.getLastCampaignTouch.mockRejectedValueOnce(new Error('bridge gone'));
119
-
120
- await expect(Amply.getLastCampaignTouch(ONE_HOUR_MS)).resolves.toBeNull();
121
- expect(warn).toHaveBeenCalled();
122
- });
123
-
124
- it('accepts zero', async () => {
125
- mockNativeModule.getLastCampaignTouch.mockResolvedValueOnce(null);
126
-
127
- await Amply.getLastCampaignTouch(0);
128
-
129
- expect(mockNativeModule.getLastCampaignTouch).toHaveBeenCalledWith(0);
130
- });
131
-
132
- it('leaves a large window to the native clamp', async () => {
133
- mockNativeModule.getLastCampaignTouch.mockResolvedValueOnce(null);
134
- const ninetyDaysMs = 90 * 24 * ONE_HOUR_MS;
135
-
136
- await Amply.getLastCampaignTouch(ninetyDaysMs);
137
-
138
- expect(mockNativeModule.getLastCampaignTouch).toHaveBeenCalledWith(ninetyDaysMs);
139
- });
140
-
141
- it.each([
142
- ['negative', -1],
143
- ['NaN', Number.NaN],
144
- ['Infinity', Number.POSITIVE_INFINITY],
145
- ['a string', '3600000'],
146
- ['undefined', undefined],
147
- ])('rejects with a RangeError for %s and never calls native', async (_label, value) => {
148
- await expect(Amply.getLastCampaignTouch(value as never)).rejects.toBeInstanceOf(RangeError);
149
- expect(mockNativeModule.getLastCampaignTouch).not.toHaveBeenCalled();
150
- });
151
- });
@@ -1,97 +0,0 @@
1
- import {formatSystemEventLabel} from '../systemEventUtils';
2
- import type {EventRecord} from '../nativeSpecs/NativeAmplyModule';
3
-
4
- function makeEvent(
5
- name: string,
6
- properties: Record<string, unknown> = {},
7
- ): EventRecord {
8
- return {name, type: 'system', timestamp: Date.now(), properties};
9
- }
10
-
11
- describe('formatSystemEventLabel', () => {
12
- describe('SessionStarted', () => {
13
- it('returns "Session started (cold)" for cold start', () => {
14
- const event = makeEvent('SessionStarted', {type: 'cold'});
15
- expect(formatSystemEventLabel(event)).toBe('Session started (cold)');
16
- });
17
-
18
- it('returns "Session started (warm)" for warm start', () => {
19
- const event = makeEvent('SessionStarted', {type: 'warm'});
20
- expect(formatSystemEventLabel(event)).toBe('Session started (warm)');
21
- });
22
-
23
- it('returns "Session started" when type is absent', () => {
24
- const event = makeEvent('SessionStarted');
25
- expect(formatSystemEventLabel(event)).toBe('Session started');
26
- });
27
-
28
- it('handles unexpected type values gracefully', () => {
29
- const event = makeEvent('SessionStarted', {type: 'unknown'});
30
- expect(formatSystemEventLabel(event)).toBe('Session started (unknown)');
31
- });
32
- });
33
-
34
- describe('other events', () => {
35
- it('formats CampaignShown with campaignId and source', () => {
36
- const event = makeEvent('CampaignShown', {
37
- campaignId: 'abc',
38
- source: 'click',
39
- });
40
- expect(formatSystemEventLabel(event)).toBe(
41
- 'Campaign shown (abc) via click',
42
- );
43
- });
44
-
45
- it('names the campaign in CampaignShown when the SDK sends campaignName', () => {
46
- const event = makeEvent('CampaignShown', {
47
- campaignId: 'abc',
48
- campaignName: 'Spring offer',
49
- source: 'click',
50
- });
51
- expect(formatSystemEventLabel(event)).toBe(
52
- 'Campaign shown "Spring offer" (abc) via click',
53
- );
54
- });
55
-
56
- it('ignores an empty campaignName in CampaignShown', () => {
57
- const event = makeEvent('CampaignShown', {
58
- campaignId: 'abc',
59
- campaignName: '',
60
- source: 'click',
61
- });
62
- expect(formatSystemEventLabel(event)).toBe(
63
- 'Campaign shown (abc) via click',
64
- );
65
- });
66
-
67
- it('formats EventTriggered with source event', () => {
68
- const event = makeEvent('EventTriggered', {sourceEvent: 'purchase'});
69
- expect(formatSystemEventLabel(event)).toBe('Event triggered (purchase)');
70
- });
71
-
72
- it('formats ConfigFetchFinished with campaign count', () => {
73
- const event = makeEvent('ConfigFetchFinished', {
74
- success: true,
75
- campaignCount: 3,
76
- });
77
- expect(formatSystemEventLabel(event)).toBe(
78
- 'Config fetch finished (3 campaigns)',
79
- );
80
- });
81
-
82
- it('formats ConfigFetchFinished failure', () => {
83
- const event = makeEvent('ConfigFetchFinished', {success: false});
84
- expect(formatSystemEventLabel(event)).toBe('Config fetch failed');
85
- });
86
-
87
- it('returns default label for SessionFinished', () => {
88
- const event = makeEvent('SessionFinished');
89
- expect(formatSystemEventLabel(event)).toBe('Session finished');
90
- });
91
-
92
- it('returns fallback for unknown events', () => {
93
- const event = makeEvent('UnknownEvent');
94
- expect(formatSystemEventLabel(event)).toBe('System event UnknownEvent');
95
- });
96
- });
97
- });