mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-23 23:52:18 +03:00
Validated on the combined batch board over tip 8a42aeeb: static gates clean (changelog, file-size 159 frozen, complexity 2621<=2774, cognitive 1181<=1223, dead-code 408<=416), typecheck:core clean, 107 focused tests green.
This drains the remaining #9985 tail — the v2 dual-write assertions (guide-settings/t40), the zai→z-ai stale id, the hackclub leftovers, and the #11162 cli-combo models (#11203). Note: guide-settings also trips this devbox's container guard (/.dockerenv present), an environment artifact unrelated to CI. Base is green again. Thank you @jonlwheat2-gif!
110 lines
3.8 KiB
TypeScript
110 lines
3.8 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 ORIGINAL_DATA_DIR = process.env.DATA_DIR;
|
|
const ORIGINAL_FETCH = globalThis.fetch;
|
|
|
|
function createTempDataDir() {
|
|
return fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-cli-combo-"));
|
|
}
|
|
|
|
async function withComboEnv(fn: (dataDir: string) => Promise<void>) {
|
|
const dataDir = createTempDataDir();
|
|
process.env.DATA_DIR = dataDir;
|
|
// Mock fetch → simulates server offline so withRuntime falls back to DB
|
|
globalThis.fetch = (async () => {
|
|
throw new Error("server offline");
|
|
}) as typeof fetch;
|
|
|
|
const originalLog = console.log;
|
|
console.log = () => {};
|
|
|
|
try {
|
|
await fn(dataDir);
|
|
} finally {
|
|
console.log = originalLog;
|
|
globalThis.fetch = ORIGINAL_FETCH;
|
|
fs.rmSync(dataDir, { recursive: true, force: true });
|
|
|
|
if (ORIGINAL_DATA_DIR === undefined) delete process.env.DATA_DIR;
|
|
else process.env.DATA_DIR = ORIGINAL_DATA_DIR;
|
|
}
|
|
}
|
|
|
|
test("combo create inserts a new combo via db module", async () => {
|
|
await withComboEnv(async () => {
|
|
const { runComboCreateCommand } = await import("../../bin/cli/commands/combo.mjs");
|
|
// #11162: combo create refuses combos without any model — pass a model
|
|
// like the sibling tests updated in that commit.
|
|
const result = await runComboCreateCommand("my-combo", "priority", {
|
|
models: ["openai/gpt-4o-mini"],
|
|
});
|
|
assert.equal(result, 0);
|
|
|
|
// Verify via the same db module
|
|
const { getComboByName } = await import("../../src/lib/db/combos.ts");
|
|
const combo = await getComboByName("my-combo");
|
|
assert.ok(combo);
|
|
assert.equal(combo.name, "my-combo");
|
|
assert.equal(combo.strategy, "priority");
|
|
});
|
|
});
|
|
|
|
test("combo create fails if combo already exists", async () => {
|
|
await withComboEnv(async () => {
|
|
const { runComboCreateCommand } = await import("../../bin/cli/commands/combo.mjs");
|
|
|
|
await runComboCreateCommand("dup-combo", "auto", { models: ["openai/gpt-4o-mini"] });
|
|
const originalError = console.error;
|
|
console.error = () => {};
|
|
const result = await runComboCreateCommand("dup-combo", "auto", {
|
|
models: ["openai/gpt-4o-mini"],
|
|
});
|
|
console.error = originalError;
|
|
|
|
assert.equal(result, 1);
|
|
});
|
|
});
|
|
|
|
test("combo delete removes the combo", async () => {
|
|
await withComboEnv(async () => {
|
|
const { runComboCreateCommand, runComboDeleteCommand } =
|
|
await import("../../bin/cli/commands/combo.mjs");
|
|
|
|
await runComboCreateCommand("to-delete", "weighted", { models: ["openai/gpt-4o-mini"] });
|
|
const result = await runComboDeleteCommand("to-delete", { yes: true });
|
|
assert.equal(result, 0);
|
|
|
|
const { getComboByName } = await import("../../src/lib/db/combos.ts");
|
|
const combo = await getComboByName("to-delete");
|
|
assert.equal(combo, null);
|
|
});
|
|
});
|
|
|
|
test("combo list returns 0 with empty combos table", async () => {
|
|
await withComboEnv(async () => {
|
|
const { runComboListCommand } = await import("../../bin/cli/commands/combo.mjs");
|
|
const result = await runComboListCommand({});
|
|
assert.equal(result, 0);
|
|
});
|
|
});
|
|
|
|
test("combo switch updates active combo when server is offline", async () => {
|
|
await withComboEnv(async () => {
|
|
const { runComboCreateCommand, runComboSwitchCommand } =
|
|
await import("../../bin/cli/commands/combo.mjs");
|
|
|
|
await runComboCreateCommand("my-switch", "round-robin", { models: ["openai/gpt-4o-mini"] });
|
|
const result = await runComboSwitchCommand("my-switch", {});
|
|
assert.equal(result, 0);
|
|
|
|
// Verify active combo written to key_value settings
|
|
const { getSettings } = await import("../../src/lib/db/settings.ts");
|
|
const settings = await getSettings();
|
|
assert.equal((settings as Record<string, unknown>).activeCombo, "my-switch");
|
|
});
|
|
});
|