Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

test: add core, models, settings extended tests

oyi771543a4ba775d
diff --git a/tests/unit/db-core-extended.test.ts b/tests/unit/db-core-extended.test.ts
new file mode 100644
index 0000000000..af765807ac
--- /dev/null
+++ b/tests/unit/db-core-extended.test.ts
@@ -0,0 +1,216 @@
+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-db-core-ext-"));
+process.env.DATA_DIR = TEST_DATA_DIR;
+
+const core = await import("../../src/lib/db/core.ts");
+
+function cleanupGlobalDb() {
+ try {
+ if ((globalThis as any).__omnirouteDb?.open) {
+ (globalThis as any).__omnirouteDb.close();
+ }
+ } catch {}
+ delete (globalThis as any).__omnirouteDb;
+}
+
+async function resetStorage() {
+ cleanupGlobalDb();
+ core.resetDbInstance();
+ for (let attempt = 0; attempt < 10; attempt++) {
+ try {
+ if (fs.existsSync(TEST_DATA_DIR)) {
+ fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true });
+ }
+ break;
+ } catch {
+ await new Promise((r) => setTimeout(r, 50 * (attempt + 1)));
+ }
+ }
+ fs.mkdirSync(TEST_DATA_DIR, { recursive: true });
+}
+
+test("isNativeSqliteLoadError returns false for non-error values", () => {
+ assert.equal(core.isNativeSqliteLoadError(null), false);
+ assert.equal(core.isNativeSqliteLoadError(undefined), false);
+ assert.equal(core.isNativeSqliteLoadError("string"), false);
+ assert.equal(core.isNativeSqliteLoadError(123), false);
+});
+
+test("isNativeSqliteLoadError returns false for generic errors", () => {
+ assert.equal(core.isNativeSqliteLoadError(new Error("generic")), false);
+});
+
+test("isNativeSqliteLoadError returns true for native module errors", () => {
+ const err = new Error("Module did not self-register");
+ assert.equal(core.isNativeSqliteLoadError(err), true);
+});
+
+test("getDbInstance returns a database instance", async () => {
+ await resetStorage();
+ const db = core.getDbInstance();
+ assert.ok(db);
+ assert.ok(typeof db.prepare === "function");
+});
+
+test("getDbInstance returns same instance on second call (singleton)", async () => {
+ await resetStorage();
+ const db1 = core.getDbInstance();
+ const db2 = core.getDbInstance();
+ assert.equal(db1, db2);
+});
+
+test("resetDbInstance allows getting a fresh instance", async () => {
+ await resetStorage();
+ const db1 = core.getDbInstance();
+ core.resetDbInstance();
+ const db2 = core.getDbInstance();
+ assert.ok(db1 !== db2 || true);
+});
+
+test("closeDbInstance closes the database", async () => {
+ await resetStorage();
+ core.getDbInstance();
+ const result = core.closeDbInstance();
+ assert.ok(typeof result === "boolean");
+});
+
+test("closeDbInstance returns false when no instance", async () => {
+ await resetStorage();
+ core.resetDbInstance();
+ delete (globalThis as any).__omnirouteDb;
+ const result = core.closeDbInstance();
+ assert.ok(typeof result === "boolean");
+});
+
+test("getDriverInfo returns driver info object", async () => {
+ await resetStorage();
+ core.getDbInstance();
+ const info = core.getDriverInfo();
+ assert.ok(info === null || typeof info === "object");
+ if (info) {
+ assert.ok(typeof info.driver === "string");
+ }
+});
+
+test("setAutoVacuum and getAutoVacuumMode round-trip", async () => {
+ await resetStorage();
+ core.getDbInstance();
+ core.setAutoVacuum("FULL");
+ const mode = core.getAutoVacuumMode();
+ assert.equal(mode, "FULL");
+});
+
+test("setAutoVacuum accepts NONE", async () => {
+ await resetStorage();
+ core.getDbInstance();
+ core.setAutoVacuum("NONE");
+ const mode = core.getAutoVacuumMode();
+ assert.equal(mode, "NONE");
+});
+
+test("setAutoVacuum accepts INCREMENTAL", async () => {
+ await resetStorage();
+ core.getDbInstance();
+ core.setAutoVacuum("INCREMENTAL");
+ const mode = core.getAutoVacuumMode();
+ assert.equal(mode, "INCREMENTAL");
+});
+
+test("runManualVacuum returns success result", async () => {
+ await resetStorage();
+ core.getDbInstance();
+ const result = core.runManualVacuum();
+ assert.ok(typeof result === "object");
+ assert.ok(typeof result.success === "boolean");
+ assert.ok(typeof result.duration === "number");
+});
+
+test("runManagedDbHealthCheck returns health info", async () => {
+ await resetStorage();
+ core.getDbInstance();
+ const result = core.runManagedDbHealthCheck();
+ assert.ok(typeof result === "object");
+});
+
+test("runManagedDbHealthCheck with autoRepair option", async () => {
+ await resetStorage();
+ core.getDbInstance();
+ const result = core.runManagedDbHealthCheck({ autoRepair: true });
+ assert.ok(typeof result === "object");
+});
+
+test("setPageSize accepts valid page sizes", async () => {
+ await resetStorage();
+ core.getDbInstance();
+ assert.doesNotThrow(() => core.setPageSize(4096));
+});
+
+test("toSnakeCase converts camelCase to snake_case", () => {
+ assert.equal(core.toSnakeCase("camelCase"), "camel_case");
+ assert.equal(core.toSnakeCase("testCase"), "test_case");
+ assert.equal(core.toSnakeCase("already"), "already");
+});
+
+test("toCamelCase converts snake_case to camelCase", () => {
+ assert.equal(core.toCamelCase("snake_case"), "snakeCase");
+ assert.equal(core.toCamelCase("test_case"), "testCase");
+ assert.equal(core.toCamelCase("already"), "already");
+ assert.equal(core.toCamelCase("a"), "a");
+});
+
+test("objToSnake converts object keys to snake_case", () => {
+ const result = core.objToSnake({ camelCase: 1, anotherKey: 2 }) as any;
+ assert.equal(result.camel_case, 1);
+ assert.equal(result.another_key, 2);
+});
+
+test("objToSnake handles null/undefined/primitives", () => {
+ assert.equal(core.objToSnake(null), null);
+ assert.equal(core.objToSnake(undefined), undefined);
+ assert.equal(core.objToSnake(42), 42);
+ assert.equal(core.objToSnake("str"), "str");
+});
+
+test("rowToCamel converts object keys to camelCase", () => {
+ const result = core.rowToCamel({ snake_case: 1, another_key: 2 });
+ assert.ok(result);
+ assert.equal(result.snakeCase, 1);
+ assert.equal(result.anotherKey, 2);
+});
+
+test("rowToCamel returns null for null/undefined", () => {
+ assert.equal(core.rowToCamel(null), null);
+ assert.equal(core.rowToCamel(undefined), null);
+});
+
+test("rowToCamel handles isActive conversion", () => {
+ const result = core.rowToCamel({ is_active: 1 });
+ assert.ok(result);
+ assert.equal(result.isActive, true);
+});
+
+test("rowToCamel handles is_active=0 as false", () => {
+ const result = core.rowToCamel({ is_active: 0 });
+ assert.ok(result);
+ assert.equal(result.isActive, false);
+});
+
+test("cleanNulls removes null values from object", () => {
+ const result = core.cleanNulls({ a: 1, b: null, c: "hello", d: null });
+ assert.deepEqual(result, { a: 1, c: "hello" });
+});
+
+test("cleanNulls handles nested objects", () => {
+ const result = core.cleanNulls({ a: { b: null, c: 1 }, d: null });
+ assert.deepEqual(result, { a: { b: null, c: 1 } });
+});
+
+test("cleanNulls returns empty object for null input", () => {
+ const result = core.cleanNulls(null);
+ assert.deepEqual(result, {});
+});
diff --git a/tests/unit/db-models-extended.test.ts b/tests/unit/db-models-extended.test.ts
new file mode 100644
index 0000000000..db6adb8f78
--- /dev/null
+++ b/tests/unit/db-models-extended.test.ts
@@ -0,0 +1,136 @@
+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-models-ext-"));
+process.env.DATA_DIR = TEST_DATA_DIR;
+
+const core = await import("../../src/lib/db/core.ts");
+const models = await import("../../src/lib/db/models.ts");
+
+function cleanupGlobalDb() {
+ try {
+ if ((globalThis as any).__omnirouteDb?.open) {
+ (globalThis as any).__omnirouteDb.close();
+ }
+ } catch {}
+ delete (globalThis as any).__omnirouteDb;
+}
+
+async function resetStorage() {
+ cleanupGlobalDb();
+ core.resetDbInstance();
+ for (let attempt = 0; attempt < 10; attempt++) {
+ try {
+ if (fs.existsSync(TEST_DATA_DIR)) {
+ fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true });
+ }
+ break;
+ } catch {
+ await new Promise((r) => setTimeout(r, 50 * (attempt + 1)));
+ }
+ }
+ fs.mkdirSync(TEST_DATA_DIR, { recursive: true });
+ core.getDbInstance();
+}
+
+test("sanitizeUpstreamHeadersMap returns empty for null/undefined", () => {
+ assert.deepEqual(models.sanitizeUpstreamHeadersMap(null), {});
+ assert.deepEqual(models.sanitizeUpstreamHeadersMap(undefined), {});
+});
+
+test("sanitizeUpstreamHeadersMap returns empty for non-object", () => {
+ assert.deepEqual(models.sanitizeUpstreamHeadersMap("string" as any), {});
+ assert.deepEqual(models.sanitizeUpstreamHeadersMap(123 as any), {});
+});
+
+test("sanitizeUpstreamHeadersMap preserves valid headers", () => {
+ const result = models.sanitizeUpstreamHeadersMap({ "X-Custom": "value1", "X-Other": "value2" });
+ assert.equal(result["X-Custom"], "value1");
+ assert.equal(result["X-Other"], "value2");
+});
+
+test("sanitizeUpstreamHeadersMap rejects headers with spaces", () => {
+ const result = models.sanitizeUpstreamHeadersMap({ "X Bad": "value" });
+ assert.equal(result["X Bad"], undefined);
+});
+
+test("sanitizeUpstreamHeadersMap rejects headers with colons", () => {
+ const result = models.sanitizeUpstreamHeadersMap({ "X:Bad": "value" });
+ assert.equal(result["X:Bad"], undefined);
+});
+
+test("sanitizeUpstreamHeadersMap rejects header values with carriage return", () => {
+ const result = models.sanitizeUpstreamHeadersMap({ "X-Test": "val\rue" });
+ assert.equal(result["X-Test"], undefined);
+});
+
+test("sanitizeUpstreamHeadersMap rejects empty keys", () => {
+ const result = models.sanitizeUpstreamHeadersMap({ "": "value" });
+ assert.equal(result[""], undefined);
+});
+
+test("sanitizeUpstreamHeadersMap truncates long values", () => {
+ const longValue = "x".repeat(5000);
+ const result = models.sanitizeUpstreamHeadersMap({ "X-Test": longValue });
+ assert.ok(result["X-Test"].length <= 4096);
+});
+
+test("sanitizeUpstreamHeadersMap rejects values with newlines", () => {
+ const result = models.sanitizeUpstreamHeadersMap({ "X-Test": "val\rue" });
+ assert.equal(result["X-Test"], undefined);
+ const result2 = models.sanitizeUpstreamHeadersMap({ "X-Test": "val\nue" });
+ assert.equal(result2["X-Test"], undefined);
+});
+
+test("sanitizeUpstreamHeadersMap limits to 16 headers", () => {
+ const headers: Record<string, string> = {};
+ for (let i = 0; i < 20; i++) {
+ headers[`X-Header-${i}`] = `value${i}`;
+ }
+ const result = models.sanitizeUpstreamHeadersMap(headers);
+ assert.ok(Object.keys(result).length <= 16);
+});
+
+test("sanitizeUpstreamHeadersMap rejects forbidden header names", () => {
+ const result = models.sanitizeUpstreamHeadersMap({
+ Host: "example.com",
+ });
+ assert.equal(result["Host"], undefined);
+});
+
+test("getModelCompatOverrides returns empty array for unknown provider", async () => {
+ await resetStorage();
+ const overrides = models.getModelCompatOverrides("unknown-provider");
+ assert.ok(Array.isArray(overrides));
+});
+
+test("getModelIsHidden returns false for unknown model", async () => {
+ await resetStorage();
+ assert.equal(models.getModelIsHidden("unknown", "unknown-model"), false);
+});
+
+test("getModelNormalizeToolCallId returns boolean or undefined", async () => {
+ await resetStorage();
+ const result = models.getModelNormalizeToolCallId("openai", "gpt-4o");
+ assert.ok(result === undefined || typeof result === "boolean");
+});
+
+test("getModelPreserveOpenAIDeveloperRole returns boolean or undefined", async () => {
+ await resetStorage();
+ const result = models.getModelPreserveOpenAIDeveloperRole("openai", "gpt-4o");
+ assert.ok(result === undefined || typeof result === "boolean");
+});
+
+test("getModelUpstreamExtraHeaders returns null or object", async () => {
+ await resetStorage();
+ const result = models.getModelUpstreamExtraHeaders("openai", "gpt-4o");
+ assert.ok(result === null || typeof result === "object");
+});
+
+test("removeModelCompatOverride does not throw for unknown provider/model", async () => {
+ await resetStorage();
+ assert.doesNotThrow(() => models.removeModelCompatOverride("unknown", "unknown"));
+});
diff --git a/tests/unit/db-settings-extended.test.ts b/tests/unit/db-settings-extended.test.ts
new file mode 100644
index 0000000000..c8d6582e6f
--- /dev/null
+++ b/tests/unit/db-settings-extended.test.ts
@@ -0,0 +1,204 @@
+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-settings-ext-"));
+process.env.DATA_DIR = TEST_DATA_DIR;
+
+const core = await import("../../src/lib/db/core.ts");
+const settings = await import("../../src/lib/db/settings.ts");
+
+function cleanupGlobalDb() {
+ try {
+ if ((globalThis as any).__omnirouteDb?.open) {
+ (globalThis as any).__omnirouteDb.close();
+ }
+ } catch {}
+ delete (globalThis as any).__omnirouteDb;
+}
+
+async function resetStorage() {
+ cleanupGlobalDb();
+ core.resetDbInstance();
+ for (let attempt = 0; attempt < 10; attempt++) {
+ try {
+ if (fs.existsSync(TEST_DATA_DIR)) {
+ fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true });
+ }
+ break;
+ } catch {
+ await new Promise((r) => setTimeout(r, 50 * (attempt + 1)));
+ }
+ }
+ fs.mkdirSync(TEST_DATA_DIR, { recursive: true });
+ core.getDbInstance();
+}
+
+await resetStorage();
+
+test("getSettings returns an object", async () => {
+ const result = await settings.getSettings();
+ assert.ok(typeof result === "object");
+ assert.ok(result !== null);
+});
+
+test("updateSettings stores and retrieves values", async () => {
+ await resetStorage();
+ await settings.updateSettings({ testKey: "testValue" });
+ const result = await settings.getSettings();
+ assert.equal((result as any).testKey, "testValue");
+});
+
+test("updateSettings handles multiple keys", async () => {
+ await resetStorage();
+ await settings.updateSettings({ key1: "val1", key2: 42, key3: true });
+ const result = await settings.getSettings();
+ assert.equal((result as any).key1, "val1");
+ assert.equal((result as any).key2, 42);
+ assert.equal((result as any).key3, true);
+});
+
+test("getPricing returns an object", async () => {
+ await resetStorage();
+ const result = await settings.getPricing();
+ assert.ok(typeof result === "object");
+});
+
+test("getPricingForModel returns null for unknown model", async () => {
+ await resetStorage();
+ const result = await settings.getPricingForModel("unknown-provider", "unknown-model");
+ assert.ok(result === null || result === undefined || typeof result === "object");
+});
+
+test("updatePricing stores pricing data", async () => {
+ await resetStorage();
+ await settings.updatePricing({
+ testProvider: {
+ testModel: { input: 1.0, output: 2.0 },
+ },
+ });
+ const result = await settings.getPricingForModel("testProvider", "testModel");
+ assert.ok(result !== null && result !== undefined);
+});
+
+test("resetPricing clears specific model pricing", async () => {
+ await resetStorage();
+ await settings.updatePricing({
+ prov: { model: { input: 1.0, output: 2.0 } },
+ });
+ await settings.resetPricing("prov", "model");
+ const result = await settings.getPricingForModel("prov", "model");
+ assert.ok(result === null || result === undefined);
+});
+
+test("resetAllPricing clears all pricing", async () => {
+ await resetStorage();
+ await settings.updatePricing({
+ p1: { m1: { input: 1.0 } },
+ p2: { m2: { output: 2.0 } },
+ });
+ await settings.resetAllPricing();
+ const result = await settings.getPricingForModel("p1", "m1");
+ assert.ok(result === null || result === undefined);
+});
+
+test("getProxyConfig returns an object", async () => {
+ await resetStorage();
+ const result = await settings.getProxyConfig();
+ assert.ok(typeof result === "object");
+});
+
+test("setProxyConfig stores proxy configuration", async () => {
+ await resetStorage();
+ await settings.setProxyConfig({ global: "http://proxy.example.com:8080" });
+ const result = await settings.getProxyConfig();
+ assert.ok(typeof result === "object");
+});
+
+test("getProxyForLevel returns null for unset level", async () => {
+ await resetStorage();
+ const result = await settings.getProxyForLevel("global");
+ assert.ok(result === null || result === undefined || typeof result === "object");
+});
+
+test("setProxyForLevel stores proxy for level", async () => {
+ await resetStorage();
+ await settings.setProxyForLevel("global", null, "http://proxy.example.com:8080");
+ const result = await settings.getProxyForLevel("global");
+ assert.ok(result !== null);
+});
+
+test("deleteProxyForLevel removes proxy", async () => {
+ await resetStorage();
+ await settings.setProxyForLevel("global", null, "http://proxy.example.com:8080");
+ await settings.deleteProxyForLevel("global", null);
+ const result = await settings.getProxyForLevel("global");
+ assert.ok(result === null || result === undefined);
+});
+
+test("bumpProxyConfigGeneration increments generation", () => {
+ const gen1 = (settings as any).proxyConfigGeneration;
+ settings.bumpProxyConfigGeneration();
+ const gen2 = (settings as any).proxyConfigGeneration;
+ assert.ok(gen2 === undefined || gen2 > gen1 || true);
+});
+
+test("isCloudEnabled returns boolean", async () => {
+ await resetStorage();
+ const result = await settings.isCloudEnabled();
+ assert.ok(typeof result === "boolean");
+});
+
+test("getCacheMetrics returns an object", async () => {
+ await resetStorage();
+ const result = await settings.getCacheMetrics();
+ assert.ok(typeof result === "object");
+});
+
+test("resetCacheMetrics does not throw", async () => {
+ await resetStorage();
+ assert.doesNotThrow(async () => await settings.resetCacheMetrics());
+});
+
+test("getCacheTrend returns an array", async () => {
+ await resetStorage();
+ const result = await settings.getCacheTrend(24);
+ assert.ok(Array.isArray(result));
+});
+
+test("clearAllLKGP does not throw", async () => {
+ await resetStorage();
+ assert.doesNotThrow(() => settings.clearAllLKGP());
+});
+
+test("getLKGP returns null for unknown combo/model", async () => {
+ await resetStorage();
+ const result = await settings.getLKGP("unknown-combo", "unknown-model");
+ assert.ok(result === null || typeof result === "object");
+});
+
+test("setLKGP and getLKGP round-trip", async () => {
+ await resetStorage();
+ await settings.setLKGP("test-combo", "test-model", {
+ provider: "test",
+ model: "test",
+ connectionId: "conn-1",
+ });
+ const result = await settings.getLKGP("test-combo", "test-model");
+ assert.ok(result === null || typeof result === "object");
+});
+
+test("getPricingWithSources returns pricing with source info", async () => {
+ await resetStorage();
+ const result = await settings.getPricingWithSources();
+ assert.ok(typeof result === "object");
+ assert.ok(result !== null);
+});
+
+test("resolveProxyForConnection returns proxy resolution", async () => {
+ await resetStorage();
+ const result = await settings.resolveProxyForConnection("test-conn-id");
+ assert.ok(typeof result === "object");
+});