@@ -92,29 +92,75 @@ suite('DatabaseSync() constructor', () => {
9292} ) ;
9393
9494suite ( 'DatabaseSync.prototype.loadExtension()' , ( ) => {
95+ test ( 'throws if database is not open' , ( t ) => {
96+ const db = new DatabaseSync ( nextDb ( ) , { open : false } ) ;
97+ t . assert . throws ( ( ) => {
98+ db . loadExtension ( ) ;
99+ } , {
100+ code : 'ERR_INVALID_STATE' ,
101+ message : / d a t a b a s e i s n o t o p e n / ,
102+ } ) ;
103+ } ) ;
104+
105+ test ( 'throws if path is not a valid sqlite extension' , ( t ) => {
106+ const db = new DatabaseSync ( nextDb ( ) , {
107+ allowLoadExtension : true ,
108+ } ) ;
109+ // Try to load a non-existent file
110+ const files = [
111+ '/dev/null' ,
112+ path ( 'a.js' ) ,
113+ path ( 'shared-memory.wasm' ) ,
114+ path ( 'crash.wat' ) ,
115+ path ( 'doc_inc_1.md' ) ,
116+ path ( 'utf8-bom.json' ) ,
117+ path ( 'x.txt' ) ,
118+ ] ;
119+ for ( const file of files ) {
120+ t . assert . throws ( ( ) => {
121+ db . loadExtension ( file ) ;
122+ } , {
123+ code : 'ERR_LOAD_SQLITE_EXTENSION' ,
124+ } , `loadExtension("${ file } ") should throw an error` ) ;
125+ }
126+ } ) ;
127+
95128 test ( 'loads an extension' , ( t ) => {
96129 const dbPath = nextDb ( ) ;
97130 const db = new DatabaseSync ( dbPath , { allowLoadExtension : true } ) ;
98131 const supportedPlatforms = [
99- [ " macos" , " x86_64" ] ,
100- [ " windows" , " x86_64" ] ,
101- [ " linux" , " x86_64" ] ,
102- [ " macos" , " aarch64" ]
132+ [ ' macos' , ' x86_64' ] ,
133+ [ ' windows' , ' x86_64' ] ,
134+ [ ' linux' , ' x86_64' ] ,
135+ [ ' macos' , ' aarch64' ] ,
103136 ] ;
104137 function validPlatform ( platform , arch ) {
105138 return (
106- supportedPlatforms . find ( ( [ p , a ] ) => platform == p && arch === a ) !== null
139+ supportedPlatforms . find ( ( [ p , a ] ) => platform = == p && arch === a ) !== null
107140 ) ;
108141 }
142+
143+ function getExtension ( platform , arch ) {
144+ switch ( platform ) {
145+ case 'darwin' :
146+ return arch === 'arm64' ? '.aarch64.dylib' : '.x86_64.dylib' ;
147+ case 'windows' :
148+ return '.dll' ;
149+ case 'linux' :
150+ return '.so' ;
151+ default :
152+ return null ;
153+ }
154+ }
109155 const platform = os . platform ( ) ;
110- const arch = os . arch ( ) ;
156+ const arch = process . arch ;
111157 if ( ! validPlatform ( platform , arch ) ) {
112- t . skip ( ) ;
158+ t . skip ( 'Unsupported platform' ) ;
113159 return ;
114160 }
115- const ext = platform === 'win32' ? 'dll' : platform === 'darwin' ? process . arch === 'arm64' ? '.aarch64.dylib' : '.x86_64.dylib' : 'so' ;
116- const extension = path ( 'sqlite' , `vec0${ ext } ` )
117- t . assert . strictEqual ( db . loadExtension ( extension ) , undefined ) ;
161+ const ext = getExtension ( platform , arch ) ;
162+ const filePath = path ( 'sqlite' , `vec0${ ext } ` ) ;
163+ t . assert . strictEqual ( db . loadExtension ( filePath ) , undefined ) ;
118164 } ) ;
119165} ) ;
120166
0 commit comments