mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-09-14 10:52:17 +03:00
Landed with the design call resolved per the owner's pick — **option 1**: the synced store is now endpoint-agnostic (persistDiscoveredModels and managedModelImport no longer drop non-chat models at write time), and chat selectability moved to read time (auto-pool expansion in autoStrategy applies filterChatSelectableModels; the models-route projection already had its chatOnly filter). Your discovery test now passes end-to-end (3/3): /api/show capabilities persist per connection and image/embedding requests route through the advertising host. Reconciliation notes: conflicted areas merged onto the current tip (adobe discovery import, requestedModel preflight signature, resolvedProvider fast-path coexists with the synced-route override — explicit resolution wins); carried base-red drains (#10055 memoization, #11071 test variants) dropped as already-landed; the managed-model-import exclusion test was propagated to the new contract (image/video models persist; the read filter still hides them from chat pickers — pinned by a new assertion). Full battery: 205/206 focused (the one red is a confirmed periodic-timer timing flake on the loaded devbox — 20/20 isolated), autoCombo vitest 30/30, combo suites 46/46, gates + typecheck clean. Thank you @yourspraveen — the capability probe + routing design was right; it just needed the store contract opened up. Fixes #11087.
141 lines
3.8 KiB
TypeScript
141 lines
3.8 KiB
TypeScript
import { test } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
|
|
import { createNodeSqliteAdapterFromDatabase } from "../../../src/lib/db/adapters/nodeSqliteShared.ts";
|
|
|
|
type Row = Record<string, unknown>;
|
|
|
|
class FakeStatement {
|
|
finalized = false;
|
|
|
|
constructor(
|
|
private readonly sql: string,
|
|
private readonly rows: Row[] = []
|
|
) {}
|
|
|
|
run(..._params: unknown[]) {
|
|
return { changes: 1n, lastInsertRowid: 7n };
|
|
}
|
|
|
|
get(..._params: unknown[]) {
|
|
if (this.sql.startsWith("PRAGMA")) {
|
|
return { journal_mode: "wal" };
|
|
}
|
|
return this.rows[0];
|
|
}
|
|
|
|
all(..._params: unknown[]) {
|
|
if (this.sql.startsWith("PRAGMA")) {
|
|
return [{ journal_mode: "wal" }];
|
|
}
|
|
return this.rows;
|
|
}
|
|
|
|
finalize() {
|
|
this.finalized = true;
|
|
}
|
|
}
|
|
|
|
class FakeDb {
|
|
closed = false;
|
|
execCalls: string[] = [];
|
|
statements: FakeStatement[] = [];
|
|
|
|
prepare(sql: string) {
|
|
const statement = new FakeStatement(sql, [{ value: "ok" }]);
|
|
this.statements.push(statement);
|
|
return statement;
|
|
}
|
|
|
|
exec(sql: string) {
|
|
this.execCalls.push(sql);
|
|
}
|
|
|
|
close() {
|
|
this.closed = true;
|
|
}
|
|
}
|
|
|
|
test("createNodeSqliteAdapterFromDatabase wraps node:sqlite statements", () => {
|
|
const db = new FakeDb();
|
|
const adapter = createNodeSqliteAdapterFromDatabase(db, ":memory:");
|
|
|
|
const insert = adapter.prepare("INSERT INTO t VALUES (?)").run("ok");
|
|
assert.deepEqual(insert, { changes: 1, lastInsertRowid: 7 });
|
|
|
|
const row = adapter.prepare("SELECT value FROM t").get() as Row;
|
|
assert.equal(row.value, "ok");
|
|
|
|
assert.equal(adapter.pragma("journal_mode", { simple: true }), "wal");
|
|
assert.deepEqual(adapter.pragma("journal_mode"), [{ journal_mode: "wal" }]);
|
|
});
|
|
|
|
test("createNodeSqliteAdapterFromDatabase uses savepoints for transactions", () => {
|
|
const db = new FakeDb();
|
|
const adapter = createNodeSqliteAdapterFromDatabase(db, ":memory:");
|
|
|
|
const run = adapter.transaction((value: string) => value.toUpperCase());
|
|
assert.equal(run("ok"), "OK");
|
|
assert.equal(db.execCalls[0].startsWith("SAVEPOINT "), true);
|
|
assert.equal(db.execCalls[1].startsWith("RELEASE "), true);
|
|
});
|
|
|
|
test("createNodeSqliteAdapterFromDatabase uses BEGIN IMMEDIATE outside transactions", () => {
|
|
const db = new FakeDb();
|
|
const adapter = createNodeSqliteAdapterFromDatabase(db, ":memory:");
|
|
|
|
adapter.immediate(() => {});
|
|
|
|
assert.deepEqual(db.execCalls, ["BEGIN IMMEDIATE", "COMMIT"]);
|
|
});
|
|
|
|
test("createNodeSqliteAdapterFromDatabase rolls back failed immediate transactions", () => {
|
|
const db = new FakeDb();
|
|
const adapter = createNodeSqliteAdapterFromDatabase(db, ":memory:");
|
|
|
|
assert.throws(() =>
|
|
adapter.immediate(() => {
|
|
throw new Error("fail");
|
|
})
|
|
);
|
|
|
|
assert.deepEqual(db.execCalls, ["BEGIN IMMEDIATE", "ROLLBACK"]);
|
|
});
|
|
|
|
test("createNodeSqliteAdapterFromDatabase nests transactions in immediate savepoints", () => {
|
|
const db = new FakeDb();
|
|
const adapter = createNodeSqliteAdapterFromDatabase(db, ":memory:");
|
|
|
|
adapter.immediate(() => {
|
|
const nested = adapter.transaction(() => {
|
|
throw new Error("inner failure");
|
|
});
|
|
assert.throws(() => nested());
|
|
});
|
|
|
|
assert.equal(db.execCalls[0], "BEGIN IMMEDIATE");
|
|
assert.match(db.execCalls[1], /^SAVEPOINT /);
|
|
assert.match(db.execCalls[2], /^ROLLBACK TO /);
|
|
assert.match(db.execCalls[3], /^RELEASE /);
|
|
assert.equal(db.execCalls[4], "COMMIT");
|
|
});
|
|
|
|
test("createNodeSqliteAdapterFromDatabase finalizes cached statements on close", () => {
|
|
const db = new FakeDb();
|
|
let closedHookCalls = 0;
|
|
const adapter = createNodeSqliteAdapterFromDatabase(db, ":memory:", () => {
|
|
closedHookCalls++;
|
|
});
|
|
|
|
adapter.prepare("SELECT 1").get();
|
|
adapter.close();
|
|
|
|
assert.equal(closedHookCalls, 1);
|
|
assert.equal(db.closed, true);
|
|
assert.equal(adapter.open, false);
|
|
assert.equal(
|
|
db.statements.every((statement) => statement.finalized),
|
|
true
|
|
);
|
|
});
|