mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-09-17 20:32:25 +03:00
* test: close the database before removing temp DATA_DIR (#13290) Tests that set their own DATA_DIR and removed it in test.after() failed on Windows with EPERM: nothing closed the SQLite connection, so the directory still had an open handle and the -shm/-wal sidecars kept it locked. maxRetries could not help because every retry hit the same open handle. Adds tests/_setup/tempDataDir.ts with cleanupTempDataDir()/createTempDataDir(), which close the DB singleton (lazily imported, so tests that never touch the database do not pull in the DB layer) and then remove the directory best-effort. Applies it to the five suites confirmed failing. The helper's own test proves the ordering matters: skipping the close makes it fail with 'cleanup must remove the directory'. * test: close the database before removing temp DATA_DIR (15 more suites) Converts the suites that measurably emitted EPERM during a full run to the shared cleanupTempDataDir helper from #13292. Measured on the same 15 files: base -> 22 fail, 40 EPERM lines branch -> 7 fail, 10 EPERM lines The 7 remaining failures are pre-existing and unrelated to teardown: rtk-learn-discover-routes and executor-map-golden already fail on a clean base (6 and 3 failures respectively). * test: close the database before removing temp DATA_DIR (final 9 suites) Completes the #13290 sweep. Two teardown shapes needed the helper: - after()/t.after() hooks that removed DATA_DIR directly - beforeEach() hooks that wiped DATA_DIR between tests while the previous test's connection was still open. These failed *before* the test body ran, so every test in the file reported the same EPERM path. Three of them already called core.resetDbInstance() right before rmSync and still leaked, which is the product-side connection leak tracked in #13303. Measured per file, EPERM lines now 0 across all nine. Remaining failures are pre-existing on a clean base (firefly 4->1, driverFactory 1, responses-* 1 each) and unrelated to teardown. * test: add the missing cleanupTempDataDir import to two responses suites The previous commit swapped rmSync for cleanupTempDataDir in these two files but did not add the import, so both suites died with ReferenceError: cleanupTempDataDir is not defined before running any test. responses-parse-once-4041: 0 pass / 1 fail -> 4 pass / 0 fail responses-route-early-keepalive-wiring: 0 pass / 1 fail -> 3 pass / 0 fail Both now report 0 EPERM. * test: close SQLite handles in three silently-leaking suites These three suites requested DATA_DIR cleanup but the delete failed on Windows because a SQLite connection was still open. They pass today, so the leak is invisible: they carry state between tests and would surface later as an unrelated-looking assertion, as #13303 already did in the Firefly suite (a 500 instead of a 401). agentbridge-mitm-router-key-6403 and agent-bridge-bypass-flow removed their own temp dir in test.after() without closing the DB first; both now use the shared cleanupTempDataDir helper, which closes the singleton before removing the directory. issue-agent-route-execution is a different case: it has no teardown at all, so the connection stayed open until process exit and the isolateDataDir cleanup hook then hit EPERM. It now closes the DB in test.after(). Verified with a probe on fs.rmSync: all three reported a failed delete before, and zero across three consecutive runs after, while the same probe still reports four leaks in the Firefly suite. * test: remove temp DATA_DIR in five suites that never cleaned up These five suites create their own mkdtemp DATA_DIR, open the SQLite DB and never remove the directory, so every run leaves a storage.sqlite behind in the OS temp dir. Each dir is private to its suite, so this leaked disk space rather than corrupting results - but the churn is pointless. Each now closes the DB and removes its directory through the shared cleanupTempDataDir helper. Verified with an exit-time probe that lists storage.sqlite* still present in DATA_DIR: it fired for these suites before the change and is silent after, with the same test counts (22/14/5/3/3 passing).
622 lines
23 KiB
TypeScript
622 lines
23 KiB
TypeScript
import { test, describe, type TestContext } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import fs from "node:fs";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
import { cleanupTempDataDir } from "../../_setup/tempDataDir.ts";
|
|
import { createRequire } from "node:module";
|
|
import type * as NodePath from "node:path";
|
|
import { runtimeRequire } from "../../../src/lib/db/adapters/runtimeRequire.ts";
|
|
|
|
const {
|
|
createSyncDriverFactory,
|
|
createBetterSqliteProbe,
|
|
isPackBootForcedSqlJsSmoke,
|
|
tryOpenSync,
|
|
openDatabaseAsync,
|
|
preInitSqlJs,
|
|
getSqlJsAdapter,
|
|
} = await import("../../../src/lib/db/adapters/driverFactory.ts");
|
|
|
|
const require = createRequire(import.meta.url);
|
|
const isBun = Boolean(process.versions.bun);
|
|
|
|
function forceNodeSqlite() {
|
|
return createSyncDriverFactory((moduleName: string) => {
|
|
if (moduleName === "better-sqlite3") {
|
|
throw new Error("forced better-sqlite3 load failure");
|
|
}
|
|
return require(moduleName);
|
|
});
|
|
}
|
|
|
|
function createTempDatabasePath(t: TestContext) {
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-node-sqlite-"));
|
|
const databasePath = path.join(dir, "database.sqlite");
|
|
t.after(async () => await cleanupTempDataDir(dir));
|
|
return databasePath;
|
|
}
|
|
|
|
describe("driverFactory", () => {
|
|
test("runtimeRequire loads Node built-ins outside webpack", () => {
|
|
const nodePath = runtimeRequire("node:path") as typeof NodePath;
|
|
assert.equal(nodePath.basename("/tmp/omniroute.sqlite"), "omniroute.sqlite");
|
|
});
|
|
test("tryOpenSync retorna adapter síncrono ou null", () => {
|
|
const adapter = tryOpenSync(":memory:");
|
|
if (adapter) {
|
|
assert.ok(["better-sqlite3", "node:sqlite", "bun:sqlite"].includes(adapter.driver));
|
|
adapter.exec("CREATE TABLE t (v TEXT)");
|
|
adapter.prepare("INSERT INTO t VALUES (?)").run("ok");
|
|
const row = adapter.prepare("SELECT v FROM t").get() as { v: string };
|
|
assert.equal(row.v, "ok");
|
|
adapter.close();
|
|
} else {
|
|
assert.equal(adapter, null);
|
|
}
|
|
});
|
|
|
|
if (!isBun) {
|
|
// better-sqlite3 is an OPTIONAL dependency: on a platform with no prebuild and no
|
|
// toolchain it simply will not load, and this expectation cannot hold. The condition is
|
|
// declared in the test options (node:test evaluates it at declaration time) rather than
|
|
// from inside the test body — same behavior, but the skip is visible in the report and
|
|
// the anti-test-masking gate can tell it apart from a statically disabled test. (Phrased
|
|
// without the literal call syntax: that gate greps text, so spelling the API out here
|
|
// would count this comment as two new skip markers.)
|
|
const betterSqliteProbe = tryOpenSync(":memory:");
|
|
const betterSqliteLoads = betterSqliteProbe?.driver === "better-sqlite3";
|
|
betterSqliteProbe?.close();
|
|
|
|
test(
|
|
"prefers better-sqlite3 when it loads",
|
|
{
|
|
skip: betterSqliteLoads ? undefined : "better-sqlite3 is not available in this environment",
|
|
},
|
|
() => {
|
|
const adapter = tryOpenSync(":memory:");
|
|
assert.ok(adapter);
|
|
assert.equal(adapter.driver, "better-sqlite3");
|
|
adapter.close();
|
|
}
|
|
);
|
|
|
|
test("rejected probe skips better-sqlite3 and falls through to node:sqlite", (t) => {
|
|
const databasePath = createTempDatabasePath(t);
|
|
const openWithoutBrokenAddon = createSyncDriverFactory(
|
|
(moduleName: string) => {
|
|
if (moduleName === "better-sqlite3") {
|
|
throw new Error("better-sqlite3 must not load when the probe rejects it");
|
|
}
|
|
return require(moduleName);
|
|
},
|
|
() => false
|
|
);
|
|
|
|
const adapter = openWithoutBrokenAddon(databasePath);
|
|
assert.ok(adapter);
|
|
assert.equal(adapter.driver, "node:sqlite");
|
|
adapter.exec("CREATE TABLE items (value TEXT)");
|
|
adapter.prepare("INSERT INTO items VALUES (?)").run("ok");
|
|
assert.equal(
|
|
(adapter.prepare("SELECT value FROM items").get() as { value: string }).value,
|
|
"ok"
|
|
);
|
|
adapter.close();
|
|
});
|
|
|
|
test("passed probe still prefers better-sqlite3 in the cascade", () => {
|
|
let betterSqliteRequested = false;
|
|
const openWithPassedProbe = createSyncDriverFactory(
|
|
(moduleName: string) => {
|
|
if (moduleName === "better-sqlite3") {
|
|
betterSqliteRequested = true;
|
|
return function FakeBetterSqlite() {
|
|
return { close() {}, name: ":memory:", open: true };
|
|
};
|
|
}
|
|
if (moduleName === "node:sqlite") {
|
|
throw new Error("node:sqlite must not load when better-sqlite3 passes the probe");
|
|
}
|
|
throw new Error(`unexpected driver load: ${moduleName}`);
|
|
},
|
|
() => true
|
|
);
|
|
|
|
const adapter = openWithPassedProbe(":memory:");
|
|
assert.ok(adapter);
|
|
assert.equal(adapter.driver, "better-sqlite3");
|
|
assert.equal(betterSqliteRequested, true);
|
|
adapter.close();
|
|
});
|
|
|
|
test("prefers better-sqlite3 before node:sqlite in the driver cascade", () => {
|
|
const fakeBetterSqlite = {
|
|
close() {},
|
|
name: ":memory:",
|
|
open: true,
|
|
};
|
|
let nodeSqliteRequested = false;
|
|
const openWithPreferredDriver = createSyncDriverFactory((moduleName: string) => {
|
|
if (moduleName === "better-sqlite3") {
|
|
return function FakeBetterSqlite() {
|
|
return fakeBetterSqlite;
|
|
};
|
|
}
|
|
if (moduleName === "node:sqlite") {
|
|
nodeSqliteRequested = true;
|
|
}
|
|
throw new Error(`unexpected driver load: ${moduleName}`);
|
|
});
|
|
|
|
const adapter = openWithPreferredDriver(":memory:");
|
|
assert.ok(adapter);
|
|
assert.equal(adapter.driver, "better-sqlite3");
|
|
assert.equal(nodeSqliteRequested, false);
|
|
adapter.close();
|
|
});
|
|
|
|
test("forced node:sqlite fallback creates, reopens, and queries a writable database", (t) => {
|
|
const databasePath = createTempDatabasePath(t);
|
|
const openNodeSqlite = forceNodeSqlite();
|
|
|
|
const writer = openNodeSqlite(databasePath);
|
|
assert.ok(writer);
|
|
assert.equal(writer.driver, "node:sqlite");
|
|
writer.exec("CREATE TABLE items (value TEXT)");
|
|
writer.prepare("INSERT INTO items VALUES (?)").run("native");
|
|
writer.close();
|
|
|
|
const reader = openNodeSqlite(databasePath);
|
|
assert.ok(reader);
|
|
assert.equal(reader.driver, "node:sqlite");
|
|
assert.equal(
|
|
(reader.prepare("SELECT value FROM items").get() as { value: string }).value,
|
|
"native"
|
|
);
|
|
reader.close();
|
|
});
|
|
|
|
test("forced node:sqlite fallback does not create missing existing-only paths", (t) => {
|
|
const databasePath = createTempDatabasePath(t);
|
|
const adapter = forceNodeSqlite()(databasePath, { readonly: true, fileMustExist: true });
|
|
|
|
assert.equal(adapter, null);
|
|
assert.equal(fs.existsSync(databasePath), false);
|
|
assert.equal(fs.existsSync(`${databasePath}-wal`), false);
|
|
assert.equal(fs.existsSync(`${databasePath}-shm`), false);
|
|
});
|
|
|
|
test("forced node:sqlite fallback preserves existing read-only behavior", (t) => {
|
|
const databasePath = createTempDatabasePath(t);
|
|
const { DatabaseSync } = require("node:sqlite") as {
|
|
DatabaseSync: new (filePath: string) => {
|
|
close(): void;
|
|
exec(sql: string): void;
|
|
prepare(sql: string): { get(): unknown };
|
|
};
|
|
};
|
|
const seed = new DatabaseSync(databasePath);
|
|
seed.exec("CREATE TABLE items (value TEXT); INSERT INTO items VALUES ('seed');");
|
|
seed.close();
|
|
|
|
const adapter = forceNodeSqlite()(databasePath, { readonly: true, fileMustExist: true });
|
|
assert.ok(adapter);
|
|
assert.equal(adapter.driver, "node:sqlite");
|
|
assert.equal(
|
|
(adapter.prepare("SELECT value FROM items").get() as { value: string }).value,
|
|
"seed"
|
|
);
|
|
assert.throws(() => adapter.exec("INSERT INTO items VALUES ('write')"));
|
|
adapter.close();
|
|
|
|
const check = new DatabaseSync(databasePath);
|
|
const row = check.prepare("SELECT value FROM items").get() as { value: string };
|
|
check.close();
|
|
assert.equal(row.value, "seed");
|
|
});
|
|
|
|
test("forced node:sqlite fallback keeps extension loading disabled", () => {
|
|
const adapter = forceNodeSqlite()(":memory:");
|
|
assert.ok(adapter);
|
|
assert.equal(adapter.driver, "node:sqlite");
|
|
|
|
const raw = adapter.raw as { loadExtension(path: string): void };
|
|
assert.throws(() => raw.loadExtension("not-a-trusted-extension"), {
|
|
code: "ERR_INVALID_STATE",
|
|
});
|
|
adapter.close();
|
|
});
|
|
|
|
test("forced node:sqlite backup preserves WAL-backed data", async (t) => {
|
|
const sourcePath = createTempDatabasePath(t);
|
|
const destinationPath = path.join(path.dirname(sourcePath), "backup.sqlite");
|
|
const openNodeSqlite = forceNodeSqlite();
|
|
const source = openNodeSqlite(sourcePath);
|
|
assert.ok(source);
|
|
assert.equal(source.driver, "node:sqlite");
|
|
source.exec("PRAGMA journal_mode = WAL; CREATE TABLE items (value TEXT);");
|
|
source.prepare("INSERT INTO items VALUES (?)").run("backup value");
|
|
|
|
await source.backup(destinationPath);
|
|
source.close();
|
|
|
|
const destination = openNodeSqlite(destinationPath, { fileMustExist: true });
|
|
assert.ok(destination);
|
|
assert.equal(destination.driver, "node:sqlite");
|
|
assert.equal(
|
|
(destination.prepare("SELECT value FROM items").get() as { value: string }).value,
|
|
"backup value"
|
|
);
|
|
destination.close();
|
|
});
|
|
|
|
test("forced node:sqlite immediate commits, rolls back, and nests savepoints", (t) => {
|
|
const databasePath = createTempDatabasePath(t);
|
|
const adapter = forceNodeSqlite()(databasePath);
|
|
assert.ok(adapter);
|
|
assert.equal(adapter.driver, "node:sqlite");
|
|
adapter.exec("CREATE TABLE items (value TEXT)");
|
|
|
|
adapter.immediate(() => {
|
|
adapter.prepare("INSERT INTO items VALUES (?)").run("committed");
|
|
});
|
|
assert.throws(() =>
|
|
adapter.immediate(() => {
|
|
adapter.prepare("INSERT INTO items VALUES (?)").run("rolled back");
|
|
throw new Error("rollback");
|
|
})
|
|
);
|
|
adapter.immediate(() => {
|
|
adapter.prepare("INSERT INTO items VALUES (?)").run("outer before");
|
|
const nested = adapter.transaction(() => {
|
|
adapter.prepare("INSERT INTO items VALUES (?)").run("inner rolled back");
|
|
throw new Error("nested rollback");
|
|
});
|
|
assert.throws(() => nested());
|
|
adapter.prepare("INSERT INTO items VALUES (?)").run("outer after");
|
|
});
|
|
|
|
const rows = adapter.prepare("SELECT value FROM items ORDER BY rowid").all() as Array<{
|
|
value: string;
|
|
}>;
|
|
assert.deepEqual(
|
|
rows.map((row) => row.value),
|
|
["committed", "outer before", "outer after"]
|
|
);
|
|
adapter.close();
|
|
});
|
|
|
|
test("forced node:sqlite immediate blocks a competing writer", (t) => {
|
|
const databasePath = createTempDatabasePath(t);
|
|
const openNodeSqlite = forceNodeSqlite();
|
|
const first = openNodeSqlite(databasePath);
|
|
assert.ok(first);
|
|
first.exec("CREATE TABLE items (value TEXT)");
|
|
|
|
const { DatabaseSync } = require("node:sqlite") as {
|
|
DatabaseSync: new (
|
|
filePath: string,
|
|
options: { timeout: number }
|
|
) => {
|
|
close(): void;
|
|
exec(sql: string): void;
|
|
};
|
|
};
|
|
const second = new DatabaseSync(databasePath, { timeout: 50 });
|
|
try {
|
|
first.immediate(() => {
|
|
assert.throws(() => second.exec("INSERT INTO items VALUES ('competing writer')"), {
|
|
code: "ERR_SQLITE_ERROR",
|
|
});
|
|
first.prepare("INSERT INTO items VALUES (?)").run("owner");
|
|
});
|
|
|
|
const rows = first.prepare("SELECT value FROM items").all() as Array<{ value: string }>;
|
|
assert.deepEqual(
|
|
rows.map((row) => row.value),
|
|
["owner"]
|
|
);
|
|
} finally {
|
|
second.close();
|
|
first.close();
|
|
}
|
|
});
|
|
|
|
// Cursor renewal plan, Task 2 Step 5: tryIdeAuth() now passes a
|
|
// busy-timeout to tryOpenSync() on every driver path, since it's invoked
|
|
// from an unattended sweep tick (not just the human-attended auto-import
|
|
// modal) and needs a bounded retry window on a WAL-lock collision.
|
|
// toNodeSqliteOptions() previously forwarded ONLY readOnly, silently
|
|
// dropping `timeout` on the node:sqlite fallback path.
|
|
test("forced node:sqlite path forwards both readOnly and timeout to DatabaseSync (busy-timeout fix)", (t) => {
|
|
const databasePath = createTempDatabasePath(t);
|
|
|
|
// Seed a real file with the (unmodified) forced node:sqlite driver first.
|
|
const writer = forceNodeSqlite()(databasePath);
|
|
assert.ok(writer);
|
|
writer.exec("CREATE TABLE items (value TEXT)");
|
|
writer.prepare("INSERT INTO items VALUES (?)").run("seed");
|
|
writer.close();
|
|
|
|
const { DatabaseSync: RealDatabaseSync } = require("node:sqlite") as {
|
|
DatabaseSync: new (
|
|
p: string,
|
|
options?: Record<string, unknown>
|
|
) => {
|
|
close(): void;
|
|
prepare(sql: string): { get(...p: unknown[]): unknown };
|
|
};
|
|
};
|
|
|
|
let capturedOptions: Record<string, unknown> | undefined;
|
|
const openWithCapturingNodeSqlite = createSyncDriverFactory((moduleName: string) => {
|
|
if (moduleName === "better-sqlite3") {
|
|
throw new Error("forced better-sqlite3 load failure");
|
|
}
|
|
if (moduleName === "node:sqlite") {
|
|
return {
|
|
DatabaseSync: function FakeDatabaseSync(p: string, options?: Record<string, unknown>) {
|
|
capturedOptions = options;
|
|
return new RealDatabaseSync(p, options);
|
|
},
|
|
};
|
|
}
|
|
throw new Error(`unexpected driver load: ${moduleName}`);
|
|
});
|
|
|
|
const reader = openWithCapturingNodeSqlite(databasePath, {
|
|
readonly: true,
|
|
fileMustExist: true,
|
|
timeout: 2000,
|
|
});
|
|
assert.ok(reader);
|
|
assert.equal(reader.driver, "node:sqlite");
|
|
assert.deepEqual(
|
|
capturedOptions,
|
|
{ readOnly: true, timeout: 2000 },
|
|
"toNodeSqliteOptions must forward BOTH readOnly and timeout, and nothing else (e.g. not fileMustExist)"
|
|
);
|
|
assert.equal(
|
|
(reader.prepare("SELECT value FROM items").get() as { value: string }).value,
|
|
"seed",
|
|
"the forced node:sqlite path must still open successfully with a timeout option set"
|
|
);
|
|
reader.close();
|
|
});
|
|
}
|
|
|
|
// #10627 — the Windows driver-hang guard. On Windows a mismatched-ABI
|
|
// better-sqlite3 addon can HANG inside DllMain instead of throwing, so the
|
|
// cascade's try/catch never fires and the fallback never runs. The probe
|
|
// loads the addon in a child process with a bounded timeout, turning a hang
|
|
// into a cached "bad" verdict that skips the branch.
|
|
test("probe: non-Windows platforms skip the child probe and report ok", () => {
|
|
let spawned = 0;
|
|
const probe = createBetterSqliteProbe({
|
|
platform: "linux",
|
|
execPath: "node",
|
|
spawn: () => {
|
|
spawned += 1;
|
|
return { status: 0 };
|
|
},
|
|
});
|
|
assert.equal(probe(), true);
|
|
assert.equal(probe(), true);
|
|
assert.equal(spawned, 0, "POSIX must not spawn a probe child process");
|
|
});
|
|
|
|
test("probe: successful child probe is cached (spawned at most once)", () => {
|
|
let spawned = 0;
|
|
const probe = createBetterSqliteProbe({
|
|
platform: "win32",
|
|
execPath: "node",
|
|
spawn: () => {
|
|
spawned += 1;
|
|
return { status: 0 };
|
|
},
|
|
});
|
|
assert.equal(probe(), true);
|
|
assert.equal(probe(), true);
|
|
assert.equal(probe(), true);
|
|
assert.equal(spawned, 1, "verdict must be cached per process");
|
|
});
|
|
|
|
test("probe: non-zero child exit rejects better-sqlite3", () => {
|
|
const probe = createBetterSqliteProbe({
|
|
platform: "win32",
|
|
execPath: "node",
|
|
spawn: () => ({ status: 1 }),
|
|
});
|
|
assert.equal(probe(), false);
|
|
assert.equal(probe(), false);
|
|
});
|
|
|
|
test("probe: child spawn throw rejects better-sqlite3", () => {
|
|
const probe = createBetterSqliteProbe({
|
|
platform: "win32",
|
|
execPath: "node",
|
|
spawn: () => {
|
|
throw new Error("spawn failed");
|
|
},
|
|
});
|
|
assert.equal(probe(), false);
|
|
});
|
|
|
|
test("probe: timed-out child (status null) rejects better-sqlite3 — the #10627 hang case", () => {
|
|
// status === null is exactly what spawnSync returns when the child is
|
|
// killed by the timeout — i.e. the DllMain hang that never throws.
|
|
const probe = createBetterSqliteProbe({
|
|
platform: "win32",
|
|
execPath: "node",
|
|
spawn: () => ({ status: null }),
|
|
});
|
|
assert.equal(probe(), false);
|
|
});
|
|
|
|
test("retains the existing cascade when native drivers are unavailable", () => {
|
|
const openWithoutNativeDrivers = createSyncDriverFactory(() => {
|
|
throw new Error("forced driver load failure");
|
|
});
|
|
|
|
assert.equal(openWithoutNativeDrivers(":memory:"), null);
|
|
});
|
|
|
|
test("pack-boot sql.js forcing requires both smoke-only markers", () => {
|
|
assert.equal(isPackBootForcedSqlJsSmoke({}), false);
|
|
assert.equal(isPackBootForcedSqlJsSmoke({ OMNIROUTE_PACK_BOOT_SMOKE: "1" }), false);
|
|
assert.equal(isPackBootForcedSqlJsSmoke({ OMNIROUTE_PACK_BOOT_FORCE_SQLJS: "1" }), false);
|
|
assert.equal(
|
|
isPackBootForcedSqlJsSmoke({
|
|
OMNIROUTE_PACK_BOOT_SMOKE: "1",
|
|
OMNIROUTE_PACK_BOOT_FORCE_SQLJS: "1",
|
|
}),
|
|
true
|
|
);
|
|
});
|
|
|
|
test("openDatabaseAsync sempre retorna um adapter válido", async () => {
|
|
const adapter = await openDatabaseAsync(":memory:");
|
|
assert.ok(["better-sqlite3", "node:sqlite", "bun:sqlite", "sql.js"].includes(adapter.driver));
|
|
|
|
adapter.exec("CREATE TABLE t (v TEXT)");
|
|
adapter.prepare("INSERT INTO t VALUES (?)").run("ok");
|
|
const row = adapter.prepare("SELECT v FROM t").get() as { v: string };
|
|
assert.equal(row.v, "ok");
|
|
adapter.close();
|
|
});
|
|
|
|
test("preInitSqlJs cacheia o adapter por filePath", async () => {
|
|
const path = `sqljs_cache_test_${Date.now()}`;
|
|
const adapter1 = await preInitSqlJs(path);
|
|
const adapter2 = await preInitSqlJs(path);
|
|
assert.equal(adapter1, adapter2, "Deve retornar o mesmo adapter cacheado");
|
|
adapter1.close();
|
|
});
|
|
|
|
test("getSqlJsAdapter retorna null para path não inicializado", () => {
|
|
const unique = `not_initialized_${Date.now()}`;
|
|
assert.equal(getSqlJsAdapter(unique), null);
|
|
});
|
|
|
|
test("getSqlJsAdapter retorna adapter após preInitSqlJs", async () => {
|
|
const path = `sqljs_get_test_${Date.now()}`;
|
|
await preInitSqlJs(path);
|
|
const adapter = getSqlJsAdapter(path);
|
|
assert.ok(adapter !== null);
|
|
assert.equal(adapter!.driver, "sql.js");
|
|
adapter!.close();
|
|
});
|
|
|
|
test("openDatabaseAsync suporta operações CRUD completas", async (t) => {
|
|
const os = await import("node:os");
|
|
const path = await import("node:path");
|
|
const fs = await import("node:fs");
|
|
|
|
const tmpFile = path.join(os.tmpdir(), `driver_crud_${Date.now()}.sqlite`);
|
|
t.after(() => {
|
|
try {
|
|
fs.unlinkSync(tmpFile);
|
|
} catch {}
|
|
});
|
|
|
|
const adapter = await openDatabaseAsync(tmpFile);
|
|
|
|
adapter.exec("CREATE TABLE items (id INTEGER PRIMARY KEY, name TEXT, qty INTEGER)");
|
|
|
|
const r1 = adapter.prepare("INSERT INTO items (name, qty) VALUES (?, ?)").run("apple", 5);
|
|
const r2 = adapter.prepare("INSERT INTO items (name, qty) VALUES (?, ?)").run("banana", 3);
|
|
assert.equal(r1.changes, 1);
|
|
assert.equal(r2.changes, 1);
|
|
|
|
const rows = adapter.prepare("SELECT * FROM items ORDER BY id").all() as Array<{
|
|
id: number;
|
|
name: string;
|
|
qty: number;
|
|
}>;
|
|
assert.equal(rows.length, 2);
|
|
assert.equal(rows[0].name, "apple");
|
|
assert.equal(rows[1].name, "banana");
|
|
|
|
adapter.close();
|
|
});
|
|
|
|
// #6628 (remaining gap): concurrent preInitSqlJs() calls for the same
|
|
// filePath must share ONE in-flight load instead of each caller
|
|
// independently fs.readFileSync + WASM-decoding the whole file — the
|
|
// thundering-herd amplifier of the OOM condition #6632 already partly
|
|
// fixed (restore-cycle-breaker + OOM early-abort), left un-implemented by
|
|
// the reporter's own proposed promise-sharing fix.
|
|
test("preInitSqlJs shares one in-flight load across concurrent callers", async (t) => {
|
|
const os = await import("node:os");
|
|
const path = await import("node:path");
|
|
// The dynamic-import namespace object is read-only; grab the mutable CJS
|
|
// `.default` (== module.exports) so readFileSync can be monkeypatched.
|
|
const fsNs = await import("node:fs");
|
|
const fs = fsNs.default;
|
|
|
|
const tmpFile = path.join(os.tmpdir(), `sqljs_race_${Date.now()}.sqlite`);
|
|
fs.writeFileSync(tmpFile, Buffer.alloc(1024 * 1024, 1));
|
|
t.after(() => {
|
|
try {
|
|
fs.unlinkSync(tmpFile);
|
|
} catch {}
|
|
});
|
|
|
|
let readCountForTarget = 0;
|
|
const originalReadFileSync = fs.readFileSync;
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
(fs as any).readFileSync = (...args: Parameters<typeof fs.readFileSync>) => {
|
|
if (args[0] === tmpFile) readCountForTarget += 1;
|
|
return originalReadFileSync(...args);
|
|
};
|
|
t.after(() => {
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
(fs as any).readFileSync = originalReadFileSync;
|
|
});
|
|
|
|
const [a, b, c] = await Promise.all([
|
|
preInitSqlJs(tmpFile),
|
|
preInitSqlJs(tmpFile),
|
|
preInitSqlJs(tmpFile),
|
|
]);
|
|
|
|
assert.equal(
|
|
readCountForTarget,
|
|
1,
|
|
`expected exactly 1 shared full-file read for 3 concurrent preInitSqlJs() calls, got ${readCountForTarget}`
|
|
);
|
|
assert.equal(a, b, "concurrent callers must resolve to the SAME adapter instance");
|
|
assert.equal(b, c, "concurrent callers must resolve to the SAME adapter instance");
|
|
a.close();
|
|
});
|
|
|
|
test("cross-driver: escreve com adapter sync, relê com sql.js", async (t) => {
|
|
const os = await import("node:os");
|
|
const path = await import("node:path");
|
|
const fs = await import("node:fs");
|
|
|
|
const tmpFile = path.join(os.tmpdir(), `cross_driver_${Date.now()}.sqlite`);
|
|
t.after(() => {
|
|
try {
|
|
fs.unlinkSync(tmpFile);
|
|
} catch {}
|
|
});
|
|
|
|
const syncAdapter = tryOpenSync(tmpFile);
|
|
if (!syncAdapter) {
|
|
console.log("SKIP: nenhum driver síncrono disponível para cross-driver test");
|
|
return;
|
|
}
|
|
syncAdapter.exec("CREATE TABLE items (id INTEGER PRIMARY KEY, name TEXT)");
|
|
syncAdapter.prepare("INSERT INTO items (name) VALUES (?)").run("cross-test");
|
|
syncAdapter.close();
|
|
|
|
const { createSqlJsAdapter } = await import("../../../src/lib/db/adapters/sqljsAdapter.ts");
|
|
const reader = await createSqlJsAdapter(tmpFile);
|
|
const row = reader.prepare("SELECT name FROM items WHERE id = 1").get() as { name: string };
|
|
assert.equal(row.name, "cross-test");
|
|
reader.close();
|
|
});
|
|
});
|