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