Skip to content

Commit e990e55

Browse files
committed
Integrated into persistence.
1 parent 35a20b7 commit e990e55

9 files changed

Lines changed: 346 additions & 471 deletions

File tree

packages/shared/sdk-client/__tests__/LDClientImpl.storage.test.ts

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ import * as mockResponseJson from './evaluation/mockResponse.json';
99
import { MockEventSource } from './streaming/LDClientImpl.mocks';
1010
import { makeTestDataManagerFactory } from './TestDataManager';
1111

12+
/**
13+
* Extracts the flags from a stored cache record JSON string.
14+
* Handles both the new format `{flags: {...}, freshness: {...}}` and
15+
* the old bare `{flagKey: {...}}` format.
16+
*/
17+
function parseFlagsFromStorage(json: string): Flags {
18+
const parsed = JSON.parse(json);
19+
return parsed.flags ?? parsed;
20+
}
21+
1222
let mockPlatform: ReturnType<typeof createBasicPlatform>;
1323
let logger: LDLogger;
1424

@@ -210,11 +220,9 @@ describe('sdk-client storage', () => {
210220
expect.stringContaining('index'),
211221
);
212222

213-
expect(mockPlatform.storage.set).toHaveBeenNthCalledWith(
214-
2,
215-
flagStorageKey,
216-
JSON.stringify(defaultPutResponse),
217-
);
223+
const storedRecord = parseFlagsFromStorage(mockPlatform.storage.set.mock.calls[1][1]);
224+
expect(mockPlatform.storage.set.mock.calls[1][0]).toBe(flagStorageKey);
225+
expect(storedRecord).toEqual(defaultPutResponse);
218226

219227
// this is defaultPutResponse
220228
expect(ldc.allFlags()).toEqual({
@@ -256,11 +264,8 @@ describe('sdk-client storage', () => {
256264
indexStorageKey,
257265
expect.stringContaining('index'),
258266
);
259-
expect(mockPlatform.storage.set).toHaveBeenNthCalledWith(
260-
2,
261-
flagStorageKey,
262-
JSON.stringify(putResponse),
263-
);
267+
expect(mockPlatform.storage.set.mock.calls[1][0]).toBe(flagStorageKey);
268+
expect(parseFlagsFromStorage(mockPlatform.storage.set.mock.calls[1][1])).toEqual(putResponse);
264269

265270
expect(emitter.emit).toHaveBeenCalledWith('change', context, defaultFlagKeys);
266271
expect(emitter.emit).toHaveBeenCalledWith('change', context, ['dev-test-flag']);
@@ -297,11 +302,8 @@ describe('sdk-client storage', () => {
297302
indexStorageKey,
298303
expect.stringContaining('index'),
299304
);
300-
expect(mockPlatform.storage.set).toHaveBeenNthCalledWith(
301-
2,
302-
flagStorageKey,
303-
JSON.stringify(putResponse),
304-
);
305+
expect(mockPlatform.storage.set.mock.calls[1][0]).toBe(flagStorageKey);
306+
expect(parseFlagsFromStorage(mockPlatform.storage.set.mock.calls[1][1])).toEqual(putResponse);
305307
expect(emitter.emit).toHaveBeenCalledWith('change', context, ['another-dev-test-flag']);
306308
});
307309

@@ -379,10 +381,9 @@ describe('sdk-client storage', () => {
379381
indexStorageKey,
380382
expect.stringContaining('index'),
381383
);
382-
expect(mockPlatform.storage.set).toHaveBeenNthCalledWith(
383-
2,
384-
flagStorageKey,
385-
JSON.stringify(defaultPutResponse),
384+
expect(mockPlatform.storage.set.mock.calls[1][0]).toBe(flagStorageKey);
385+
expect(parseFlagsFromStorage(mockPlatform.storage.set.mock.calls[1][1])).toEqual(
386+
defaultPutResponse,
386387
);
387388

388389
// we expect one change from the local storage init, but no further change from the PUT
@@ -421,7 +422,7 @@ describe('sdk-client storage', () => {
421422
await changePromise;
422423
await jest.runAllTimersAsync();
423424

424-
const flagsInStorage = JSON.parse(mockPlatform.storage.set.mock.lastCall[1]) as Flags;
425+
const flagsInStorage = parseFlagsFromStorage(mockPlatform.storage.set.mock.lastCall[1]);
425426
expect(ldc.allFlags()).toMatchObject({ 'dev-test-flag': true });
426427
expect(flagsInStorage['dev-test-flag'].reason).toEqual({
427428
kind: 'RULE_MATCH',
@@ -455,7 +456,7 @@ describe('sdk-client storage', () => {
455456
await changePromise;
456457
await jest.runAllTimersAsync();
457458

458-
const flagsInStorage = JSON.parse(mockPlatform.storage.set.mock.lastCall[1]) as Flags;
459+
const flagsInStorage = parseFlagsFromStorage(mockPlatform.storage.set.mock.lastCall[1]);
459460
expect(ldc.allFlags()).toMatchObject({ 'dev-test-flag': false });
460461
expect(mockPlatform.storage.set).toHaveBeenCalledTimes(4);
461462
expect(flagsInStorage['dev-test-flag'].version).toEqual(patchResponse.version);
@@ -482,7 +483,7 @@ describe('sdk-client storage', () => {
482483
await changePromise;
483484
await jest.runAllTimersAsync();
484485

485-
const flagsInStorage = JSON.parse(mockPlatform.storage.set.mock.lastCall[1]) as Flags;
486+
const flagsInStorage = parseFlagsFromStorage(mockPlatform.storage.set.mock.lastCall[1]);
486487
expect(ldc.allFlags()).toHaveProperty('another-dev-test-flag');
487488
expect(mockPlatform.storage.set).toHaveBeenNthCalledWith(
488489
4,
@@ -556,7 +557,7 @@ describe('sdk-client storage', () => {
556557
await changePromise;
557558
await jest.runAllTimersAsync();
558559

559-
const flagsInStorage = JSON.parse(mockPlatform.storage.set.mock.lastCall[1]) as Flags;
560+
const flagsInStorage = parseFlagsFromStorage(mockPlatform.storage.set.mock.lastCall[1]);
560561
expect(ldc.allFlags()).not.toHaveProperty('dev-test-flag');
561562
expect(mockPlatform.storage.set).toHaveBeenNthCalledWith(
562563
4,
@@ -645,7 +646,7 @@ describe('sdk-client storage', () => {
645646
await changePromise;
646647
await jest.runAllTimersAsync();
647648

648-
const flagsInStorage = JSON.parse(mockPlatform.storage.set.mock.lastCall[1]) as Flags;
649+
const flagsInStorage = parseFlagsFromStorage(mockPlatform.storage.set.mock.lastCall[1]);
649650

650651
expect(mockPlatform.storage.set).toHaveBeenCalledTimes(4); // two index saves and two flag saves
651652
expect(flagsInStorage['does-not-exist']).toMatchObject({ ...deleteResponse, deleted: true });

packages/shared/sdk-client/__tests__/datasource/FreshnessTracker.test.ts

Lines changed: 0 additions & 225 deletions
This file was deleted.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { calculatePollDelay } from '../../../src/datasource/fdv2/calculatePollDelay';
2+
3+
describe('calculatePollDelay', () => {
4+
it('returns 0 when freshness is undefined (poll immediately)', () => {
5+
expect(calculatePollDelay(undefined, 60000, 1000)).toBe(0);
6+
});
7+
8+
it('returns remaining interval when data was recently received', () => {
9+
// Freshness at 1000, now at 1500, interval 2000 → 1500 remaining
10+
expect(calculatePollDelay(1000, 2000, 1500)).toBe(1500);
11+
});
12+
13+
it('returns 0 when data is stale beyond poll interval', () => {
14+
// Freshness at 1000, now at 5000, interval 2000 → 0 (stale)
15+
expect(calculatePollDelay(1000, 2000, 5000)).toBe(0);
16+
});
17+
18+
it('returns 0 when elapsed time exactly matches interval', () => {
19+
expect(calculatePollDelay(1000, 2000, 3000)).toBe(0);
20+
});
21+
22+
it('returns full interval when freshness equals now', () => {
23+
expect(calculatePollDelay(5000, 60000, 5000)).toBe(60000);
24+
});
25+
});

0 commit comments

Comments
 (0)