11/**
2- * Copyright 2019-2020, 2022, 2024, Optimizely
2+ * Copyright 2019-2020, 2022, 2024, 2026, Optimizely
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
@@ -23,6 +23,8 @@ import { resolvablePromise } from '../utils/promise/resolvablePromise';
2323import { getMockDatafileManager } from '../tests/mock/mock_datafile_manager' ;
2424import { wait } from '../tests/testUtils' ;
2525
26+ import { MAX_INVALID_DATAFILE_EMIT } from './project_config_manager' ;
27+
2628describe ( 'ProjectConfigManagerImpl' , ( ) => {
2729 describe ( 'a logger is passed in the constructor' , ( ) => {
2830 it ( 'should set name on the logger passed into the constructor' , ( ) => {
@@ -201,14 +203,14 @@ describe('ProjectConfigManagerImpl', () => {
201203 describe ( 'when datafile is invalid' , ( ) => {
202204 it ( 'should reject onRunning() with the same error if datafileManager.onRunning() rejects' , async ( ) => {
203205 const datafileManager = getMockDatafileManager ( { onRunning : Promise . reject ( new Error ( 'test error' ) ) } ) ;
204- const manager = new ProjectConfigManagerImpl ( { datafile : JSON . stringify ( { } ) , datafileManager } ) ;
206+ const manager = new ProjectConfigManagerImpl ( { datafile : 'ab' , datafileManager } ) ;
205207 manager . start ( ) ;
206208 await expect ( manager . onRunning ( ) ) . rejects . toThrow ( 'DatafileManager failed to start, reason: test error' ) ;
207209 } ) ;
208210
209211 it ( 'should resolve onRunning() if datafileManager.onUpdate() is fired and should update config' , async ( ) => {
210212 const datafileManager = getMockDatafileManager ( { onRunning : Promise . resolve ( ) } ) ;
211- const manager = new ProjectConfigManagerImpl ( { datafile : JSON . stringify ( { } ) , datafileManager } ) ;
213+ const manager = new ProjectConfigManagerImpl ( { datafile : 'ab' , datafileManager } ) ;
212214 manager . start ( ) ;
213215 datafileManager . pushUpdate ( JSON . stringify ( testData . getTestProjectConfig ( ) ) ) ;
214216 await expect ( manager . onRunning ( ) ) . resolves . not . toThrow ( ) ;
@@ -217,7 +219,7 @@ describe('ProjectConfigManagerImpl', () => {
217219
218220 it ( 'should resolve onRunning(), update config and call onUpdate listeners if datafileManager.onUpdate() is fired' , async ( ) => {
219221 const datafileManager = getMockDatafileManager ( { onRunning : Promise . resolve ( ) } ) ;
220- const manager = new ProjectConfigManagerImpl ( { datafile : JSON . stringify ( { } ) , datafileManager } ) ;
222+ const manager = new ProjectConfigManagerImpl ( { datafile : 'ab' , datafileManager } ) ;
221223 manager . start ( ) ;
222224 const listener = vi . fn ( ) ;
223225 manager . onUpdate ( listener ) ;
@@ -230,20 +232,52 @@ describe('ProjectConfigManagerImpl', () => {
230232
231233 it ( 'should return undefined from getConfig() before onRunning() resolves' , async ( ) => {
232234 const datafileManager = getMockDatafileManager ( { onRunning : Promise . resolve ( ) } ) ;
233- const manager = new ProjectConfigManagerImpl ( { datafile : JSON . stringify ( { } ) , datafileManager } ) ;
235+ const manager = new ProjectConfigManagerImpl ( { datafile : 'ab' , datafileManager } ) ;
234236 manager . start ( ) ;
235237 expect ( manager . getConfig ( ) ) . toBeUndefined ( ) ;
236238 } ) ;
237239
238240 it ( 'should return the correct config from getConfig() after onRunning() resolves' , async ( ) => {
239241 const datafileManager = getMockDatafileManager ( { onRunning : Promise . resolve ( ) } ) ;
240- const manager = new ProjectConfigManagerImpl ( { datafile : JSON . stringify ( { } ) , datafileManager } ) ;
242+ const manager = new ProjectConfigManagerImpl ( { datafile : 'ab' , datafileManager } ) ;
241243 manager . start ( ) ;
242244
243245 datafileManager . pushUpdate ( JSON . stringify ( testData . getTestProjectConfig ( ) ) ) ;
244246 await expect ( manager . onRunning ( ) ) . resolves . not . toThrow ( ) ;
245247 expect ( manager . getConfig ( ) ) . toEqual ( createProjectConfig ( JSON . stringify ( testData . getTestProjectConfig ( ) ) ) ) ;
246248 } ) ;
249+
250+ it ( 'should reject onRunning() and onTerminated() if datafileManger emits invalid datafile \
251+ MAX_INVALID_DATAFILE_EMIT times before running' , async ( ) => {
252+ const datafileManager = getMockDatafileManager ( { onRunning : Promise . resolve ( ) } ) ;
253+ const manager = new ProjectConfigManagerImpl ( { datafile : 'ab' , datafileManager } ) ;
254+ manager . start ( ) ;
255+
256+ for ( let i = 0 ; i < MAX_INVALID_DATAFILE_EMIT ; i ++ ) {
257+ // emit invalid datafile
258+ datafileManager . pushUpdate ( 'ab' ) ;
259+ }
260+
261+ await expect ( manager . onRunning ( ) ) . rejects . toThrow ( ) ;
262+ await expect ( manager . onTerminated ( ) ) . rejects . toThrow ( ) ;
263+ } ) ;
264+
265+ it ( 'should resolve onRunning and update config when datafileManager emits valid datafile before \
266+ emitting invalid datafile MAX_INVALID_DATAFILE_EMIT times' , async ( ) => {
267+ const datafileManager = getMockDatafileManager ( { onRunning : Promise . resolve ( ) } ) ;
268+ const manager = new ProjectConfigManagerImpl ( { datafile : 'ab' , datafileManager } ) ;
269+ manager . start ( ) ;
270+
271+ for ( let i = 0 ; i < MAX_INVALID_DATAFILE_EMIT - 1 ; i ++ ) {
272+ // emit invalid datafile
273+ datafileManager . pushUpdate ( 'ab' ) ;
274+ }
275+
276+ datafileManager . pushUpdate ( JSON . stringify ( testData . getTestProjectConfig ( ) ) ) ;
277+
278+ await expect ( manager . onRunning ( ) ) . resolves . not . toThrow ( ) ;
279+ expect ( manager . getConfig ( ) ) . toEqual ( createProjectConfig ( JSON . stringify ( testData . getTestProjectConfig ( ) ) ) ) ;
280+ } ) ;
247281 } ) ;
248282 } ) ;
249283
@@ -255,16 +289,40 @@ describe('ProjectConfigManagerImpl', () => {
255289 await expect ( manager . onRunning ( ) ) . rejects . toThrow ( 'DatafileManager failed to start, reason: test error' ) ;
256290 } ) ;
257291
258- it ( 'should reject onRunning() and onTerminated if datafileManager emits an invalid datafile in the first onUpdate' , async ( ) => {
292+ it ( 'should reject onRunning() and onTerminated() if datafileManger emits invalid datafile \
293+ MAX_INVALID_DATAFILE_EMIT times before running' , async ( ) => {
259294 const datafileManager = getMockDatafileManager ( { onRunning : Promise . resolve ( ) } ) ;
260295 const manager = new ProjectConfigManagerImpl ( { datafileManager } ) ;
261296 manager . start ( ) ;
262- datafileManager . pushUpdate ( 'foo' ) ;
297+
298+ for ( let i = 0 ; i < MAX_INVALID_DATAFILE_EMIT ; i ++ ) {
299+ // emit invalid datafile
300+ datafileManager . pushUpdate ( 'ab' ) ;
301+ }
302+
263303 await expect ( manager . onRunning ( ) ) . rejects . toThrow ( ) ;
264304 await expect ( manager . onTerminated ( ) ) . rejects . toThrow ( ) ;
265305 } ) ;
266306
267- it ( 'should resolve onRunning(), update config and call onUpdate listeners if datafileManager.onUpdate() is fired' , async ( ) => {
307+ it ( 'should resolve onRunning and update config when datafile manger emits valid datafile before \
308+ emitting inbalid datafile MAX_INVALID_DATAFILE_EMIT times' , async ( ) => {
309+ const datafileManager = getMockDatafileManager ( { onRunning : Promise . resolve ( ) } ) ;
310+ const manager = new ProjectConfigManagerImpl ( { datafileManager } ) ;
311+ manager . start ( ) ;
312+
313+ for ( let i = 0 ; i < MAX_INVALID_DATAFILE_EMIT - 1 ; i ++ ) {
314+ // emit invalid datafile
315+ datafileManager . pushUpdate ( 'ab' ) ;
316+ }
317+
318+ datafileManager . pushUpdate ( JSON . stringify ( testData . getTestProjectConfig ( ) ) ) ;
319+
320+ await expect ( manager . onRunning ( ) ) . resolves . not . toThrow ( ) ;
321+ expect ( manager . getConfig ( ) ) . toEqual ( createProjectConfig ( JSON . stringify ( testData . getTestProjectConfig ( ) ) ) ) ;
322+ } ) ;
323+
324+ it ( 'should resolve onRunning(), update config and call onUpdate listeners if datafileManager.onUpdate() is fired \
325+ with valid datafile on the first call' , async ( ) => {
268326 const datafileManager = getMockDatafileManager ( { onRunning : Promise . resolve ( ) } ) ;
269327 const manager = new ProjectConfigManagerImpl ( { datafileManager } ) ;
270328 manager . start ( ) ;
0 commit comments