mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-09-18 21:02:50 +03:00
Merged after a maintainer rework that kept every one of @HouMinXi's commits intact. **What the rework added on top of the contribution:** the new DB health-check behaviour is gated behind a default-off feature flag (`src/shared/constants/featureFlagDefinitions.ts`, `defaultValue: "false"`), documented in `docs/reference/FEATURE_FLAGS.md` with the description key carried into all 66 locales, so the release default is unchanged and the new bounds only apply when an operator opts in. The optional-FTS5 migration set was reconciled by hand with the "180" entry that landed meanwhile (`src/lib/db/migrationRunner/constants.ts`). **Carried from your rebased head:** the `/api/db/health` local-only classification in `src/server/authz/routeGuard.ts` plus its `routeGuard` assertion — `runManagedDbHealthCheck()` forks native diagnostics into a child process, so Hard Rules #15/#17 apply. Re-verified here: 37 pass / 0 fail. Validated as a combined board first (this PR merged with the 21 siblings of the same wave on the release tip): eslint with the frozen suppressions, typecheck:core, check:open-sse-typecheck, complexity, cognitive-complexity, changelog-integrity, i18n new-key coverage, docs-counts, docs-sync, migration-numbering, provider-consistency and a duplicate-identifier audit all green, plus 176 passing / 0 failing focused node:test cases across the 25 test files the wave touches and the dashboard test under Vitest (2/0). Then re-validated alone on the fresh tip before this merge: conflicts re-resolved, file sizes rebaselined for this PR's own growth, eslint and this PR's focused tests re-run. Thank you for the depth of this one — the resource-bounds suite and the sql.js startup/backup coverage are the kind of tests that keep a database layer honest.
218 lines
6.7 KiB
TypeScript
218 lines
6.7 KiB
TypeScript
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import fs from "node:fs";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
|
|
const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-db-core-ext-"));
|
|
process.env.DATA_DIR = TEST_DATA_DIR;
|
|
|
|
const core = await import("../../src/lib/db/core.ts");
|
|
|
|
function cleanupGlobalDb() {
|
|
try {
|
|
if ((globalThis as any).__omnirouteDb?.open) {
|
|
(globalThis as any).__omnirouteDb.close();
|
|
}
|
|
} catch {}
|
|
delete (globalThis as any).__omnirouteDb;
|
|
}
|
|
|
|
async function resetStorage() {
|
|
cleanupGlobalDb();
|
|
core.resetDbInstance();
|
|
for (let attempt = 0; attempt < 10; attempt++) {
|
|
try {
|
|
if (fs.existsSync(TEST_DATA_DIR)) {
|
|
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
|
|
}
|
|
break;
|
|
} catch {
|
|
await new Promise((r) => setTimeout(r, 50 * (attempt + 1)));
|
|
}
|
|
}
|
|
fs.mkdirSync(TEST_DATA_DIR, { recursive: true });
|
|
}
|
|
|
|
test("isNativeSqliteLoadError returns false for non-error values", () => {
|
|
assert.equal(core.isNativeSqliteLoadError(null), false);
|
|
assert.equal(core.isNativeSqliteLoadError(undefined), false);
|
|
assert.equal(core.isNativeSqliteLoadError("string"), false);
|
|
assert.equal(core.isNativeSqliteLoadError(123), false);
|
|
});
|
|
|
|
test("isNativeSqliteLoadError returns false for generic errors", () => {
|
|
assert.equal(core.isNativeSqliteLoadError(new Error("generic")), false);
|
|
});
|
|
|
|
test("isNativeSqliteLoadError returns true for native module errors", () => {
|
|
const err = new Error("Module did not self-register");
|
|
assert.equal(core.isNativeSqliteLoadError(err), true);
|
|
});
|
|
|
|
test("getDbInstance returns a database instance", async () => {
|
|
await resetStorage();
|
|
const db = core.getDbInstance();
|
|
assert.ok(db);
|
|
assert.ok(typeof db.prepare === "function");
|
|
});
|
|
|
|
test("getDbInstance returns same instance on second call (singleton)", async () => {
|
|
await resetStorage();
|
|
const db1 = core.getDbInstance();
|
|
const db2 = core.getDbInstance();
|
|
assert.equal(db1, db2);
|
|
});
|
|
|
|
test("resetDbInstance allows getting a fresh instance", async () => {
|
|
await resetStorage();
|
|
core.getDbInstance();
|
|
core.resetDbInstance();
|
|
const db2 = core.getDbInstance();
|
|
assert.ok(db2);
|
|
assert.ok(typeof db2.prepare === "function");
|
|
});
|
|
|
|
test("closeDbInstance closes the database", async () => {
|
|
await resetStorage();
|
|
core.getDbInstance();
|
|
const result = core.closeDbInstance();
|
|
assert.ok(typeof result === "boolean");
|
|
});
|
|
|
|
test("closeDbInstance returns false when no instance", async () => {
|
|
await resetStorage();
|
|
core.resetDbInstance();
|
|
delete (globalThis as any).__omnirouteDb;
|
|
const result = core.closeDbInstance();
|
|
assert.ok(typeof result === "boolean");
|
|
});
|
|
|
|
test("getDriverInfo returns driver info object", async () => {
|
|
await resetStorage();
|
|
core.getDbInstance();
|
|
const info = core.getDriverInfo();
|
|
assert.ok(info === null || typeof info === "object");
|
|
if (info) {
|
|
assert.ok(typeof info.driver === "string");
|
|
}
|
|
});
|
|
|
|
test("setAutoVacuum and getAutoVacuumMode round-trip", async () => {
|
|
await resetStorage();
|
|
core.getDbInstance();
|
|
core.setAutoVacuum("FULL");
|
|
const mode = core.getAutoVacuumMode();
|
|
assert.equal(mode, "FULL");
|
|
});
|
|
|
|
test("setAutoVacuum accepts NONE", async () => {
|
|
await resetStorage();
|
|
core.getDbInstance();
|
|
core.setAutoVacuum("NONE");
|
|
const mode = core.getAutoVacuumMode();
|
|
assert.equal(mode, "NONE");
|
|
});
|
|
|
|
test("setAutoVacuum accepts INCREMENTAL", async () => {
|
|
await resetStorage();
|
|
core.getDbInstance();
|
|
core.setAutoVacuum("INCREMENTAL");
|
|
const mode = core.getAutoVacuumMode();
|
|
assert.equal(mode, "INCREMENTAL");
|
|
});
|
|
|
|
test("runManualVacuum returns success result", async () => {
|
|
await resetStorage();
|
|
core.getDbInstance();
|
|
const result = core.runManualVacuum();
|
|
assert.ok(typeof result === "object");
|
|
assert.ok(typeof result.success === "boolean");
|
|
assert.ok(typeof result.duration === "number");
|
|
});
|
|
|
|
test("runManagedDbHealthCheck returns health info", async () => {
|
|
await resetStorage();
|
|
core.getDbInstance();
|
|
const result = await core.runManagedDbHealthCheck();
|
|
assert.ok(typeof result === "object");
|
|
});
|
|
|
|
test("runManagedDbHealthCheck with autoRepair option", async () => {
|
|
await resetStorage();
|
|
core.getDbInstance();
|
|
const result = await core.runManagedDbHealthCheck({ autoRepair: true });
|
|
assert.ok(typeof result === "object");
|
|
});
|
|
|
|
test("setPageSize accepts valid page sizes", async () => {
|
|
await resetStorage();
|
|
core.getDbInstance();
|
|
assert.doesNotThrow(() => core.setPageSize(4096));
|
|
});
|
|
|
|
test("toSnakeCase converts camelCase to snake_case", () => {
|
|
assert.equal(core.toSnakeCase("camelCase"), "camel_case");
|
|
assert.equal(core.toSnakeCase("testCase"), "test_case");
|
|
assert.equal(core.toSnakeCase("already"), "already");
|
|
});
|
|
|
|
test("toCamelCase converts snake_case to camelCase", () => {
|
|
assert.equal(core.toCamelCase("snake_case"), "snakeCase");
|
|
assert.equal(core.toCamelCase("test_case"), "testCase");
|
|
assert.equal(core.toCamelCase("already"), "already");
|
|
assert.equal(core.toCamelCase("a"), "a");
|
|
});
|
|
|
|
test("objToSnake converts object keys to snake_case", () => {
|
|
const result = core.objToSnake({ camelCase: 1, anotherKey: 2 }) as any;
|
|
assert.equal(result.camel_case, 1);
|
|
assert.equal(result.another_key, 2);
|
|
});
|
|
|
|
test("objToSnake handles null/undefined/primitives", () => {
|
|
assert.equal(core.objToSnake(null), null);
|
|
assert.equal(core.objToSnake(undefined), undefined);
|
|
assert.equal(core.objToSnake(42), 42);
|
|
assert.equal(core.objToSnake("str"), "str");
|
|
});
|
|
|
|
test("rowToCamel converts object keys to camelCase", () => {
|
|
const result = core.rowToCamel({ snake_case: 1, another_key: 2 });
|
|
assert.ok(result);
|
|
assert.equal(result.snakeCase, 1);
|
|
assert.equal(result.anotherKey, 2);
|
|
});
|
|
|
|
test("rowToCamel returns null for null/undefined", () => {
|
|
assert.equal(core.rowToCamel(null), null);
|
|
assert.equal(core.rowToCamel(undefined), null);
|
|
});
|
|
|
|
test("rowToCamel handles isActive conversion", () => {
|
|
const result = core.rowToCamel({ is_active: 1 });
|
|
assert.ok(result);
|
|
assert.equal(result.isActive, true);
|
|
});
|
|
|
|
test("rowToCamel handles is_active=0 as false", () => {
|
|
const result = core.rowToCamel({ is_active: 0 });
|
|
assert.ok(result);
|
|
assert.equal(result.isActive, false);
|
|
});
|
|
|
|
test("cleanNulls removes null values from object", () => {
|
|
const result = core.cleanNulls({ a: 1, b: null, c: "hello", d: null });
|
|
assert.deepEqual(result, { a: 1, c: "hello" });
|
|
});
|
|
|
|
test("cleanNulls handles nested objects", () => {
|
|
const result = core.cleanNulls({ a: { b: null, c: 1 }, d: null });
|
|
assert.deepEqual(result, { a: { b: null, c: 1 } });
|
|
});
|
|
|
|
test("cleanNulls returns empty object for null input", () => {
|
|
const result = core.cleanNulls(null);
|
|
assert.deepEqual(result, {});
|
|
});
|