@adtrackify/at-service-common 4.0.14 → 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.
- package/dist/cjs/__tests__/identity-cache/identity-cache-tier-routing.spec.js +29 -25
- package/dist/cjs/__tests__/identity-cache/identity-cache-tier-routing.spec.js.map +1 -1
- package/dist/cjs/__tests__/identity-cache/identity-gateway-cache-hit.spec.js +138 -0
- package/dist/cjs/__tests__/identity-cache/identity-gateway-cache-hit.spec.js.map +1 -0
- package/dist/cjs/__tests__/identity-cache/identity-gateway-cache-miss.spec.js +153 -0
- package/dist/cjs/__tests__/identity-cache/identity-gateway-cache-miss.spec.js.map +1 -0
- package/dist/cjs/__tests__/identity-cache/identity-gateway-constructor.spec.d.ts +1 -0
- package/dist/cjs/__tests__/identity-cache/identity-gateway-constructor.spec.js +72 -0
- package/dist/cjs/__tests__/identity-cache/identity-gateway-constructor.spec.js.map +1 -0
- package/dist/cjs/__tests__/identity-cache/identity-gateway-edge-cases.spec.d.ts +1 -0
- package/dist/cjs/__tests__/identity-cache/identity-gateway-edge-cases.spec.js +89 -0
- package/dist/cjs/__tests__/identity-cache/identity-gateway-edge-cases.spec.js.map +1 -0
- package/dist/cjs/__tests__/identity-cache/identity-gateway-enqueue-async.spec.d.ts +1 -0
- package/dist/cjs/__tests__/identity-cache/identity-gateway-enqueue-async.spec.js +136 -0
- package/dist/cjs/__tests__/identity-cache/identity-gateway-enqueue-async.spec.js.map +1 -0
- package/dist/cjs/__tests__/identity-cache/identity-gateway-stale-cache.spec.d.ts +1 -0
- package/dist/cjs/__tests__/identity-cache/identity-gateway-stale-cache.spec.js +224 -0
- package/dist/cjs/__tests__/identity-cache/identity-gateway-stale-cache.spec.js.map +1 -0
- package/dist/cjs/__tests__/identity-cache/identity-gateway-test-setup.d.ts +28 -0
- package/dist/cjs/__tests__/identity-cache/identity-gateway-test-setup.js +90 -0
- package/dist/cjs/__tests__/identity-cache/identity-gateway-test-setup.js.map +1 -0
- package/dist/cjs/services/db/identity-cache-dynamodb-service/identity-gateway-service.d.ts +11 -12
- package/dist/cjs/services/db/identity-cache-dynamodb-service/identity-gateway-service.js +15 -18
- package/dist/cjs/services/db/identity-cache-dynamodb-service/identity-gateway-service.js.map +1 -1
- package/dist/cjs/services/db/identity-cache-dynamodb-service/types.d.ts +2 -4
- package/dist/cjs/services/db/identity-cache-dynamodb-service/types.js.map +1 -1
- package/dist/esm/__tests__/identity-cache/identity-cache-tier-routing.spec.js +29 -25
- package/dist/esm/__tests__/identity-cache/identity-cache-tier-routing.spec.js.map +1 -1
- package/dist/esm/__tests__/identity-cache/identity-gateway-cache-hit.spec.d.ts +1 -0
- package/dist/esm/__tests__/identity-cache/identity-gateway-cache-hit.spec.js +136 -0
- package/dist/esm/__tests__/identity-cache/identity-gateway-cache-hit.spec.js.map +1 -0
- package/dist/esm/__tests__/identity-cache/identity-gateway-cache-miss.spec.d.ts +1 -0
- package/dist/esm/__tests__/identity-cache/identity-gateway-cache-miss.spec.js +151 -0
- package/dist/esm/__tests__/identity-cache/identity-gateway-cache-miss.spec.js.map +1 -0
- package/dist/esm/__tests__/identity-cache/identity-gateway-constructor.spec.d.ts +1 -0
- package/dist/esm/__tests__/identity-cache/identity-gateway-constructor.spec.js +70 -0
- package/dist/esm/__tests__/identity-cache/identity-gateway-constructor.spec.js.map +1 -0
- package/dist/esm/__tests__/identity-cache/identity-gateway-edge-cases.spec.d.ts +1 -0
- package/dist/esm/__tests__/identity-cache/identity-gateway-edge-cases.spec.js +87 -0
- package/dist/esm/__tests__/identity-cache/identity-gateway-edge-cases.spec.js.map +1 -0
- package/dist/esm/__tests__/identity-cache/identity-gateway-enqueue-async.spec.d.ts +1 -0
- package/dist/esm/__tests__/identity-cache/identity-gateway-enqueue-async.spec.js +134 -0
- package/dist/esm/__tests__/identity-cache/identity-gateway-enqueue-async.spec.js.map +1 -0
- package/dist/esm/__tests__/identity-cache/identity-gateway-stale-cache.spec.d.ts +1 -0
- package/dist/esm/__tests__/identity-cache/identity-gateway-stale-cache.spec.js +222 -0
- package/dist/esm/__tests__/identity-cache/identity-gateway-stale-cache.spec.js.map +1 -0
- package/dist/esm/__tests__/identity-cache/identity-gateway-test-setup.d.ts +28 -0
- package/dist/esm/__tests__/identity-cache/identity-gateway-test-setup.js +83 -0
- package/dist/esm/__tests__/identity-cache/identity-gateway-test-setup.js.map +1 -0
- package/dist/esm/services/db/identity-cache-dynamodb-service/identity-gateway-service.d.ts +11 -12
- package/dist/esm/services/db/identity-cache-dynamodb-service/identity-gateway-service.js +15 -18
- package/dist/esm/services/db/identity-cache-dynamodb-service/identity-gateway-service.js.map +1 -1
- package/dist/esm/services/db/identity-cache-dynamodb-service/types.d.ts +2 -4
- package/dist/esm/services/db/identity-cache-dynamodb-service/types.js.map +1 -1
- package/package.json +1 -1
- package/dist/cjs/__tests__/identity-cache/identity-gateway-service.spec.js +0 -1244
- package/dist/cjs/__tests__/identity-cache/identity-gateway-service.spec.js.map +0 -1
- package/dist/esm/__tests__/identity-cache/identity-gateway-service.spec.js +0 -1242
- package/dist/esm/__tests__/identity-cache/identity-gateway-service.spec.js.map +0 -1
- /package/dist/cjs/__tests__/identity-cache/{identity-gateway-service.spec.d.ts → identity-gateway-cache-hit.spec.d.ts} +0 -0
- /package/dist/{esm/__tests__/identity-cache/identity-gateway-service.spec.d.ts → cjs/__tests__/identity-cache/identity-gateway-cache-miss.spec.d.ts} +0 -0
|
@@ -1,1242 +0,0 @@
|
|
|
1
|
-
const mockLambdaInvokeFunction = 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: mockLambdaInvokeFunction,
|
|
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
|
-
const mockGetIdentity = jest.fn();
|
|
34
|
-
jest.mock('../../clients/internal-api/identity-client.js', () => ({
|
|
35
|
-
IdentityClient: jest.fn().mockImplementation(() => ({
|
|
36
|
-
getIdentity: mockGetIdentity,
|
|
37
|
-
})),
|
|
38
|
-
}));
|
|
39
|
-
import { IdentityGatewayService, } from '../../services/db/identity-cache-dynamodb-service/identity-gateway-service.js';
|
|
40
|
-
import { IDENTITY_CACHE_WITH_CACHING_OUTCOME, } from '../../services/db/identity-cache-dynamodb-service/types.js';
|
|
41
|
-
import { DynamoDbClient, SQSClient } from '../../clients/index.js';
|
|
42
|
-
import { Logger } from '../../helpers/index.js';
|
|
43
|
-
const mockedDynamoDbClient = DynamoDbClient;
|
|
44
|
-
const mockedLogger = Logger;
|
|
45
|
-
const mockedSQSClient = SQSClient;
|
|
46
|
-
describe('IdentityGatewayService new features', () => {
|
|
47
|
-
const pixelId = 'pixel123';
|
|
48
|
-
const lambdaArn = 'arn:aws:lambda:us-east-1:123456789:function:identity-private';
|
|
49
|
-
beforeEach(() => {
|
|
50
|
-
jest.clearAllMocks();
|
|
51
|
-
mockedDynamoDbClient.safeGet.mockReset();
|
|
52
|
-
mockedDynamoDbClient.safePut.mockReset();
|
|
53
|
-
mockedDynamoDbClient.safeDelete.mockReset();
|
|
54
|
-
mockedDynamoDbClient.safeBatchGet.mockReset();
|
|
55
|
-
mockedDynamoDbClient.safeBatchWrite.mockReset();
|
|
56
|
-
mockedDynamoDbClient.safeQueryByGSI.mockReset();
|
|
57
|
-
mockedDynamoDbClient.batchWrite.mockReset();
|
|
58
|
-
mockGetIdentity.mockReset();
|
|
59
|
-
mockBuildAndSendMessage.mockReset();
|
|
60
|
-
mockSQSClientConstructor.mockReset();
|
|
61
|
-
mockedSQSClient.mockClear();
|
|
62
|
-
IdentityGatewayService.identityClient = undefined;
|
|
63
|
-
IdentityGatewayService.sqsClient = undefined;
|
|
64
|
-
IdentityGatewayService.sqsQueueUrl = undefined;
|
|
65
|
-
IdentityGatewayService.configure({ identifyPrivateLambdaArn: lambdaArn });
|
|
66
|
-
});
|
|
67
|
-
describe('configure()', () => {
|
|
68
|
-
it('should configure with direct queue URL', () => {
|
|
69
|
-
const options = {
|
|
70
|
-
asyncUpdateQueueUrl: 'https://sqs.us-east-1.amazonaws.com/123456789/test-queue',
|
|
71
|
-
};
|
|
72
|
-
IdentityGatewayService.configure(options);
|
|
73
|
-
expect(IdentityGatewayService.sqsQueueUrl).toBe(options.asyncUpdateQueueUrl);
|
|
74
|
-
});
|
|
75
|
-
it('should configure with region/accountId/queueName', () => {
|
|
76
|
-
const options = {
|
|
77
|
-
awsRegion: 'us-east-1',
|
|
78
|
-
awsAccountId: '123456789',
|
|
79
|
-
asyncUpdateQueueName: 'identity-async-queue',
|
|
80
|
-
};
|
|
81
|
-
IdentityGatewayService.configure(options);
|
|
82
|
-
expect(mockSQSClientConstructor).toHaveBeenCalledWith('us-east-1', '123456789', 'identity-async-queue');
|
|
83
|
-
expect(IdentityGatewayService.sqsClient).toBeDefined();
|
|
84
|
-
expect(IdentityGatewayService.sqsQueueUrl).toBeDefined();
|
|
85
|
-
});
|
|
86
|
-
it('should fallback to environment variable for queue name', () => {
|
|
87
|
-
const originalEnv = process.env.IDENTIFY_ASYNC_UPDATE_QUEUE_NAME;
|
|
88
|
-
process.env.IDENTIFY_ASYNC_UPDATE_QUEUE_NAME = 'env-queue-name';
|
|
89
|
-
try {
|
|
90
|
-
const options = {
|
|
91
|
-
awsRegion: 'us-west-2',
|
|
92
|
-
awsAccountId: '987654321',
|
|
93
|
-
};
|
|
94
|
-
IdentityGatewayService.configure(options);
|
|
95
|
-
expect(mockSQSClientConstructor).toHaveBeenCalledWith('us-west-2', '987654321', 'env-queue-name');
|
|
96
|
-
}
|
|
97
|
-
finally {
|
|
98
|
-
if (originalEnv !== undefined) {
|
|
99
|
-
process.env.IDENTIFY_ASYNC_UPDATE_QUEUE_NAME = originalEnv;
|
|
100
|
-
}
|
|
101
|
-
else {
|
|
102
|
-
delete process.env.IDENTIFY_ASYNC_UPDATE_QUEUE_NAME;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
});
|
|
106
|
-
it('should not configure SQS when region/accountId provided but no queue name', () => {
|
|
107
|
-
const originalEnv = process.env.IDENTIFY_ASYNC_UPDATE_QUEUE_NAME;
|
|
108
|
-
delete process.env.IDENTIFY_ASYNC_UPDATE_QUEUE_NAME;
|
|
109
|
-
try {
|
|
110
|
-
const options = {
|
|
111
|
-
awsRegion: 'us-east-1',
|
|
112
|
-
awsAccountId: '123456789',
|
|
113
|
-
};
|
|
114
|
-
IdentityGatewayService.configure(options);
|
|
115
|
-
expect(mockSQSClientConstructor).not.toHaveBeenCalled();
|
|
116
|
-
expect(IdentityGatewayService.sqsClient).toBeUndefined();
|
|
117
|
-
}
|
|
118
|
-
finally {
|
|
119
|
-
if (originalEnv !== undefined) {
|
|
120
|
-
process.env.IDENTIFY_ASYNC_UPDATE_QUEUE_NAME = originalEnv;
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
});
|
|
124
|
-
it('should not configure SQS when only region provided', () => {
|
|
125
|
-
const options = {
|
|
126
|
-
awsRegion: 'us-east-1',
|
|
127
|
-
};
|
|
128
|
-
IdentityGatewayService.configure(options);
|
|
129
|
-
expect(mockSQSClientConstructor).not.toHaveBeenCalled();
|
|
130
|
-
expect(IdentityGatewayService.sqsClient).toBeUndefined();
|
|
131
|
-
});
|
|
132
|
-
});
|
|
133
|
-
describe('enqueueAsyncIdentityUpdate()', () => {
|
|
134
|
-
const testIdentity = {
|
|
135
|
-
identityId: 'identity456',
|
|
136
|
-
traits: { emails: ['test@email.com'] },
|
|
137
|
-
};
|
|
138
|
-
const testContext = {
|
|
139
|
-
identity: testIdentity,
|
|
140
|
-
};
|
|
141
|
-
it('should return error result when SQS client is not configured', async () => {
|
|
142
|
-
const result = await IdentityGatewayService.enqueueAsyncIdentityUpdate({
|
|
143
|
-
pixelId,
|
|
144
|
-
identity: testIdentity,
|
|
145
|
-
context: testContext,
|
|
146
|
-
reason: 'test reason',
|
|
147
|
-
eventId: 'event123',
|
|
148
|
-
});
|
|
149
|
-
expect(result.success).toBe(false);
|
|
150
|
-
expect(result.error).toBe('SQS client not configured');
|
|
151
|
-
expect(result.messageId).toBeUndefined();
|
|
152
|
-
expect(mockedLogger.warn).toHaveBeenCalledWith('IdentityGateway - enqueueAsyncIdentityUpdate: SQS client not configured', expect.objectContaining({
|
|
153
|
-
pixelId,
|
|
154
|
-
identityId: testIdentity.identityId,
|
|
155
|
-
reason: 'test reason',
|
|
156
|
-
eventId: 'event123',
|
|
157
|
-
}));
|
|
158
|
-
});
|
|
159
|
-
it('should successfully enqueue message when SQS client is configured', async () => {
|
|
160
|
-
IdentityGatewayService.configure({
|
|
161
|
-
awsRegion: 'us-east-1',
|
|
162
|
-
awsAccountId: '123456789',
|
|
163
|
-
asyncUpdateQueueName: 'test-queue',
|
|
164
|
-
});
|
|
165
|
-
mockBuildAndSendMessage.mockResolvedValueOnce({
|
|
166
|
-
MessageId: 'sqs-message-id-123',
|
|
167
|
-
});
|
|
168
|
-
const result = await IdentityGatewayService.enqueueAsyncIdentityUpdate({
|
|
169
|
-
pixelId,
|
|
170
|
-
identity: testIdentity,
|
|
171
|
-
context: testContext,
|
|
172
|
-
reason: 'stale cache traits',
|
|
173
|
-
eventId: 'event456',
|
|
174
|
-
});
|
|
175
|
-
expect(result.success).toBe(true);
|
|
176
|
-
expect(result.messageId).toBe('sqs-message-id-123');
|
|
177
|
-
expect(result.error).toBeUndefined();
|
|
178
|
-
expect(mockBuildAndSendMessage).toHaveBeenCalledWith('identify.updateAsync', expect.objectContaining({
|
|
179
|
-
pixelId,
|
|
180
|
-
incomingIdentityId: testIdentity.identityId,
|
|
181
|
-
context: testContext,
|
|
182
|
-
}));
|
|
183
|
-
expect(mockedLogger.debug).toHaveBeenCalledWith('IdentityGateway - enqueueAsyncIdentityUpdate: message sent', expect.objectContaining({
|
|
184
|
-
pixelId,
|
|
185
|
-
identityId: testIdentity.identityId,
|
|
186
|
-
reason: 'stale cache traits',
|
|
187
|
-
eventId: 'event456',
|
|
188
|
-
sqsMessageId: 'sqs-message-id-123',
|
|
189
|
-
}));
|
|
190
|
-
});
|
|
191
|
-
it('should fail open with logging when SQS send fails', async () => {
|
|
192
|
-
IdentityGatewayService.configure({
|
|
193
|
-
awsRegion: 'us-east-1',
|
|
194
|
-
awsAccountId: '123456789',
|
|
195
|
-
asyncUpdateQueueName: 'test-queue',
|
|
196
|
-
});
|
|
197
|
-
mockBuildAndSendMessage.mockRejectedValueOnce(new Error('SQS send failed'));
|
|
198
|
-
const result = await IdentityGatewayService.enqueueAsyncIdentityUpdate({
|
|
199
|
-
pixelId,
|
|
200
|
-
identity: testIdentity,
|
|
201
|
-
context: testContext,
|
|
202
|
-
reason: 'test reason',
|
|
203
|
-
eventId: 'event789',
|
|
204
|
-
});
|
|
205
|
-
expect(result.success).toBe(false);
|
|
206
|
-
expect(result.error).toBe('SQS send failed');
|
|
207
|
-
expect(result.messageId).toBeUndefined();
|
|
208
|
-
expect(mockedLogger.warn).toHaveBeenCalledWith('IdentityGateway - enqueueAsyncIdentityUpdate: failed to enqueue (failing open)', expect.objectContaining({
|
|
209
|
-
error: expect.any(Error),
|
|
210
|
-
errorMessage: 'SQS send failed',
|
|
211
|
-
pixelId,
|
|
212
|
-
identityId: testIdentity.identityId,
|
|
213
|
-
reason: 'test reason',
|
|
214
|
-
eventId: 'event789',
|
|
215
|
-
}));
|
|
216
|
-
});
|
|
217
|
-
it('should build message shape matching IdentifyAsyncUpdateMessage interface', async () => {
|
|
218
|
-
IdentityGatewayService.configure({
|
|
219
|
-
awsRegion: 'us-east-1',
|
|
220
|
-
awsAccountId: '123456789',
|
|
221
|
-
asyncUpdateQueueName: 'test-queue',
|
|
222
|
-
});
|
|
223
|
-
mockBuildAndSendMessage.mockResolvedValueOnce({ MessageId: 'test-id' });
|
|
224
|
-
const identity = {
|
|
225
|
-
identityId: 'id-xyz',
|
|
226
|
-
traits: { emails: ['user@example.com'], userIds: ['user-1'] },
|
|
227
|
-
};
|
|
228
|
-
const context = { identity };
|
|
229
|
-
await IdentityGatewayService.enqueueAsyncIdentityUpdate({
|
|
230
|
-
pixelId: 'pixel-abc',
|
|
231
|
-
identity,
|
|
232
|
-
context,
|
|
233
|
-
reason: 'test',
|
|
234
|
-
});
|
|
235
|
-
const messageArg = mockBuildAndSendMessage.mock.calls[0][1];
|
|
236
|
-
expect(messageArg).toEqual({
|
|
237
|
-
pixelId: 'pixel-abc',
|
|
238
|
-
incomingIdentityId: 'id-xyz',
|
|
239
|
-
context,
|
|
240
|
-
});
|
|
241
|
-
});
|
|
242
|
-
it('should return error when identity has no identityId', async () => {
|
|
243
|
-
IdentityGatewayService.configure({
|
|
244
|
-
awsRegion: 'us-east-1',
|
|
245
|
-
awsAccountId: '123456789',
|
|
246
|
-
asyncUpdateQueueName: 'test-queue',
|
|
247
|
-
});
|
|
248
|
-
const identityWithoutId = {
|
|
249
|
-
traits: { emails: ['anon@example.com'] },
|
|
250
|
-
};
|
|
251
|
-
const context = { identity: identityWithoutId };
|
|
252
|
-
const result = await IdentityGatewayService.enqueueAsyncIdentityUpdate({
|
|
253
|
-
pixelId,
|
|
254
|
-
identity: identityWithoutId,
|
|
255
|
-
context,
|
|
256
|
-
reason: 'anonymous identity',
|
|
257
|
-
});
|
|
258
|
-
expect(result.success).toBe(false);
|
|
259
|
-
expect(result.error).toBe('Missing required pixelId or identityId');
|
|
260
|
-
expect(mockBuildAndSendMessage).not.toHaveBeenCalled();
|
|
261
|
-
});
|
|
262
|
-
it('should handle optional eventId parameter', async () => {
|
|
263
|
-
IdentityGatewayService.configure({
|
|
264
|
-
awsRegion: 'us-east-1',
|
|
265
|
-
awsAccountId: '123456789',
|
|
266
|
-
asyncUpdateQueueName: 'test-queue',
|
|
267
|
-
});
|
|
268
|
-
mockBuildAndSendMessage.mockResolvedValueOnce({ MessageId: 'test-id' });
|
|
269
|
-
await IdentityGatewayService.enqueueAsyncIdentityUpdate({
|
|
270
|
-
pixelId,
|
|
271
|
-
identity: testIdentity,
|
|
272
|
-
context: testContext,
|
|
273
|
-
reason: 'test',
|
|
274
|
-
});
|
|
275
|
-
expect(mockedLogger.debug).toHaveBeenCalledWith('IdentityGateway - enqueueAsyncIdentityUpdate: message sent', expect.objectContaining({
|
|
276
|
-
eventId: undefined,
|
|
277
|
-
}));
|
|
278
|
-
});
|
|
279
|
-
});
|
|
280
|
-
describe('resolveIdentityWithCaching() with skipCacheWrite option', () => {
|
|
281
|
-
const incomingIdentity = {
|
|
282
|
-
identityId: 'identity456',
|
|
283
|
-
traits: { emails: ['test@email.com'] },
|
|
284
|
-
};
|
|
285
|
-
describe('default behavior (no options)', () => {
|
|
286
|
-
it('should write to cache when Neptune returns new identity', async () => {
|
|
287
|
-
mockedDynamoDbClient.safeGet.mockResolvedValueOnce(null);
|
|
288
|
-
mockGetIdentity.mockResolvedValueOnce({
|
|
289
|
-
identityId: 'resolved-identity',
|
|
290
|
-
traits: { emails: ['test@email.com', 'resolved@email.com'] },
|
|
291
|
-
});
|
|
292
|
-
const result = await IdentityGatewayService.resolveIdentityWithCaching(pixelId, incomingIdentity);
|
|
293
|
-
expect(result.didWriteCache).toBe(false);
|
|
294
|
-
expect(mockedDynamoDbClient.safeBatchWrite).not.toHaveBeenCalled();
|
|
295
|
-
});
|
|
296
|
-
});
|
|
297
|
-
describe('skipCacheWrite: false', () => {
|
|
298
|
-
it('should write to cache (same as default)', async () => {
|
|
299
|
-
mockedDynamoDbClient.safeGet.mockResolvedValueOnce(null);
|
|
300
|
-
mockGetIdentity.mockResolvedValueOnce({
|
|
301
|
-
identityId: 'resolved-identity',
|
|
302
|
-
traits: { emails: ['test@email.com'] },
|
|
303
|
-
});
|
|
304
|
-
const result = await IdentityGatewayService.resolveIdentityWithCaching(pixelId, incomingIdentity);
|
|
305
|
-
expect(result.didWriteCache).toBe(false);
|
|
306
|
-
expect(mockedDynamoDbClient.safeBatchWrite).not.toHaveBeenCalled();
|
|
307
|
-
});
|
|
308
|
-
});
|
|
309
|
-
describe('skipCacheWrite: true', () => {
|
|
310
|
-
it('should NOT write to cache on Neptune write-back path', async () => {
|
|
311
|
-
mockedDynamoDbClient.safeGet.mockResolvedValueOnce(null);
|
|
312
|
-
mockGetIdentity.mockResolvedValueOnce({
|
|
313
|
-
identityId: 'resolved-identity',
|
|
314
|
-
traits: { emails: ['test@email.com', 'resolved@email.com'] },
|
|
315
|
-
});
|
|
316
|
-
const result = await IdentityGatewayService.resolveIdentityWithCaching(pixelId, incomingIdentity);
|
|
317
|
-
expect(result.identity?.identityId).toBe('resolved-identity');
|
|
318
|
-
expect(result.didWriteCache).toBe(false);
|
|
319
|
-
expect(result.outcome).toBe(IDENTITY_CACHE_WITH_CACHING_OUTCOME.NEPTUNE_RESOLVED);
|
|
320
|
-
expect(mockedDynamoDbClient.safeBatchWrite).not.toHaveBeenCalled();
|
|
321
|
-
});
|
|
322
|
-
it('should enqueue async update on address update path (never writes sync)', async () => {
|
|
323
|
-
IdentityGatewayService.configure({
|
|
324
|
-
awsRegion: 'us-east-1',
|
|
325
|
-
awsAccountId: '123456789',
|
|
326
|
-
asyncUpdateQueueName: 'test-queue',
|
|
327
|
-
});
|
|
328
|
-
mockBuildAndSendMessage.mockResolvedValueOnce({ MessageId: 'test-msg-id' });
|
|
329
|
-
const cachedResponse = {
|
|
330
|
-
pk: `identity#${pixelId}#identity456`,
|
|
331
|
-
pixelId,
|
|
332
|
-
identityId: 'identity456',
|
|
333
|
-
response: {
|
|
334
|
-
identityId: 'identity456',
|
|
335
|
-
traits: {
|
|
336
|
-
emails: ['test@email.com'],
|
|
337
|
-
addresses: [{ city: 'Old City', upa: 1704067200000 }],
|
|
338
|
-
},
|
|
339
|
-
},
|
|
340
|
-
updatedAt: new Date().toISOString(),
|
|
341
|
-
};
|
|
342
|
-
mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
|
|
343
|
-
const identityWithNewAddress = {
|
|
344
|
-
identityId: 'identity456',
|
|
345
|
-
traits: {
|
|
346
|
-
emails: ['test@email.com'],
|
|
347
|
-
addresses: [{ city: 'New City', upa: 1717200000000 }],
|
|
348
|
-
},
|
|
349
|
-
};
|
|
350
|
-
const result = await IdentityGatewayService.resolveIdentityWithCaching(pixelId, identityWithNewAddress);
|
|
351
|
-
if (result.outcome === IDENTITY_CACHE_WITH_CACHING_OUTCOME.ADDRESS_UPDATE_ASYNC) {
|
|
352
|
-
expect(result.didWriteCache).toBe(false);
|
|
353
|
-
expect(result.didEnqueueAsync).toBe(true);
|
|
354
|
-
expect(mockedDynamoDbClient.safeBatchWrite).not.toHaveBeenCalled();
|
|
355
|
-
expect(mockBuildAndSendMessage).toHaveBeenCalled();
|
|
356
|
-
}
|
|
357
|
-
});
|
|
358
|
-
it('should still return resolved identity when skipCacheWrite is true', async () => {
|
|
359
|
-
mockedDynamoDbClient.safeGet.mockResolvedValueOnce(null);
|
|
360
|
-
mockGetIdentity.mockResolvedValueOnce({
|
|
361
|
-
identityId: 'neptune-resolved-id',
|
|
362
|
-
traits: { emails: ['test@email.com', 'new@email.com'] },
|
|
363
|
-
});
|
|
364
|
-
const result = await IdentityGatewayService.resolveIdentityWithCaching(pixelId, incomingIdentity);
|
|
365
|
-
expect(result.identity).toBeDefined();
|
|
366
|
-
expect(result.identity?.identityId).toBe('neptune-resolved-id');
|
|
367
|
-
expect(result.identity?.traits?.emails).toContain('new@email.com');
|
|
368
|
-
});
|
|
369
|
-
it('should return fresh cache hit without writing (skipCacheWrite irrelevant for hits)', async () => {
|
|
370
|
-
const cachedResponse = {
|
|
371
|
-
pk: `identity#${pixelId}#identity456`,
|
|
372
|
-
pixelId,
|
|
373
|
-
identityId: 'identity456',
|
|
374
|
-
response: {
|
|
375
|
-
identityId: 'identity456',
|
|
376
|
-
traits: { emails: ['test@email.com'] },
|
|
377
|
-
},
|
|
378
|
-
updatedAt: new Date().toISOString(),
|
|
379
|
-
};
|
|
380
|
-
mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
|
|
381
|
-
const result = await IdentityGatewayService.resolveIdentityWithCaching(pixelId, incomingIdentity);
|
|
382
|
-
expect(result.outcome).toBe(IDENTITY_CACHE_WITH_CACHING_OUTCOME.FRESH_CACHE_HIT);
|
|
383
|
-
expect(result.didWriteCache).toBe(false);
|
|
384
|
-
expect(mockGetIdentity).not.toHaveBeenCalled();
|
|
385
|
-
expect(mockedDynamoDbClient.safeBatchWrite).not.toHaveBeenCalled();
|
|
386
|
-
});
|
|
387
|
-
});
|
|
388
|
-
});
|
|
389
|
-
describe('edge cases', () => {
|
|
390
|
-
it('should return invalid input when pixelId is undefined', async () => {
|
|
391
|
-
const result = await IdentityGatewayService.resolveIdentityWithCaching(undefined, { identityId: 'test' });
|
|
392
|
-
expect(result.identity).toBeUndefined();
|
|
393
|
-
expect(result.outcome).toBe(IDENTITY_CACHE_WITH_CACHING_OUTCOME.INVALID_INPUT);
|
|
394
|
-
expect(result.didReadCache).toBe(false);
|
|
395
|
-
expect(result.didWriteCache).toBe(false);
|
|
396
|
-
});
|
|
397
|
-
it('should return invalid input when incomingIdentity is null', async () => {
|
|
398
|
-
const result = await IdentityGatewayService.resolveIdentityWithCaching(pixelId, null);
|
|
399
|
-
expect(result.identity).toBeUndefined();
|
|
400
|
-
expect(result.outcome).toBe(IDENTITY_CACHE_WITH_CACHING_OUTCOME.INVALID_INPUT);
|
|
401
|
-
});
|
|
402
|
-
it('should return invalid input when both pixelId and identity are missing', async () => {
|
|
403
|
-
const result = await IdentityGatewayService.resolveIdentityWithCaching('', undefined);
|
|
404
|
-
expect(result.identity).toBeUndefined();
|
|
405
|
-
expect(result.outcome).toBe(IDENTITY_CACHE_WITH_CACHING_OUTCOME.INVALID_INPUT);
|
|
406
|
-
});
|
|
407
|
-
it('should gracefully fallback when cache read fails (fail-open pattern)', async () => {
|
|
408
|
-
mockedDynamoDbClient.safeGet.mockRejectedValueOnce(new Error('Unexpected DynamoDB error'));
|
|
409
|
-
mockGetIdentity.mockResolvedValueOnce({
|
|
410
|
-
identityId: 'neptune-resolved',
|
|
411
|
-
traits: { emails: ['test@email.com'] },
|
|
412
|
-
});
|
|
413
|
-
const result = await IdentityGatewayService.resolveIdentityWithCaching(pixelId, { identityId: 'test-id', traits: { emails: ['test@email.com'] } });
|
|
414
|
-
expect(result.identity).toBeDefined();
|
|
415
|
-
expect(result.identity?.identityId).toBe('neptune-resolved');
|
|
416
|
-
expect(result.didReadCache).toBe(true);
|
|
417
|
-
expect(result.didCallNeptune).toBe(true);
|
|
418
|
-
});
|
|
419
|
-
it('should reject identity with undefined identityId in enqueue', async () => {
|
|
420
|
-
IdentityGatewayService.configure({
|
|
421
|
-
awsRegion: 'us-east-1',
|
|
422
|
-
awsAccountId: '123456789',
|
|
423
|
-
asyncUpdateQueueName: 'test-queue',
|
|
424
|
-
});
|
|
425
|
-
const identity = { traits: { emails: ['test@email.com'] } };
|
|
426
|
-
const context = { identity };
|
|
427
|
-
const result = await IdentityGatewayService.enqueueAsyncIdentityUpdate({
|
|
428
|
-
pixelId,
|
|
429
|
-
identity,
|
|
430
|
-
context,
|
|
431
|
-
reason: 'anonymous user',
|
|
432
|
-
});
|
|
433
|
-
expect(result.success).toBe(false);
|
|
434
|
-
expect(result.error).toBe('Missing required pixelId or identityId');
|
|
435
|
-
expect(mockBuildAndSendMessage).not.toHaveBeenCalled();
|
|
436
|
-
expect(mockedLogger.warn).toHaveBeenCalledWith('IdentityGateway - enqueueAsyncIdentityUpdate: missing required params', expect.objectContaining({
|
|
437
|
-
pixelId,
|
|
438
|
-
identityId: undefined,
|
|
439
|
-
}));
|
|
440
|
-
});
|
|
441
|
-
});
|
|
442
|
-
describe('safeWriteBackToCache behavior with skipCacheWrite', () => {
|
|
443
|
-
it('should not call safeWriteBackToCache when skipCacheWrite is true and Neptune returns new identity', async () => {
|
|
444
|
-
mockedDynamoDbClient.safeGet.mockResolvedValueOnce(null);
|
|
445
|
-
mockGetIdentity.mockResolvedValueOnce({
|
|
446
|
-
identityId: 'new-resolved-identity',
|
|
447
|
-
traits: { emails: ['test@email.com'] },
|
|
448
|
-
});
|
|
449
|
-
await IdentityGatewayService.resolveIdentityWithCaching(pixelId, { identityId: 'original-id', traits: { emails: ['test@email.com'] } });
|
|
450
|
-
expect(mockedDynamoDbClient.safeBatchWrite).not.toHaveBeenCalled();
|
|
451
|
-
});
|
|
452
|
-
});
|
|
453
|
-
describe('result fields consistency', () => {
|
|
454
|
-
it('should include all required fields in resolveIdentityWithCaching result', async () => {
|
|
455
|
-
mockedDynamoDbClient.safeGet.mockResolvedValueOnce(null);
|
|
456
|
-
mockGetIdentity.mockResolvedValueOnce({
|
|
457
|
-
identityId: 'resolved-id',
|
|
458
|
-
traits: { emails: ['test@email.com'] },
|
|
459
|
-
});
|
|
460
|
-
const result = await IdentityGatewayService.resolveIdentityWithCaching(pixelId, { identityId: 'incoming-id', traits: { emails: ['test@email.com'] } });
|
|
461
|
-
expect(result).toEqual(expect.objectContaining({
|
|
462
|
-
outcome: expect.any(String),
|
|
463
|
-
pixelId,
|
|
464
|
-
identityId: 'resolved-id',
|
|
465
|
-
didReadCache: true,
|
|
466
|
-
didCallNeptune: true,
|
|
467
|
-
didWriteCache: false,
|
|
468
|
-
didSkipNeptune: false,
|
|
469
|
-
didFallbackToIncoming: false,
|
|
470
|
-
didEnqueueAsync: false,
|
|
471
|
-
}));
|
|
472
|
-
});
|
|
473
|
-
it('should include traitChanges in summary when identity changes', async () => {
|
|
474
|
-
const cachedResponse = {
|
|
475
|
-
pk: `identity#${pixelId}#identity456`,
|
|
476
|
-
pixelId,
|
|
477
|
-
identityId: 'identity456',
|
|
478
|
-
response: {
|
|
479
|
-
identityId: 'identity456',
|
|
480
|
-
traits: { emails: ['old@email.com'] },
|
|
481
|
-
},
|
|
482
|
-
updatedAt: new Date().toISOString(),
|
|
483
|
-
};
|
|
484
|
-
mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
|
|
485
|
-
mockGetIdentity.mockResolvedValueOnce({
|
|
486
|
-
identityId: 'identity456',
|
|
487
|
-
traits: { emails: ['old@email.com', 'new@email.com'] },
|
|
488
|
-
});
|
|
489
|
-
const result = await IdentityGatewayService.resolveIdentityWithCaching(pixelId, { identityId: 'identity456', traits: { emails: ['old@email.com', 'new@email.com'] } });
|
|
490
|
-
if (result.outcome === IDENTITY_CACHE_WITH_CACHING_OUTCOME.NEPTUNE_RESOLVED) {
|
|
491
|
-
expect(result.traitChanges).toBeDefined();
|
|
492
|
-
}
|
|
493
|
-
});
|
|
494
|
-
});
|
|
495
|
-
describe('refactored flow: merge and staleness computation', () => {
|
|
496
|
-
describe('mergeAndComputeIdentityStaleness behavior', () => {
|
|
497
|
-
it('should merge incoming traits with cached traits on cache hit', async () => {
|
|
498
|
-
const cachedResponse = {
|
|
499
|
-
pk: `identity#${pixelId}#merge-test`,
|
|
500
|
-
pixelId,
|
|
501
|
-
identityId: 'merge-test',
|
|
502
|
-
response: {
|
|
503
|
-
identityId: 'merge-test',
|
|
504
|
-
traits: {
|
|
505
|
-
emails: ['cached@email.com'],
|
|
506
|
-
phones: ['555-cached'],
|
|
507
|
-
},
|
|
508
|
-
},
|
|
509
|
-
updatedAt: new Date().toISOString(),
|
|
510
|
-
};
|
|
511
|
-
mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
|
|
512
|
-
const incomingIdentity = {
|
|
513
|
-
identityId: 'merge-test',
|
|
514
|
-
traits: {
|
|
515
|
-
emails: ['cached@email.com'],
|
|
516
|
-
},
|
|
517
|
-
};
|
|
518
|
-
const result = await IdentityGatewayService.resolveIdentityWithCaching(pixelId, incomingIdentity);
|
|
519
|
-
expect(result.identity?.traits?.emails).toContain('cached@email.com');
|
|
520
|
-
expect(result.identity?.traits?.phones).toContain('555-cached');
|
|
521
|
-
});
|
|
522
|
-
it('should compute staleness after cache hit', async () => {
|
|
523
|
-
const cachedResponse = {
|
|
524
|
-
pk: `identity#${pixelId}#staleness-compute`,
|
|
525
|
-
pixelId,
|
|
526
|
-
identityId: 'staleness-compute',
|
|
527
|
-
response: {
|
|
528
|
-
identityId: 'staleness-compute',
|
|
529
|
-
traits: {
|
|
530
|
-
emails: ['old@email.com'],
|
|
531
|
-
},
|
|
532
|
-
},
|
|
533
|
-
updatedAt: new Date().toISOString(),
|
|
534
|
-
};
|
|
535
|
-
mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
|
|
536
|
-
mockGetIdentity.mockResolvedValueOnce({
|
|
537
|
-
identityId: 'staleness-compute',
|
|
538
|
-
traits: { emails: ['old@email.com', 'new@email.com'] },
|
|
539
|
-
});
|
|
540
|
-
const incomingIdentity = {
|
|
541
|
-
identityId: 'staleness-compute',
|
|
542
|
-
traits: {
|
|
543
|
-
emails: ['old@email.com', 'new@email.com'],
|
|
544
|
-
},
|
|
545
|
-
};
|
|
546
|
-
const result = await IdentityGatewayService.resolveIdentityWithCaching(pixelId, incomingIdentity);
|
|
547
|
-
expect(result.didCallNeptune).toBe(true);
|
|
548
|
-
expect(result.stalenessResult?.staleTraits).toContain('emails');
|
|
549
|
-
});
|
|
550
|
-
});
|
|
551
|
-
describe('deep equality check', () => {
|
|
552
|
-
it('should treat as cache hit when merged identity is deep equal to cached', async () => {
|
|
553
|
-
const cachedResponse = {
|
|
554
|
-
pk: `identity#${pixelId}#deep-equal`,
|
|
555
|
-
pixelId,
|
|
556
|
-
identityId: 'deep-equal',
|
|
557
|
-
response: {
|
|
558
|
-
identityId: 'deep-equal',
|
|
559
|
-
traits: {
|
|
560
|
-
emails: ['same@email.com'],
|
|
561
|
-
phones: ['555-1234'],
|
|
562
|
-
},
|
|
563
|
-
},
|
|
564
|
-
updatedAt: new Date().toISOString(),
|
|
565
|
-
};
|
|
566
|
-
mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
|
|
567
|
-
const incomingIdentity = {
|
|
568
|
-
identityId: 'deep-equal',
|
|
569
|
-
traits: {
|
|
570
|
-
emails: ['same@email.com'],
|
|
571
|
-
phones: ['555-1234'],
|
|
572
|
-
},
|
|
573
|
-
};
|
|
574
|
-
const result = await IdentityGatewayService.resolveIdentityWithCaching(pixelId, incomingIdentity);
|
|
575
|
-
expect(result.outcome).toBe(IDENTITY_CACHE_WITH_CACHING_OUTCOME.FRESH_CACHE_HIT);
|
|
576
|
-
expect(result.didCallNeptune).toBe(false);
|
|
577
|
-
expect(mockGetIdentity).not.toHaveBeenCalled();
|
|
578
|
-
});
|
|
579
|
-
it('should strip address timestamps before deep equality comparison', async () => {
|
|
580
|
-
const now = Date.now();
|
|
581
|
-
const cachedResponse = {
|
|
582
|
-
pk: `identity#${pixelId}#timestamp-strip`,
|
|
583
|
-
pixelId,
|
|
584
|
-
identityId: 'timestamp-strip',
|
|
585
|
-
response: {
|
|
586
|
-
identityId: 'timestamp-strip',
|
|
587
|
-
traits: {
|
|
588
|
-
emails: ['test@email.com'],
|
|
589
|
-
addresses: [{ city: 'NYC', cra: now - 10000, upa: now - 5000 }],
|
|
590
|
-
},
|
|
591
|
-
},
|
|
592
|
-
updatedAt: new Date().toISOString(),
|
|
593
|
-
};
|
|
594
|
-
mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
|
|
595
|
-
const incomingIdentity = {
|
|
596
|
-
identityId: 'timestamp-strip',
|
|
597
|
-
traits: {
|
|
598
|
-
emails: ['test@email.com'],
|
|
599
|
-
addresses: [{ city: 'NYC', cra: now, upa: now }],
|
|
600
|
-
},
|
|
601
|
-
};
|
|
602
|
-
const result = await IdentityGatewayService.resolveIdentityWithCaching(pixelId, incomingIdentity);
|
|
603
|
-
expect(result.outcome).toBe(IDENTITY_CACHE_WITH_CACHING_OUTCOME.FRESH_CACHE_HIT);
|
|
604
|
-
expect(result.didCallNeptune).toBe(false);
|
|
605
|
-
});
|
|
606
|
-
});
|
|
607
|
-
describe('volatile-only override', () => {
|
|
608
|
-
it('should treat VOLATILE_ONLY + not deep equal as cache hit', async () => {
|
|
609
|
-
const cachedResponse = {
|
|
610
|
-
pk: `identity#${pixelId}#volatile-override`,
|
|
611
|
-
pixelId,
|
|
612
|
-
identityId: 'volatile-override',
|
|
613
|
-
response: {
|
|
614
|
-
identityId: 'volatile-override',
|
|
615
|
-
traits: {
|
|
616
|
-
emails: ['same@email.com'],
|
|
617
|
-
},
|
|
618
|
-
},
|
|
619
|
-
updatedAt: new Date().toISOString(),
|
|
620
|
-
};
|
|
621
|
-
mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
|
|
622
|
-
const incomingIdentity = {
|
|
623
|
-
identityId: 'volatile-override',
|
|
624
|
-
traits: {
|
|
625
|
-
emails: ['same@email.com'],
|
|
626
|
-
ipAddress: '10.0.0.1',
|
|
627
|
-
userAgent: 'Mozilla/5.0 (New)',
|
|
628
|
-
},
|
|
629
|
-
};
|
|
630
|
-
const result = await IdentityGatewayService.resolveIdentityWithCaching(pixelId, incomingIdentity);
|
|
631
|
-
expect(result.outcome).toBe(IDENTITY_CACHE_WITH_CACHING_OUTCOME.VOLATILE_ONLY_CACHE_HIT);
|
|
632
|
-
expect(result.didCallNeptune).toBe(false);
|
|
633
|
-
expect(result.didWriteCache).toBe(false);
|
|
634
|
-
});
|
|
635
|
-
it('should return merged identity for VOLATILE_ONLY tier', async () => {
|
|
636
|
-
const cachedResponse = {
|
|
637
|
-
pk: `identity#${pixelId}#volatile-merged`,
|
|
638
|
-
pixelId,
|
|
639
|
-
identityId: 'volatile-merged',
|
|
640
|
-
response: {
|
|
641
|
-
identityId: 'volatile-merged',
|
|
642
|
-
traits: {
|
|
643
|
-
emails: ['test@email.com'],
|
|
644
|
-
phones: ['555-cached'],
|
|
645
|
-
},
|
|
646
|
-
},
|
|
647
|
-
updatedAt: new Date().toISOString(),
|
|
648
|
-
};
|
|
649
|
-
mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
|
|
650
|
-
const incomingIdentity = {
|
|
651
|
-
identityId: 'volatile-merged',
|
|
652
|
-
traits: {
|
|
653
|
-
emails: ['test@email.com'],
|
|
654
|
-
ipAddress: '10.0.0.1',
|
|
655
|
-
},
|
|
656
|
-
};
|
|
657
|
-
const result = await IdentityGatewayService.resolveIdentityWithCaching(pixelId, incomingIdentity);
|
|
658
|
-
expect(result.outcome).toBe(IDENTITY_CACHE_WITH_CACHING_OUTCOME.VOLATILE_ONLY_CACHE_HIT);
|
|
659
|
-
expect(result.identity?.traits?.emails).toContain('test@email.com');
|
|
660
|
-
expect(result.identity?.traits?.phones).toContain('555-cached');
|
|
661
|
-
expect(result.identity?.traits?.ipAddress).toBe('10.0.0.1');
|
|
662
|
-
});
|
|
663
|
-
});
|
|
664
|
-
describe('skipNeptune derivation from staleTier', () => {
|
|
665
|
-
it('should set skipNeptune=false for IDENTITY_STALE tier', async () => {
|
|
666
|
-
const cachedResponse = {
|
|
667
|
-
pk: `identity#${pixelId}#call-neptune`,
|
|
668
|
-
pixelId,
|
|
669
|
-
identityId: 'call-neptune',
|
|
670
|
-
response: {
|
|
671
|
-
identityId: 'call-neptune',
|
|
672
|
-
traits: {
|
|
673
|
-
emails: ['old@email.com'],
|
|
674
|
-
},
|
|
675
|
-
},
|
|
676
|
-
updatedAt: new Date().toISOString(),
|
|
677
|
-
};
|
|
678
|
-
mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
|
|
679
|
-
mockGetIdentity.mockResolvedValueOnce({
|
|
680
|
-
identityId: 'call-neptune',
|
|
681
|
-
traits: { emails: ['old@email.com', 'new@email.com'] },
|
|
682
|
-
});
|
|
683
|
-
const incomingIdentity = {
|
|
684
|
-
identityId: 'call-neptune',
|
|
685
|
-
traits: {
|
|
686
|
-
emails: ['old@email.com', 'new@email.com'],
|
|
687
|
-
},
|
|
688
|
-
};
|
|
689
|
-
const result = await IdentityGatewayService.resolveIdentityWithCaching(pixelId, incomingIdentity);
|
|
690
|
-
expect(result.didSkipNeptune).toBe(false);
|
|
691
|
-
expect(result.didCallNeptune).toBe(true);
|
|
692
|
-
expect(mockGetIdentity).toHaveBeenCalled();
|
|
693
|
-
});
|
|
694
|
-
});
|
|
695
|
-
describe('writeReason derivation from staleTier', () => {
|
|
696
|
-
it('should set writeReason=stale_traits for IDENTITY_STALE tier', async () => {
|
|
697
|
-
const cachedResponse = {
|
|
698
|
-
pk: `identity#${pixelId}#stale-reason`,
|
|
699
|
-
pixelId,
|
|
700
|
-
identityId: 'stale-reason',
|
|
701
|
-
response: {
|
|
702
|
-
identityId: 'stale-reason',
|
|
703
|
-
traits: {
|
|
704
|
-
emails: ['old@email.com'],
|
|
705
|
-
},
|
|
706
|
-
},
|
|
707
|
-
updatedAt: new Date().toISOString(),
|
|
708
|
-
};
|
|
709
|
-
mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
|
|
710
|
-
mockGetIdentity.mockResolvedValueOnce({
|
|
711
|
-
identityId: 'stale-reason',
|
|
712
|
-
traits: { emails: ['old@email.com', 'new@email.com'] },
|
|
713
|
-
});
|
|
714
|
-
const incomingIdentity = {
|
|
715
|
-
identityId: 'stale-reason',
|
|
716
|
-
traits: {
|
|
717
|
-
emails: ['old@email.com', 'new@email.com'],
|
|
718
|
-
},
|
|
719
|
-
};
|
|
720
|
-
const result = await IdentityGatewayService.resolveIdentityWithCaching(pixelId, incomingIdentity);
|
|
721
|
-
expect(result.writeReason).toBeUndefined();
|
|
722
|
-
expect(result.outcome).toBe(IDENTITY_CACHE_WITH_CACHING_OUTCOME.NEPTUNE_RESOLVED);
|
|
723
|
-
});
|
|
724
|
-
});
|
|
725
|
-
describe('IDENTITY_STALE → Neptune path', () => {
|
|
726
|
-
it('should call Neptune for identity-defining trait changes', async () => {
|
|
727
|
-
const cachedResponse = {
|
|
728
|
-
pk: `identity#${pixelId}#neptune-path`,
|
|
729
|
-
pixelId,
|
|
730
|
-
identityId: 'neptune-path',
|
|
731
|
-
response: {
|
|
732
|
-
identityId: 'neptune-path',
|
|
733
|
-
traits: {
|
|
734
|
-
emails: ['old@email.com'],
|
|
735
|
-
},
|
|
736
|
-
},
|
|
737
|
-
updatedAt: new Date().toISOString(),
|
|
738
|
-
};
|
|
739
|
-
mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
|
|
740
|
-
mockGetIdentity.mockResolvedValueOnce({
|
|
741
|
-
identityId: 'neptune-path',
|
|
742
|
-
traits: { emails: ['old@email.com', 'new@email.com'] },
|
|
743
|
-
});
|
|
744
|
-
const incomingIdentity = {
|
|
745
|
-
identityId: 'neptune-path',
|
|
746
|
-
traits: {
|
|
747
|
-
emails: ['old@email.com', 'new@email.com'],
|
|
748
|
-
},
|
|
749
|
-
};
|
|
750
|
-
const result = await IdentityGatewayService.resolveIdentityWithCaching(pixelId, incomingIdentity);
|
|
751
|
-
expect(result.didCallNeptune).toBe(true);
|
|
752
|
-
expect(mockGetIdentity).toHaveBeenCalledWith(pixelId, expect.objectContaining({
|
|
753
|
-
identity: expect.objectContaining({
|
|
754
|
-
identityId: expect.any(String),
|
|
755
|
-
}),
|
|
756
|
-
}));
|
|
757
|
-
});
|
|
758
|
-
it('should write Neptune result to cache when different from cached', async () => {
|
|
759
|
-
const cachedResponse = {
|
|
760
|
-
pk: `identity#${pixelId}#neptune-write`,
|
|
761
|
-
pixelId,
|
|
762
|
-
identityId: 'neptune-write',
|
|
763
|
-
response: {
|
|
764
|
-
identityId: 'neptune-write',
|
|
765
|
-
traits: {
|
|
766
|
-
emails: ['old@email.com'],
|
|
767
|
-
},
|
|
768
|
-
},
|
|
769
|
-
updatedAt: new Date().toISOString(),
|
|
770
|
-
};
|
|
771
|
-
mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
|
|
772
|
-
mockGetIdentity.mockResolvedValueOnce({
|
|
773
|
-
identityId: 'neptune-write',
|
|
774
|
-
traits: {
|
|
775
|
-
emails: ['old@email.com', 'new@email.com'],
|
|
776
|
-
phones: ['555-neptune'],
|
|
777
|
-
},
|
|
778
|
-
});
|
|
779
|
-
const incomingIdentity = {
|
|
780
|
-
identityId: 'neptune-write',
|
|
781
|
-
traits: {
|
|
782
|
-
emails: ['old@email.com', 'new@email.com'],
|
|
783
|
-
},
|
|
784
|
-
};
|
|
785
|
-
const result = await IdentityGatewayService.resolveIdentityWithCaching(pixelId, incomingIdentity);
|
|
786
|
-
expect(result.didWriteCache).toBe(false);
|
|
787
|
-
expect(result.outcome).toBe(IDENTITY_CACHE_WITH_CACHING_OUTCOME.NEPTUNE_RESOLVED);
|
|
788
|
-
expect(mockedDynamoDbClient.safeBatchWrite).not.toHaveBeenCalled();
|
|
789
|
-
});
|
|
790
|
-
it('should skip cache write when Neptune result matches cached', async () => {
|
|
791
|
-
const cachedResponse = {
|
|
792
|
-
pk: `identity#${pixelId}#neptune-skip-write`,
|
|
793
|
-
pixelId,
|
|
794
|
-
identityId: 'neptune-skip-write',
|
|
795
|
-
response: {
|
|
796
|
-
identityId: 'neptune-skip-write',
|
|
797
|
-
traits: {
|
|
798
|
-
emails: ['existing@email.com'],
|
|
799
|
-
},
|
|
800
|
-
},
|
|
801
|
-
updatedAt: new Date().toISOString(),
|
|
802
|
-
};
|
|
803
|
-
mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
|
|
804
|
-
mockGetIdentity.mockResolvedValueOnce({
|
|
805
|
-
identityId: 'neptune-skip-write',
|
|
806
|
-
traits: {
|
|
807
|
-
emails: ['existing@email.com'],
|
|
808
|
-
},
|
|
809
|
-
});
|
|
810
|
-
const incomingIdentity = {
|
|
811
|
-
identityId: 'neptune-skip-write',
|
|
812
|
-
traits: {
|
|
813
|
-
emails: ['existing@email.com', 'thought-new@email.com'],
|
|
814
|
-
},
|
|
815
|
-
};
|
|
816
|
-
const result = await IdentityGatewayService.resolveIdentityWithCaching(pixelId, incomingIdentity);
|
|
817
|
-
expect(result.didCallNeptune).toBe(true);
|
|
818
|
-
expect(result.didWriteCache).toBe(false);
|
|
819
|
-
expect(result.outcome).toBe(IDENTITY_CACHE_WITH_CACHING_OUTCOME.NEPTUNE_RESOLVED);
|
|
820
|
-
});
|
|
821
|
-
it('should return NEPTUNE_RESOLUTION_FAILED when Neptune throws', async () => {
|
|
822
|
-
const cachedResponse = {
|
|
823
|
-
pk: `identity#${pixelId}#neptune-throws`,
|
|
824
|
-
pixelId,
|
|
825
|
-
identityId: 'neptune-throws',
|
|
826
|
-
response: {
|
|
827
|
-
identityId: 'neptune-throws',
|
|
828
|
-
traits: {
|
|
829
|
-
emails: ['old@email.com'],
|
|
830
|
-
},
|
|
831
|
-
},
|
|
832
|
-
updatedAt: new Date().toISOString(),
|
|
833
|
-
};
|
|
834
|
-
mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
|
|
835
|
-
mockGetIdentity.mockRejectedValueOnce(new Error('Neptune invocation failed'));
|
|
836
|
-
const incomingIdentity = {
|
|
837
|
-
identityId: 'neptune-throws',
|
|
838
|
-
traits: {
|
|
839
|
-
emails: ['old@email.com', 'new@email.com'],
|
|
840
|
-
},
|
|
841
|
-
};
|
|
842
|
-
const result = await IdentityGatewayService.resolveIdentityWithCaching(pixelId, incomingIdentity);
|
|
843
|
-
expect(result.didCallNeptune).toBe(true);
|
|
844
|
-
expect(result.didWriteCache).toBe(false);
|
|
845
|
-
expect(result.outcome).toBe(IDENTITY_CACHE_WITH_CACHING_OUTCOME.NEPTUNE_RESOLUTION_FAILED);
|
|
846
|
-
expect(result.identity).toBeDefined();
|
|
847
|
-
expect(result.identity?.identityId).toBe('neptune-throws');
|
|
848
|
-
expect(result.identity?.traits?.emails).toEqual(['old@email.com', 'new@email.com']);
|
|
849
|
-
});
|
|
850
|
-
});
|
|
851
|
-
});
|
|
852
|
-
describe('resolveCacheMissViaNeptune helper', () => {
|
|
853
|
-
describe('cache miss without discovered identity ID', () => {
|
|
854
|
-
it('should call Neptune with incoming identity when no discovered ID', async () => {
|
|
855
|
-
mockedDynamoDbClient.safeGet.mockResolvedValueOnce(null);
|
|
856
|
-
mockGetIdentity.mockResolvedValueOnce({
|
|
857
|
-
identityId: 'neptune-resolved-id',
|
|
858
|
-
traits: { emails: ['test@email.com'] },
|
|
859
|
-
});
|
|
860
|
-
const incomingIdentity = {
|
|
861
|
-
identityId: 'incoming-id',
|
|
862
|
-
traits: { emails: ['test@email.com'] },
|
|
863
|
-
};
|
|
864
|
-
const result = await IdentityGatewayService.resolveIdentityWithCaching(pixelId, incomingIdentity);
|
|
865
|
-
expect(mockGetIdentity).toHaveBeenCalledWith(pixelId, expect.objectContaining({
|
|
866
|
-
identity: expect.objectContaining({
|
|
867
|
-
identityId: 'incoming-id',
|
|
868
|
-
}),
|
|
869
|
-
}));
|
|
870
|
-
expect(result.outcome).toBe(IDENTITY_CACHE_WITH_CACHING_OUTCOME.NEPTUNE_RESOLVED);
|
|
871
|
-
expect(result.writeReason).toBeUndefined();
|
|
872
|
-
});
|
|
873
|
-
it('should set state.didReadCache=true and state.didCallNeptune=true', async () => {
|
|
874
|
-
mockedDynamoDbClient.safeGet.mockResolvedValueOnce(null);
|
|
875
|
-
mockGetIdentity.mockResolvedValueOnce({ identityId: 'resolved', traits: {} });
|
|
876
|
-
const result = await IdentityGatewayService.resolveIdentityWithCaching(pixelId, { identityId: 'test', traits: {} });
|
|
877
|
-
expect(result.didReadCache).toBe(true);
|
|
878
|
-
expect(result.didCallNeptune).toBe(true);
|
|
879
|
-
});
|
|
880
|
-
});
|
|
881
|
-
describe('email pointer discovery with discovered identity ID', () => {
|
|
882
|
-
it('should call Neptune with discovered identity ID when email lookup succeeds', async () => {
|
|
883
|
-
const incomingNoId = {
|
|
884
|
-
traits: { emails: ['discovered@email.com'] },
|
|
885
|
-
};
|
|
886
|
-
mockedDynamoDbClient.safeGet.mockResolvedValueOnce({
|
|
887
|
-
pk: `email#${pixelId}#discovered@email.com`,
|
|
888
|
-
identityId: 'discovered-identity-id',
|
|
889
|
-
});
|
|
890
|
-
mockGetIdentity.mockResolvedValueOnce({
|
|
891
|
-
identityId: 'discovered-identity-id',
|
|
892
|
-
traits: { emails: ['discovered@email.com', 'other@email.com'] },
|
|
893
|
-
});
|
|
894
|
-
const result = await IdentityGatewayService.resolveIdentityWithCaching(pixelId, incomingNoId);
|
|
895
|
-
expect(result.identity?.identityId).toBe('discovered-identity-id');
|
|
896
|
-
});
|
|
897
|
-
it('should use incoming identity in Neptune call (not discovered ID)', async () => {
|
|
898
|
-
mockedDynamoDbClient.safeGet.mockResolvedValueOnce({
|
|
899
|
-
pk: `email#${pixelId}#test@email.com`,
|
|
900
|
-
identityId: 'discovered-from-email',
|
|
901
|
-
});
|
|
902
|
-
mockGetIdentity.mockResolvedValueOnce({
|
|
903
|
-
identityId: 'neptune-resolved',
|
|
904
|
-
traits: { emails: ['test@email.com'] },
|
|
905
|
-
});
|
|
906
|
-
await IdentityGatewayService.resolveIdentityWithCaching(pixelId, { traits: { emails: ['test@email.com'] } });
|
|
907
|
-
expect(mockGetIdentity).toHaveBeenCalledWith(pixelId, expect.objectContaining({
|
|
908
|
-
identity: expect.objectContaining({
|
|
909
|
-
traits: expect.objectContaining({ emails: ['test@email.com'] }),
|
|
910
|
-
}),
|
|
911
|
-
}));
|
|
912
|
-
});
|
|
913
|
-
});
|
|
914
|
-
describe('Neptune resolution success → write-back', () => {
|
|
915
|
-
it('should write to cache when Neptune returns identity', async () => {
|
|
916
|
-
mockedDynamoDbClient.safeGet.mockResolvedValueOnce(null);
|
|
917
|
-
mockGetIdentity.mockResolvedValueOnce({
|
|
918
|
-
identityId: 'neptune-success',
|
|
919
|
-
traits: { emails: ['test@email.com'], phones: ['555-1234'] },
|
|
920
|
-
});
|
|
921
|
-
const result = await IdentityGatewayService.resolveIdentityWithCaching(pixelId, { identityId: 'incoming', traits: { emails: ['test@email.com'] } });
|
|
922
|
-
expect(result.didWriteCache).toBe(false);
|
|
923
|
-
expect(result.outcome).toBe(IDENTITY_CACHE_WITH_CACHING_OUTCOME.NEPTUNE_RESOLVED);
|
|
924
|
-
expect(mockedDynamoDbClient.safeBatchWrite).not.toHaveBeenCalled();
|
|
925
|
-
});
|
|
926
|
-
it('should set resolvedIdentity to Neptune response', async () => {
|
|
927
|
-
mockedDynamoDbClient.safeGet.mockResolvedValueOnce(null);
|
|
928
|
-
mockGetIdentity.mockResolvedValueOnce({
|
|
929
|
-
identityId: 'from-neptune',
|
|
930
|
-
traits: { emails: ['neptune@email.com'] },
|
|
931
|
-
});
|
|
932
|
-
const result = await IdentityGatewayService.resolveIdentityWithCaching(pixelId, { identityId: 'original', traits: { emails: ['original@email.com'] } });
|
|
933
|
-
expect(result.identity?.identityId).toBe('from-neptune');
|
|
934
|
-
expect(result.identity?.traits?.emails).toContain('neptune@email.com');
|
|
935
|
-
});
|
|
936
|
-
it('should include traitChanges with "no cached identity" reason', async () => {
|
|
937
|
-
mockedDynamoDbClient.safeGet.mockResolvedValueOnce(null);
|
|
938
|
-
mockGetIdentity.mockResolvedValueOnce({
|
|
939
|
-
identityId: 'new-identity',
|
|
940
|
-
traits: { emails: ['new@email.com'] },
|
|
941
|
-
});
|
|
942
|
-
const result = await IdentityGatewayService.resolveIdentityWithCaching(pixelId, { identityId: 'incoming', traits: { emails: ['incoming@email.com'] } });
|
|
943
|
-
expect(result.traitChanges).toEqual({
|
|
944
|
-
reason: { resolved: 'no cached identity' },
|
|
945
|
-
});
|
|
946
|
-
});
|
|
947
|
-
});
|
|
948
|
-
describe('Neptune resolution failure → fallback to incoming', () => {
|
|
949
|
-
it('should fallback to incoming identity when Neptune fails', async () => {
|
|
950
|
-
mockedDynamoDbClient.safeGet.mockResolvedValueOnce(null);
|
|
951
|
-
mockGetIdentity.mockRejectedValueOnce(new Error('Neptune unavailable'));
|
|
952
|
-
const incomingIdentity = {
|
|
953
|
-
identityId: 'fallback-incoming',
|
|
954
|
-
traits: { emails: ['fallback@email.com'] },
|
|
955
|
-
};
|
|
956
|
-
const result = await IdentityGatewayService.resolveIdentityWithCaching(pixelId, incomingIdentity);
|
|
957
|
-
expect(result.outcome).toBe(IDENTITY_CACHE_WITH_CACHING_OUTCOME.NEPTUNE_RESOLUTION_FAILED);
|
|
958
|
-
expect(result.didFallbackToIncoming).toBe(true);
|
|
959
|
-
expect(result.identity?.identityId).toBe('fallback-incoming');
|
|
960
|
-
});
|
|
961
|
-
it('should set didWriteCache=false when Neptune fails', async () => {
|
|
962
|
-
mockedDynamoDbClient.safeGet.mockResolvedValueOnce(null);
|
|
963
|
-
mockGetIdentity.mockRejectedValueOnce(new Error('Lambda timeout'));
|
|
964
|
-
const result = await IdentityGatewayService.resolveIdentityWithCaching(pixelId, { identityId: 'test', traits: {} });
|
|
965
|
-
expect(result.didWriteCache).toBe(false);
|
|
966
|
-
expect(mockedDynamoDbClient.safeBatchWrite).not.toHaveBeenCalled();
|
|
967
|
-
});
|
|
968
|
-
it('should use identityForNeptune (with discovered ID) as fallback when available', async () => {
|
|
969
|
-
mockedDynamoDbClient.safeGet.mockResolvedValueOnce({
|
|
970
|
-
pk: `email#${pixelId}#test@email.com`,
|
|
971
|
-
identityId: 'discovered-id',
|
|
972
|
-
});
|
|
973
|
-
mockGetIdentity.mockRejectedValueOnce(new Error('Neptune error'));
|
|
974
|
-
const result = await IdentityGatewayService.resolveIdentityWithCaching(pixelId, { traits: { emails: ['test@email.com'] } });
|
|
975
|
-
expect(result.didFallbackToIncoming).toBe(true);
|
|
976
|
-
expect(result.identity?.identityId).toBe('discovered-id');
|
|
977
|
-
expect(result.identity?.traits?.emails).toContain('test@email.com');
|
|
978
|
-
});
|
|
979
|
-
});
|
|
980
|
-
describe('skipCacheWrite=true behavior', () => {
|
|
981
|
-
it('should NOT write to cache when skipCacheWrite=true', async () => {
|
|
982
|
-
mockedDynamoDbClient.safeGet.mockResolvedValueOnce(null);
|
|
983
|
-
mockGetIdentity.mockResolvedValueOnce({
|
|
984
|
-
identityId: 'skip-write-identity',
|
|
985
|
-
traits: { emails: ['test@email.com'] },
|
|
986
|
-
});
|
|
987
|
-
const result = await IdentityGatewayService.resolveIdentityWithCaching(pixelId, { identityId: 'incoming', traits: { emails: ['test@email.com'] } });
|
|
988
|
-
expect(result.didWriteCache).toBe(false);
|
|
989
|
-
expect(mockedDynamoDbClient.safeBatchWrite).not.toHaveBeenCalled();
|
|
990
|
-
expect(result.identity?.identityId).toBe('skip-write-identity');
|
|
991
|
-
expect(result.outcome).toBe(IDENTITY_CACHE_WITH_CACHING_OUTCOME.NEPTUNE_RESOLVED);
|
|
992
|
-
});
|
|
993
|
-
it('should still call Neptune when skipCacheWrite=true', async () => {
|
|
994
|
-
mockedDynamoDbClient.safeGet.mockResolvedValueOnce(null);
|
|
995
|
-
mockGetIdentity.mockResolvedValueOnce({ identityId: 'resolved', traits: {} });
|
|
996
|
-
await IdentityGatewayService.resolveIdentityWithCaching(pixelId, { identityId: 'test', traits: {} });
|
|
997
|
-
expect(mockGetIdentity).toHaveBeenCalled();
|
|
998
|
-
});
|
|
999
|
-
});
|
|
1000
|
-
});
|
|
1001
|
-
describe('resolveStaleViaNeptune helper', () => {
|
|
1002
|
-
describe('Neptune resolution success with changes → write-back', () => {
|
|
1003
|
-
it('should write to cache when Neptune returns different identity', async () => {
|
|
1004
|
-
const cachedResponse = {
|
|
1005
|
-
pk: `identity#${pixelId}#stale-changes`,
|
|
1006
|
-
pixelId,
|
|
1007
|
-
identityId: 'stale-changes',
|
|
1008
|
-
response: {
|
|
1009
|
-
identityId: 'stale-changes',
|
|
1010
|
-
traits: { emails: ['old@email.com'] },
|
|
1011
|
-
},
|
|
1012
|
-
updatedAt: new Date().toISOString(),
|
|
1013
|
-
};
|
|
1014
|
-
mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
|
|
1015
|
-
mockGetIdentity.mockResolvedValueOnce({
|
|
1016
|
-
identityId: 'stale-changes',
|
|
1017
|
-
traits: { emails: ['old@email.com', 'new@email.com'], phones: ['555-new'] },
|
|
1018
|
-
});
|
|
1019
|
-
const result = await IdentityGatewayService.resolveIdentityWithCaching(pixelId, { identityId: 'stale-changes', traits: { emails: ['old@email.com', 'new@email.com'] } });
|
|
1020
|
-
expect(result.didWriteCache).toBe(false);
|
|
1021
|
-
expect(result.outcome).toBe(IDENTITY_CACHE_WITH_CACHING_OUTCOME.NEPTUNE_RESOLVED);
|
|
1022
|
-
expect(mockedDynamoDbClient.safeBatchWrite).not.toHaveBeenCalled();
|
|
1023
|
-
});
|
|
1024
|
-
it('should compute traitChanges between cached and Neptune identity', async () => {
|
|
1025
|
-
const cachedResponse = {
|
|
1026
|
-
pk: `identity#${pixelId}#trait-changes`,
|
|
1027
|
-
pixelId,
|
|
1028
|
-
identityId: 'trait-changes',
|
|
1029
|
-
response: {
|
|
1030
|
-
identityId: 'trait-changes',
|
|
1031
|
-
traits: {
|
|
1032
|
-
emails: ['old@email.com'],
|
|
1033
|
-
phones: ['555-old'],
|
|
1034
|
-
},
|
|
1035
|
-
},
|
|
1036
|
-
updatedAt: new Date().toISOString(),
|
|
1037
|
-
};
|
|
1038
|
-
mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
|
|
1039
|
-
mockGetIdentity.mockResolvedValueOnce({
|
|
1040
|
-
identityId: 'trait-changes',
|
|
1041
|
-
traits: {
|
|
1042
|
-
emails: ['old@email.com', 'new@email.com'],
|
|
1043
|
-
phones: ['555-old', '555-new'],
|
|
1044
|
-
},
|
|
1045
|
-
});
|
|
1046
|
-
const result = await IdentityGatewayService.resolveIdentityWithCaching(pixelId, { identityId: 'trait-changes', traits: { emails: ['old@email.com', 'new@email.com'] } });
|
|
1047
|
-
expect(result.traitChanges).toBeDefined();
|
|
1048
|
-
expect(result.traitChanges?.emails).toBeDefined();
|
|
1049
|
-
});
|
|
1050
|
-
});
|
|
1051
|
-
describe('Neptune resolution success without changes → skip write', () => {
|
|
1052
|
-
it('should skip cache write when Neptune matches cached', async () => {
|
|
1053
|
-
const cachedResponse = {
|
|
1054
|
-
pk: `identity#${pixelId}#no-changes`,
|
|
1055
|
-
pixelId,
|
|
1056
|
-
identityId: 'no-changes',
|
|
1057
|
-
response: {
|
|
1058
|
-
identityId: 'no-changes',
|
|
1059
|
-
traits: { emails: ['same@email.com'] },
|
|
1060
|
-
},
|
|
1061
|
-
updatedAt: new Date().toISOString(),
|
|
1062
|
-
};
|
|
1063
|
-
mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
|
|
1064
|
-
mockGetIdentity.mockResolvedValueOnce({
|
|
1065
|
-
identityId: 'no-changes',
|
|
1066
|
-
traits: { emails: ['same@email.com'] },
|
|
1067
|
-
});
|
|
1068
|
-
const result = await IdentityGatewayService.resolveIdentityWithCaching(pixelId, { identityId: 'no-changes', traits: { emails: ['same@email.com', 'thought-new@email.com'] } });
|
|
1069
|
-
expect(result.didCallNeptune).toBe(true);
|
|
1070
|
-
expect(result.didWriteCache).toBe(false);
|
|
1071
|
-
expect(result.outcome).toBe(IDENTITY_CACHE_WITH_CACHING_OUTCOME.NEPTUNE_RESOLVED);
|
|
1072
|
-
expect(mockedDynamoDbClient.safeBatchWrite).not.toHaveBeenCalled();
|
|
1073
|
-
});
|
|
1074
|
-
it('should still return Neptune identity even when skipping write', async () => {
|
|
1075
|
-
const cachedResponse = {
|
|
1076
|
-
pk: `identity#${pixelId}#return-identity`,
|
|
1077
|
-
pixelId,
|
|
1078
|
-
identityId: 'return-identity',
|
|
1079
|
-
response: {
|
|
1080
|
-
identityId: 'return-identity',
|
|
1081
|
-
traits: { emails: ['cached@email.com'] },
|
|
1082
|
-
},
|
|
1083
|
-
updatedAt: new Date().toISOString(),
|
|
1084
|
-
};
|
|
1085
|
-
mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
|
|
1086
|
-
mockGetIdentity.mockResolvedValueOnce({
|
|
1087
|
-
identityId: 'return-identity',
|
|
1088
|
-
traits: { emails: ['cached@email.com'] },
|
|
1089
|
-
});
|
|
1090
|
-
const result = await IdentityGatewayService.resolveIdentityWithCaching(pixelId, { identityId: 'return-identity', traits: { emails: ['cached@email.com', 'extra@email.com'] } });
|
|
1091
|
-
expect(result.identity?.identityId).toBe('return-identity');
|
|
1092
|
-
});
|
|
1093
|
-
it('should strip address timestamps for comparison', async () => {
|
|
1094
|
-
const now = Date.now();
|
|
1095
|
-
const cachedResponse = {
|
|
1096
|
-
pk: `identity#${pixelId}#addr-timestamp`,
|
|
1097
|
-
pixelId,
|
|
1098
|
-
identityId: 'addr-timestamp',
|
|
1099
|
-
response: {
|
|
1100
|
-
identityId: 'addr-timestamp',
|
|
1101
|
-
traits: {
|
|
1102
|
-
emails: ['test@email.com'],
|
|
1103
|
-
addresses: [{ city: 'NYC', cra: now - 1000, upa: now - 500 }],
|
|
1104
|
-
},
|
|
1105
|
-
},
|
|
1106
|
-
updatedAt: new Date().toISOString(),
|
|
1107
|
-
};
|
|
1108
|
-
mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
|
|
1109
|
-
mockGetIdentity.mockResolvedValueOnce({
|
|
1110
|
-
identityId: 'addr-timestamp',
|
|
1111
|
-
traits: {
|
|
1112
|
-
emails: ['test@email.com'],
|
|
1113
|
-
addresses: [{ city: 'NYC', cra: now, upa: now }],
|
|
1114
|
-
},
|
|
1115
|
-
});
|
|
1116
|
-
const result = await IdentityGatewayService.resolveIdentityWithCaching(pixelId, { identityId: 'addr-timestamp', traits: { emails: ['test@email.com', 'new@email.com'] } });
|
|
1117
|
-
expect(result.didWriteCache).toBe(false);
|
|
1118
|
-
expect(result.outcome).toBe(IDENTITY_CACHE_WITH_CACHING_OUTCOME.NEPTUNE_RESOLVED);
|
|
1119
|
-
});
|
|
1120
|
-
});
|
|
1121
|
-
describe('Neptune resolution failure', () => {
|
|
1122
|
-
it('should return NEPTUNE_RESOLUTION_FAILED when Neptune fails', async () => {
|
|
1123
|
-
const cachedResponse = {
|
|
1124
|
-
pk: `identity#${pixelId}#neptune-fail`,
|
|
1125
|
-
pixelId,
|
|
1126
|
-
identityId: 'neptune-fail',
|
|
1127
|
-
response: {
|
|
1128
|
-
identityId: 'neptune-fail',
|
|
1129
|
-
traits: { emails: ['cached@email.com'] },
|
|
1130
|
-
},
|
|
1131
|
-
updatedAt: new Date().toISOString(),
|
|
1132
|
-
};
|
|
1133
|
-
mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
|
|
1134
|
-
mockGetIdentity.mockRejectedValueOnce(new Error('Lambda invocation failed'));
|
|
1135
|
-
const result = await IdentityGatewayService.resolveIdentityWithCaching(pixelId, { identityId: 'neptune-fail', traits: { emails: ['cached@email.com', 'new@email.com'] } });
|
|
1136
|
-
expect(result.didCallNeptune).toBe(true);
|
|
1137
|
-
expect(result.outcome).toBe(IDENTITY_CACHE_WITH_CACHING_OUTCOME.NEPTUNE_RESOLUTION_FAILED);
|
|
1138
|
-
});
|
|
1139
|
-
it('should not write to cache when Neptune fails', async () => {
|
|
1140
|
-
const cachedResponse = {
|
|
1141
|
-
pk: `identity#${pixelId}#no-write-fail`,
|
|
1142
|
-
pixelId,
|
|
1143
|
-
identityId: 'no-write-fail',
|
|
1144
|
-
response: {
|
|
1145
|
-
identityId: 'no-write-fail',
|
|
1146
|
-
traits: { emails: ['test@email.com'] },
|
|
1147
|
-
},
|
|
1148
|
-
updatedAt: new Date().toISOString(),
|
|
1149
|
-
};
|
|
1150
|
-
mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
|
|
1151
|
-
mockGetIdentity.mockRejectedValueOnce(new Error('Network error'));
|
|
1152
|
-
const result = await IdentityGatewayService.resolveIdentityWithCaching(pixelId, { identityId: 'no-write-fail', traits: { emails: ['test@email.com', 'extra@email.com'] } });
|
|
1153
|
-
expect(result.didWriteCache).toBe(false);
|
|
1154
|
-
expect(mockedDynamoDbClient.safeBatchWrite).not.toHaveBeenCalled();
|
|
1155
|
-
});
|
|
1156
|
-
it('should still return merged identity when Neptune fails', async () => {
|
|
1157
|
-
const cachedResponse = {
|
|
1158
|
-
pk: `identity#${pixelId}#merged-on-fail`,
|
|
1159
|
-
pixelId,
|
|
1160
|
-
identityId: 'merged-on-fail',
|
|
1161
|
-
response: {
|
|
1162
|
-
identityId: 'merged-on-fail',
|
|
1163
|
-
traits: {
|
|
1164
|
-
emails: ['cached@email.com'],
|
|
1165
|
-
phones: ['555-cached'],
|
|
1166
|
-
},
|
|
1167
|
-
},
|
|
1168
|
-
updatedAt: new Date().toISOString(),
|
|
1169
|
-
};
|
|
1170
|
-
mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
|
|
1171
|
-
mockGetIdentity.mockRejectedValueOnce(new Error('Timeout'));
|
|
1172
|
-
const result = await IdentityGatewayService.resolveIdentityWithCaching(pixelId, { identityId: 'merged-on-fail', traits: { emails: ['cached@email.com', 'new@email.com'] } });
|
|
1173
|
-
expect(result.identity).toBeDefined();
|
|
1174
|
-
});
|
|
1175
|
-
});
|
|
1176
|
-
describe('skipCacheWrite=true behavior', () => {
|
|
1177
|
-
it('should NOT write to cache when skipCacheWrite=true even with changes', async () => {
|
|
1178
|
-
const cachedResponse = {
|
|
1179
|
-
pk: `identity#${pixelId}#skip-stale`,
|
|
1180
|
-
pixelId,
|
|
1181
|
-
identityId: 'skip-stale',
|
|
1182
|
-
response: {
|
|
1183
|
-
identityId: 'skip-stale',
|
|
1184
|
-
traits: { emails: ['old@email.com'] },
|
|
1185
|
-
},
|
|
1186
|
-
updatedAt: new Date().toISOString(),
|
|
1187
|
-
};
|
|
1188
|
-
mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
|
|
1189
|
-
mockGetIdentity.mockResolvedValueOnce({
|
|
1190
|
-
identityId: 'skip-stale',
|
|
1191
|
-
traits: { emails: ['old@email.com', 'new@email.com'], phones: ['555-new'] },
|
|
1192
|
-
});
|
|
1193
|
-
const result = await IdentityGatewayService.resolveIdentityWithCaching(pixelId, { identityId: 'skip-stale', traits: { emails: ['old@email.com', 'new@email.com'] } });
|
|
1194
|
-
expect(result.didWriteCache).toBe(false);
|
|
1195
|
-
expect(mockedDynamoDbClient.safeBatchWrite).not.toHaveBeenCalled();
|
|
1196
|
-
expect(result.outcome).toBe(IDENTITY_CACHE_WITH_CACHING_OUTCOME.NEPTUNE_RESOLVED);
|
|
1197
|
-
});
|
|
1198
|
-
it('should still call Neptune when skipCacheWrite=true', async () => {
|
|
1199
|
-
const cachedResponse = {
|
|
1200
|
-
pk: `identity#${pixelId}#call-neptune-skip`,
|
|
1201
|
-
pixelId,
|
|
1202
|
-
identityId: 'call-neptune-skip',
|
|
1203
|
-
response: {
|
|
1204
|
-
identityId: 'call-neptune-skip',
|
|
1205
|
-
traits: { emails: ['test@email.com'] },
|
|
1206
|
-
},
|
|
1207
|
-
updatedAt: new Date().toISOString(),
|
|
1208
|
-
};
|
|
1209
|
-
mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
|
|
1210
|
-
mockGetIdentity.mockResolvedValueOnce({
|
|
1211
|
-
identityId: 'call-neptune-skip',
|
|
1212
|
-
traits: { emails: ['test@email.com', 'added@email.com'] },
|
|
1213
|
-
});
|
|
1214
|
-
await IdentityGatewayService.resolveIdentityWithCaching(pixelId, { identityId: 'call-neptune-skip', traits: { emails: ['test@email.com', 'added@email.com'] } });
|
|
1215
|
-
expect(mockGetIdentity).toHaveBeenCalled();
|
|
1216
|
-
});
|
|
1217
|
-
it('should return Neptune identity when skipCacheWrite=true', async () => {
|
|
1218
|
-
const cachedResponse = {
|
|
1219
|
-
pk: `identity#${pixelId}#return-skip`,
|
|
1220
|
-
pixelId,
|
|
1221
|
-
identityId: 'return-skip',
|
|
1222
|
-
response: {
|
|
1223
|
-
identityId: 'return-skip',
|
|
1224
|
-
traits: { emails: ['cached@email.com'] },
|
|
1225
|
-
},
|
|
1226
|
-
updatedAt: new Date().toISOString(),
|
|
1227
|
-
};
|
|
1228
|
-
mockedDynamoDbClient.safeGet.mockResolvedValueOnce(cachedResponse);
|
|
1229
|
-
mockGetIdentity.mockResolvedValueOnce({
|
|
1230
|
-
identityId: 'return-skip',
|
|
1231
|
-
traits: {
|
|
1232
|
-
emails: ['cached@email.com', 'neptune@email.com'],
|
|
1233
|
-
phones: ['555-neptune'],
|
|
1234
|
-
},
|
|
1235
|
-
});
|
|
1236
|
-
const result = await IdentityGatewayService.resolveIdentityWithCaching(pixelId, { identityId: 'return-skip', traits: { emails: ['cached@email.com', 'neptune@email.com'] } });
|
|
1237
|
-
expect(result.identity?.traits?.phones).toContain('555-neptune');
|
|
1238
|
-
});
|
|
1239
|
-
});
|
|
1240
|
-
});
|
|
1241
|
-
});
|
|
1242
|
-
//# sourceMappingURL=identity-gateway-service.spec.js.map
|