1- import { LDOptions } from '../../src' ;
1+ import {
2+ ClientContext ,
3+ DataSourceOptions ,
4+ isStandardOptions ,
5+ LDFeatureStore ,
6+ LDOptions ,
7+ PollingDataSourceOptions ,
8+ StandardDataSourceOptions ,
9+ } from '../../src' ;
210import Configuration from '../../src/options/Configuration' ;
11+ import InMemoryFeatureStore from '../../src/store/InMemoryFeatureStore' ;
312import TestLogger , { LogLevel } from '../Logger' ;
413
514function withLogger ( options : LDOptions ) : LDOptions {
@@ -25,15 +34,16 @@ describe.each([undefined, null, 'potat0', 17, [], {}])('constructed without opti
2534 expect ( config . flushInterval ) . toBe ( 5 ) ;
2635 expect ( config . logger ) . toBeUndefined ( ) ;
2736 expect ( config . offline ) . toBe ( false ) ;
28- expect ( config . pollInterval ) . toBe ( 30 ) ;
37+ expect ( ( config . dataSystem . dataSource as StandardDataSourceOptions ) . pollInterval ) . toEqual ( 30 ) ;
2938 expect ( config . privateAttributes ) . toStrictEqual ( [ ] ) ;
3039 expect ( config . proxyOptions ) . toBeUndefined ( ) ;
3140 expect ( config . sendEvents ) . toBe ( true ) ;
3241 expect ( config . serviceEndpoints . streaming ) . toEqual ( 'https://stream.launchdarkly.com' ) ;
3342 expect ( config . serviceEndpoints . polling ) . toEqual ( 'https://sdk.launchdarkly.com' ) ;
3443 expect ( config . serviceEndpoints . events ) . toEqual ( 'https://events.launchdarkly.com' ) ;
35- expect ( config . stream ) . toBe ( true ) ;
36- expect ( config . streamInitialReconnectDelay ) . toEqual ( 1 ) ;
44+ expect (
45+ ( config . dataSystem . dataSource as StandardDataSourceOptions ) . streamInitialReconnectDelay ,
46+ ) . toEqual ( 1 ) ;
3747 expect ( config . tags . value ) . toBeUndefined ( ) ;
3848 expect ( config . timeout ) . toEqual ( 5 ) ;
3949 expect ( config . tlsParams ) . toBeUndefined ( ) ;
@@ -179,7 +189,9 @@ describe('when setting different options', () => {
179189 ] ) ( 'allow setting and validates pollInterval' , ( value , expected , warnings ) => {
180190 // @ts -ignore
181191 const config = new Configuration ( withLogger ( { pollInterval : value } ) ) ;
182- expect ( config . pollInterval ) . toEqual ( expected ) ;
192+ expect ( ( config . dataSystem . dataSource as StandardDataSourceOptions ) . pollInterval ) . toEqual (
193+ expected ,
194+ ) ;
183195 expect ( logger ( config ) . getCount ( ) ) . toEqual ( warnings ) ;
184196 } ) ;
185197
@@ -207,7 +219,7 @@ describe('when setting different options', () => {
207219 ] ) ( 'allows setting stream and validates stream' , ( value , expected , warnings ) => {
208220 // @ts -ignore
209221 const config = new Configuration ( withLogger ( { stream : value } ) ) ;
210- expect ( config . stream ) . toEqual ( expected ) ;
222+ expect ( isStandardOptions ( config . dataSystem . dataSource ) ) . toEqual ( expected ) ;
211223 expect ( logger ( config ) . getCount ( ) ) . toEqual ( warnings ) ;
212224 } ) ;
213225
@@ -409,7 +421,99 @@ describe('when setting different options', () => {
409421 ] ) ;
410422 } ) ;
411423
412- it ( 'drop' , ( ) => {
424+ it ( 'drops invalid dataystem data source options and replaces with defaults' , ( ) => {
425+ const config = new Configuration (
426+ withLogger ( {
427+ dataSystem : { dataSource : { bogus : 'myBogusOptions' } as unknown as DataSourceOptions } ,
428+ } ) ,
429+ ) ;
430+ expect ( isStandardOptions ( config . dataSystem . dataSource ) ) . toEqual ( true ) ;
431+ logger ( config ) . expectMessages ( [
432+ {
433+ level : LogLevel . Warn ,
434+ matches : / C o n f i g o p t i o n " d a t a S o u r c e " s h o u l d b e o f t y p e D a t a S o u r c e O p t i o n s / ,
435+ } ,
436+ ] ) ;
437+ } ) ;
413438
414- }
439+ it ( 'validates the datasystem persitent store is a factory or object' , ( ) => {
440+ const config1 = new Configuration (
441+ withLogger ( {
442+ dataSystem : {
443+ persistentStore : ( ) => new InMemoryFeatureStore ( ) ,
444+ } ,
445+ } ) ,
446+ ) ;
447+ expect ( isStandardOptions ( config1 . dataSystem . dataSource ) ) . toEqual ( true ) ;
448+ expect ( logger ( config1 ) . getCount ( ) ) . toEqual ( 0 ) ;
449+
450+ const config2 = new Configuration (
451+ withLogger ( {
452+ dataSystem : {
453+ persistentStore : 'bogus type' as unknown as LDFeatureStore ,
454+ } ,
455+ } ) ,
456+ ) ;
457+ expect ( isStandardOptions ( config2 . dataSystem . dataSource ) ) . toEqual ( true ) ;
458+ logger ( config2 ) . expectMessages ( [
459+ {
460+ level : LogLevel . Warn ,
461+ matches : / C o n f i g o p t i o n " p e r s i s t e n t S t o r e " s h o u l d b e o f t y p e L D F e a t u r e S t o r e / ,
462+ } ,
463+ ] ) ;
464+ } ) ;
465+
466+ it ( 'it provides reasonable defaults when datasystem is provided, but some options are missing' , ( ) => {
467+ const config = new Configuration (
468+ withLogger ( {
469+ dataSystem : { } ,
470+ } ) ,
471+ ) ;
472+ expect ( isStandardOptions ( config . dataSystem . dataSource ) ) . toEqual ( true ) ;
473+ expect ( logger ( config ) . getCount ( ) ) . toEqual ( 0 ) ;
474+ } ) ;
475+
476+ it ( 'it provides reasonable defaults within the dataSystem.dataSource options when they are missing' , ( ) => {
477+ const config = new Configuration (
478+ withLogger ( {
479+ dataSystem : { dataSource : { type : 'standard' } } ,
480+ } ) ,
481+ ) ;
482+ expect ( isStandardOptions ( config . dataSystem . dataSource ) ) . toEqual ( true ) ;
483+ expect ( logger ( config ) . getCount ( ) ) . toEqual ( 0 ) ;
484+ } ) ;
485+
486+ it ( 'ignores deprecated top level options when dataSystem.dataSource options are provided' , ( ) => {
487+ const config = new Configuration (
488+ withLogger ( {
489+ pollInterval : 501 , // should be ignored
490+ streamInitialReconnectDelay : 502 , // should be ignored
491+ dataSystem : {
492+ dataSource : { type : 'standard' , pollInterval : 100 , streamInitialReconnectDelay : 200 } , // should be used
493+ } ,
494+ } ) ,
495+ ) ;
496+ expect ( isStandardOptions ( config . dataSystem . dataSource ) ) . toEqual ( true ) ;
497+ expect ( ( config . dataSystem . dataSource as StandardDataSourceOptions ) . pollInterval ) . toEqual ( 100 ) ;
498+ expect (
499+ ( config . dataSystem . dataSource as StandardDataSourceOptions ) . streamInitialReconnectDelay ,
500+ ) . toEqual ( 200 ) ;
501+ expect ( logger ( config ) . getCount ( ) ) . toEqual ( 0 ) ;
502+ } ) ;
503+
504+ it ( 'ignores top level featureStore in favor of the datasystem persitent store' , ( ) => {
505+ const shouldNotBeUsed = new InMemoryFeatureStore ( ) ;
506+ const shouldBeUsed = new InMemoryFeatureStore ( ) ;
507+ const config = new Configuration (
508+ withLogger ( {
509+ featureStore : shouldNotBeUsed ,
510+ dataSystem : {
511+ persistentStore : shouldBeUsed ,
512+ } ,
513+ } ) ,
514+ ) ;
515+ // @ts -ignore
516+ const result = config . dataSystem . featureStoreFactory ( null ) ;
517+ expect ( result ) . toEqual ( shouldBeUsed ) ;
518+ } ) ;
415519} ) ;
0 commit comments