1- import { execSync } from "child_process" ;
2- import * as fs from "fs" ;
3- import * as path from "path" ;
1+ import { execSync } from "node: child_process" ;
2+ import * as fs from "node: fs" ;
3+ import * as path from "node: path" ;
44import { DatabaseSync } from "../src" ;
55import { getDirname , rm } from "./test-utils" ;
66
7+ // Build the test extension at module load time so we can conditionally skip tests
8+ const extensionDir = path . join ( getDirname ( ) , "fixtures" , "test-extension" ) ;
9+
10+ // Track extension build status
11+ let testExtensionPath : string | undefined ;
12+ let extensionBuildError : string | undefined ;
13+
14+ function buildExtension ( ) : void {
15+ // Try to build the extension - but don't throw on failure
16+ // On some platforms (e.g., ARM64 QEMU emulation), native builds may fail
17+ try {
18+ execSync ( "node build.js" , { cwd : extensionDir , stdio : "inherit" } ) ;
19+ } catch ( error ) {
20+ extensionBuildError = `Failed to build test extension: ${ error } ` ;
21+ return ;
22+ }
23+
24+ // SQLite automatically adds the platform-specific extension, so we just provide the base name
25+ const basePath = path . join ( extensionDir , "test_extension" ) ;
26+
27+ // Verify the extension was built - check with actual file extension
28+ let actualExtensionPath : string ;
29+ if ( process . platform === "win32" ) {
30+ actualExtensionPath = basePath + ".dll" ;
31+ } else if ( process . platform === "darwin" ) {
32+ actualExtensionPath = basePath + ".dylib" ;
33+ } else {
34+ actualExtensionPath = basePath + ".so" ;
35+ }
36+
37+ if ( ! fs . existsSync ( actualExtensionPath ) ) {
38+ extensionBuildError = `Test extension not found at ${ actualExtensionPath } ` ;
39+ return ;
40+ }
41+
42+ testExtensionPath = basePath ;
43+ }
44+
45+ // Build at module load time
46+ buildExtension ( ) ;
47+
48+ // Log build status
49+ if ( extensionBuildError ) {
50+ console . warn ( extensionBuildError ) ;
51+ console . warn (
52+ "Tests that require the real extension will be skipped on this platform" ,
53+ ) ;
54+ }
55+
56+ // Conditional describe for tests that require the real extension
57+ const describeWithExtension = testExtensionPath ? describe : describe . skip ;
58+
759describe ( "Extension Loading Tests" , ( ) => {
8- // Build the test extension before running tests
9- let testExtensionPath : string ;
10-
11- beforeAll ( ( ) => {
12- const extensionDir = path . join ( getDirname ( ) , "fixtures" , "test-extension" ) ;
13-
14- // Build the extension
15- try {
16- execSync ( "node build.js" , { cwd : extensionDir , stdio : "inherit" } ) ;
17- } catch ( error ) {
18- console . error ( "Failed to build test extension:" , error ) ;
19- throw new Error ( "Test extension build failed" ) ;
20- }
21-
22- // SQLite automatically adds the platform-specific extension, so we just provide the base name
23- testExtensionPath = path . join ( extensionDir , "test_extension" ) ;
24-
25- // Verify the extension was built - check with actual file extension
26- let actualExtensionPath : string ;
27- if ( process . platform === "win32" ) {
28- actualExtensionPath = testExtensionPath + ".dll" ;
29- } else if ( process . platform === "darwin" ) {
30- actualExtensionPath = testExtensionPath + ".dylib" ;
31- } else {
32- actualExtensionPath = testExtensionPath + ".so" ;
33- }
34-
35- if ( ! fs . existsSync ( actualExtensionPath ) ) {
36- throw new Error ( `Test extension not found at ${ actualExtensionPath } ` ) ;
37- }
38- } ) ;
3960 describe ( "allowExtension option" , ( ) => {
4061 test ( "extension loading is disabled by default" , ( ) => {
4162 const db = new DatabaseSync ( ":memory:" ) ;
@@ -239,14 +260,16 @@ describe("Extension Loading Tests", () => {
239260 } ) ;
240261 } ) ;
241262
242- describe ( "loading real extension" , ( ) => {
263+ // These tests require the real extension to be built
264+ // They will be skipped on platforms where native builds fail (e.g., ARM64 QEMU emulation)
265+ describeWithExtension ( "loading real extension" , ( ) => {
243266 test ( "can load test extension and use its functions" , ( ) => {
244267 const db = new DatabaseSync ( ":memory:" , { allowExtension : true } ) ;
245268 db . enableLoadExtension ( true ) ;
246269
247270 // Load the test extension
248271 expect ( ( ) => {
249- db . loadExtension ( testExtensionPath ) ;
272+ db . loadExtension ( testExtensionPath ! ) ;
250273 } ) . not . toThrow ( ) ;
251274
252275 // Test the version function
@@ -274,7 +297,7 @@ describe("Extension Loading Tests", () => {
274297
275298 // Load with explicit entry point
276299 expect ( ( ) => {
277- db . loadExtension ( testExtensionPath , "sqlite3_testextension_init" ) ;
300+ db . loadExtension ( testExtensionPath ! , "sqlite3_testextension_init" ) ;
278301 } ) . not . toThrow ( ) ;
279302
280303 // Verify it loaded
@@ -291,7 +314,7 @@ describe("Extension Loading Tests", () => {
291314 db . enableLoadExtension ( true ) ;
292315
293316 // Load extension
294- db . loadExtension ( testExtensionPath ) ;
317+ db . loadExtension ( testExtensionPath ! ) ;
295318
296319 // Disable extension loading
297320 db . enableLoadExtension ( false ) ;
@@ -302,7 +325,7 @@ describe("Extension Loading Tests", () => {
302325
303326 // But can't load new extensions
304327 expect ( ( ) => {
305- db . loadExtension ( testExtensionPath ) ;
328+ db . loadExtension ( testExtensionPath ! ) ;
306329 } ) . toThrow ( / E x t e n s i o n l o a d i n g i s n o t e n a b l e d / ) ;
307330
308331 db . close ( ) ;
@@ -311,7 +334,7 @@ describe("Extension Loading Tests", () => {
311334 test ( "extension functions work with various data types" , ( ) => {
312335 const db = new DatabaseSync ( ":memory:" , { allowExtension : true } ) ;
313336 db . enableLoadExtension ( true ) ;
314- db . loadExtension ( testExtensionPath ) ;
337+ db . loadExtension ( testExtensionPath ! ) ;
315338
316339 // Test with integers
317340 const intResult = db . prepare ( "SELECT test_extension_add(42, 8)" ) . get ( ) ;
@@ -345,7 +368,7 @@ describe("Extension Loading Tests", () => {
345368 test ( "extension function errors are properly handled" , ( ) => {
346369 const db = new DatabaseSync ( ":memory:" , { allowExtension : true } ) ;
347370 db . enableLoadExtension ( true ) ;
348- db . loadExtension ( testExtensionPath ) ;
371+ db . loadExtension ( testExtensionPath ! ) ;
349372
350373 // Wrong number of arguments for add
351374 expect ( ( ) => {
@@ -370,7 +393,7 @@ describe("Extension Loading Tests", () => {
370393 db . enableLoadExtension ( true ) ;
371394
372395 // Load extension
373- db . loadExtension ( testExtensionPath ) ;
396+ db . loadExtension ( testExtensionPath ! ) ;
374397
375398 // Create a table and use extension function
376399 db . exec ( "CREATE TABLE test (input TEXT, output TEXT)" ) ;
0 commit comments