Files
OmniRoute/tests/unit/db/omp.test.ts
Hamsa_M 1bc6da5318 feat(cli): add CLI tools for pi, omp, letta, codewhale and jcode (#6318)
* feat(cli): add CLI tools for pi, omp, letta, codewhale and jcode

* fix(build): resolve CI build and lint errors

* fix(cli): resolve merge conflicts, add tests, align error handling for cli-additions

Resolve duplicate codewhale key from base merge, add unit/integration
tests for omp/letta settings routes and the omp DB module, and align
omp-settings/letta-settings error handling with sanitizeErrorMessage()
+ the pattern used by sibling jcode/pi/codewhale routes in this PR.

Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>

* chore(quality): correct cliRuntime.ts file-size baseline to actual post-merge line count

Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>

* fix(changelog): re-restore #6318 bullet after release sync

Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>

* fix(merge): restore #6126 clinepass files reverted by release auto-resolve + baseline re-merge

The release sync's auto-resolve reverted sibling PR #6126's clinepass work
(registry, catalog, oauth constants, clineAuth.ts, token-refresh case, tests)
and the file-size baseline — all outside this PR's scope. Restored to the
release versions, re-applied only this PR's own baseline entries, restored the
#6126 CHANGELOG bullet (re-inserting only this PR's own).

Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>

* fix(db): re-export db/omp from localDb (check:db-rules #2)

Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>

* fix(db): keep localDb.ts at the 800-line cap after the omp re-export

Folded the MemoryVecMeta type re-export into the memoryVec named-export block
(inline 'type' specifier) so adding the db/omp line stays within the new-file
cap.

Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>

* fix(cli): reduce #6318 scope to omp + letta (pi/codewhale/jcode already shipped)

pi, codewhale, and jcode landed via a separate PR before this one was
reconciled — re-adding parallel versions of their catalog entries, routes,
dashboard card, and i18n strings would have been a straight regression
(duplicate "pi" key silently shadowing the release's own entry, orphaned
JcodeToolCard/BaseUrlSelect/ApiKeySelect/cliEndpointMatch UI files with no
release-side wiring, and unrelated formatting/refactor drift in
codewhale-settings/pi-settings/config-generator/routeGuard picked up along
the way).

This PR now ships only the two tools that are genuinely new: omp (Oh My Pi)
and letta. Both settings routes shell out to `which omp`/`which letta` to
detect the local install, so they're loopback-gated in
LOCAL_ONLY_API_PREFIXES (Hard Rules #15/#17) in addition to the shared
requireCliToolsAuth() guard every cli-tools route requires
(tests/unit/cli-tools-auth-hardening.test.ts) — neither route had the guard
wired in yet. cli-catalog-counts.test.ts is updated to the real cardinality
(8 agent entries / 32 total, since omp+letta are both category "agent";
pi/codewhale/jcode were always category "code" and are unaffected). The
integration tests for omp/letta now pass a Request object to GET/DELETE and
assert the 401-when-auth-required path, matching the pattern already used by
the codewhale/jcode sibling routes. complexity-baseline.json is back to the
release's 2053 (the #6318 rebaseline note is gone — dropping the duplicate
JcodeToolCard.tsx/BaseUrlSelect.tsx removed the violations it was covering);
file-size-baseline.json's cliTools.ts entry shrank 955->915 to match the
smaller real file. CHANGELOG bullet rewritten to describe only omp+letta,
with a note on why pi/codewhale/jcode aren't part of this PR; also restores
the Kiro External IdP bullet that a prior merge auto-resolve had dropped
from the living section.

Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>

* test(cli-tools): align cli-tools-schema registry count with omp+letta (30→32)

Second exact-count guard missed in the scope-reduction pass; same legitimate
alignment as cli-catalog-counts.

Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>

* fix(cli-tools): omp entry needs docsUrl (CliCatalogEntrySchema requires it)

https://github.com/can1357/oh-my-pi — verified official repo.

Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>

* chore(quality): cliTools.ts frozen 915→916 (+1 omp docsUrl line, own growth)

Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>

* chore(changelog): restore base + re-insert #6318 bullet after release sync

Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>

* chore(sync): merge release tip + restore own CHANGELOG bullet

Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>

---------

Co-authored-by: hamsa0x7 <hamsa0x7@users.noreply.github.com>
Co-authored-by: Diego Rodrigues de Sa e Souza <diegosouza.pw@gmail.com>
Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>
2026-07-10 01:27:52 -03:00

141 lines
4.7 KiB
TypeScript

/**
* 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 });
});
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));
});
});