|
| 1 | +import { test } from 'node:test'; |
| 2 | +import * as assert from 'node:assert/strict'; |
| 3 | +import * as fs from 'node:fs'; |
| 4 | +import * as os from 'node:os'; |
| 5 | +import * as path from 'node:path'; |
| 6 | +import { spawnSync } from 'node:child_process'; |
| 7 | + |
| 8 | +test('consumer import by package name works', async () => { |
| 9 | + // Create temp project with node_modules alias to this repo |
| 10 | + const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'sqlite3-vec-import-')); |
| 11 | + const nm = path.join(tmp, 'node_modules', '@dao-xyz'); |
| 12 | + fs.mkdirSync(nm, { recursive: true }); |
| 13 | + const linkTarget = process.cwd(); |
| 14 | + const pkgPath = path.join(nm, 'sqlite3-vec'); |
| 15 | + try { |
| 16 | + fs.symlinkSync(linkTarget, pkgPath, 'dir'); |
| 17 | + } catch (e) { |
| 18 | + // Fallback: copy if symlink not permitted |
| 19 | + fs.cpSync(linkTarget, pkgPath, { recursive: true, dereference: true }); |
| 20 | + } |
| 21 | + |
| 22 | + // Write an ESM script which imports the package by name |
| 23 | + const runner = path.join(tmp, 'runner.mjs'); |
| 24 | + fs.writeFileSync( |
| 25 | + runner, |
| 26 | + `import { createDatabase, resolveNativeExtensionPath } from '@dao-xyz/sqlite3-vec'; |
| 27 | + const db = await createDatabase({}); |
| 28 | + await db.open(); |
| 29 | + const s = await db.prepare('select sqlite_version() as v'); |
| 30 | + const row = s.get?.([]); |
| 31 | + if (!row || !(row.v || row[0])) throw new Error('no sqlite_version'); |
| 32 | + // Not asserting ext presence; just ensure the API is callable |
| 33 | + void resolveNativeExtensionPath(); |
| 34 | + await db.close(); |
| 35 | + console.log('ok'); |
| 36 | + `, |
| 37 | + ); |
| 38 | + |
| 39 | + const res = spawnSync(process.execPath, [runner], { |
| 40 | + cwd: tmp, |
| 41 | + stdio: 'pipe', |
| 42 | + env: { ...process.env, NODE_ENV: 'test' }, |
| 43 | + encoding: 'utf8', |
| 44 | + }); |
| 45 | + if (res.status !== 0) { |
| 46 | + console.error('[import-name] stderr:', res.stderr); |
| 47 | + console.error('[import-name] stdout:', res.stdout); |
| 48 | + } |
| 49 | + assert.equal(res.status, 0); |
| 50 | + assert.match(res.stdout || '', /ok/); |
| 51 | +}); |
0 commit comments