Skip to content

Commit 38af523

Browse files
committed
test(server-node): adding additional tests for initialization fix
this commit also changes the initialization callback funciton to `initializedCallback` from `basisRecieved` to be more clear on what the callback is for.
1 parent 935b994 commit 38af523

File tree

2 files changed

+70
-8
lines changed

2 files changed

+70
-8
lines changed

packages/shared/sdk-server/__tests__/data_sources/createPayloadListenersFDv2.test.ts

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,23 +104,23 @@ const changesTransferNone = {
104104

105105
describe('createPayloadListenerFDv2', () => {
106106
let dataSourceUpdates: LDTransactionalDataSourceUpdates;
107-
let basisReceived: jest.Mock;
107+
let initializedCallback: jest.Mock;
108108

109109
beforeEach(() => {
110110
dataSourceUpdates = {
111111
init: jest.fn(),
112112
upsert: jest.fn(),
113113
applyChanges: jest.fn(),
114114
};
115-
basisReceived = jest.fn();
115+
initializedCallback = jest.fn();
116116
});
117117

118118
afterEach(() => {
119119
jest.resetAllMocks();
120120
});
121121

122122
test('data source updates called with basis true', async () => {
123-
const listener = createPayloadListener(dataSourceUpdates, logger, basisReceived);
123+
const listener = createPayloadListener(dataSourceUpdates, logger, initializedCallback);
124124
listener(fullTransferPayload);
125125

126126
expect(logger.debug).toHaveBeenCalledWith(expect.stringMatching(/initializing/i));
@@ -141,7 +141,7 @@ describe('createPayloadListenerFDv2', () => {
141141
});
142142

143143
test('data source updates called with basis false', async () => {
144-
const listener = createPayloadListener(dataSourceUpdates, logger, basisReceived);
144+
const listener = createPayloadListener(dataSourceUpdates, logger, initializedCallback);
145145
listener(changesTransferPayload);
146146

147147
expect(logger.debug).toHaveBeenCalledWith(expect.stringMatching(/updating/i));
@@ -175,10 +175,72 @@ describe('createPayloadListenerFDv2', () => {
175175
});
176176

177177
test('data source updates not called when basis is false and changes are empty', async () => {
178-
const listener = createPayloadListener(dataSourceUpdates, logger, basisReceived);
178+
const listener = createPayloadListener(dataSourceUpdates, logger, initializedCallback);
179179
listener(changesTransferNone);
180180

181181
expect(logger.debug).toBeCalledWith(expect.stringMatching(/ignoring/i));
182182
expect(dataSourceUpdates.applyChanges).toHaveBeenCalledTimes(0);
183183
});
184+
185+
test('calls initializedCallback when state is non-empty (initial)', () => {
186+
const listener = createPayloadListener(dataSourceUpdates, logger, initializedCallback);
187+
let capturedCallback: (() => void) | undefined;
188+
189+
dataSourceUpdates.applyChanges = jest.fn((_basis, _data, callback) => {
190+
capturedCallback = callback;
191+
});
192+
193+
listener(fullTransferPayload);
194+
195+
expect(capturedCallback).toBeDefined();
196+
expect(initializedCallback).not.toHaveBeenCalled();
197+
198+
// Simulate applyChanges calling the callback
199+
capturedCallback?.();
200+
201+
expect(initializedCallback).toHaveBeenCalledTimes(1);
202+
});
203+
204+
test('does not call initializedCallback when state is empty (file data initializer)', () => {
205+
const fileDataPayload = {
206+
initMetadata: {
207+
environmentId: 'envId',
208+
},
209+
payload: {
210+
id: 'payloadID',
211+
version: 99,
212+
state: '',
213+
basis: true,
214+
updates: [
215+
{
216+
kind: 'flag',
217+
key: 'flagkey',
218+
version: 1,
219+
object: {
220+
key: 'flagkey',
221+
version: 1,
222+
},
223+
},
224+
],
225+
},
226+
};
227+
228+
const listener = createPayloadListener(dataSourceUpdates, logger, initializedCallback);
229+
let capturedCallback: (() => void) | undefined;
230+
231+
dataSourceUpdates.applyChanges = jest.fn((_basis, _data, callback) => {
232+
capturedCallback = callback;
233+
});
234+
235+
listener(fileDataPayload);
236+
237+
expect(capturedCallback).toBeDefined();
238+
expect(initializedCallback).not.toHaveBeenCalled();
239+
240+
// Simulate applyChanges calling the callback
241+
capturedCallback?.();
242+
243+
// Should still not be called because state is empty
244+
expect(initializedCallback).not.toHaveBeenCalled();
245+
});
184246
});

packages/shared/sdk-server/src/data_sources/createPayloadListenerFDv2.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export const createPayloadListener =
2222
(
2323
dataSourceUpdates: LDTransactionalDataSourceUpdates,
2424
logger?: LDLogger,
25-
basisReceived: VoidFunction = () => {},
25+
initializedCallback: VoidFunction = () => {},
2626
) =>
2727
(dataContainer: DataCallbackContainer) => {
2828
const { initMetadata, payload } = dataContainer;
@@ -70,10 +70,10 @@ export const createPayloadListener =
7070
converted,
7171
() => {
7272
if (payload.state !== '') {
73-
// NOTE: this is a workaround for now. The only condition that we will consider a valid basis
73+
// NOTE: The only condition that we will consider a valid basis
7474
// is when there is a valid selector. Currently, the only data source that does not have a
7575
// valid selector is the file data initializer, which will have a blank selector.
76-
basisReceived();
76+
initializedCallback();
7777
}
7878
},
7979
initMetadata,

0 commit comments

Comments
 (0)