@@ -375,16 +375,89 @@ describe("kysely sync helpers", () => {
375375 expect ( prepares . calls ( ) ) . toBe ( 2 ) ;
376376 } ) ;
377377
378+ it ( "preserves close return values and double-close errors" , ( ) => {
379+ const baseline = new DatabaseSync ( ":memory:" ) ;
380+ expect ( baseline . close ( ) ) . toBeUndefined ( ) ;
381+ const baselineError = captureError ( ( ) => baseline . close ( ) ) ;
382+
383+ const cached = new DatabaseSync ( ":memory:" ) ;
384+ enableNodeSqliteKyselyStatementCache ( cached ) ;
385+ expect ( cached . close ( ) ) . toBeUndefined ( ) ;
386+ expect ( errorShape ( captureError ( ( ) => cached . close ( ) ) ) ) . toEqual ( errorShape ( baselineError ) ) ;
387+ } ) ;
388+
389+ it ( "preserves close errors when invalidation is installed after the handle closes" , ( ) => {
390+ const baseline = new DatabaseSync ( ":memory:" ) ;
391+ baseline . close ( ) ;
392+ const baselineError = captureError ( ( ) => baseline . close ( ) ) ;
393+
394+ const cached = new DatabaseSync ( ":memory:" ) ;
395+ cached . close ( ) ;
396+ enableNodeSqliteKyselyStatementCache ( cached ) ;
397+ expect ( errorShape ( captureError ( ( ) => cached . close ( ) ) ) ) . toEqual ( errorShape ( baselineError ) ) ;
398+ } ) ;
399+
400+ it ( "clears cached statements before propagating a close failure" , ( ) => {
401+ const cached = new DatabaseSync ( ":memory:" ) ;
402+ cached . exec ( "create table items (id integer primary key, name text not null)" ) ;
403+ cached . exec ( "insert into items (id, name) values (1, 'Ada')" ) ;
404+ const closeError = new Error ( "synthetic close failure" ) ;
405+ Object . defineProperty ( cached , "close" , {
406+ configurable : true ,
407+ writable : true ,
408+ value ( ) : never {
409+ throw closeError ;
410+ } ,
411+ } ) ;
412+ const db = getNodeSqliteKysely < SyncHelperTestDatabase > ( cached ) ;
413+ const select = db . selectFrom ( "items" ) . selectAll ( ) ;
414+ const prepares = countPrepares ( cached ) ;
415+
416+ try {
417+ expect ( executeSqliteQuerySync ( cached , select ) . rows ) . toEqual ( [ { id : 1 , name : "Ada" } ] ) ;
418+ expect ( executeSqliteQuerySync ( cached , select ) . rows ) . toEqual ( [ { id : 1 , name : "Ada" } ] ) ;
419+ expect ( executeSqliteQuerySync ( cached , select ) . rows ) . toEqual ( [ { id : 1 , name : "Ada" } ] ) ;
420+ expect ( prepares . calls ( ) ) . toBe ( 2 ) ;
421+
422+ expect ( ( ) => cached . close ( ) ) . toThrow ( closeError ) ;
423+ expect ( executeSqliteQuerySync ( cached , select ) . rows ) . toEqual ( [ { id : 1 , name : "Ada" } ] ) ;
424+ expect ( prepares . calls ( ) ) . toBe ( 3 ) ;
425+ } finally {
426+ clearNodeSqliteKyselyCacheForDatabase ( cached ) ;
427+ delete ( cached as { close ?: DatabaseSync [ "close" ] } ) . close ;
428+ cached . close ( ) ;
429+ }
430+ } ) ;
431+
378432 it ( "allows databases and prepared statements to collect after lifecycle clearing" , ( ) => {
379- expect ( runRetentionScenario ( { clearBeforeDrop : true } ) ) . toEqual ( {
433+ expect ( runRetentionScenario ( { cleanup : "clear-and-close" } ) ) . toEqual ( {
380434 databaseCollected : true ,
381435 statementsCollected : 2 ,
382436 statementCount : 2 ,
383437 } ) ;
384438 } ) ;
385439
440+ it ( "allows databases and prepared statements to collect after close" , ( ) => {
441+ expect ( runRetentionScenario ( { cleanup : "close" } ) ) . toEqual ( {
442+ databaseCollected : true ,
443+ statementsCollected : 2 ,
444+ statementCount : 2 ,
445+ } ) ;
446+ } ) ;
447+
448+ it . skipIf ( typeof DatabaseSync . prototype [ Symbol . dispose ] !== "function" ) (
449+ "allows databases and prepared statements to collect after disposal" ,
450+ ( ) => {
451+ expect ( runRetentionScenario ( { cleanup : "dispose" } ) ) . toEqual ( {
452+ databaseCollected : true ,
453+ statementsCollected : 2 ,
454+ statementCount : 2 ,
455+ } ) ;
456+ } ,
457+ ) ;
458+
386459 it ( "retains a cached database when lifecycle clearing is skipped" , ( ) => {
387- expect ( runRetentionScenario ( { clearBeforeDrop : false } ) ) . toEqual ( {
460+ expect ( runRetentionScenario ( { cleanup : "drop" } ) ) . toEqual ( {
388461 databaseCollected : false ,
389462 statementsCollected : 1 ,
390463 statementCount : 2 ,
@@ -432,18 +505,42 @@ function countPrepares(database: DatabaseSync): { calls: () => number } {
432505 return { calls : ( ) => calls } ;
433506}
434507
435- function runRetentionScenario ( options : { clearBeforeDrop : boolean } ) : {
508+ function captureError ( operation : ( ) => void ) : unknown {
509+ try {
510+ operation ( ) ;
511+ } catch ( error ) {
512+ return error ;
513+ }
514+ throw new Error ( "expected operation to throw" ) ;
515+ }
516+
517+ function errorShape ( error : unknown ) : { code : string | undefined ; message : string ; name : string } {
518+ expect ( error ) . toBeInstanceOf ( Error ) ;
519+ const sqliteError = error as Error & { code ?: string } ;
520+ return {
521+ code : sqliteError . code ,
522+ message : sqliteError . message ,
523+ name : sqliteError . name ,
524+ } ;
525+ }
526+
527+ function runRetentionScenario ( options : {
528+ cleanup : "clear-and-close" | "close" | "dispose" | "drop" ;
529+ } ) : {
436530 databaseCollected : boolean ;
437531 statementsCollected : number ;
438532 statementCount : number ;
439533} {
440534 const moduleUrl = new URL ( "./kysely-sync.ts" , import . meta. url ) . href ;
441- const cleanup = options . clearBeforeDrop
442- ? `
535+ const cleanup = {
536+ "clear-and-close" : `
443537 clearNodeSqliteKyselyCacheForDatabase(database);
444538 database.close();
445- `
446- : "" ;
539+ ` ,
540+ close : "database.close();" ,
541+ dispose : "database[Symbol.dispose]();" ,
542+ drop : "" ,
543+ } [ options . cleanup ] ;
447544 const script = `
448545 import { DatabaseSync } from "node:sqlite";
449546 import {
0 commit comments