Files
OmniRoute/tests/unit/db/omp.test.ts
Diego Rodrigues de Sa e Souza 93265eede3 test(infra): retry recursive temp-dir removal on main (main twin of #11968) (#12246)
* test(infra): retry recursive temp-dir removal on main (main twin of #11968)

`main` has been red since b342c1a361 on the vitest and integration gates:

  ✖ tests/unit/autoCombo/provider-family-combos.test.ts > auto/<family>
  ✖ chat pipeline applies Codex OAuth fingerprint and priority tier inside combos

Both call resetStorage() from beforeEach, which does an fs.rmSync(TEST_DATA_DIR,
{recursive: true, force: true}) with no retry, and intermittently loses the race
with a not-yet-released SQLite handle (ENOTEMPTY).

release/v3.8.51 fixed this in #11968 with a mechanical codemod adding
maxRetries/retryDelay to every recursive rm/rmSync/rmdirSync under tests/, but
that PR landed only on the release branch. Because main only receives work at
the release squash, it stayed broken for the whole cycle — and repo-wide gates
then turn every open PR into main red on checks unrelated to their diff.

This is the --base main twin: re-runs the same codemod that already shipped on
the release branch (scripts/ad-hoc/codemod-rm-maxretries.mjs), so the two
branches converge on identical test-teardown semantics. Test-only; no product
logic is touched.

The remaining three failures reported on #12133 (unit full suite exceeding its
4800s ceiling, package-artifact exceeding 1200s, and the boot-smoke that is
skipped as a consequence) are runner-contention timeouts, not code defects —
validate-release-green.mjs runs those heavy gates concurrently on one shared
hosted runner. There is no fix to port for those.

* chore(scripts): carry the rm-maxretries codemod onto main alongside its output

The codemod that generated the previous commit lives in the repo on
release/v3.8.51 (added by #11968) but was never on main. Bringing it over keeps
the tool next to the change it produced, so the transformation stays
reproducible and auditable from either branch.
2026-09-01 01:48:00 -03:00

151 lines
5.5 KiB
TypeScript

// ENVIRONMENT NOTE (sandbox better-sqlite3 / glibc limitation, not a code defect):
// This test constructs or exercises a real better-sqlite3-backed SQLite database.
// better-sqlite3 is a native addon; production and CI load it normally, but some
// sandboxes/dev boxes ship a system glibc older than the prebuilt binary requires
// ("GLIBC_2.29 not found"), so the native module fails to dlopen and any test that
// reaches better-sqlite3 directly (or asserts stdout that the load-failure warning
// would pollute) fails HERE while passing in CI. This is a known environment
// limitation, not a defect in the code under test: the OmniRoute runtime itself
// cascades to node:sqlite/sql.js when better-sqlite3 is unavailable. See
// tests/unit/_helpers/betterSqlite3Availability.ts for a guard helper.
/**
* Unit tests for src/lib/db/omp.ts — OMP (Oh My Pi) credential CRUD.
*
* omp.ts opens the third-party OMP CLI's OWN local sqlite database
* (~/.omp/agent/agent.db) directly, per request — NOT OmniRoute's own DB.
* These tests cover both the happy path (round trip against a fixture DB
* with the omp CLI's real `auth_credentials` schema) and the missing-DB-file
* path (omp CLI never run yet), which each exported function must handle
* gracefully without throwing.
*/
import { describe, it, beforeEach, afterEach } 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 Database from "better-sqlite3";
const {
getOmpCredentials,
saveOmpCredentials,
deleteOmpCredentials,
} = await import("../../../src/lib/db/omp.ts");
const PROVIDER_ID = "omniroute";
let tmpHome: string;
let origHome: string | undefined;
function getOmpDbPath() {
return path.join(tmpHome, ".omp", "agent", "agent.db");
}
/** Simulate the omp CLI having already created its sqlite DB + schema. */
function seedOmpDb() {
const dbPath = getOmpDbPath();
fs.mkdirSync(path.dirname(dbPath), { recursive: true });
const db = new Database(dbPath);
db.exec(`
CREATE TABLE IF NOT EXISTS auth_credentials (
provider TEXT NOT NULL,
credential_type TEXT NOT NULL,
data TEXT,
disabled_cause TEXT,
identity_key TEXT,
created_at INTEGER,
updated_at INTEGER
)
`);
db.close();
}
beforeEach(() => {
tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "omp-db-test-"));
origHome = process.env.HOME;
process.env.HOME = tmpHome;
});
afterEach(() => {
process.env.HOME = origHome;
fs.rmSync(tmpHome, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
});
describe("db/omp.ts — getOmpCredentials", () => {
it("returns hasOmniRoute:false without throwing when the omp DB file does not exist", () => {
assert.ok(!fs.existsSync(getOmpDbPath()), "precondition: no DB file yet");
const creds = getOmpCredentials(PROVIDER_ID);
assert.deepEqual(creds, { hasOmniRoute: false, baseUrl: null, apiKey: null });
});
it("returns hasOmniRoute:false when the DB exists but has no matching row", () => {
seedOmpDb();
const creds = getOmpCredentials(PROVIDER_ID);
assert.deepEqual(creds, { hasOmniRoute: false, baseUrl: null, apiKey: null });
});
it("returns hasOmniRoute:false gracefully when the schema itself is missing (corrupt/foreign DB)", () => {
const dbPath = getOmpDbPath();
fs.mkdirSync(path.dirname(dbPath), { recursive: true });
// Valid sqlite file, but no auth_credentials table at all.
const db = new Database(dbPath);
db.exec("CREATE TABLE unrelated (id INTEGER)");
db.close();
const creds = getOmpCredentials(PROVIDER_ID);
assert.deepEqual(creds, { hasOmniRoute: false, baseUrl: null, apiKey: null });
});
});
describe("db/omp.ts — saveOmpCredentials + getOmpCredentials round trip", () => {
it("persists apiKey/baseUrl so a subsequent read sees them", () => {
seedOmpDb();
saveOmpCredentials(PROVIDER_ID, "sk-test-omp-key", "http://localhost:20128/v1");
const creds = getOmpCredentials(PROVIDER_ID);
assert.equal(creds.hasOmniRoute, true);
assert.equal(creds.apiKey, "sk-test-omp-key");
assert.equal(creds.baseUrl, "http://localhost:20128/v1");
});
it("overwrites an existing row for the same provider instead of duplicating it", () => {
seedOmpDb();
saveOmpCredentials(PROVIDER_ID, "sk-old-key", "http://localhost:20128/v1");
saveOmpCredentials(PROVIDER_ID, "sk-new-key", "http://localhost:20129/v1");
const dbPath = getOmpDbPath();
const db = new Database(dbPath, { readonly: true });
const rows = db
.prepare("SELECT data FROM auth_credentials WHERE provider = ?")
.all(PROVIDER_ID) as { data: string }[];
db.close();
assert.equal(rows.length, 1, "must not accumulate duplicate rows for the same provider");
const parsed = JSON.parse(rows[0].data);
assert.equal(parsed.apiKey, "sk-new-key");
assert.equal(parsed.baseUrl, "http://localhost:20129/v1");
});
});
describe("db/omp.ts — deleteOmpCredentials", () => {
it("removes the row so a subsequent get reports hasOmniRoute:false", () => {
seedOmpDb();
saveOmpCredentials(PROVIDER_ID, "sk-test-omp-key", "http://localhost:20128/v1");
assert.equal(getOmpCredentials(PROVIDER_ID).hasOmniRoute, true);
deleteOmpCredentials(PROVIDER_ID);
assert.deepEqual(getOmpCredentials(PROVIDER_ID), {
hasOmniRoute: false,
baseUrl: null,
apiKey: null,
});
});
it("does not throw when deleting a provider that was never saved", () => {
seedOmpDb();
assert.doesNotThrow(() => deleteOmpCredentials(PROVIDER_ID));
});
});