Files
OmniRoute/tests/unit/db/stats-virtual-table.test.ts
Demiurge The Single ed0d14604d fix(db): tolerate unavailable virtual table modules in stats (#7313)
* chore(ci): add .mergify.yml to main — Mergify only reads config from the default branch (#7168)

* fix(ci): add the auto-enqueue pull_request_rule to the Mergify config (queue_conditions alone are eligibility-only) (#7179)

* fix(ci): migrate Mergify auto-enqueue to merge_protections_settings.auto_merge_conditions (rules-based path is EOL 2026-07-16) (#7216)

* fix(ci): drop Mergify batch settings (batching is a paid-tier feature; free plan queue is serial) (#7220)

* fix(ci): merge queue tolerates the advisory dast-smoke failure (its GH-hosted build hang dequeued every attempt) (#7225)

* fix(db): tolerate unavailable virtual table modules in stats

* test(db): cover missing count rows

* test(ci): make the #6634 selfref guard hermetic — main's copy hard-fails every PR (#7341)

main's copy of this test still does git I/O inside a unit test:

    const baseSrc = git(['show', 'origin/main:' + FILE]);

Runners check out a shallow single ref, so origin/main does not resolve and the
test dies with 'fatal: invalid object name origin/main'. Every PR into main
fails Unit Tests (7/8) on it — today that is #7313, #7315, #7316, #7334, #7336
and #7337, six PRs red on a defect none of them introduced. #7313 has no other
red at all.

release/v3.8.49 already carries a fix (2e42b8efc, #7174: try/catch, fetch
origin/main on demand, t.skip() when unreachable), but it only reaches main at
release time — so main stays broken for the whole cycle. Cherry-picking it would
also import a new problem: PR Test Policy classifies t.skip() as a silenced
assertion, which we watched it correctly catch on #7300 today.

This is the hermetic version instead (ported from #7327, which does the same for
the release branch): read the file straight off disk, compare against an empty
base so baseTaut/baseExtTaut are 0 — the strictest possible comparison point —
and call evaluateMasking() directly. No git ref, no fetch, no skip, nothing the
runner's checkout depth can break.

The #6634 regression stays covered: the guard's logic lives in
SELF_TEST_FIXTURE_RE (check-test-masking.mjs:337), not in the test. Proven both
ways on main before committing — neutralise SELF_TEST_FIXTURE_RE to /$^/ and
the test FAILS; restore it and it passes 2/2, with check-test-masking.mjs left
byte-identical.

Co-authored-by: growab <nekron@icloud.com>

* chore(quality): tighten main's coverage baseline to the CI's real numbers (#7347)

main's ratchet had been failing --require-tighten on every PR: 11 metrics
improved but the baseline was never tightened. Same class as the #6634
selfref guard — an infra fix that lands only on the release branch leaves
main red for the whole cycle, and every PR into main pays for it.

Values are the merged-coverage numbers from a run on main itself (a local
run measures ~68% vs CI's ~80%; the baseline's own note warns about that
gap). Only the 11 coverage values change — gitleaks and semgrepFindings
keep main's own state.

No changelog fragment: #7326 carries it on release/v3.8.49, and a second
one here would double the entry at release time.

---------

Co-authored-by: Diego Rodrigues de Sa e Souza <8016841+diegosouzapw@users.noreply.github.com>
Co-authored-by: roomhacker <roomhacker@bezrabotnyi.com>
Co-authored-by: growab <nekron@icloud.com>
2026-07-18 21:18:26 -03:00

69 lines
2.2 KiB
TypeScript

import assert from "node:assert/strict";
import test from "node:test";
import type { SqliteAdapter } from "../../../src/lib/db/adapters/types.ts";
import { getDatabaseStats } from "../../../src/lib/db/stats.ts";
function createAdapter(
virtualTableError = "no such module: vec0",
regularRow: { count: number } | undefined = { count: 3 },
returnUndefinedRegularRow = false
): SqliteAdapter {
return {
driver: "better-sqlite3",
open: true,
name: ":memory:",
raw: {},
pragma(name: string) {
return name === "page_size" ? 4096 : name === "page_count" ? 2 : -2000;
},
prepare(sql: string) {
if (sql.includes("FROM sqlite_master WHERE type='table'")) {
return { all: () => [{ name: "regular" }, { name: "vec_memories" }] } as never;
}
if (sql.includes('COUNT(*) as count FROM "regular"')) {
return { get: () => (returnUndefinedRegularRow ? undefined : regularRow) } as never;
}
if (sql.includes('COUNT(*) as count FROM "vec_memories"')) {
throw new Error(virtualTableError);
}
if (sql.includes("FROM dbstat")) {
return { get: () => ({ size: 1024 }) } as never;
}
if (sql.includes("FROM sqlite_master WHERE type='index'")) {
return { all: () => [] } as never;
}
throw new Error(`Unexpected SQL: ${sql}`);
},
exec() {},
transaction: (fn) => fn,
immediate(fn) {
fn();
},
async backup() {},
checkpoint() {},
close() {},
};
}
test("database stats tolerate virtual tables whose module is unavailable", () => {
const stats = getDatabaseStats(createAdapter());
assert.deepEqual(stats.tables, [
{ name: "regular", rowCount: 3, size: 1024 },
{ name: "vec_memories", rowCount: 0, size: 1024 },
]);
});
test("database stats do not mask unrelated table errors", () => {
assert.throws(() => getDatabaseStats(createAdapter("database disk image is malformed")), {
message: "database disk image is malformed",
});
});
test("database stats tolerate an undefined COUNT result", () => {
const stats = getDatabaseStats(createAdapter("no such module: vec0", { count: 3 }, true));
assert.equal(stats.tables[0]?.rowCount, 0);
});