@@ -3,19 +3,21 @@ import { afterEach, describe, expect, it, vi } from 'vitest'
33import type { DatabaseErrorResponse } from '../errors.js'
44import type { ApplicationContext } from '../index.js'
55
6- type Handler = ( data : unknown ) => Promise < unknown >
7-
86type MockClient = {
97 queries : Array < { sql : string ; values ?: unknown [ ] } >
108 query : ReturnType < typeof vi . fn >
119 release : ReturnType < typeof vi . fn >
1210}
1311
1412type MockedEnvironment = {
15- app : ApplicationContext ,
13+ app : ApplicationContext
1614 messaging : ReturnType < typeof setupLoopbackMessaging >
1715 clients : MockClient [ ]
18- pools : Array < { config : Record < string , unknown > ; ended : boolean ; queries : Array < { sql : string ; values ?: unknown [ ] } > } >
16+ pools : Array < {
17+ config : Record < string , unknown >
18+ ended : boolean
19+ queries : Array < { sql : string ; values ?: unknown [ ] } >
20+ } >
1921}
2022
2123function createMockClient ( rows : unknown [ ] ) : MockClient {
@@ -31,14 +33,11 @@ function createMockClient(rows: unknown[]): MockClient {
3133 return client
3234}
3335
34- async function loadApp ( options : {
35- env ?: Record < string , string >
36- queryRows ?: unknown [ ]
37- tenantRows ?: unknown [ ]
38- } = { } ) : Promise < MockedEnvironment > {
36+ async function loadApp (
37+ options : { env ?: Record < string , string > ; queryRows ?: unknown [ ] ; tenantRows ?: unknown [ ] } = { }
38+ ) : Promise < MockedEnvironment > {
3939 vi . resetModules ( )
4040
41- const handlers = new Map < string , Handler > ( )
4241 const clients : MockClient [ ] = [ ]
4342 const pools : MockedEnvironment [ 'pools' ] = [ ]
4443 const queryRows = options . queryRows || [ { ok : true } ]
@@ -100,7 +99,7 @@ async function loadApp(options: {
10099 } ) )
101100
102101 // We need dynamic import due to the mocking of PostgreSQL modules above
103- const { create} = await import ( '../index.js' )
102+ const { create } = await import ( '../index.js' )
104103
105104 const messaging = setupLoopbackMessaging ( 'db' )
106105 const app = create ( )
@@ -120,7 +119,7 @@ describe('database Watt application messaging handlers', () => {
120119 it ( 'registers prefixed handlers and exposes no server' , async ( ) => {
121120 const { app } = await loadApp ( )
122121
123- expect ( app . isBackgroundApplication ) . toBe ( true )
122+ expect ( app . isBackgroundApplication ) . toBe ( true )
124123 } )
125124
126125 it ( 'executes stateless queries against the single-tenant destination' , async ( ) => {
@@ -138,6 +137,19 @@ describe('database Watt application messaging handlers', () => {
138137 expect ( clients [ 0 ] . release ) . toHaveBeenCalledWith ( undefined )
139138 } )
140139
140+ it ( 'marks single-tenant DATABASE_POOL_URL destinations as external pools' , async ( ) => {
141+ const { messaging, pools } = await loadApp ( {
142+ env : { DATABASE_POOL_URL : 'postgres://pooler' } ,
143+ } )
144+
145+ await messaging . send ( 'database' , 'database.query' , {
146+ destination : 'default' ,
147+ sql : 'SELECT 1' ,
148+ } )
149+
150+ expect ( pools [ 0 ] . config ) . toMatchObject ( { connectionString : 'postgres://pooler' } )
151+ } )
152+
141153 it ( 'returns validation errors from malformed requests' , async ( ) => {
142154 const { messaging } = await loadApp ( )
143155
@@ -200,7 +212,9 @@ describe('database Watt application messaging handlers', () => {
200212 lockId : begin . lockId ,
201213 sql : 'SELECT 1' ,
202214 } )
203- const commit = await messaging . send ( 'database' , 'database.commitTransaction' , { lockId : begin . lockId } )
215+ const commit = await messaging . send ( 'database' , 'database.commitTransaction' , {
216+ lockId : begin . lockId ,
217+ } )
204218
205219 expect ( commit ) . toEqual ( { committed : true } )
206220 expect ( clients [ 0 ] . queries . map ( ( query ) => query . sql ) ) . toEqual ( [
@@ -300,5 +314,3 @@ describe('database Watt application messaging handlers', () => {
300314 } )
301315 } )
302316} )
303-
304-
0 commit comments