@@ -19,6 +19,7 @@ import { describe, it, expect, beforeEach, vi } from 'vitest';
1919const mockPoolQuery = vi . fn ( ) ;
2020const mockPoolEnd = vi . fn ( ) ;
2121const mockPoolCtor = vi . fn ( ) ;
22+ const mockPoolOn = vi . fn ( ) ;
2223
2324vi . mock ( 'pg' , ( ) => {
2425 class Pool {
@@ -27,6 +28,7 @@ vi.mock('pg', () => {
2728 }
2829 query = mockPoolQuery ;
2930 end = mockPoolEnd ;
31+ on = mockPoolOn ;
3032 }
3133 return { Pool } ;
3234} ) ;
@@ -114,6 +116,28 @@ describe('PostgreSQL - helper', async () => {
114116 } ) ;
115117 } ) ;
116118
119+ describe ( 'pool error handling' , ( ) => {
120+ it ( 'registers an idle-client error listener that logs without crashing' , async ( ) => {
121+ getDatabaseMock . mockReturnValue ( {
122+ type : 'postgres' ,
123+ enabled : true ,
124+ connectionString : 'postgresql://localhost/x' ,
125+ } ) ;
126+ const errorSpy = vi . spyOn ( console , 'error' ) . mockImplementation ( ( ) => undefined ) ;
127+
128+ await connect ( ) ;
129+
130+ const errorRegistration = mockPoolOn . mock . calls . find ( ( call ) => call [ 0 ] === 'error' ) ;
131+ expect ( errorRegistration ) . toBeDefined ( ) ;
132+
133+ const handler = errorRegistration ! [ 1 ] as ( err : Error ) => void ;
134+ expect ( ( ) => handler ( new Error ( 'connection terminated unexpectedly' ) ) ) . not . toThrow ( ) ;
135+ expect ( errorSpy ) . toHaveBeenCalled ( ) ;
136+
137+ errorSpy . mockRestore ( ) ;
138+ } ) ;
139+ } ) ;
140+
117141 describe ( 'getSessionStore' , ( ) => {
118142 it ( 'throws when connection string is missing — no MemoryStore fallback' , ( ) => {
119143 getDatabaseMock . mockReturnValue ( {
0 commit comments