@adtrackify/at-service-common 4.0.13 → 4.0.15

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 (109) hide show
  1. package/dist/cjs/__tests__/identity-cache/helpers.spec.js +37 -38
  2. package/dist/cjs/__tests__/identity-cache/helpers.spec.js.map +1 -1
  3. package/dist/cjs/__tests__/identity-cache/identity-cache-tier-routing.spec.js +92 -349
  4. package/dist/cjs/__tests__/identity-cache/identity-cache-tier-routing.spec.js.map +1 -1
  5. package/dist/cjs/__tests__/identity-cache/identity-client.spec.js +360 -0
  6. package/dist/cjs/__tests__/identity-cache/identity-client.spec.js.map +1 -0
  7. package/dist/cjs/__tests__/identity-cache/identity-gateway-cache-hit.spec.js +138 -0
  8. package/dist/cjs/__tests__/identity-cache/identity-gateway-cache-hit.spec.js.map +1 -0
  9. package/dist/cjs/__tests__/identity-cache/identity-gateway-cache-miss.spec.js +153 -0
  10. package/dist/cjs/__tests__/identity-cache/identity-gateway-cache-miss.spec.js.map +1 -0
  11. package/dist/cjs/__tests__/identity-cache/identity-gateway-constructor.spec.js +72 -0
  12. package/dist/cjs/__tests__/identity-cache/identity-gateway-constructor.spec.js.map +1 -0
  13. package/dist/cjs/__tests__/identity-cache/identity-gateway-edge-cases.spec.js +89 -0
  14. package/dist/cjs/__tests__/identity-cache/identity-gateway-edge-cases.spec.js.map +1 -0
  15. package/dist/cjs/__tests__/identity-cache/identity-gateway-enqueue-async.spec.js +136 -0
  16. package/dist/cjs/__tests__/identity-cache/identity-gateway-enqueue-async.spec.js.map +1 -0
  17. package/dist/cjs/__tests__/identity-cache/identity-gateway-stale-cache.spec.js +224 -0
  18. package/dist/cjs/__tests__/identity-cache/identity-gateway-stale-cache.spec.js.map +1 -0
  19. package/dist/cjs/__tests__/identity-cache/identity-gateway-test-setup.d.ts +28 -0
  20. package/dist/cjs/__tests__/identity-cache/identity-gateway-test-setup.js +90 -0
  21. package/dist/cjs/__tests__/identity-cache/identity-gateway-test-setup.js.map +1 -0
  22. package/dist/cjs/clients/internal-api/identity-client.d.ts +12 -2
  23. package/dist/cjs/clients/internal-api/identity-client.js +59 -13
  24. package/dist/cjs/clients/internal-api/identity-client.js.map +1 -1
  25. package/dist/cjs/services/db/identity-cache-dynamodb-service/identity-cache-dynamodb-db-service.d.ts +1 -7
  26. package/dist/cjs/services/db/identity-cache-dynamodb-service/identity-cache-dynamodb-db-service.js +0 -305
  27. package/dist/cjs/services/db/identity-cache-dynamodb-service/identity-cache-dynamodb-db-service.js.map +1 -1
  28. package/dist/cjs/services/db/identity-cache-dynamodb-service/identity-gateway-service.d.ts +14 -14
  29. package/dist/cjs/services/db/identity-cache-dynamodb-service/identity-gateway-service.js +122 -330
  30. package/dist/cjs/services/db/identity-cache-dynamodb-service/identity-gateway-service.js.map +1 -1
  31. package/dist/cjs/services/db/identity-cache-dynamodb-service/index.d.ts +0 -1
  32. package/dist/cjs/services/db/identity-cache-dynamodb-service/index.js +0 -1
  33. package/dist/cjs/services/db/identity-cache-dynamodb-service/index.js.map +1 -1
  34. package/dist/cjs/services/db/identity-cache-dynamodb-service/types.d.ts +8 -158
  35. package/dist/cjs/services/db/identity-cache-dynamodb-service/types.js +1 -15
  36. package/dist/cjs/services/db/identity-cache-dynamodb-service/types.js.map +1 -1
  37. package/dist/esm/__tests__/identity-cache/helpers.spec.js +26 -27
  38. package/dist/esm/__tests__/identity-cache/helpers.spec.js.map +1 -1
  39. package/dist/esm/__tests__/identity-cache/identity-cache-tier-routing.spec.js +92 -349
  40. package/dist/esm/__tests__/identity-cache/identity-cache-tier-routing.spec.js.map +1 -1
  41. package/dist/esm/__tests__/identity-cache/identity-client.spec.js +358 -0
  42. package/dist/esm/__tests__/identity-cache/identity-client.spec.js.map +1 -0
  43. package/dist/esm/__tests__/identity-cache/identity-gateway-cache-hit.spec.d.ts +1 -0
  44. package/dist/esm/__tests__/identity-cache/identity-gateway-cache-hit.spec.js +136 -0
  45. package/dist/esm/__tests__/identity-cache/identity-gateway-cache-hit.spec.js.map +1 -0
  46. package/dist/esm/__tests__/identity-cache/identity-gateway-cache-miss.spec.d.ts +1 -0
  47. package/dist/esm/__tests__/identity-cache/identity-gateway-cache-miss.spec.js +151 -0
  48. package/dist/esm/__tests__/identity-cache/identity-gateway-cache-miss.spec.js.map +1 -0
  49. package/dist/esm/__tests__/identity-cache/identity-gateway-constructor.spec.d.ts +1 -0
  50. package/dist/esm/__tests__/identity-cache/identity-gateway-constructor.spec.js +70 -0
  51. package/dist/esm/__tests__/identity-cache/identity-gateway-constructor.spec.js.map +1 -0
  52. package/dist/esm/__tests__/identity-cache/identity-gateway-edge-cases.spec.d.ts +1 -0
  53. package/dist/esm/__tests__/identity-cache/identity-gateway-edge-cases.spec.js +87 -0
  54. package/dist/esm/__tests__/identity-cache/identity-gateway-edge-cases.spec.js.map +1 -0
  55. package/dist/esm/__tests__/identity-cache/identity-gateway-enqueue-async.spec.d.ts +1 -0
  56. package/dist/esm/__tests__/identity-cache/identity-gateway-enqueue-async.spec.js +134 -0
  57. package/dist/esm/__tests__/identity-cache/identity-gateway-enqueue-async.spec.js.map +1 -0
  58. package/dist/esm/__tests__/identity-cache/identity-gateway-stale-cache.spec.d.ts +1 -0
  59. package/dist/esm/__tests__/identity-cache/identity-gateway-stale-cache.spec.js +222 -0
  60. package/dist/esm/__tests__/identity-cache/identity-gateway-stale-cache.spec.js.map +1 -0
  61. package/dist/esm/__tests__/identity-cache/identity-gateway-test-setup.d.ts +28 -0
  62. package/dist/esm/__tests__/identity-cache/identity-gateway-test-setup.js +83 -0
  63. package/dist/esm/__tests__/identity-cache/identity-gateway-test-setup.js.map +1 -0
  64. package/dist/esm/clients/internal-api/identity-client.d.ts +12 -2
  65. package/dist/esm/clients/internal-api/identity-client.js +60 -14
  66. package/dist/esm/clients/internal-api/identity-client.js.map +1 -1
  67. package/dist/esm/services/db/identity-cache-dynamodb-service/identity-cache-dynamodb-db-service.d.ts +1 -7
  68. package/dist/esm/services/db/identity-cache-dynamodb-service/identity-cache-dynamodb-db-service.js +1 -303
  69. package/dist/esm/services/db/identity-cache-dynamodb-service/identity-cache-dynamodb-db-service.js.map +1 -1
  70. package/dist/esm/services/db/identity-cache-dynamodb-service/identity-gateway-service.d.ts +14 -14
  71. package/dist/esm/services/db/identity-cache-dynamodb-service/identity-gateway-service.js +125 -330
  72. package/dist/esm/services/db/identity-cache-dynamodb-service/identity-gateway-service.js.map +1 -1
  73. package/dist/esm/services/db/identity-cache-dynamodb-service/index.d.ts +0 -1
  74. package/dist/esm/services/db/identity-cache-dynamodb-service/index.js +0 -1
  75. package/dist/esm/services/db/identity-cache-dynamodb-service/index.js.map +1 -1
  76. package/dist/esm/services/db/identity-cache-dynamodb-service/types.d.ts +8 -158
  77. package/dist/esm/services/db/identity-cache-dynamodb-service/types.js +0 -14
  78. package/dist/esm/services/db/identity-cache-dynamodb-service/types.js.map +1 -1
  79. package/package.json +2 -2
  80. package/dist/cjs/__tests__/identity-cache/identity-cache-dynamodb-db-service-v2-comparison.spec.js +0 -646
  81. package/dist/cjs/__tests__/identity-cache/identity-cache-dynamodb-db-service-v2-comparison.spec.js.map +0 -1
  82. package/dist/cjs/__tests__/identity-cache/identity-cache-dynamodb-service-v2-comparison.spec.js +0 -325
  83. package/dist/cjs/__tests__/identity-cache/identity-cache-dynamodb-service-v2-comparison.spec.js.map +0 -1
  84. package/dist/cjs/__tests__/identity-cache/identity-cache-dynamodb-service.spec.js +0 -1286
  85. package/dist/cjs/__tests__/identity-cache/identity-cache-dynamodb-service.spec.js.map +0 -1
  86. package/dist/cjs/__tests__/identity-cache/identity-gateway-service.spec.js +0 -2170
  87. package/dist/cjs/__tests__/identity-cache/identity-gateway-service.spec.js.map +0 -1
  88. package/dist/cjs/services/db/identity-cache-dynamodb-service/helpers.d.ts +0 -26
  89. package/dist/cjs/services/db/identity-cache-dynamodb-service/helpers.js +0 -252
  90. package/dist/cjs/services/db/identity-cache-dynamodb-service/helpers.js.map +0 -1
  91. package/dist/esm/__tests__/identity-cache/identity-cache-dynamodb-db-service-v2-comparison.spec.js +0 -644
  92. package/dist/esm/__tests__/identity-cache/identity-cache-dynamodb-db-service-v2-comparison.spec.js.map +0 -1
  93. package/dist/esm/__tests__/identity-cache/identity-cache-dynamodb-service-v2-comparison.spec.js +0 -323
  94. package/dist/esm/__tests__/identity-cache/identity-cache-dynamodb-service-v2-comparison.spec.js.map +0 -1
  95. package/dist/esm/__tests__/identity-cache/identity-cache-dynamodb-service.spec.js +0 -1284
  96. package/dist/esm/__tests__/identity-cache/identity-cache-dynamodb-service.spec.js.map +0 -1
  97. package/dist/esm/__tests__/identity-cache/identity-gateway-service.spec.js +0 -2168
  98. package/dist/esm/__tests__/identity-cache/identity-gateway-service.spec.js.map +0 -1
  99. package/dist/esm/services/db/identity-cache-dynamodb-service/helpers.d.ts +0 -26
  100. package/dist/esm/services/db/identity-cache-dynamodb-service/helpers.js +0 -235
  101. package/dist/esm/services/db/identity-cache-dynamodb-service/helpers.js.map +0 -1
  102. /package/dist/cjs/__tests__/identity-cache/{identity-cache-dynamodb-db-service-v2-comparison.spec.d.ts → identity-client.spec.d.ts} +0 -0
  103. /package/dist/cjs/__tests__/identity-cache/{identity-cache-dynamodb-service-v2-comparison.spec.d.ts → identity-gateway-cache-hit.spec.d.ts} +0 -0
  104. /package/dist/cjs/__tests__/identity-cache/{identity-cache-dynamodb-service.spec.d.ts → identity-gateway-cache-miss.spec.d.ts} +0 -0
  105. /package/dist/cjs/__tests__/identity-cache/{identity-gateway-service.spec.d.ts → identity-gateway-constructor.spec.d.ts} +0 -0
  106. /package/dist/{esm/__tests__/identity-cache/identity-cache-dynamodb-db-service-v2-comparison.spec.d.ts → cjs/__tests__/identity-cache/identity-gateway-edge-cases.spec.d.ts} +0 -0
  107. /package/dist/{esm/__tests__/identity-cache/identity-cache-dynamodb-service-v2-comparison.spec.d.ts → cjs/__tests__/identity-cache/identity-gateway-enqueue-async.spec.d.ts} +0 -0
  108. /package/dist/{esm/__tests__/identity-cache/identity-cache-dynamodb-service.spec.d.ts → cjs/__tests__/identity-cache/identity-gateway-stale-cache.spec.d.ts} +0 -0
  109. /package/dist/esm/__tests__/identity-cache/{identity-gateway-service.spec.d.ts → identity-client.spec.d.ts} +0 -0
