@@ -353,9 +353,26 @@ describe('Database Service (db/index.ts)', () => {
353353 } ) ;
354354
355355 describe ( 'getDb and getSchema' , ( ) => {
356- it ( 'should throw if not initialized' , ( ) => {
357- expect ( ( ) => getDb ( ) ) . toThrow ( 'Database not initialized. Call initializeDatabase() first.' ) ;
358- expect ( ( ) => getSchema ( ) ) . toThrow ( 'Database schema not generated. Call initializeDatabase() first.' ) ;
356+ it ( 'should return safe proxy/schema when not initialized (graceful startup)' , ( ) => {
357+ // New behavior: getDb() returns a safe proxy instead of throwing
358+ const db = getDb ( ) ;
359+ expect ( db ) . toBeDefined ( ) ;
360+ expect ( typeof db ) . toBe ( 'object' ) ;
361+
362+ // New behavior: getSchema() returns a generated schema instead of throwing
363+ const schema = getSchema ( ) ;
364+ expect ( schema ) . toBeDefined ( ) ;
365+ expect ( typeof schema ) . toBe ( 'object' ) ;
366+ } ) ;
367+
368+ it ( 'should throw helpful error when trying to use proxy database operations' , ( ) => {
369+ const db = getDb ( ) ;
370+
371+ // The proxy should throw helpful errors when operations are attempted
372+ expect ( ( ) => db . select ( ) ) . toThrow ( 'Database not available. Please complete the setup process at /setup first.' ) ;
373+ expect ( ( ) => db . insert ( ) ) . toThrow ( 'Database not available. Please complete the setup process at /setup first.' ) ;
374+ expect ( ( ) => db . update ( ) ) . toThrow ( 'Database not available. Please complete the setup process at /setup first.' ) ;
375+ expect ( ( ) => db . delete ( ) ) . toThrow ( 'Database not available. Please complete the setup process at /setup first.' ) ;
359376 } ) ;
360377
361378 // This test is removed as it has issues with state persistence between test isolation
@@ -375,11 +392,14 @@ describe('Database Service (db/index.ts)', () => {
375392 } ) ;
376393
377394 describe ( 'executeDbOperation' , ( ) => {
378- it ( 'should throw when database is not initialized' , ( ) => {
379- const operation = vi . fn ( ) ;
395+ it ( 'should throw when database operations are attempted via proxy' , ( ) => {
396+ const operation = vi . fn ( ( db , schema ) => {
397+ // This will trigger the proxy error when trying to use db operations
398+ return db . select ( ) ;
399+ } ) ;
380400
381- expect ( ( ) => executeDbOperation ( operation ) ) . toThrow ( 'Database not initialized ' ) ;
382- expect ( operation ) . not . toHaveBeenCalled ( ) ;
401+ expect ( ( ) => executeDbOperation ( operation ) ) . toThrow ( 'Database not available. Please complete the setup process at /setup first. ' ) ;
402+ expect ( operation ) . toHaveBeenCalled ( ) ; // Operation is called but throws when using db
383403 } ) ;
384404
385405 // These tests are removed as they have issues with state persistence between test isolation
0 commit comments