@@ -426,4 +426,152 @@ describe('Server Integration Tests', () => {
426426 } ) ;
427427 } ) ;
428428 } ) ;
429+ } ) ;
430+
431+ describe ( 'Server Error Handling - Additional Coverage' , ( ) => {
432+ let consoleErrorSpy ;
433+
434+ beforeEach ( ( ) => {
435+ consoleErrorSpy = jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } ) ;
436+ jest . clearAllMocks ( ) ;
437+ } ) ;
438+
439+ afterEach ( ( ) => {
440+ consoleErrorSpy . mockRestore ( ) ;
441+ } ) ;
442+
443+ describe ( 'Initialization Error Handling' , ( ) => {
444+ test ( 'should handle and log repository initialization errors' , async ( ) => {
445+ const { initializeApp } = await import ( '../../src/notes-api-server.js' ) ;
446+
447+ const failingRepository = {
448+ init : jest . fn ( ) . mockRejectedValue ( new Error ( 'Database connection timeout' ) )
449+ } ;
450+
451+ await expect ( initializeApp ( failingRepository ) ) . rejects . toThrow ( 'Database connection timeout' ) ;
452+ expect ( consoleErrorSpy ) . toHaveBeenCalledWith ( 'Failed to initialize application:' , expect . any ( Error ) ) ;
453+ } ) ;
454+
455+ test ( 'should handle concurrent initialization requests' , async ( ) => {
456+ const { initializeApp } = await import ( '../../src/notes-api-server.js' ) ;
457+
458+ const slowRepository = {
459+ init : jest . fn ( ) . mockImplementation ( ( ) =>
460+ new Promise ( resolve => setTimeout ( ( ) => resolve ( ) , 50 ) )
461+ )
462+ } ;
463+
464+ // Start multiple initialization attempts concurrently
465+ const promise1 = initializeApp ( slowRepository ) ;
466+ const promise2 = initializeApp ( slowRepository ) ;
467+
468+ const [ result1 , result2 ] = await Promise . all ( [ promise1 , promise2 ] ) ;
469+
470+ expect ( result1 . app ) . toBeDefined ( ) ;
471+ expect ( result2 . app ) . toBeDefined ( ) ;
472+ expect ( slowRepository . init ) . toHaveBeenCalledTimes ( 2 ) ;
473+ } ) ;
474+
475+ test ( 'should handle timeout during repository initialization' , async ( ) => {
476+ const { initializeApp } = await import ( '../../src/notes-api-server.js' ) ;
477+
478+ const timeoutRepository = {
479+ init : jest . fn ( ) . mockImplementation ( ( ) =>
480+ new Promise ( ( _ , reject ) =>
481+ setTimeout ( ( ) => reject ( new Error ( 'Connection timeout' ) ) , 50 )
482+ )
483+ )
484+ } ;
485+
486+ await expect ( initializeApp ( timeoutRepository ) ) . rejects . toThrow ( 'Connection timeout' ) ;
487+ expect ( consoleErrorSpy ) . toHaveBeenCalledWith ( 'Failed to initialize application:' , expect . any ( Error ) ) ;
488+ } ) ;
489+ } ) ;
490+
491+ describe ( 'Environment Configuration' , ( ) => {
492+ let originalEnv ;
493+
494+ beforeEach ( ( ) => {
495+ originalEnv = { ...process . env } ;
496+ } ) ;
497+
498+ afterEach ( ( ) => {
499+ process . env = originalEnv ;
500+ } ) ;
501+
502+ test ( 'should handle missing environment variables gracefully' , async ( ) => {
503+ // Test with completely clean environment
504+ delete process . env . HOST ;
505+ delete process . env . PORT ;
506+ delete process . env . DB_VENDOR ;
507+
508+ const { createNoteRepository } = await import ( '../../src/notes-api-server.js' ) ;
509+ const repository = createNoteRepository ( ) ;
510+
511+ expect ( repository ) . toBeDefined ( ) ;
512+ } ) ;
513+
514+ test ( 'should default to CouchDB for invalid database vendor' , async ( ) => {
515+ process . env . DB_VENDOR = 'invalid-database' ;
516+
517+ const { createNoteRepository } = await import ( '../../src/notes-api-server.js' ) ;
518+ const repository = createNoteRepository ( ) ;
519+
520+ // Should create CouchDB repository as fallback
521+ expect ( repository ) . toBeDefined ( ) ;
522+ expect ( repository . constructor . name ) . toBe ( 'MockNoteRepository' ) ; // In test environment
523+ } ) ;
524+
525+ test ( 'should handle empty string environment variables' , async ( ) => {
526+ process . env . HOST = '' ;
527+ process . env . PORT = '' ;
528+ process . env . DB_VENDOR = '' ;
529+
530+ const { createNoteRepository } = await import ( '../../src/notes-api-server.js' ) ;
531+ const repository = createNoteRepository ( ) ;
532+
533+ expect ( repository ) . toBeDefined ( ) ;
534+ } ) ;
535+ } ) ;
536+
537+ describe ( 'Error Propagation' , ( ) => {
538+ test ( 'should properly propagate initialization errors' , async ( ) => {
539+ const { initializeApp } = await import ( '../../src/notes-api-server.js' ) ;
540+
541+ const customError = new Error ( 'Custom initialization error' ) ;
542+ customError . code = 'CUSTOM_ERROR' ;
543+
544+ const failingRepository = {
545+ init : jest . fn ( ) . mockRejectedValue ( customError )
546+ } ;
547+
548+ try {
549+ await initializeApp ( failingRepository ) ;
550+ fail ( 'Should have thrown an error' ) ;
551+ } catch ( error ) {
552+ expect ( error . message ) . toBe ( 'Custom initialization error' ) ;
553+ expect ( error . code ) . toBe ( 'CUSTOM_ERROR' ) ;
554+ expect ( consoleErrorSpy ) . toHaveBeenCalledWith ( 'Failed to initialize application:' , error ) ;
555+ }
556+ } ) ;
557+
558+ test ( 'should handle various error types during initialization' , async ( ) => {
559+ const { initializeApp } = await import ( '../../src/notes-api-server.js' ) ;
560+
561+ const errorTypes = [
562+ new TypeError ( 'Type error during init' ) ,
563+ new ReferenceError ( 'Reference error during init' ) ,
564+ new RangeError ( 'Range error during init' ) ,
565+ new Error ( 'Generic error during init' )
566+ ] ;
567+
568+ for ( const errorType of errorTypes ) {
569+ const failingRepository = {
570+ init : jest . fn ( ) . mockRejectedValue ( errorType )
571+ } ;
572+
573+ await expect ( initializeApp ( failingRepository ) ) . rejects . toThrow ( errorType . message ) ;
574+ }
575+ } ) ;
576+ } ) ;
429577} ) ;
0 commit comments