OmniRoute/tests/unit/runtime/sqliteRuntime.test.ts
diegosouzapw 8f915b18b0 feat(runtime): dynamic SQLite runtime installer with 5-step fallback chain
Adds bin/cli/runtime/sqliteRuntime.mjs that resolves better-sqlite3 from:
(1) bundled optionalDependency, (2) ~/.omniroute/runtime/ install,
(3) lazy npm install into runtime dir, (4) node:sqlite stdlib (Node >=22.5),
(5) bundled sql.js WASM. Each native binary is validated against expected
platform magic bytes (ELF/Mach-O/PE) before load.

Adds bin/cli/runtime/magicBytes.mjs with validateBinaryMagic() helper
(9 tests). Adds bin/cli/runtime/index.mjs as warmUpRuntimes() orchestrator.

Adds scripts/postinstall.mjs warm-up hook (non-fatal, skipped in CI).
Integrates it as the last step of scripts/build/postinstall.mjs.

Extends src/lib/db/core.ts with ensureDbInitialized() (async, idempotent)
and getDriverInfo() so the startup orchestrator can await the resolver
before any DB access, enabling graceful degradation without crashing the
process on missing better-sqlite3.

Solves Windows EBUSY error on 'npm install -g omniroute@latest' while the
previous version is still running, and works in environments without C++
build tools or with unreachable npm registry.

Documents OMNIROUTE_SKIP_POSTINSTALL in .env.example and ENVIRONMENT.md.

Ref: 9router/cli/hooks/sqliteRuntime.js (pattern origin).
2026-05-15 00:05:49 -03:00

48 lines
1.8 KiB
TypeScript

import { test } from "node:test";
import assert from "node:assert/strict";
import { existsSync } from "node:fs";
import { join } from "node:path";
import { homedir } from "node:os";
import { loadSqliteRuntime, clearRuntimeCache } from "../../../bin/cli/runtime/sqliteRuntime.mjs";
test("loadSqliteRuntime returns a result with driver and source", async () => {
clearRuntimeCache();
const r = await loadSqliteRuntime();
assert.ok(r, "result is truthy");
assert.ok(typeof r.driver === "object", "driver is object");
assert.ok(typeof r.source === "string", "source is string");
});
test("loaded driver is one of the known kinds", async () => {
clearRuntimeCache();
const r = await loadSqliteRuntime();
assert.ok(
["better-sqlite3", "node-sqlite", "sql-js"].includes(r.driver.kind),
`kind="${r.driver.kind}" must be known`
);
});
test("loadSqliteRuntime caches the result (same object reference)", async () => {
clearRuntimeCache();
const a = await loadSqliteRuntime();
const b = await loadSqliteRuntime();
assert.strictEqual(a, b, "second call returns cached object");
});
test("runtime dir exists after loadSqliteRuntime when runtime source used", async () => {
clearRuntimeCache();
const r = await loadSqliteRuntime();
if (r.source === "runtime-installed-now" || r.source === "runtime") {
const runtimeDir = join(homedir(), ".omniroute", "runtime");
assert.ok(existsSync(runtimeDir), "runtime dir created");
}
});
test("clearRuntimeCache allows fresh resolution", async () => {
clearRuntimeCache();
const first = await loadSqliteRuntime();
clearRuntimeCache();
const second = await loadSqliteRuntime();
// Both should resolve to the same kind, but are different call results
assert.equal(first.driver.kind, second.driver.kind, "same driver kind after cache clear");
});