@@ -9,6 +9,16 @@ import * as mockResponseJson from './evaluation/mockResponse.json';
99import { MockEventSource } from './streaming/LDClientImpl.mocks' ;
1010import { 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+
1222let mockPlatform : ReturnType < typeof createBasicPlatform > ;
1323let 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 } ) ;
0 commit comments