@@ -1,2168 +0,0 @@
1
- const mockInvokeFunction = jest.fn();
2
- const mockBuildAndSendMessage = jest.fn();
3
- const mockSQSClientConstructor = jest.fn();
4
- jest.mock('../../clients/index.js', () => ({
5
- DynamoDbClient: {
6
- safeGet: jest.fn(),
7
- safePut: jest.fn(),
8
- safeDelete: jest.fn(),
9
- safeBatchGet: jest.fn(),
10
- safeBatchWrite: jest.fn(),
11
- safeQueryByGSI: jest.fn(),
12
- batchWrite: jest.fn(),
13
- },
14
- LambdaInvokeClient: jest.fn().mockImplementation(() => ({
15
- invokeFunction: mockInvokeFunction,
16
- })),
17
- SQSClient: jest.fn().mockImplementation((_region, _accountId, _queueName) => {
18
- mockSQSClientConstructor(_region, _accountId, _queueName);
19
- return {
20
- buildAndSendMessage: mockBuildAndSendMessage,
21
- queueUrl: `https://sqs.${_region}.amazonaws.com/${_accountId}/${_queueName}`,
22
- };
23
- }),
24
- }));
25
- jest.mock('../../helpers/index.js', () => ({
26
- Logger: {
27
- debug: jest.fn(),
28
- info: jest.fn(),
29
- warn: jest.fn(),
30
- error: jest.fn(),
31
- },
32
- }));
33
- import { IdentityGatewayService, } from '../../services/db/identity-cache-dynamodb-service/identity-gateway-service.js';
34
- import { IDENTITY_CACHE_WITH_CACHING_OUTCOME, } from '../../services/db/identity-cache-dynamodb-service/types.js';
35
- import { DynamoDbClient, SQSClient } from '../../clients/index.js';
36
- import { Logger } from '../../helpers/index.js';
37
- const mockedDynamoDbClient = DynamoDbClient;
38
- const mockedLogger = Logger;
39
- const mockedSQSClient = SQSClient;
40
- describe('IdentityGatewayService new features', () => {
41
- const pixelId = 'pixel123';
42
- const lambdaArn = 'arn:aws:lambda:us-east-1:123456789:function:identity-private';
43
- beforeEach(() => {
44
- jest.clearAllMocks();
45
- mockedDynamoDbClient.safeGet.mockReset();
46
- mockedDynamoDbClient.safePut.mockReset();
47
- mockedDynamoDbClient.safeDelete.mockReset();
48
- mockedDynamoDbClient.safeBatchGet.mockReset();
49
- mockedDynamoDbClient.safeBatchWrite.mockReset();
50
- mockedDynamoDbClient.safeQueryByGSI.mockReset();
51
- mockedDynamoDbClient.batchWrite.mockReset();
52
- mockInvokeFunction.mockReset();
53
- mockBuildAndSendMessage.mockReset();
54
- mockSQSClientConstructor.mockReset();
55
- mockedSQSClient.mockClear();
56
- IdentityGatewayService.lambdaInvokeClient = undefined;
57
- IdentityGatewayService.sqsClient = undefined;
58
- IdentityGatewayService.sqsQueueUrl = undefined;
59
- });
60
- describe('configure()', () => {
61
- it('should configure with direct queue URL', () => {
62
- const options = {
63
- asyncUpdateQueueUrl: 'https://sqs.us-east-1.amazonaws.com/123456789/test-queue',
64
- };
65
- IdentityGatewayService.configure(options);
66
- expect(IdentityGatewayService.sqsQueueUrl).toBe(options.asyncUpdateQueueUrl);
67
- });
68
- it('should configure with region/accountId/queueName', () => {
69
- const options = {
70
- awsRegion: 'us-east-1',
71
- awsAccountId: '123456789',
72
- asyncUpdateQueueName: 'identity-async-queue',
73
- };
74
- IdentityGatewayService.configure(options);
75
- expect(mockSQSClientConstructor).toHaveBeenCalledWith('us-east-1', '123456789', 'identity-async-queue');
76
- expect(IdentityGatewayService.sqsClient).toBeDefined();
77
- expect(IdentityGatewayService.sqsQueueUrl).toBeDefined();
78
- });
79
- it('should fallback to environment variable for queue name', () => {
80
- const originalEnv = process.env.IDENTIFY_ASYNC_UPDATE_QUEUE_NAME;
81
- process.env.IDENTIFY_ASYNC_UPDATE_QUEUE_NAME = 'env-queue-name';
82
- try {
83
- const options = {
84
- awsRegion: 'us-west-2',
85
- awsAccountId: '987654321',
86
- };
87
- IdentityGatewayService.configure(options);
88
- expect(mockSQSClientConstructor).toHaveBeenCalledWith('us-west-2', '987654321', 'env-queue-name');
89
- }
90
- finally {
91
- if (originalEnv !== undefined) {
92
- process.env.IDENTIFY_ASYNC_UPDATE_QUEUE_NAME = originalEnv;
93
- }
94
- else {
95
- delete process.env.IDENTIFY_ASYNC_UPDATE_QUEUE_NAME;
96
- }
97
- }
98
- });
99
- it('should not configure SQS when region/accountId provided but no queue name', () => {
100
- const originalEnv = process.env.IDENTIFY_ASYNC_UPDATE_QUEUE_NAME;
101
- delete process.env.IDENTIFY_ASYNC_UPDATE_QUEUE_NAME;
102
- try {
103
- const options = {
104
- awsRegion: 'us-east-1',
105
- awsAccountId: '123456789',
106
- };
107
- IdentityGatewayService.configure(options);
108
- expect(mockSQSClientConstructor).not.toHaveBeenCalled();
109
- expect(IdentityGatewayService.sqsClient).toBeUndefined();
110
- }
111
- finally {
112
- if (originalEnv !== undefined) {
113
- process.env.IDENTIFY_ASYNC_UPDATE_QUEUE_NAME = originalEnv;
114
- }
115
- }
116
- });
117
- it('should not configure SQS when only region provided', () => {
118
- const options = {
119
- awsRegion: 'us-east-1',
120
- };
121
- IdentityGatewayService.configure(options);
122
- expect(mockSQSClientConstructor).not.toHaveBeenCalled();
123
- expect(IdentityGatewayService.sqsClient).toBeUndefined();
124
- });
125
- });
126
- describe('enqueueAsyncIdentityUpdate()', () => {
127
- const testIdentity = {
128
- identityId: 'identity456',
129
- traits: { emails: ['test@email.com'] },
130
- };
131
- const testContext = {
132
- identity: testIdentity,
133
- };
134
- it('should return error result when SQS client is not configured', async () => {
135
- const result = await IdentityGatewayService.enqueueAsyncIdentityUpdate({
136
- pixelId,
137
- identity: testIdentity,
138
- context: testContext,
139
- reason: 'test reason',
140
- eventId: 'event123',
141
- });
142
- expect(result.success).toBe(false);
143
- expect(result.error).toBe('SQS client not configured');
144
- expect(result.messageId).toBeUndefined();
145
- expect(mockedLogger.warn).toHaveBeenCalledWith('IdentityGateway - enqueueAsyncIdentityUpdate: SQS client not configured', expect.objectContaining({
146
- pixelId,
147
- identityId: testIdentity.identityId,
148
- reason: 'test reason',
149
- eventId: 'event123',
150
- }));
151
- });
152
- it('should successfully enqueue message when SQS client is configured', async () => {
153
- IdentityGatewayService.configure({
154
- awsRegion: 'us-east-1',
155
- awsAccountId: '123456789',
156
- asyncUpdateQueueName: 'test-queue',
157
- });
158
- mockBuildAndSendMessage.mockResolvedValueOnce({
159
- MessageId: 'sqs-message-id-123',
160
- });
161
- const result = await IdentityGatewayService.enqueueAsyncIdentityUpdate({
162
- pixelId,
163
- identity: testIdentity,
164
- context: testContext,
165
- reason: 'stale cache traits',
166
- eventId: 'event456',
167
- });
168
- expect(result.success).toBe(true);
169
- expect(result.messageId).toBe('sqs-message-id-123');
170
- expect(result.error).toBeUndefined();
171
- expect(mockBuildAndSendMessage).toHaveBeenCalledWith('identify.updateAsync', expect.objectContaining({
172
- pixelId,
173
- incomingIdentityId: testIdentity.identityId,
174
- context: testContext,
175
- }));
176
- expect(mockedLogger.debug).toHaveBeenCalledWith('IdentityGateway - enqueueAsyncIdentityUpdate: message sent', expect.objectContaining({
177
- pixelId,
178
- identityId: testIdentity.identityId,
179
- reason: 'stale cache traits',
180
- eventId: 'event456',
181
- sqsMessageId: 'sqs-message-id-123',
182
- }));
183
- });
184
- it('should fail open with logging when SQS send fails', async () => {
185
- IdentityGatewayService.configure({
186
- awsRegion: 'us-east-1',
187
- awsAccountId: '123456789',
188
- asyncUpdateQueueName: 'test-queue',
189
- });
190
- mockBuildAndSendMessage.mockRejectedValueOnce(new Error('SQS send failed'));
191
- const result = await IdentityGatewayService.enqueueAsyncIdentityUpdate({
192
- pixelId,
193
- identity: testIdentity,
194
- context: testContext,
195
- reason: 'test reason',
196
- eventId: 'event789',
197
- });
198
- expect(result.success).toBe(false);
199
- expect(result.error).toBe('SQS send failed');
200
- expect(result.messageId).toBeUndefined();
201
- expect(mockedLogger.warn).toHaveBeenCalledWith('IdentityGateway - enqueueAsyncIdentityUpdate: failed to enqueue (failing open)', expect.objectContaining({
202
- error: expect.any(Error),
203
- errorMessage: 'SQS send failed',
204
- pixelId,
205
- identityId: testIdentity.identityId,
206
- reason: 'test reason',
207
- eventId: 'event789',
208
- }));
209
- });
210
- it('should build message shape matching IdentifyAsyncUpdateMessage interface', async () => {
211
- IdentityGatewayService.configure({
212
- awsRegion: 'us-east-1',
213
- awsAccountId: '123456789',
214
- asyncUpdateQueueName: 'test-queue',
215
- });
216
- mockBuildAndSendMessage.mockResolvedValueOnce({ MessageId: 'test-id' });
217
- const identity = {
218
- identityId: 'id-xyz',
219
- traits: { emails: ['user@example.com'], userIds: ['user-1'] },
220
- };
221
- const context = { identity };
222
- await IdentityGatewayService.enqueueAsyncIdentityUpdate({
223
- pixelId: 'pixel-abc',
224
- identity,
225
- context,
226
- reason: 'test',
227
- });
228
- const messageArg = mockBuildAndSendMessage.mock.calls[0][1];
229
- expect(messageArg).toEqual({
230
- pixelId: 'pixel-abc',
231
- incomingIdentityId: 'id-xyz',
232
- context,
233
- });
234
- });
235
- it('should return error when identity has no identityId', async () => {
236
- IdentityGatewayService.configure({
237
- awsRegion: 'us-east-1',
238
- awsAccountId: '123456789',
239
- asyncUpdateQueueName: 'test-queue',
240
- });
241
- const identityWithoutId = {
242
- traits: { emails: ['anon@example.com'] },
243
- };
244
- const context = { identity: identityWithoutId };
245
- const result = await IdentityGatewayService.enqueueAsyncIdentityUpdate({
246
- pixelId,
247
- identity: identityWithoutId,
248
- context,
249
- reason: 'anonymous identity',
250
- });
251
- expect(result.success).toBe(false);
252
- expect(result.error).toBe('Missing required pixelId or identityId');
253
- expect(mockBuildAndSendMessage).not.toHaveBeenCalled();
254
- });
255
- it('should handle optional eventId parameter', async () => {
256
- IdentityGatewayService.configure({
257
- awsRegion: 'us-east-1',
258
- awsAccountId: '123456789',
259
- asyncUpdateQueueName: 'test-queue',
260
- });
261
- mockBuildAndSendMessage.mockResolvedValueOnce({ MessageId: 'test-id' });
262
- await IdentityGatewayService.enqueueAsyncIdentityUpdate({
263
- pixelId,
264
- identity: testIdentity,
265
- context: testContext,
266
- reason: 'test',
267
- });
268
- expect(mockedLogger.debug).toHaveBeenCalledWith('IdentityGateway - enqueueAsyncIdentityUpdate: message sent', expect.objectContaining({
269
- eventId: undefined,
270
- }));
271
- });
272
- });
273
- describe('getIdentityWithCachingV2() with skipCacheWrite option', () => {
274
- const incomingIdentity = {
275
- identityId: 'identity456',
276
- traits: { emails: ['test@email.com'] },
277
- };
278
- describe('default behavior (no options)', () => {
279
- it('should write to cache when Neptune returns new identity', async () => {
280
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(null);
281
- mockInvokeFunction.mockResolvedValueOnce({
282
- statusCode: 200,
283
- body: JSON.stringify({
284
- identity: {
285
- identityId: 'resolved-identity',
286
- traits: { emails: ['test@email.com', 'resolved@email.com'] },
287
- },
288
- }),
289
- });
290
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, incomingIdentity, lambdaArn);
291
- expect(result.summary.didWriteCache).toBe(false);
292
- expect(mockedDynamoDbClient.safeBatchWrite).not.toHaveBeenCalled();
293
- });
294
- });
295
- describe('skipCacheWrite: false', () => {
296
- it('should write to cache (same as default)', async () => {
297
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(null);
298
- mockInvokeFunction.mockResolvedValueOnce({
299
- statusCode: 200,
300
- body: JSON.stringify({
301
- identity: {
302
- identityId: 'resolved-identity',
303
- traits: { emails: ['test@email.com'] },
304
- },
305
- }),
306
- });
307
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, incomingIdentity, lambdaArn, { skipCacheWrite: false });
308
- expect(result.summary.didWriteCache).toBe(false);
309
- expect(mockedDynamoDbClient.safeBatchWrite).not.toHaveBeenCalled();
310
- });
311
- });
312
- describe('skipCacheWrite: true', () => {
313
- it('should NOT write to cache on Neptune write-back path', async () => {
314
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(null);
315
- mockInvokeFunction.mockResolvedValueOnce({
316
- statusCode: 200,
317
- body: JSON.stringify({
318
- identity: {
319
- identityId: 'resolved-identity',
320
- traits: { emails: ['test@email.com', 'resolved@email.com'] },
321
- },
322
- }),
323
- });
324
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, incomingIdentity, lambdaArn, { skipCacheWrite: true });
325
- expect(result.identity?.identityId).toBe('resolved-identity');
326
- expect(result.summary.didWriteCache).toBe(false);
327
- expect(result.summary.outcome).toBe(IDENTITY_CACHE_WITH_CACHING_OUTCOME.NEPTUNE_RESOLVED);
328
- expect(mockedDynamoDbClient.safeBatchWrite).not.toHaveBeenCalled();
329
- });
330
- it('should enqueue async update on address update path (never writes sync)', async () => {
331
- IdentityGatewayService.configure({
332
- awsRegion: 'us-east-1',
333
- awsAccountId: '123456789',
334
- asyncUpdateQueueName: 'test-queue',
335
- });
336
- mockBuildAndSendMessage.mockResolvedValueOnce({ MessageId: 'test-msg-id' });
337
- const cachedResponse = {
338
- pk: `identity#${pixelId}#identity456`,
339
- pixelId,
340
- identityId: 'identity456',
341
- response: {
342
- identityId: 'identity456',
343
- traits: {
344
- emails: ['test@email.com'],
345
- addresses: [{ city: 'Old City', upa: 1704067200000 }],
346
- },
347
- },
348
- updatedAt: new Date().toISOString(),
349
- };
350
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
351
- const identityWithNewAddress = {
352
- identityId: 'identity456',
353
- traits: {
354
- emails: ['test@email.com'],
355
- addresses: [{ city: 'New City', upa: 1717200000000 }],
356
- },
357
- };
358
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, identityWithNewAddress, lambdaArn, { skipCacheWrite: true });
359
- if (result.summary.outcome === IDENTITY_CACHE_WITH_CACHING_OUTCOME.ADDRESS_UPDATE_ASYNC) {
360
- expect(result.summary.didWriteCache).toBe(false);
361
- expect(result.summary.didEnqueueAsync).toBe(true);
362
- expect(mockedDynamoDbClient.safeBatchWrite).not.toHaveBeenCalled();
363
- expect(mockBuildAndSendMessage).toHaveBeenCalled();
364
- }
365
- });
366
- it('should still return resolved identity when skipCacheWrite is true', async () => {
367
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(null);
368
- mockInvokeFunction.mockResolvedValueOnce({
369
- statusCode: 200,
370
- body: JSON.stringify({
371
- identity: {
372
- identityId: 'neptune-resolved-id',
373
- traits: { emails: ['test@email.com', 'new@email.com'] },
374
- },
375
- }),
376
- });
377
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, incomingIdentity, lambdaArn, { skipCacheWrite: true });
378
- expect(result.identity).toBeDefined();
379
- expect(result.identity?.identityId).toBe('neptune-resolved-id');
380
- expect(result.identity?.traits?.emails).toContain('new@email.com');
381
- });
382
- it('should return fresh cache hit without writing (skipCacheWrite irrelevant for hits)', async () => {
383
- const cachedResponse = {
384
- pk: `identity#${pixelId}#identity456`,
385
- pixelId,
386
- identityId: 'identity456',
387
- response: {
388
- identityId: 'identity456',
389
- traits: { emails: ['test@email.com'] },
390
- },
391
- updatedAt: new Date().toISOString(),
392
- };
393
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
394
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, incomingIdentity, lambdaArn, { skipCacheWrite: true });
395
- expect(result.summary.outcome).toBe(IDENTITY_CACHE_WITH_CACHING_OUTCOME.FRESH_CACHE_HIT);
396
- expect(result.summary.didWriteCache).toBe(false);
397
- expect(mockInvokeFunction).not.toHaveBeenCalled();
398
- expect(mockedDynamoDbClient.safeBatchWrite).not.toHaveBeenCalled();
399
- });
400
- });
401
- describe('address update path - always enqueues async (never writes sync)', () => {
402
- it('should enqueue async (not write cache) when address update is detected', async () => {
403
- IdentityGatewayService.configure({
404
- awsRegion: 'us-east-1',
405
- awsAccountId: '123456789',
406
- asyncUpdateQueueName: 'test-queue',
407
- });
408
- mockBuildAndSendMessage.mockResolvedValueOnce({ MessageId: 'test-msg-id' });
409
- const cachedResponse = {
410
- pk: `identity#${pixelId}#identity456`,
411
- pixelId,
412
- identityId: 'identity456',
413
- response: {
414
- identityId: 'identity456',
415
- traits: {
416
- emails: ['test@email.com'],
417
- addresses: [{ city: 'Old City', upa: 1704067200000 }],
418
- },
419
- },
420
- updatedAt: new Date().toISOString(),
421
- };
422
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
423
- const identityWithNewAddress = {
424
- identityId: 'identity456',
425
- traits: {
426
- emails: ['test@email.com'],
427
- addresses: [{ city: 'New City', upa: 1717200000000 }],
428
- },
429
- };
430
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, identityWithNewAddress, lambdaArn, { skipCacheWrite: false });
431
- if (result.summary.outcome === IDENTITY_CACHE_WITH_CACHING_OUTCOME.ADDRESS_UPDATE_ASYNC) {
432
- expect(result.summary.didWriteCache).toBe(false);
433
- expect(result.summary.didEnqueueAsync).toBe(true);
434
- expect(mockedDynamoDbClient.safeBatchWrite).not.toHaveBeenCalled();
435
- expect(mockBuildAndSendMessage).toHaveBeenCalledWith('identify.updateAsync', expect.objectContaining({
436
- pixelId,
437
- incomingIdentityId: 'identity456',
438
- }));
439
- }
440
- });
441
- it('should return merged identity without waiting for cache write', async () => {
442
- IdentityGatewayService.configure({
443
- awsRegion: 'us-east-1',
444
- awsAccountId: '123456789',
445
- asyncUpdateQueueName: 'test-queue',
446
- });
447
- mockBuildAndSendMessage.mockResolvedValueOnce({ MessageId: 'test-msg-id' });
448
- const cachedResponse = {
449
- pk: `identity#${pixelId}#identity456`,
450
- pixelId,
451
- identityId: 'identity456',
452
- response: {
453
- identityId: 'identity456',
454
- traits: {
455
- emails: ['test@email.com'],
456
- addresses: [],
457
- },
458
- },
459
- updatedAt: new Date().toISOString(),
460
- };
461
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
462
- const identityWithNewAddress = {
463
- identityId: 'identity456',
464
- traits: {
465
- emails: ['test@email.com'],
466
- addresses: [{ city: 'New City', firstName: 'John' }],
467
- },
468
- };
469
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, identityWithNewAddress, lambdaArn);
470
- expect(result.identity).toBeDefined();
471
- expect(result.identity?.identityId).toBe('identity456');
472
- expect(result.summary.didSkipNeptune).toBe(true);
473
- expect(result.summary.didCallNeptune).toBe(false);
474
- expect(result.summary.outcome).toBe(IDENTITY_CACHE_WITH_CACHING_OUTCOME.ADDRESS_UPDATE_ASYNC);
475
- });
476
- it('should set didEnqueueAsync to false when SQS enqueue fails', async () => {
477
- IdentityGatewayService.configure({
478
- awsRegion: 'us-east-1',
479
- awsAccountId: '123456789',
480
- asyncUpdateQueueName: 'test-queue',
481
- });
482
- mockBuildAndSendMessage.mockRejectedValueOnce(new Error('SQS error'));
483
- const cachedResponse = {
484
- pk: `identity#${pixelId}#identity456`,
485
- pixelId,
486
- identityId: 'identity456',
487
- response: {
488
- identityId: 'identity456',
489
- traits: {
490
- emails: ['test@email.com'],
491
- addresses: [],
492
- },
493
- },
494
- updatedAt: new Date().toISOString(),
495
- };
496
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
497
- const identityWithNewAddress = {
498
- identityId: 'identity456',
499
- traits: {
500
- emails: ['test@email.com'],
501
- addresses: [{ city: 'New City', firstName: 'John' }],
502
- },
503
- };
504
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, identityWithNewAddress, lambdaArn);
505
- expect(result.summary.outcome).toBe(IDENTITY_CACHE_WITH_CACHING_OUTCOME.ADDRESS_UPDATE_ASYNC);
506
- expect(result.identity).toBeDefined();
507
- expect(result.summary.didEnqueueAsync).toBe(false);
508
- });
509
- });
510
- });
511
- describe('edge cases', () => {
512
- it('should return invalid input when pixelId is undefined', async () => {
513
- const result = await IdentityGatewayService.getIdentityWithCachingV2(undefined, { identityId: 'test' }, lambdaArn);
514
- expect(result.identity).toBeUndefined();
515
- expect(result.summary.outcome).toBe(IDENTITY_CACHE_WITH_CACHING_OUTCOME.INVALID_INPUT);
516
- expect(result.summary.didReadCache).toBe(false);
517
- expect(result.summary.didWriteCache).toBe(false);
518
- });
519
- it('should return invalid input when incomingIdentity is null', async () => {
520
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, null, lambdaArn);
521
- expect(result.identity).toBeUndefined();
522
- expect(result.summary.outcome).toBe(IDENTITY_CACHE_WITH_CACHING_OUTCOME.INVALID_INPUT);
523
- });
524
- it('should return invalid input when both pixelId and identity are missing', async () => {
525
- const result = await IdentityGatewayService.getIdentityWithCachingV2('', undefined, lambdaArn);
526
- expect(result.identity).toBeUndefined();
527
- expect(result.summary.outcome).toBe(IDENTITY_CACHE_WITH_CACHING_OUTCOME.INVALID_INPUT);
528
- });
529
- it('should gracefully fallback when cache read fails (fail-open pattern)', async () => {
530
- mockedDynamoDbClient.safeGet.mockRejectedValueOnce(new Error('Unexpected DynamoDB error'));
531
- mockInvokeFunction.mockResolvedValueOnce({
532
- statusCode: 200,
533
- body: JSON.stringify({
534
- identity: {
535
- identityId: 'neptune-resolved',
536
- traits: { emails: ['test@email.com'] },
537
- },
538
- }),
539
- });
540
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, { identityId: 'test-id', traits: { emails: ['test@email.com'] } }, lambdaArn);
541
- expect(result.identity).toBeDefined();
542
- expect(result.identity?.identityId).toBe('neptune-resolved');
543
- expect(result.summary.didReadCache).toBe(true);
544
- expect(result.summary.didCallNeptune).toBe(true);
545
- });
546
- it('should reject identity with undefined identityId in enqueue', async () => {
547
- IdentityGatewayService.configure({
548
- awsRegion: 'us-east-1',
549
- awsAccountId: '123456789',
550
- asyncUpdateQueueName: 'test-queue',
551
- });
552
- const identity = { traits: { emails: ['test@email.com'] } };
553
- const context = { identity };
554
- const result = await IdentityGatewayService.enqueueAsyncIdentityUpdate({
555
- pixelId,
556
- identity,
557
- context,
558
- reason: 'anonymous user',
559
- });
560
- expect(result.success).toBe(false);
561
- expect(result.error).toBe('Missing required pixelId or identityId');
562
- expect(mockBuildAndSendMessage).not.toHaveBeenCalled();
563
- expect(mockedLogger.warn).toHaveBeenCalledWith('IdentityGateway - enqueueAsyncIdentityUpdate: missing required params', expect.objectContaining({
564
- pixelId,
565
- identityId: undefined,
566
- }));
567
- });
568
- });
569
- describe('safeWriteBackToCache behavior with skipCacheWrite', () => {
570
- it('should not call safeWriteBackToCache when skipCacheWrite is true and Neptune returns new identity', async () => {
571
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(null);
572
- mockInvokeFunction.mockResolvedValueOnce({
573
- statusCode: 200,
574
- body: JSON.stringify({
575
- identity: {
576
- identityId: 'new-resolved-identity',
577
- traits: { emails: ['test@email.com'] },
578
- },
579
- }),
580
- });
581
- await IdentityGatewayService.getIdentityWithCachingV2(pixelId, { identityId: 'original-id', traits: { emails: ['test@email.com'] } }, lambdaArn, { skipCacheWrite: true });
582
- expect(mockedDynamoDbClient.safeBatchWrite).not.toHaveBeenCalled();
583
- });
584
- it('should call safeWriteBackToCache when skipCacheWrite is false and Neptune returns new identity', async () => {
585
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(null);
586
- mockInvokeFunction.mockResolvedValueOnce({
587
- statusCode: 200,
588
- body: JSON.stringify({
589
- identity: {
590
- identityId: 'new-resolved-identity',
591
- traits: { emails: ['test@email.com'] },
592
- },
593
- }),
594
- });
595
- await IdentityGatewayService.getIdentityWithCachingV2(pixelId, { identityId: 'original-id', traits: { emails: ['test@email.com'] } }, lambdaArn, { skipCacheWrite: false });
596
- expect(mockedDynamoDbClient.safeBatchWrite).not.toHaveBeenCalled();
597
- });
598
- });
599
- describe('summary fields consistency', () => {
600
- it('should include all required summary fields in getIdentityWithCachingV2 result', async () => {
601
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(null);
602
- mockInvokeFunction.mockResolvedValueOnce({
603
- statusCode: 200,
604
- body: JSON.stringify({
605
- identity: {
606
- identityId: 'resolved-id',
607
- traits: { emails: ['test@email.com'] },
608
- },
609
- }),
610
- });
611
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, { identityId: 'incoming-id', traits: { emails: ['test@email.com'] } }, lambdaArn, { skipCacheWrite: true });
612
- expect(result.summary).toEqual(expect.objectContaining({
613
- outcome: expect.any(String),
614
- pixelId,
615
- identityId: 'resolved-id',
616
- didReadCache: true,
617
- didCallNeptune: true,
618
- didWriteCache: false,
619
- didSkipNeptune: false,
620
- didFallbackToIncoming: false,
621
- didEnqueueAsync: false,
622
- staleTraits: expect.any(Array),
623
- }));
624
- });
625
- it('should include traitChanges in summary when identity changes', async () => {
626
- const cachedResponse = {
627
- pk: `identity#${pixelId}#identity456`,
628
- pixelId,
629
- identityId: 'identity456',
630
- response: {
631
- identityId: 'identity456',
632
- traits: { emails: ['old@email.com'] },
633
- },
634
- updatedAt: new Date().toISOString(),
635
- };
636
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
637
- mockInvokeFunction.mockResolvedValueOnce({
638
- statusCode: 200,
639
- body: JSON.stringify({
640
- identity: {
641
- identityId: 'identity456',
642
- traits: { emails: ['old@email.com', 'new@email.com'] },
643
- },
644
- }),
645
- });
646
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, { identityId: 'identity456', traits: { emails: ['old@email.com', 'new@email.com'] } }, lambdaArn);
647
- if (result.summary.outcome === IDENTITY_CACHE_WITH_CACHING_OUTCOME.NEPTUNE_RESOLVED) {
648
- expect(result.summary.traitChanges).toBeDefined();
649
- }
650
- });
651
- });
652
- describe('ADDRESS_UPDATE async behavior (comprehensive)', () => {
653
- describe('ADDRESS_UPDATE enqueues async (not sync write)', () => {
654
- it('should enqueue async when staleness tier is ADDRESS_UPDATE with non-IP address', async () => {
655
- IdentityGatewayService.configure({
656
- awsRegion: 'us-east-1',
657
- awsAccountId: '123456789',
658
- asyncUpdateQueueName: 'identity-async-queue',
659
- });
660
- mockBuildAndSendMessage.mockResolvedValueOnce({ MessageId: 'async-msg-123' });
661
- const cachedResponse = {
662
- pk: `identity#${pixelId}#identity456`,
663
- pixelId,
664
- identityId: 'identity456',
665
- response: {
666
- identityId: 'identity456',
667
- traits: {
668
- emails: ['test@email.com'],
669
- addresses: [],
670
- },
671
- },
672
- updatedAt: new Date().toISOString(),
673
- };
674
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
675
- const incomingWithAddress = {
676
- identityId: 'identity456',
677
- traits: {
678
- emails: ['test@email.com'],
679
- addresses: [{ firstName: 'John', lastName: 'Doe', city: 'New York' }],
680
- },
681
- };
682
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, incomingWithAddress, lambdaArn);
683
- expect(result.summary.outcome).toBe(IDENTITY_CACHE_WITH_CACHING_OUTCOME.ADDRESS_UPDATE_ASYNC);
684
- expect(mockBuildAndSendMessage).toHaveBeenCalledWith('identify.updateAsync', expect.objectContaining({
685
- pixelId,
686
- incomingIdentityId: 'identity456',
687
- }));
688
- expect(result.summary.didEnqueueAsync).toBe(true);
689
- expect(result.summary.didWriteCache).toBe(false);
690
- expect(result.summary.didCallNeptune).toBe(false);
691
- expect(result.summary.didSkipNeptune).toBe(true);
692
- expect(mockedDynamoDbClient.safeBatchWrite).not.toHaveBeenCalled();
693
- expect(mockInvokeFunction).not.toHaveBeenCalled();
694
- });
695
- it('should return merged identity with updated addresses', async () => {
696
- IdentityGatewayService.configure({
697
- awsRegion: 'us-east-1',
698
- awsAccountId: '123456789',
699
- asyncUpdateQueueName: 'identity-async-queue',
700
- });
701
- mockBuildAndSendMessage.mockResolvedValueOnce({ MessageId: 'msg-456' });
702
- const cachedResponse = {
703
- pk: `identity#${pixelId}#identity456`,
704
- pixelId,
705
- identityId: 'identity456',
706
- response: {
707
- identityId: 'identity456',
708
- traits: {
709
- emails: ['test@email.com'],
710
- phones: ['555-1234'],
711
- addresses: [],
712
- },
713
- },
714
- updatedAt: new Date().toISOString(),
715
- };
716
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
717
- const incomingWithAddress = {
718
- identityId: 'identity456',
719
- traits: {
720
- emails: ['test@email.com'],
721
- addresses: [{ firstName: 'Jane', city: 'Boston', zip: '02101' }],
722
- },
723
- };
724
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, incomingWithAddress, lambdaArn);
725
- expect(result.identity).toBeDefined();
726
- expect(result.identity?.identityId).toBe('identity456');
727
- expect(result.identity?.traits?.emails).toContain('test@email.com');
728
- expect(result.identity?.traits?.addresses).toHaveLength(1);
729
- expect(result.identity?.traits?.addresses?.[0]?.firstName).toBe('Jane');
730
- });
731
- it('should include writeReason ADDRESS_UPDATE in summary', async () => {
732
- IdentityGatewayService.configure({
733
- awsRegion: 'us-east-1',
734
- awsAccountId: '123456789',
735
- asyncUpdateQueueName: 'identity-async-queue',
736
- });
737
- mockBuildAndSendMessage.mockResolvedValueOnce({ MessageId: 'msg-789' });
738
- const cachedResponse = {
739
- pk: `identity#${pixelId}#identity456`,
740
- pixelId,
741
- identityId: 'identity456',
742
- response: {
743
- identityId: 'identity456',
744
- traits: {
745
- emails: ['test@email.com'],
746
- addresses: [],
747
- },
748
- },
749
- updatedAt: new Date().toISOString(),
750
- };
751
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
752
- const incomingWithAddress = {
753
- identityId: 'identity456',
754
- traits: {
755
- emails: ['test@email.com'],
756
- addresses: [{ firstName: 'Bob', city: 'Seattle' }],
757
- },
758
- };
759
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, incomingWithAddress, lambdaArn);
760
- expect(result.summary.writeReason).toBe('address_update');
761
- });
762
- it('should include traitChanges for address update', async () => {
763
- IdentityGatewayService.configure({
764
- awsRegion: 'us-east-1',
765
- awsAccountId: '123456789',
766
- asyncUpdateQueueName: 'identity-async-queue',
767
- });
768
- mockBuildAndSendMessage.mockResolvedValueOnce({ MessageId: 'msg-trait' });
769
- const cachedResponse = {
770
- pk: `identity#${pixelId}#identity456`,
771
- pixelId,
772
- identityId: 'identity456',
773
- response: {
774
- identityId: 'identity456',
775
- traits: {
776
- emails: ['test@email.com'],
777
- addresses: [],
778
- },
779
- },
780
- updatedAt: new Date().toISOString(),
781
- };
782
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
783
- const incomingWithAddress = {
784
- identityId: 'identity456',
785
- traits: {
786
- emails: ['test@email.com'],
787
- addresses: [{ firstName: 'Alice', city: 'Chicago' }],
788
- },
789
- };
790
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, incomingWithAddress, lambdaArn);
791
- expect(result.summary.traitChanges).toBeDefined();
792
- });
793
- });
794
- describe('ADDRESS_UPDATE async failure handling', () => {
795
- it('should set didEnqueueAsync to false when enqueue fails', async () => {
796
- IdentityGatewayService.configure({
797
- awsRegion: 'us-east-1',
798
- awsAccountId: '123456789',
799
- asyncUpdateQueueName: 'identity-async-queue',
800
- });
801
- mockBuildAndSendMessage.mockRejectedValueOnce(new Error('SQS unavailable'));
802
- const cachedResponse = {
803
- pk: `identity#${pixelId}#identity456`,
804
- pixelId,
805
- identityId: 'identity456',
806
- response: {
807
- identityId: 'identity456',
808
- traits: {
809
- emails: ['test@email.com'],
810
- addresses: [],
811
- },
812
- },
813
- updatedAt: new Date().toISOString(),
814
- };
815
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
816
- const incomingWithAddress = {
817
- identityId: 'identity456',
818
- traits: {
819
- emails: ['test@email.com'],
820
- addresses: [{ firstName: 'Test', city: 'TestCity' }],
821
- },
822
- };
823
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, incomingWithAddress, lambdaArn);
824
- expect(result.summary.outcome).toBe(IDENTITY_CACHE_WITH_CACHING_OUTCOME.ADDRESS_UPDATE_ASYNC);
825
- expect(result.summary.didEnqueueAsync).toBe(false);
826
- expect(result.identity).toBeDefined();
827
- expect(result.identity?.identityId).toBe('identity456');
828
- });
829
- it('should log warning when enqueue fails', async () => {
830
- IdentityGatewayService.configure({
831
- awsRegion: 'us-east-1',
832
- awsAccountId: '123456789',
833
- asyncUpdateQueueName: 'identity-async-queue',
834
- });
835
- mockBuildAndSendMessage.mockRejectedValueOnce(new Error('Network timeout'));
836
- const cachedResponse = {
837
- pk: `identity#${pixelId}#identity456`,
838
- pixelId,
839
- identityId: 'identity456',
840
- response: {
841
- identityId: 'identity456',
842
- traits: {
843
- emails: ['test@email.com'],
844
- addresses: [],
845
- },
846
- },
847
- updatedAt: new Date().toISOString(),
848
- };
849
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
850
- const incomingWithAddress = {
851
- identityId: 'identity456',
852
- traits: {
853
- emails: ['test@email.com'],
854
- addresses: [{ firstName: 'Test', city: 'TestCity' }],
855
- },
856
- };
857
- await IdentityGatewayService.getIdentityWithCachingV2(pixelId, incomingWithAddress, lambdaArn);
858
- expect(mockedLogger.warn).toHaveBeenCalledWith('IdentityGateway - enqueueAsyncIdentityUpdate: failed to enqueue (failing open)', expect.objectContaining({
859
- error: expect.any(Error),
860
- errorMessage: 'Network timeout',
861
- pixelId,
862
- }));
863
- });
864
- it('should set didEnqueueAsync to false when SQS client is not configured', async () => {
865
- const cachedResponse = {
866
- pk: `identity#${pixelId}#identity456`,
867
- pixelId,
868
- identityId: 'identity456',
869
- response: {
870
- identityId: 'identity456',
871
- traits: {
872
- emails: ['test@email.com'],
873
- addresses: [],
874
- },
875
- },
876
- updatedAt: new Date().toISOString(),
877
- };
878
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
879
- const incomingWithAddress = {
880
- identityId: 'identity456',
881
- traits: {
882
- emails: ['test@email.com'],
883
- addresses: [{ firstName: 'Test', city: 'TestCity' }],
884
- },
885
- };
886
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, incomingWithAddress, lambdaArn);
887
- expect(result.summary.outcome).toBe(IDENTITY_CACHE_WITH_CACHING_OUTCOME.ADDRESS_UPDATE_ASYNC);
888
- expect(result.summary.didEnqueueAsync).toBe(false);
889
- expect(result.identity).toBeDefined();
890
- });
891
- });
892
- describe('IDENTITY_STALE path (comparison with ADDRESS_UPDATE)', () => {
893
- it('should call Neptune when staleness tier is IDENTITY_STALE (not ADDRESS_UPDATE)', async () => {
894
- IdentityGatewayService.configure({
895
- awsRegion: 'us-east-1',
896
- awsAccountId: '123456789',
897
- asyncUpdateQueueName: 'identity-async-queue',
898
- });
899
- const cachedResponse = {
900
- pk: `identity#${pixelId}#identity456`,
901
- pixelId,
902
- identityId: 'identity456',
903
- response: {
904
- identityId: 'identity456',
905
- traits: {
906
- emails: ['old@email.com'],
907
- },
908
- },
909
- updatedAt: new Date().toISOString(),
910
- };
911
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
912
- mockInvokeFunction.mockResolvedValueOnce({
913
- statusCode: 200,
914
- body: JSON.stringify({
915
- identity: {
916
- identityId: 'identity456',
917
- traits: {
918
- emails: ['old@email.com', 'new@email.com'],
919
- },
920
- },
921
- }),
922
- });
923
- const incomingWithNewEmail = {
924
- identityId: 'identity456',
925
- traits: {
926
- emails: ['old@email.com', 'new@email.com'],
927
- },
928
- };
929
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, incomingWithNewEmail, lambdaArn);
930
- expect(mockInvokeFunction).toHaveBeenCalled();
931
- expect(result.summary.didCallNeptune).toBe(true);
932
- expect(result.summary.didSkipNeptune).toBe(false);
933
- expect(mockBuildAndSendMessage).not.toHaveBeenCalled();
934
- expect(result.summary.didEnqueueAsync).toBe(false);
935
- expect(result.summary.outcome).not.toBe(IDENTITY_CACHE_WITH_CACHING_OUTCOME.ADDRESS_UPDATE_ASYNC);
936
- });
937
- it('should NOT call Neptune when staleness tier is ADDRESS_UPDATE', async () => {
938
- IdentityGatewayService.configure({
939
- awsRegion: 'us-east-1',
940
- awsAccountId: '123456789',
941
- asyncUpdateQueueName: 'identity-async-queue',
942
- });
943
- mockBuildAndSendMessage.mockResolvedValueOnce({ MessageId: 'msg-no-neptune' });
944
- const cachedResponse = {
945
- pk: `identity#${pixelId}#identity456`,
946
- pixelId,
947
- identityId: 'identity456',
948
- response: {
949
- identityId: 'identity456',
950
- traits: {
951
- emails: ['test@email.com'],
952
- addresses: [],
953
- },
954
- },
955
- updatedAt: new Date().toISOString(),
956
- };
957
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
958
- const incomingWithAddress = {
959
- identityId: 'identity456',
960
- traits: {
961
- emails: ['test@email.com'],
962
- addresses: [{ firstName: 'Test', city: 'TestCity' }],
963
- },
964
- };
965
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, incomingWithAddress, lambdaArn);
966
- expect(mockInvokeFunction).not.toHaveBeenCalled();
967
- expect(result.summary.didCallNeptune).toBe(false);
968
- expect(result.summary.didSkipNeptune).toBe(true);
969
- expect(result.summary.outcome).toBe(IDENTITY_CACHE_WITH_CACHING_OUTCOME.ADDRESS_UPDATE_ASYNC);
970
- });
971
- it('should write cache from Neptune in IDENTITY_STALE path (identity-service handles it)', async () => {
972
- const cachedResponse = {
973
- pk: `identity#${pixelId}#identity456`,
974
- pixelId,
975
- identityId: 'identity456',
976
- response: {
977
- identityId: 'identity456',
978
- traits: {
979
- emails: ['old@email.com'],
980
- },
981
- },
982
- updatedAt: new Date().toISOString(),
983
- };
984
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
985
- mockInvokeFunction.mockResolvedValueOnce({
986
- statusCode: 200,
987
- body: JSON.stringify({
988
- identity: {
989
- identityId: 'identity456',
990
- traits: {
991
- emails: ['old@email.com', 'new@email.com'],
992
- },
993
- },
994
- }),
995
- });
996
- const incomingWithNewEmail = {
997
- identityId: 'identity456',
998
- traits: {
999
- emails: ['old@email.com', 'new@email.com'],
1000
- },
1001
- };
1002
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, incomingWithNewEmail, lambdaArn);
1003
- expect(result.summary.didCallNeptune).toBe(true);
1004
- expect(result.summary.didWriteCache).toBe(false);
1005
- expect(mockedDynamoDbClient.safeBatchWrite).not.toHaveBeenCalled();
1006
- });
1007
- });
1008
- describe('summary correctness for ADDRESS_UPDATE path', () => {
1009
- it('should have correct summary fields for successful ADDRESS_UPDATE async enqueue', async () => {
1010
- IdentityGatewayService.configure({
1011
- awsRegion: 'us-east-1',
1012
- awsAccountId: '123456789',
1013
- asyncUpdateQueueName: 'identity-async-queue',
1014
- });
1015
- mockBuildAndSendMessage.mockResolvedValueOnce({ MessageId: 'summary-test-msg' });
1016
- const cachedResponse = {
1017
- pk: `identity#${pixelId}#identity456`,
1018
- pixelId,
1019
- identityId: 'identity456',
1020
- response: {
1021
- identityId: 'identity456',
1022
- traits: {
1023
- emails: ['test@email.com'],
1024
- addresses: [],
1025
- },
1026
- },
1027
- updatedAt: new Date().toISOString(),
1028
- };
1029
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
1030
- const incomingWithAddress = {
1031
- identityId: 'identity456',
1032
- traits: {
1033
- emails: ['test@email.com'],
1034
- addresses: [{ firstName: 'Summary', city: 'SummaryCity' }],
1035
- },
1036
- };
1037
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, incomingWithAddress, lambdaArn);
1038
- expect(result.summary).toEqual(expect.objectContaining({
1039
- outcome: IDENTITY_CACHE_WITH_CACHING_OUTCOME.ADDRESS_UPDATE_ASYNC,
1040
- pixelId,
1041
- identityId: 'identity456',
1042
- didReadCache: true,
1043
- didCallNeptune: false,
1044
- didWriteCache: false,
1045
- didSkipNeptune: true,
1046
- didFallbackToIncoming: false,
1047
- didEnqueueAsync: true,
1048
- writeReason: 'address_update',
1049
- }));
1050
- });
1051
- it('should have correct summary fields for failed ADDRESS_UPDATE async enqueue', async () => {
1052
- IdentityGatewayService.configure({
1053
- awsRegion: 'us-east-1',
1054
- awsAccountId: '123456789',
1055
- asyncUpdateQueueName: 'identity-async-queue',
1056
- });
1057
- mockBuildAndSendMessage.mockRejectedValueOnce(new Error('Enqueue failed'));
1058
- const cachedResponse = {
1059
- pk: `identity#${pixelId}#identity456`,
1060
- pixelId,
1061
- identityId: 'identity456',
1062
- response: {
1063
- identityId: 'identity456',
1064
- traits: {
1065
- emails: ['test@email.com'],
1066
- addresses: [],
1067
- },
1068
- },
1069
- updatedAt: new Date().toISOString(),
1070
- };
1071
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
1072
- const incomingWithAddress = {
1073
- identityId: 'identity456',
1074
- traits: {
1075
- emails: ['test@email.com'],
1076
- addresses: [{ firstName: 'Fail', city: 'FailCity' }],
1077
- },
1078
- };
1079
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, incomingWithAddress, lambdaArn);
1080
- expect(result.summary).toEqual(expect.objectContaining({
1081
- outcome: IDENTITY_CACHE_WITH_CACHING_OUTCOME.ADDRESS_UPDATE_ASYNC,
1082
- pixelId,
1083
- identityId: 'identity456',
1084
- didReadCache: true,
1085
- didCallNeptune: false,
1086
- didWriteCache: false,
1087
- didSkipNeptune: true,
1088
- didFallbackToIncoming: false,
1089
- didEnqueueAsync: false,
1090
- writeReason: 'address_update',
1091
- }));
1092
- });
1093
- });
1094
- describe('IP-sourced vs non-IP address behavior', () => {
1095
- it('should NOT trigger ADDRESS_UPDATE for IP-sourced addresses (VOLATILE_ONLY tier)', async () => {
1096
- IdentityGatewayService.configure({
1097
- awsRegion: 'us-east-1',
1098
- awsAccountId: '123456789',
1099
- asyncUpdateQueueName: 'identity-async-queue',
1100
- });
1101
- const cachedResponse = {
1102
- pk: `identity#${pixelId}#identity456`,
1103
- pixelId,
1104
- identityId: 'identity456',
1105
- response: {
1106
- identityId: 'identity456',
1107
- traits: {
1108
- emails: ['test@email.com'],
1109
- addresses: [],
1110
- },
1111
- },
1112
- updatedAt: new Date().toISOString(),
1113
- };
1114
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
1115
- const incomingWithIpAddress = {
1116
- identityId: 'identity456',
1117
- traits: {
1118
- emails: ['test@email.com'],
1119
- addresses: [{ city: 'IPCity', country: 'US' }],
1120
- },
1121
- };
1122
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, incomingWithIpAddress, lambdaArn);
1123
- expect(result.summary.outcome).toBe(IDENTITY_CACHE_WITH_CACHING_OUTCOME.VOLATILE_ONLY_CACHE_HIT);
1124
- expect(result.summary.didEnqueueAsync).toBe(false);
1125
- expect(mockBuildAndSendMessage).not.toHaveBeenCalled();
1126
- });
1127
- it('should trigger ADDRESS_UPDATE for non-IP address (has PII fields)', async () => {
1128
- IdentityGatewayService.configure({
1129
- awsRegion: 'us-east-1',
1130
- awsAccountId: '123456789',
1131
- asyncUpdateQueueName: 'identity-async-queue',
1132
- });
1133
- mockBuildAndSendMessage.mockResolvedValueOnce({ MessageId: 'non-ip-msg' });
1134
- const cachedResponse = {
1135
- pk: `identity#${pixelId}#identity456`,
1136
- pixelId,
1137
- identityId: 'identity456',
1138
- response: {
1139
- identityId: 'identity456',
1140
- traits: {
1141
- emails: ['test@email.com'],
1142
- addresses: [],
1143
- },
1144
- },
1145
- updatedAt: new Date().toISOString(),
1146
- };
1147
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
1148
- const incomingWithNonIpAddress = {
1149
- identityId: 'identity456',
1150
- traits: {
1151
- emails: ['test@email.com'],
1152
- addresses: [{ firstName: 'John', city: 'NonIPCity' }],
1153
- },
1154
- };
1155
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, incomingWithNonIpAddress, lambdaArn);
1156
- expect(result.summary.outcome).toBe(IDENTITY_CACHE_WITH_CACHING_OUTCOME.ADDRESS_UPDATE_ASYNC);
1157
- expect(result.summary.didEnqueueAsync).toBe(true);
1158
- expect(mockBuildAndSendMessage).toHaveBeenCalled();
1159
- });
1160
- });
1161
- });
1162
- describe('refactored flow: merge and staleness computation', () => {
1163
- describe('mergeAndComputeIdentityStaleness behavior', () => {
1164
- it('should merge incoming traits with cached traits on cache hit', async () => {
1165
- const cachedResponse = {
1166
- pk: `identity#${pixelId}#merge-test`,
1167
- pixelId,
1168
- identityId: 'merge-test',
1169
- response: {
1170
- identityId: 'merge-test',
1171
- traits: {
1172
- emails: ['cached@email.com'],
1173
- phones: ['555-cached'],
1174
- },
1175
- },
1176
- updatedAt: new Date().toISOString(),
1177
- };
1178
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
1179
- const incomingIdentity = {
1180
- identityId: 'merge-test',
1181
- traits: {
1182
- emails: ['cached@email.com'],
1183
- },
1184
- };
1185
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, incomingIdentity, lambdaArn);
1186
- expect(result.identity?.traits?.emails).toContain('cached@email.com');
1187
- expect(result.identity?.traits?.phones).toContain('555-cached');
1188
- });
1189
- it('should compute staleness after cache hit', async () => {
1190
- const cachedResponse = {
1191
- pk: `identity#${pixelId}#staleness-compute`,
1192
- pixelId,
1193
- identityId: 'staleness-compute',
1194
- response: {
1195
- identityId: 'staleness-compute',
1196
- traits: {
1197
- emails: ['old@email.com'],
1198
- },
1199
- },
1200
- updatedAt: new Date().toISOString(),
1201
- };
1202
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
1203
- mockInvokeFunction.mockResolvedValueOnce({
1204
- statusCode: 200,
1205
- body: JSON.stringify({
1206
- identity: {
1207
- identityId: 'staleness-compute',
1208
- traits: { emails: ['old@email.com', 'new@email.com'] },
1209
- },
1210
- }),
1211
- });
1212
- const incomingIdentity = {
1213
- identityId: 'staleness-compute',
1214
- traits: {
1215
- emails: ['old@email.com', 'new@email.com'],
1216
- },
1217
- };
1218
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, incomingIdentity, lambdaArn);
1219
- expect(result.summary.didCallNeptune).toBe(true);
1220
- expect(result.summary.staleTraits).toContain('emails');
1221
- });
1222
- });
1223
- describe('deep equality check', () => {
1224
- it('should treat as cache hit when merged identity is deep equal to cached', async () => {
1225
- const cachedResponse = {
1226
- pk: `identity#${pixelId}#deep-equal`,
1227
- pixelId,
1228
- identityId: 'deep-equal',
1229
- response: {
1230
- identityId: 'deep-equal',
1231
- traits: {
1232
- emails: ['same@email.com'],
1233
- phones: ['555-1234'],
1234
- },
1235
- },
1236
- updatedAt: new Date().toISOString(),
1237
- };
1238
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
1239
- const incomingIdentity = {
1240
- identityId: 'deep-equal',
1241
- traits: {
1242
- emails: ['same@email.com'],
1243
- phones: ['555-1234'],
1244
- },
1245
- };
1246
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, incomingIdentity, lambdaArn);
1247
- expect(result.summary.outcome).toBe(IDENTITY_CACHE_WITH_CACHING_OUTCOME.FRESH_CACHE_HIT);
1248
- expect(result.summary.didCallNeptune).toBe(false);
1249
- expect(mockInvokeFunction).not.toHaveBeenCalled();
1250
- });
1251
- it('should strip address timestamps before deep equality comparison', async () => {
1252
- const now = Date.now();
1253
- const cachedResponse = {
1254
- pk: `identity#${pixelId}#timestamp-strip`,
1255
- pixelId,
1256
- identityId: 'timestamp-strip',
1257
- response: {
1258
- identityId: 'timestamp-strip',
1259
- traits: {
1260
- emails: ['test@email.com'],
1261
- addresses: [{ city: 'NYC', cra: now - 10000, upa: now - 5000 }],
1262
- },
1263
- },
1264
- updatedAt: new Date().toISOString(),
1265
- };
1266
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
1267
- const incomingIdentity = {
1268
- identityId: 'timestamp-strip',
1269
- traits: {
1270
- emails: ['test@email.com'],
1271
- addresses: [{ city: 'NYC', cra: now, upa: now }],
1272
- },
1273
- };
1274
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, incomingIdentity, lambdaArn);
1275
- expect(result.summary.outcome).toBe(IDENTITY_CACHE_WITH_CACHING_OUTCOME.FRESH_CACHE_HIT);
1276
- expect(result.summary.didCallNeptune).toBe(false);
1277
- });
1278
- });
1279
- describe('volatile-only override', () => {
1280
- it('should treat VOLATILE_ONLY + not deep equal as cache hit', async () => {
1281
- const cachedResponse = {
1282
- pk: `identity#${pixelId}#volatile-override`,
1283
- pixelId,
1284
- identityId: 'volatile-override',
1285
- response: {
1286
- identityId: 'volatile-override',
1287
- traits: {
1288
- emails: ['same@email.com'],
1289
- },
1290
- },
1291
- updatedAt: new Date().toISOString(),
1292
- };
1293
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
1294
- const incomingIdentity = {
1295
- identityId: 'volatile-override',
1296
- traits: {
1297
- emails: ['same@email.com'],
1298
- ipAddress: '10.0.0.1',
1299
- userAgent: 'Mozilla/5.0 (New)',
1300
- },
1301
- };
1302
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, incomingIdentity, lambdaArn);
1303
- expect(result.summary.outcome).toBe(IDENTITY_CACHE_WITH_CACHING_OUTCOME.VOLATILE_ONLY_CACHE_HIT);
1304
- expect(result.summary.didCallNeptune).toBe(false);
1305
- expect(result.summary.didWriteCache).toBe(false);
1306
- });
1307
- it('should return merged identity for VOLATILE_ONLY tier', async () => {
1308
- const cachedResponse = {
1309
- pk: `identity#${pixelId}#volatile-merged`,
1310
- pixelId,
1311
- identityId: 'volatile-merged',
1312
- response: {
1313
- identityId: 'volatile-merged',
1314
- traits: {
1315
- emails: ['test@email.com'],
1316
- phones: ['555-cached'],
1317
- },
1318
- },
1319
- updatedAt: new Date().toISOString(),
1320
- };
1321
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
1322
- const incomingIdentity = {
1323
- identityId: 'volatile-merged',
1324
- traits: {
1325
- emails: ['test@email.com'],
1326
- ipAddress: '10.0.0.1',
1327
- },
1328
- };
1329
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, incomingIdentity, lambdaArn);
1330
- expect(result.summary.outcome).toBe(IDENTITY_CACHE_WITH_CACHING_OUTCOME.VOLATILE_ONLY_CACHE_HIT);
1331
- expect(result.identity?.traits?.emails).toContain('test@email.com');
1332
- expect(result.identity?.traits?.phones).toContain('555-cached');
1333
- expect(result.identity?.traits?.ipAddress).toBe('10.0.0.1');
1334
- });
1335
- });
1336
- describe('skipNeptune derivation from staleTier', () => {
1337
- it('should set skipNeptune=true for ADDRESS_UPDATE tier', async () => {
1338
- IdentityGatewayService.configure({
1339
- awsRegion: 'us-east-1',
1340
- awsAccountId: '123456789',
1341
- asyncUpdateQueueName: 'identity-async-queue',
1342
- });
1343
- mockBuildAndSendMessage.mockResolvedValueOnce({ MessageId: 'skip-neptune-test' });
1344
- const cachedResponse = {
1345
- pk: `identity#${pixelId}#skip-neptune-addr`,
1346
- pixelId,
1347
- identityId: 'skip-neptune-addr',
1348
- response: {
1349
- identityId: 'skip-neptune-addr',
1350
- traits: {
1351
- emails: ['test@email.com'],
1352
- addresses: [],
1353
- },
1354
- },
1355
- updatedAt: new Date().toISOString(),
1356
- };
1357
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
1358
- const incomingIdentity = {
1359
- identityId: 'skip-neptune-addr',
1360
- traits: {
1361
- emails: ['test@email.com'],
1362
- addresses: [{ firstName: 'John', city: 'NYC' }],
1363
- },
1364
- };
1365
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, incomingIdentity, lambdaArn);
1366
- expect(result.summary.didSkipNeptune).toBe(true);
1367
- expect(result.summary.didCallNeptune).toBe(false);
1368
- expect(mockInvokeFunction).not.toHaveBeenCalled();
1369
- });
1370
- it('should set skipNeptune=false for IDENTITY_STALE tier', async () => {
1371
- const cachedResponse = {
1372
- pk: `identity#${pixelId}#call-neptune`,
1373
- pixelId,
1374
- identityId: 'call-neptune',
1375
- response: {
1376
- identityId: 'call-neptune',
1377
- traits: {
1378
- emails: ['old@email.com'],
1379
- },
1380
- },
1381
- updatedAt: new Date().toISOString(),
1382
- };
1383
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
1384
- mockInvokeFunction.mockResolvedValueOnce({
1385
- statusCode: 200,
1386
- body: JSON.stringify({
1387
- identity: {
1388
- identityId: 'call-neptune',
1389
- traits: { emails: ['old@email.com', 'new@email.com'] },
1390
- },
1391
- }),
1392
- });
1393
- const incomingIdentity = {
1394
- identityId: 'call-neptune',
1395
- traits: {
1396
- emails: ['old@email.com', 'new@email.com'],
1397
- },
1398
- };
1399
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, incomingIdentity, lambdaArn);
1400
- expect(result.summary.didSkipNeptune).toBe(false);
1401
- expect(result.summary.didCallNeptune).toBe(true);
1402
- expect(mockInvokeFunction).toHaveBeenCalled();
1403
- });
1404
- });
1405
- describe('writeReason derivation from staleTier', () => {
1406
- it('should set writeReason=address_update for ADDRESS_UPDATE tier', async () => {
1407
- IdentityGatewayService.configure({
1408
- awsRegion: 'us-east-1',
1409
- awsAccountId: '123456789',
1410
- asyncUpdateQueueName: 'identity-async-queue',
1411
- });
1412
- mockBuildAndSendMessage.mockResolvedValueOnce({ MessageId: 'addr-reason-test' });
1413
- const cachedResponse = {
1414
- pk: `identity#${pixelId}#addr-reason`,
1415
- pixelId,
1416
- identityId: 'addr-reason',
1417
- response: {
1418
- identityId: 'addr-reason',
1419
- traits: {
1420
- emails: ['test@email.com'],
1421
- addresses: [],
1422
- },
1423
- },
1424
- updatedAt: new Date().toISOString(),
1425
- };
1426
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
1427
- const incomingIdentity = {
1428
- identityId: 'addr-reason',
1429
- traits: {
1430
- emails: ['test@email.com'],
1431
- addresses: [{ firstName: 'Test', city: 'TestCity' }],
1432
- },
1433
- };
1434
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, incomingIdentity, lambdaArn);
1435
- expect(result.summary.writeReason).toBe('address_update');
1436
- });
1437
- it('should set writeReason=stale_traits for IDENTITY_STALE tier', async () => {
1438
- const cachedResponse = {
1439
- pk: `identity#${pixelId}#stale-reason`,
1440
- pixelId,
1441
- identityId: 'stale-reason',
1442
- response: {
1443
- identityId: 'stale-reason',
1444
- traits: {
1445
- emails: ['old@email.com'],
1446
- },
1447
- },
1448
- updatedAt: new Date().toISOString(),
1449
- };
1450
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
1451
- mockInvokeFunction.mockResolvedValueOnce({
1452
- statusCode: 200,
1453
- body: JSON.stringify({
1454
- identity: {
1455
- identityId: 'stale-reason',
1456
- traits: { emails: ['old@email.com', 'new@email.com'] },
1457
- },
1458
- }),
1459
- });
1460
- const incomingIdentity = {
1461
- identityId: 'stale-reason',
1462
- traits: {
1463
- emails: ['old@email.com', 'new@email.com'],
1464
- },
1465
- };
1466
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, incomingIdentity, lambdaArn);
1467
- expect(result.summary.writeReason).toBeUndefined();
1468
- expect(result.summary.outcome).toBe(IDENTITY_CACHE_WITH_CACHING_OUTCOME.NEPTUNE_RESOLVED);
1469
- });
1470
- });
1471
- describe('ADDRESS_UPDATE → enqueueAsyncIdentityUpdate path', () => {
1472
- it('should enqueue async update instead of calling Neptune', async () => {
1473
- IdentityGatewayService.configure({
1474
- awsRegion: 'us-east-1',
1475
- awsAccountId: '123456789',
1476
- asyncUpdateQueueName: 'identity-async-queue',
1477
- });
1478
- mockBuildAndSendMessage.mockResolvedValueOnce({ MessageId: 'async-path-test' });
1479
- const cachedResponse = {
1480
- pk: `identity#${pixelId}#async-enqueue`,
1481
- pixelId,
1482
- identityId: 'async-enqueue',
1483
- response: {
1484
- identityId: 'async-enqueue',
1485
- traits: {
1486
- emails: ['test@email.com'],
1487
- addresses: [],
1488
- },
1489
- },
1490
- updatedAt: new Date().toISOString(),
1491
- };
1492
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
1493
- const incomingIdentity = {
1494
- identityId: 'async-enqueue',
1495
- traits: {
1496
- emails: ['test@email.com'],
1497
- addresses: [{ firstName: 'Async', city: 'AsyncCity' }],
1498
- },
1499
- };
1500
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, incomingIdentity, lambdaArn);
1501
- expect(result.summary.outcome).toBe(IDENTITY_CACHE_WITH_CACHING_OUTCOME.ADDRESS_UPDATE_ASYNC);
1502
- expect(result.summary.didEnqueueAsync).toBe(true);
1503
- expect(result.summary.didCallNeptune).toBe(false);
1504
- expect(mockBuildAndSendMessage).toHaveBeenCalledWith('identify.updateAsync', expect.objectContaining({
1505
- pixelId,
1506
- incomingIdentityId: 'async-enqueue',
1507
- }));
1508
- expect(mockInvokeFunction).not.toHaveBeenCalled();
1509
- });
1510
- it('should return merged identity immediately (no Neptune wait)', async () => {
1511
- IdentityGatewayService.configure({
1512
- awsRegion: 'us-east-1',
1513
- awsAccountId: '123456789',
1514
- asyncUpdateQueueName: 'identity-async-queue',
1515
- });
1516
- mockBuildAndSendMessage.mockResolvedValueOnce({ MessageId: 'immediate-return' });
1517
- const cachedResponse = {
1518
- pk: `identity#${pixelId}#immediate`,
1519
- pixelId,
1520
- identityId: 'immediate',
1521
- response: {
1522
- identityId: 'immediate',
1523
- traits: {
1524
- emails: ['test@email.com'],
1525
- phones: ['555-cached'],
1526
- addresses: [],
1527
- },
1528
- },
1529
- updatedAt: new Date().toISOString(),
1530
- };
1531
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
1532
- const incomingIdentity = {
1533
- identityId: 'immediate',
1534
- traits: {
1535
- emails: ['test@email.com'],
1536
- addresses: [{ firstName: 'Immediate', city: 'ImmediateCity' }],
1537
- },
1538
- };
1539
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, incomingIdentity, lambdaArn);
1540
- expect(result.identity).toBeDefined();
1541
- expect(result.identity?.identityId).toBe('immediate');
1542
- expect(result.identity?.traits?.phones).toContain('555-cached');
1543
- expect(result.identity?.traits?.addresses).toHaveLength(1);
1544
- });
1545
- });
1546
- describe('IDENTITY_STALE → Neptune path', () => {
1547
- it('should call Neptune for identity-defining trait changes', async () => {
1548
- const cachedResponse = {
1549
- pk: `identity#${pixelId}#neptune-path`,
1550
- pixelId,
1551
- identityId: 'neptune-path',
1552
- response: {
1553
- identityId: 'neptune-path',
1554
- traits: {
1555
- emails: ['old@email.com'],
1556
- },
1557
- },
1558
- updatedAt: new Date().toISOString(),
1559
- };
1560
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
1561
- mockInvokeFunction.mockResolvedValueOnce({
1562
- statusCode: 200,
1563
- body: JSON.stringify({
1564
- identity: {
1565
- identityId: 'neptune-path',
1566
- traits: { emails: ['old@email.com', 'new@email.com'] },
1567
- },
1568
- }),
1569
- });
1570
- const incomingIdentity = {
1571
- identityId: 'neptune-path',
1572
- traits: {
1573
- emails: ['old@email.com', 'new@email.com'],
1574
- },
1575
- };
1576
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, incomingIdentity, lambdaArn);
1577
- expect(result.summary.didCallNeptune).toBe(true);
1578
- expect(mockInvokeFunction).toHaveBeenCalledWith(lambdaArn, expect.objectContaining({
1579
- pixelId,
1580
- }));
1581
- });
1582
- it('should write Neptune result to cache when different from cached', async () => {
1583
- const cachedResponse = {
1584
- pk: `identity#${pixelId}#neptune-write`,
1585
- pixelId,
1586
- identityId: 'neptune-write',
1587
- response: {
1588
- identityId: 'neptune-write',
1589
- traits: {
1590
- emails: ['old@email.com'],
1591
- },
1592
- },
1593
- updatedAt: new Date().toISOString(),
1594
- };
1595
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
1596
- mockInvokeFunction.mockResolvedValueOnce({
1597
- statusCode: 200,
1598
- body: JSON.stringify({
1599
- identity: {
1600
- identityId: 'neptune-write',
1601
- traits: {
1602
- emails: ['old@email.com', 'new@email.com'],
1603
- phones: ['555-neptune'],
1604
- },
1605
- },
1606
- }),
1607
- });
1608
- const incomingIdentity = {
1609
- identityId: 'neptune-write',
1610
- traits: {
1611
- emails: ['old@email.com', 'new@email.com'],
1612
- },
1613
- };
1614
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, incomingIdentity, lambdaArn);
1615
- expect(result.summary.didWriteCache).toBe(false);
1616
- expect(result.summary.outcome).toBe(IDENTITY_CACHE_WITH_CACHING_OUTCOME.NEPTUNE_RESOLVED);
1617
- expect(mockedDynamoDbClient.safeBatchWrite).not.toHaveBeenCalled();
1618
- });
1619
- it('should skip cache write when Neptune result matches cached', async () => {
1620
- const cachedResponse = {
1621
- pk: `identity#${pixelId}#neptune-skip-write`,
1622
- pixelId,
1623
- identityId: 'neptune-skip-write',
1624
- response: {
1625
- identityId: 'neptune-skip-write',
1626
- traits: {
1627
- emails: ['existing@email.com'],
1628
- },
1629
- },
1630
- updatedAt: new Date().toISOString(),
1631
- };
1632
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
1633
- mockInvokeFunction.mockResolvedValueOnce({
1634
- statusCode: 200,
1635
- body: JSON.stringify({
1636
- identity: {
1637
- identityId: 'neptune-skip-write',
1638
- traits: {
1639
- emails: ['existing@email.com'],
1640
- },
1641
- },
1642
- }),
1643
- });
1644
- const incomingIdentity = {
1645
- identityId: 'neptune-skip-write',
1646
- traits: {
1647
- emails: ['existing@email.com', 'thought-new@email.com'],
1648
- },
1649
- };
1650
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, incomingIdentity, lambdaArn);
1651
- expect(result.summary.didCallNeptune).toBe(true);
1652
- expect(result.summary.didWriteCache).toBe(false);
1653
- expect(result.summary.outcome).toBe(IDENTITY_CACHE_WITH_CACHING_OUTCOME.NEPTUNE_RESOLVED);
1654
- });
1655
- it('should return NEPTUNE_RESOLUTION_FAILED when Neptune throws', async () => {
1656
- const cachedResponse = {
1657
- pk: `identity#${pixelId}#neptune-throws`,
1658
- pixelId,
1659
- identityId: 'neptune-throws',
1660
- response: {
1661
- identityId: 'neptune-throws',
1662
- traits: {
1663
- emails: ['old@email.com'],
1664
- },
1665
- },
1666
- updatedAt: new Date().toISOString(),
1667
- };
1668
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
1669
- mockInvokeFunction.mockRejectedValueOnce(new Error('Neptune invocation failed'));
1670
- const incomingIdentity = {
1671
- identityId: 'neptune-throws',
1672
- traits: {
1673
- emails: ['old@email.com', 'new@email.com'],
1674
- },
1675
- };
1676
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, incomingIdentity, lambdaArn);
1677
- expect(result.summary.didCallNeptune).toBe(true);
1678
- expect(result.summary.didWriteCache).toBe(false);
1679
- expect(result.summary.outcome).toBe(IDENTITY_CACHE_WITH_CACHING_OUTCOME.NEPTUNE_RESOLUTION_FAILED);
1680
- expect(result.identity).toBeDefined();
1681
- expect(result.identity?.identityId).toBe('neptune-throws');
1682
- expect(result.identity?.traits?.emails).toEqual(['old@email.com', 'new@email.com']);
1683
- });
1684
- });
1685
- });
1686
- describe('resolveCacheMissViaNeptune helper', () => {
1687
- describe('cache miss without discovered identity ID', () => {
1688
- it('should call Neptune with incoming identity when no discovered ID', async () => {
1689
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(null);
1690
- mockInvokeFunction.mockResolvedValueOnce({
1691
- statusCode: 200,
1692
- body: JSON.stringify({
1693
- identity: {
1694
- identityId: 'neptune-resolved-id',
1695
- traits: { emails: ['test@email.com'] },
1696
- },
1697
- }),
1698
- });
1699
- const incomingIdentity = {
1700
- identityId: 'incoming-id',
1701
- traits: { emails: ['test@email.com'] },
1702
- };
1703
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, incomingIdentity, lambdaArn);
1704
- expect(mockInvokeFunction).toHaveBeenCalledWith(lambdaArn, expect.objectContaining({
1705
- pixelId,
1706
- context: expect.objectContaining({
1707
- identity: expect.objectContaining({
1708
- identityId: 'incoming-id',
1709
- }),
1710
- }),
1711
- }));
1712
- expect(result.summary.outcome).toBe(IDENTITY_CACHE_WITH_CACHING_OUTCOME.NEPTUNE_RESOLVED);
1713
- expect(result.summary.hasDiscoveredIdentityId).toBeUndefined();
1714
- expect(result.summary.writeReason).toBeUndefined();
1715
- });
1716
- it('should set state.didReadCache=true and state.didCallNeptune=true', async () => {
1717
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(null);
1718
- mockInvokeFunction.mockResolvedValueOnce({
1719
- statusCode: 200,
1720
- body: JSON.stringify({
1721
- identity: { identityId: 'resolved', traits: {} },
1722
- }),
1723
- });
1724
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, { identityId: 'test', traits: {} }, lambdaArn);
1725
- expect(result.summary.didReadCache).toBe(true);
1726
- expect(result.summary.didCallNeptune).toBe(true);
1727
- });
1728
- });
1729
- describe('email pointer discovery with discovered identity ID', () => {
1730
- it('should call Neptune with discovered identity ID when email lookup succeeds', async () => {
1731
- const incomingNoId = {
1732
- traits: { emails: ['discovered@email.com'] },
1733
- };
1734
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce({
1735
- pk: `email#${pixelId}#discovered@email.com`,
1736
- identityId: 'discovered-identity-id',
1737
- });
1738
- mockInvokeFunction.mockResolvedValueOnce({
1739
- statusCode: 200,
1740
- body: JSON.stringify({
1741
- identity: {
1742
- identityId: 'discovered-identity-id',
1743
- traits: { emails: ['discovered@email.com', 'other@email.com'] },
1744
- },
1745
- }),
1746
- });
1747
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, incomingNoId, lambdaArn);
1748
- expect(result.identity?.identityId).toBe('discovered-identity-id');
1749
- });
1750
- it('should use incoming identity in Neptune call (not discovered ID)', async () => {
1751
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce({
1752
- pk: `email#${pixelId}#test@email.com`,
1753
- identityId: 'discovered-from-email',
1754
- });
1755
- mockInvokeFunction.mockResolvedValueOnce({
1756
- statusCode: 200,
1757
- body: JSON.stringify({
1758
- identity: {
1759
- identityId: 'neptune-resolved',
1760
- traits: { emails: ['test@email.com'] },
1761
- },
1762
- }),
1763
- });
1764
- await IdentityGatewayService.getIdentityWithCachingV2(pixelId, { traits: { emails: ['test@email.com'] } }, lambdaArn);
1765
- expect(mockInvokeFunction).toHaveBeenCalledWith(lambdaArn, expect.objectContaining({
1766
- pixelId,
1767
- context: expect.objectContaining({
1768
- identity: expect.objectContaining({
1769
- traits: { emails: ['test@email.com'] },
1770
- }),
1771
- }),
1772
- }));
1773
- });
1774
- });
1775
- describe('Neptune resolution success → write-back', () => {
1776
- it('should write to cache when Neptune returns identity', async () => {
1777
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(null);
1778
- mockInvokeFunction.mockResolvedValueOnce({
1779
- statusCode: 200,
1780
- body: JSON.stringify({
1781
- identity: {
1782
- identityId: 'neptune-success',
1783
- traits: { emails: ['test@email.com'], phones: ['555-1234'] },
1784
- },
1785
- }),
1786
- });
1787
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, { identityId: 'incoming', traits: { emails: ['test@email.com'] } }, lambdaArn);
1788
- expect(result.summary.didWriteCache).toBe(false);
1789
- expect(result.summary.outcome).toBe(IDENTITY_CACHE_WITH_CACHING_OUTCOME.NEPTUNE_RESOLVED);
1790
- expect(mockedDynamoDbClient.safeBatchWrite).not.toHaveBeenCalled();
1791
- });
1792
- it('should set resolvedIdentity to Neptune response', async () => {
1793
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(null);
1794
- mockInvokeFunction.mockResolvedValueOnce({
1795
- statusCode: 200,
1796
- body: JSON.stringify({
1797
- identity: {
1798
- identityId: 'from-neptune',
1799
- traits: { emails: ['neptune@email.com'] },
1800
- },
1801
- }),
1802
- });
1803
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, { identityId: 'original', traits: { emails: ['original@email.com'] } }, lambdaArn);
1804
- expect(result.identity?.identityId).toBe('from-neptune');
1805
- expect(result.identity?.traits?.emails).toContain('neptune@email.com');
1806
- });
1807
- it('should include traitChanges with "no cached identity" reason', async () => {
1808
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(null);
1809
- mockInvokeFunction.mockResolvedValueOnce({
1810
- statusCode: 200,
1811
- body: JSON.stringify({
1812
- identity: {
1813
- identityId: 'new-identity',
1814
- traits: { emails: ['new@email.com'] },
1815
- },
1816
- }),
1817
- });
1818
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, { identityId: 'incoming', traits: { emails: ['incoming@email.com'] } }, lambdaArn);
1819
- expect(result.summary.traitChanges).toEqual({
1820
- reason: { resolved: 'no cached identity' },
1821
- });
1822
- });
1823
- });
1824
- describe('Neptune resolution failure → fallback to incoming', () => {
1825
- it('should fallback to incoming identity when Neptune fails', async () => {
1826
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(null);
1827
- mockInvokeFunction.mockRejectedValueOnce(new Error('Neptune unavailable'));
1828
- const incomingIdentity = {
1829
- identityId: 'fallback-incoming',
1830
- traits: { emails: ['fallback@email.com'] },
1831
- };
1832
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, incomingIdentity, lambdaArn);
1833
- expect(result.summary.outcome).toBe(IDENTITY_CACHE_WITH_CACHING_OUTCOME.NEPTUNE_RESOLUTION_FAILED);
1834
- expect(result.summary.didFallbackToIncoming).toBe(true);
1835
- expect(result.identity?.identityId).toBe('fallback-incoming');
1836
- });
1837
- it('should set didWriteCache=false when Neptune fails', async () => {
1838
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(null);
1839
- mockInvokeFunction.mockRejectedValueOnce(new Error('Lambda timeout'));
1840
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, { identityId: 'test', traits: {} }, lambdaArn);
1841
- expect(result.summary.didWriteCache).toBe(false);
1842
- expect(mockedDynamoDbClient.safeBatchWrite).not.toHaveBeenCalled();
1843
- });
1844
- it('should use identityForNeptune (with discovered ID) as fallback when available', async () => {
1845
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce({
1846
- pk: `email#${pixelId}#test@email.com`,
1847
- identityId: 'discovered-id',
1848
- });
1849
- mockInvokeFunction.mockRejectedValueOnce(new Error('Neptune error'));
1850
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, { traits: { emails: ['test@email.com'] } }, lambdaArn);
1851
- expect(result.summary.didFallbackToIncoming).toBe(true);
1852
- expect(result.identity?.identityId).toBe('discovered-id');
1853
- expect(result.identity?.traits?.emails).toContain('test@email.com');
1854
- });
1855
- });
1856
- describe('skipCacheWrite=true behavior', () => {
1857
- it('should NOT write to cache when skipCacheWrite=true', async () => {
1858
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(null);
1859
- mockInvokeFunction.mockResolvedValueOnce({
1860
- statusCode: 200,
1861
- body: JSON.stringify({
1862
- identity: {
1863
- identityId: 'skip-write-identity',
1864
- traits: { emails: ['test@email.com'] },
1865
- },
1866
- }),
1867
- });
1868
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, { identityId: 'incoming', traits: { emails: ['test@email.com'] } }, lambdaArn, { skipCacheWrite: true });
1869
- expect(result.summary.didWriteCache).toBe(false);
1870
- expect(mockedDynamoDbClient.safeBatchWrite).not.toHaveBeenCalled();
1871
- expect(result.identity?.identityId).toBe('skip-write-identity');
1872
- expect(result.summary.outcome).toBe(IDENTITY_CACHE_WITH_CACHING_OUTCOME.NEPTUNE_RESOLVED);
1873
- });
1874
- it('should still call Neptune when skipCacheWrite=true', async () => {
1875
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(null);
1876
- mockInvokeFunction.mockResolvedValueOnce({
1877
- statusCode: 200,
1878
- body: JSON.stringify({
1879
- identity: { identityId: 'resolved', traits: {} },
1880
- }),
1881
- });
1882
- await IdentityGatewayService.getIdentityWithCachingV2(pixelId, { identityId: 'test', traits: {} }, lambdaArn, { skipCacheWrite: true });
1883
- expect(mockInvokeFunction).toHaveBeenCalled();
1884
- });
1885
- });
1886
- });
1887
- describe('resolveStaleViaNeptune helper', () => {
1888
- describe('Neptune resolution success with changes → write-back', () => {
1889
- it('should write to cache when Neptune returns different identity', async () => {
1890
- const cachedResponse = {
1891
- pk: `identity#${pixelId}#stale-changes`,
1892
- pixelId,
1893
- identityId: 'stale-changes',
1894
- response: {
1895
- identityId: 'stale-changes',
1896
- traits: { emails: ['old@email.com'] },
1897
- },
1898
- updatedAt: new Date().toISOString(),
1899
- };
1900
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
1901
- mockInvokeFunction.mockResolvedValueOnce({
1902
- statusCode: 200,
1903
- body: JSON.stringify({
1904
- identity: {
1905
- identityId: 'stale-changes',
1906
- traits: { emails: ['old@email.com', 'new@email.com'], phones: ['555-new'] },
1907
- },
1908
- }),
1909
- });
1910
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, { identityId: 'stale-changes', traits: { emails: ['old@email.com', 'new@email.com'] } }, lambdaArn);
1911
- expect(result.summary.didWriteCache).toBe(false);
1912
- expect(result.summary.outcome).toBe(IDENTITY_CACHE_WITH_CACHING_OUTCOME.NEPTUNE_RESOLVED);
1913
- expect(mockedDynamoDbClient.safeBatchWrite).not.toHaveBeenCalled();
1914
- });
1915
- it('should compute traitChanges between cached and Neptune identity', async () => {
1916
- const cachedResponse = {
1917
- pk: `identity#${pixelId}#trait-changes`,
1918
- pixelId,
1919
- identityId: 'trait-changes',
1920
- response: {
1921
- identityId: 'trait-changes',
1922
- traits: {
1923
- emails: ['old@email.com'],
1924
- phones: ['555-old'],
1925
- },
1926
- },
1927
- updatedAt: new Date().toISOString(),
1928
- };
1929
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
1930
- mockInvokeFunction.mockResolvedValueOnce({
1931
- statusCode: 200,
1932
- body: JSON.stringify({
1933
- identity: {
1934
- identityId: 'trait-changes',
1935
- traits: {
1936
- emails: ['old@email.com', 'new@email.com'],
1937
- phones: ['555-old', '555-new'],
1938
- },
1939
- },
1940
- }),
1941
- });
1942
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, { identityId: 'trait-changes', traits: { emails: ['old@email.com', 'new@email.com'] } }, lambdaArn);
1943
- expect(result.summary.traitChanges).toBeDefined();
1944
- expect(result.summary.traitChanges?.emails).toBeDefined();
1945
- });
1946
- });
1947
- describe('Neptune resolution success without changes → skip write', () => {
1948
- it('should skip cache write when Neptune matches cached', async () => {
1949
- const cachedResponse = {
1950
- pk: `identity#${pixelId}#no-changes`,
1951
- pixelId,
1952
- identityId: 'no-changes',
1953
- response: {
1954
- identityId: 'no-changes',
1955
- traits: { emails: ['same@email.com'] },
1956
- },
1957
- updatedAt: new Date().toISOString(),
1958
- };
1959
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
1960
- mockInvokeFunction.mockResolvedValueOnce({
1961
- statusCode: 200,
1962
- body: JSON.stringify({
1963
- identity: {
1964
- identityId: 'no-changes',
1965
- traits: { emails: ['same@email.com'] },
1966
- },
1967
- }),
1968
- });
1969
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, { identityId: 'no-changes', traits: { emails: ['same@email.com', 'thought-new@email.com'] } }, lambdaArn);
1970
- expect(result.summary.didCallNeptune).toBe(true);
1971
- expect(result.summary.didWriteCache).toBe(false);
1972
- expect(result.summary.outcome).toBe(IDENTITY_CACHE_WITH_CACHING_OUTCOME.NEPTUNE_RESOLVED);
1973
- expect(mockedDynamoDbClient.safeBatchWrite).not.toHaveBeenCalled();
1974
- });
1975
- it('should still return Neptune identity even when skipping write', async () => {
1976
- const cachedResponse = {
1977
- pk: `identity#${pixelId}#return-identity`,
1978
- pixelId,
1979
- identityId: 'return-identity',
1980
- response: {
1981
- identityId: 'return-identity',
1982
- traits: { emails: ['cached@email.com'] },
1983
- },
1984
- updatedAt: new Date().toISOString(),
1985
- };
1986
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
1987
- mockInvokeFunction.mockResolvedValueOnce({
1988
- statusCode: 200,
1989
- body: JSON.stringify({
1990
- identity: {
1991
- identityId: 'return-identity',
1992
- traits: { emails: ['cached@email.com'] },
1993
- },
1994
- }),
1995
- });
1996
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, { identityId: 'return-identity', traits: { emails: ['cached@email.com', 'extra@email.com'] } }, lambdaArn);
1997
- expect(result.identity?.identityId).toBe('return-identity');
1998
- });
1999
- it('should strip address timestamps for comparison', async () => {
2000
- const now = Date.now();
2001
- const cachedResponse = {
2002
- pk: `identity#${pixelId}#addr-timestamp`,
2003
- pixelId,
2004
- identityId: 'addr-timestamp',
2005
- response: {
2006
- identityId: 'addr-timestamp',
2007
- traits: {
2008
- emails: ['test@email.com'],
2009
- addresses: [{ city: 'NYC', cra: now - 1000, upa: now - 500 }],
2010
- },
2011
- },
2012
- updatedAt: new Date().toISOString(),
2013
- };
2014
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
2015
- mockInvokeFunction.mockResolvedValueOnce({
2016
- statusCode: 200,
2017
- body: JSON.stringify({
2018
- identity: {
2019
- identityId: 'addr-timestamp',
2020
- traits: {
2021
- emails: ['test@email.com'],
2022
- addresses: [{ city: 'NYC', cra: now, upa: now }],
2023
- },
2024
- },
2025
- }),
2026
- });
2027
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, { identityId: 'addr-timestamp', traits: { emails: ['test@email.com', 'new@email.com'] } }, lambdaArn);
2028
- expect(result.summary.didWriteCache).toBe(false);
2029
- expect(result.summary.outcome).toBe(IDENTITY_CACHE_WITH_CACHING_OUTCOME.NEPTUNE_RESOLVED);
2030
- });
2031
- });
2032
- describe('Neptune resolution failure', () => {
2033
- it('should return NEPTUNE_RESOLUTION_FAILED when Neptune fails', async () => {
2034
- const cachedResponse = {
2035
- pk: `identity#${pixelId}#neptune-fail`,
2036
- pixelId,
2037
- identityId: 'neptune-fail',
2038
- response: {
2039
- identityId: 'neptune-fail',
2040
- traits: { emails: ['cached@email.com'] },
2041
- },
2042
- updatedAt: new Date().toISOString(),
2043
- };
2044
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
2045
- mockInvokeFunction.mockRejectedValueOnce(new Error('Lambda invocation failed'));
2046
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, { identityId: 'neptune-fail', traits: { emails: ['cached@email.com', 'new@email.com'] } }, lambdaArn);
2047
- expect(result.summary.didCallNeptune).toBe(true);
2048
- expect(result.summary.outcome).toBe(IDENTITY_CACHE_WITH_CACHING_OUTCOME.NEPTUNE_RESOLUTION_FAILED);
2049
- });
2050
- it('should not write to cache when Neptune fails', async () => {
2051
- const cachedResponse = {
2052
- pk: `identity#${pixelId}#no-write-fail`,
2053
- pixelId,
2054
- identityId: 'no-write-fail',
2055
- response: {
2056
- identityId: 'no-write-fail',
2057
- traits: { emails: ['test@email.com'] },
2058
- },
2059
- updatedAt: new Date().toISOString(),
2060
- };
2061
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
2062
- mockInvokeFunction.mockRejectedValueOnce(new Error('Network error'));
2063
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, { identityId: 'no-write-fail', traits: { emails: ['test@email.com', 'extra@email.com'] } }, lambdaArn);
2064
- expect(result.summary.didWriteCache).toBe(false);
2065
- expect(mockedDynamoDbClient.safeBatchWrite).not.toHaveBeenCalled();
2066
- });
2067
- it('should still return merged identity when Neptune fails', async () => {
2068
- const cachedResponse = {
2069
- pk: `identity#${pixelId}#merged-on-fail`,
2070
- pixelId,
2071
- identityId: 'merged-on-fail',
2072
- response: {
2073
- identityId: 'merged-on-fail',
2074
- traits: {
2075
- emails: ['cached@email.com'],
2076
- phones: ['555-cached'],
2077
- },
2078
- },
2079
- updatedAt: new Date().toISOString(),
2080
- };
2081
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
2082
- mockInvokeFunction.mockRejectedValueOnce(new Error('Timeout'));
2083
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, { identityId: 'merged-on-fail', traits: { emails: ['cached@email.com', 'new@email.com'] } }, lambdaArn);
2084
- expect(result.identity).toBeDefined();
2085
- });
2086
- });
2087
- describe('skipCacheWrite=true behavior', () => {
2088
- it('should NOT write to cache when skipCacheWrite=true even with changes', async () => {
2089
- const cachedResponse = {
2090
- pk: `identity#${pixelId}#skip-stale`,
2091
- pixelId,
2092
- identityId: 'skip-stale',
2093
- response: {
2094
- identityId: 'skip-stale',
2095
- traits: { emails: ['old@email.com'] },
2096
- },
2097
- updatedAt: new Date().toISOString(),
2098
- };
2099
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
2100
- mockInvokeFunction.mockResolvedValueOnce({
2101
- statusCode: 200,
2102
- body: JSON.stringify({
2103
- identity: {
2104
- identityId: 'skip-stale',
2105
- traits: { emails: ['old@email.com', 'new@email.com'], phones: ['555-new'] },
2106
- },
2107
- }),
2108
- });
2109
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, { identityId: 'skip-stale', traits: { emails: ['old@email.com', 'new@email.com'] } }, lambdaArn, { skipCacheWrite: true });
2110
- expect(result.summary.didWriteCache).toBe(false);
2111
- expect(mockedDynamoDbClient.safeBatchWrite).not.toHaveBeenCalled();
2112
- expect(result.summary.outcome).toBe(IDENTITY_CACHE_WITH_CACHING_OUTCOME.NEPTUNE_RESOLVED);
2113
- });
2114
- it('should still call Neptune when skipCacheWrite=true', async () => {
2115
- const cachedResponse = {
2116
- pk: `identity#${pixelId}#call-neptune-skip`,
2117
- pixelId,
2118
- identityId: 'call-neptune-skip',
2119
- response: {
2120
- identityId: 'call-neptune-skip',
2121
- traits: { emails: ['test@email.com'] },
2122
- },
2123
- updatedAt: new Date().toISOString(),
2124
- };
2125
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
2126
- mockInvokeFunction.mockResolvedValueOnce({
2127
- statusCode: 200,
2128
- body: JSON.stringify({
2129
- identity: {
2130
- identityId: 'call-neptune-skip',
2131
- traits: { emails: ['test@email.com', 'added@email.com'] },
2132
- },
2133
- }),
2134
- });
2135
- await IdentityGatewayService.getIdentityWithCachingV2(pixelId, { identityId: 'call-neptune-skip', traits: { emails: ['test@email.com', 'added@email.com'] } }, lambdaArn, { skipCacheWrite: true });
2136
- expect(mockInvokeFunction).toHaveBeenCalled();
2137
- });
2138
- it('should return Neptune identity when skipCacheWrite=true', async () => {
2139
- const cachedResponse = {
2140
- pk: `identity#${pixelId}#return-skip`,
2141
- pixelId,
2142
- identityId: 'return-skip',
2143
- response: {
2144
- identityId: 'return-skip',
2145
- traits: { emails: ['cached@email.com'] },
2146
- },
2147
- updatedAt: new Date().toISOString(),
2148
- };
2149
- mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
2150
- mockInvokeFunction.mockResolvedValueOnce({
2151
- statusCode: 200,
2152
- body: JSON.stringify({
2153
- identity: {
2154
- identityId: 'return-skip',
2155
- traits: {
2156
- emails: ['cached@email.com', 'neptune@email.com'],
2157
- phones: ['555-neptune'],
2158
- },
2159
- },
2160
- }),
2161
- });
2162
- const result = await IdentityGatewayService.getIdentityWithCachingV2(pixelId, { identityId: 'return-skip', traits: { emails: ['cached@email.com', 'neptune@email.com'] } }, lambdaArn, { skipCacheWrite: true });
2163
- expect(result.identity?.traits?.phones).toContain('555-neptune');
2164
- });
2165
- });
2166
- });
2167
- });
2168
- //# sourceMappingURL=identity-gateway-service.spec.js.map