@@ -12,6 +12,10 @@ import { describe, test, expect, mock, beforeEach, afterEach } from "bun:test"
1212// DuckDB: wrapDuckDBError + lock retry
1313// ---------------------------------------------------------------------------
1414describe ( "DuckDB driver" , ( ) => {
15+ function openCallback ( optsOrCb : any , cb ?: ( err : Error | null ) => void ) : ( err : Error | null ) => void {
16+ return typeof optsOrCb === "function" ? optsOrCb : cb !
17+ }
18+
1519 // Test wrapDuckDBError logic inline (it's a closure, so we test via connect behavior)
1620 describe ( "wrapDuckDBError" , ( ) => {
1721 test ( "wraps SQLITE_BUSY errors with user-friendly message" , async ( ) => {
@@ -28,8 +32,8 @@ describe("DuckDB driver", () => {
2832 mock . module ( "duckdb" , ( ) => ( {
2933 default : {
3034 Database : class {
31- constructor(_path: string, _opts : any, cb: (err: Error | null) => void) {
32- setTimeout(() => cb (null), 0)
35+ constructor ( _path : string , optsOrCb : any , cb ? : ( err : Error | null ) => void ) {
36+ setTimeout ( ( ) => openCallback ( optsOrCb , cb ) ( null ) , 0 )
3337 }
3438 connect ( ) {
3539 return mockDb . connect ( )
@@ -62,8 +66,8 @@ describe("DuckDB driver", () => {
6266 mock . module ( "duckdb" , ( ) => ( {
6367 default : {
6468 Database : class {
65- constructor(_path: string, _opts : any, cb: (err: Error | null) => void) {
66- setTimeout(() => cb (null), 0)
69+ constructor ( _path : string , optsOrCb : any , cb ? : ( err : Error | null ) => void ) {
70+ setTimeout ( ( ) => openCallback ( optsOrCb , cb ) ( null ) , 0 )
6771 }
6872 connect ( ) {
6973 return {
@@ -97,8 +101,8 @@ describe("DuckDB driver", () => {
97101 mock . module ( "duckdb" , ( ) => ( {
98102 default : {
99103 Database : class {
100- constructor(_path: string, _opts : any, cb: (err: Error | null) => void) {
101- setTimeout(() => cb (null), 0)
104+ constructor ( _path : string , optsOrCb : any , cb ? : ( err : Error | null ) => void ) {
105+ setTimeout ( ( ) => openCallback ( optsOrCb , cb ) ( null ) , 0 )
102106 }
103107 connect ( ) {
104108 return {
@@ -135,14 +139,16 @@ describe("DuckDB driver", () => {
135139 mock . module ( "duckdb" , ( ) => ( {
136140 default : {
137141 Database : class {
138- constructor(_path: string, opts: any, cb: (err: Error | null) => void) {
142+ constructor ( _path : string , optsOrCb : any , cb ?: ( err : Error | null ) => void ) {
143+ const opts = typeof optsOrCb === "function" ? undefined : optsOrCb
144+ const done = openCallback ( optsOrCb , cb )
139145 connectAttempts ++
140146 if ( connectAttempts === 1 && ! opts ?. access_mode ) {
141147 // First attempt fails with lock error
142- setTimeout(() => cb (new Error("DUCKDB_LOCKED: file is locked")), 0)
148+ setTimeout ( ( ) => done ( new Error ( "DUCKDB_LOCKED: file is locked" ) ) , 0 )
143149 } else {
144150 // READ_ONLY retry succeeds
145- setTimeout(() => cb (null), 0)
151+ setTimeout ( ( ) => done ( null ) , 0 )
146152 }
147153 }
148154 connect ( ) {
@@ -171,8 +177,8 @@ describe("DuckDB driver", () => {
171177 mock . module ( "duckdb" , ( ) => ( {
172178 default : {
173179 Database : class {
174- constructor(_path: string, _opts : any, cb: (err: Error | null) => void) {
175- setTimeout(() => cb (new Error("DUCKDB_LOCKED")), 0)
180+ constructor ( _path : string , optsOrCb : any , cb ? : ( err : Error | null ) => void ) {
181+ setTimeout ( ( ) => openCallback ( optsOrCb , cb ) ( new Error ( "DUCKDB_LOCKED" ) ) , 0 )
176182 }
177183 connect ( ) {
178184 return { }
@@ -195,6 +201,34 @@ describe("DuckDB driver", () => {
195201 expect ( e . message ) . toBe ( "DUCKDB_LOCKED" )
196202 }
197203 } )
204+
205+ test ( "handles native open callback invoked synchronously" , async ( ) => {
206+ mock . module ( "duckdb" , ( ) => ( {
207+ default : {
208+ Database : class {
209+ constructor ( _path : string , optsOrCb : any , cb ?: ( err : Error | null ) => void ) {
210+ openCallback ( optsOrCb , cb ) ( null )
211+ }
212+ connect ( ) {
213+ return {
214+ all : ( _sql : string , cb : ( err : Error | null , rows : any [ ] ) => void ) => {
215+ cb ( null , [ { ok : 1 } ] )
216+ } ,
217+ }
218+ }
219+ close ( cb : any ) {
220+ if ( cb ) cb ( null )
221+ }
222+ } ,
223+ } ,
224+ } ) )
225+
226+ const { connect } = await import ( "../src/duckdb" )
227+ const connector = await connect ( { type : "duckdb" , path : ":memory:" } )
228+ await connector . connect ( )
229+ expect ( await connector . execute ( "SELECT 1" ) ) . toMatchObject ( { columns : [ "ok" ] , rows : [ [ 1 ] ] , row_count : 1 } )
230+ await connector . close ( )
231+ } )
198232 } )
199233} )
200234
0 commit comments