Files
OmniRoute/tests/unit/db-sqljs-full-startup.test.ts
Bob.Hou e7999c477b fix(db): bound health scans and isolate native diagnostics (#13717)
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.
2026-09-16 12:59:45 -03:00

56 lines
2.1 KiB
TypeScript

import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { spawnSync } from "node:child_process";
import test from "node:test";
// Separate processes exercise a real fallback-to-native restart, not a mock
// capability flag on an already initialized connection.
test("sql.js defers dependent FTS migrations until a native restart", () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "sqljs-full-startup-"));
const code = `
import assert from 'node:assert/strict';
const core = await import('./src/lib/db/core.ts');
try {
await core.ensureDbInitialized();
const db = core.getDbInstance();
const fallback = process.env.OMNIROUTE_PACK_BOOT_FORCE_SQLJS === '1';
assert.equal(db.driver === 'sql.js', fallback);
for (const version of ['022', '023', '178']) {
const applied = db.prepare('SELECT version FROM _omniroute_migrations WHERE version=?').get(version);
assert.equal(Boolean(applied), !fallback, version);
}
if (!fallback) {
assert.ok(db.prepare("SELECT name FROM sqlite_master WHERE name='memory_fts'").get());
assert.ok(db.prepare("SELECT name FROM sqlite_master WHERE name='memory_fts_au'").get());
}
} finally { await core.shutdownDbInstance(); }
`;
try {
for (const forceSqljs of ["1", "0"]) {
const child = spawnSync(
process.execPath,
["--import", "tsx/esm", "--input-type=module", "-e", code],
{
cwd: process.cwd(),
env: {
...process.env,
DATA_DIR: dir,
NODE_ENV: "test",
APP_LOG_TO_FILE: "false",
DISABLE_SQLITE_AUTO_BACKUP: "true",
OMNIROUTE_PACK_BOOT_SMOKE: "1",
OMNIROUTE_PACK_BOOT_FORCE_SQLJS: forceSqljs,
},
encoding: "utf8",
timeout: 30000,
}
);
assert.equal(child.status, 0, `driver=${forceSqljs}\n${child.stdout}\n${child.stderr}`);
}
} finally {
fs.rmSync(dir, { recursive: true, force: true });
}
});