mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-09-13 18:32:12 +03:00
* test(infra): retry recursive temp-dir removal instead of failing a shard on ENOTEMPTY (#11966) Two shards on release/v3.8.51 went red in one day with the same signature — "ENOTEMPTY, Directory not empty: /tmp/omniroute-<test>-XXXXXX" — from combo-same-provider-cascade (Unit Tests fast-path 4/4, on a PR that touches only .github/) and auth-policy-embeddings-webfetch-7785 (the 20k-test TIA step). Both pass alone and on re-run: the cleanup races something still writing into the directory (SQLite WAL/-shm checkpoint, a worker, the backup) and under a loaded hosted runner the window opens. 1154 test files do their own cleanup with fs.rmSync(dir, { recursive: true, force: true }); 57 already asked for retries. One-shot codemod (scripts/ad-hoc/codemod-rm-maxretries.mjs, kept for the record): every rm / rmSync / rmdirSync option object with `recursive: true` and no `maxRetries` gains `maxRetries: 5, retryDelay: 100` — Node itself then retries ENOTEMPTY/EBUSY/EPERM for up to ~0.5 s before giving up. 2243 call sites in 1292 files under tests/, the shared tests/_setup/isolateDataDir.ts exit hook included. Only the option object changes: no call site, assertion or import is touched. Validation: prettier and ESLint (with the frozen suppressions) clean on all 1292 files; a random 20-file sample runs green (quota-redis-store hangs identically on the untouched tree — it needs a Redis on localhost, an environment matter). The four unit shards on this PR are the full run. * fix(quality): let check-forgotten-sibling-tests read a 1,000-file diff The gate shells out to `git diff` through execFileSync with Node's default 1 MB maxBuffer; the 1,292-file codemod in this PR is the first diff large enough to overflow it, and the gate died with `spawnSync git ENOBUFS` before comparing anything. 64 MB is far above any real PR and costs nothing when unused.
144 lines
4.1 KiB
TypeScript
144 lines
4.1 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-config-audit-"));
|
|
process.env.DATA_DIR = TEST_DATA_DIR;
|
|
process.env.DISABLE_SQLITE_AUTO_BACKUP = "true";
|
|
|
|
const core = await import("../../src/lib/db/core.ts");
|
|
const cleanup = await import("../../src/lib/db/cleanup.ts");
|
|
const audit = await import("../../src/domain/configAudit.ts");
|
|
|
|
type CountRow = { c: number };
|
|
|
|
function resetStorage() {
|
|
core.resetDbInstance();
|
|
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
|
|
fs.mkdirSync(TEST_DATA_DIR, { recursive: true });
|
|
}
|
|
|
|
function countRows(): number {
|
|
const db = core.getDbInstance();
|
|
const row = db.prepare("SELECT COUNT(*) AS c FROM config_audit_log").get() as CountRow;
|
|
return row.c;
|
|
}
|
|
|
|
function insertOldRow(id: string, daysAgo: number) {
|
|
const db = core.getDbInstance();
|
|
const old = new Date(Date.now() - daysAgo * 24 * 60 * 60 * 1000).toISOString();
|
|
db.prepare(
|
|
`INSERT INTO config_audit_log
|
|
(id, timestamp, action, target, target_id, target_name, before_json, after_json, diff_json, source, note)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
|
|
).run(
|
|
id,
|
|
old,
|
|
"update",
|
|
"provider",
|
|
"p1",
|
|
"P1",
|
|
null,
|
|
null,
|
|
JSON.stringify({ added: [], removed: [], changed: [], isEmpty: true }),
|
|
"api",
|
|
null
|
|
);
|
|
}
|
|
|
|
test.beforeEach(() => {
|
|
resetStorage();
|
|
});
|
|
|
|
test.after(() => {
|
|
resetStorage();
|
|
});
|
|
|
|
test("recordChange persists to SQLite, not memory", () => {
|
|
const db = core.getDbInstance();
|
|
const tableRow = db
|
|
.prepare(
|
|
"SELECT count(*) as c FROM sqlite_master WHERE type='table' AND name='config_audit_log'"
|
|
)
|
|
.get() as CountRow;
|
|
assert.equal(tableRow.c, 1);
|
|
|
|
const e = audit.recordChange(
|
|
"update",
|
|
"provider",
|
|
"p1",
|
|
"My Provider",
|
|
{ a: 1 },
|
|
{ a: 2 },
|
|
"api",
|
|
null
|
|
);
|
|
assert.equal(countRows(), 1);
|
|
|
|
const { entries, total } = audit.getAuditLog({ target: "provider" });
|
|
assert.equal(total, 1);
|
|
assert.equal(entries[0].id, e.id);
|
|
assert.deepEqual(entries[0].diff.changed, [{ key: "a", from: 1, to: 2 }]);
|
|
});
|
|
|
|
test("pagination + filters read from SQLite", () => {
|
|
audit.recordChange("create", "combo", "c1", "C1", null, { models: ["m1"] }, "dashboard");
|
|
audit.recordChange(
|
|
"update",
|
|
"combo",
|
|
"c1",
|
|
"C1",
|
|
{ models: ["m1"] },
|
|
{ models: ["m1", "m2"] },
|
|
"api"
|
|
);
|
|
|
|
const { entries, total } = audit.getAuditLog({ target: "combo", limit: 1, offset: 0 });
|
|
assert.equal(total, 2);
|
|
assert.equal(entries.length, 1);
|
|
});
|
|
|
|
test("getRollbackState returns the before snapshot", () => {
|
|
const e = audit.recordChange("update", "policy", "pol1", "Pol", { x: 1 }, { x: 2 }, "api");
|
|
assert.deepEqual(audit.getRollbackState(e.id), { x: 1 });
|
|
});
|
|
|
|
test("computeDiff stays pure", () => {
|
|
const d = audit.computeDiff({ a: 1 }, { a: 2, b: 3 });
|
|
assert.deepEqual(d.added, ["b"]);
|
|
assert.deepEqual(d.changed, [{ key: "a", from: 1, to: 2 }]);
|
|
});
|
|
|
|
test("resetAuditLog clears persisted rows", () => {
|
|
audit.recordChange("update", "provider", "p1", "P1", { a: 1 }, { a: 2 }, "api");
|
|
assert.equal(countRows(), 1);
|
|
audit.resetAuditLog();
|
|
assert.equal(countRows(), 0);
|
|
});
|
|
|
|
test("cleanupConfigAudit prunes rows beyond retentionDays", async () => {
|
|
insertOldRow("audit-old", 40);
|
|
const r = await cleanup.cleanupConfigAudit(30);
|
|
assert.equal(r.deleted, 1);
|
|
assert.equal(countRows(), 0);
|
|
});
|
|
|
|
test("cleanupConfigAudit keeps recent rows within retention", async () => {
|
|
insertOldRow("audit-recent", 5);
|
|
const r = await cleanup.cleanupConfigAudit(30);
|
|
assert.equal(r.deleted, 0);
|
|
assert.equal(countRows(), 1);
|
|
});
|
|
|
|
test("runAutoCleanup includes a configAudit result", async () => {
|
|
insertOldRow("audit-old-2", 40);
|
|
const result = await cleanup.runAutoCleanup();
|
|
assert.ok(result.results.configAudit);
|
|
assert.equal(typeof result.results.configAudit.deleted, "number");
|
|
assert.equal(typeof result.results.configAudit.errors, "number");
|
|
assert.equal(result.results.configAudit.deleted, 1);
|
|
assert.equal(countRows(), 0);
|
|
});
|