Files
OmniRoute/tests/unit/cli/model-completion-shell.test.ts
Diego Rodrigues de Sa e Souza 5282351753 feat(cli): validate and add models on an explicit strict connection (#14506)
Adds the opt-in 'omniroute models test-add' operation and POST /api/provider-models/validate-and-add: a model is registered only after bounded generation, streamed synthetic tool-call and continuation proofs on the selected connection. Protected surface (9 generated skills/**/SKILL.md + endpoints.md, flag documentation only): explicit owner approval given in chat; diff reviewed for external execution (none beyond a localhost curl example). Tip merged cleanly. Independent validation: 126/126 tests across 16 files, typecheck:core, open-sse typecheck, eslint on touched files, check:cycles, check:docs-all, file-size, changelog-integrity and complexity green. Route reviewed: strict Zod schema with allowInference literal true, management auth always required, errors via buildErrorBody, no client-supplied base URL. Not validated: live provider run on the VPS.
2026-10-01 22:22:46 -03:00

90 lines
3.2 KiB
TypeScript

import assert from "node:assert/strict";
import { execFileSync } from "node:child_process";
import test from "node:test";
import { Command } from "commander";
import { registerCompletion, runCompletionCommand } from "../../../bin/cli/commands/completion.mjs";
import { registerModels } from "../../../bin/cli/commands/models.mjs";
import { registerModelValidation } from "../../../bin/cli/commands/model-validation.mjs";
function registeredProgram() {
const program = new Command();
registerCompletion(program);
registerModels(program);
registerModelValidation(program);
return program;
}
async function completion(shell: string) {
const chunks: string[] = [];
const original = process.stdout.write;
process.stdout.write = ((chunk: string | Uint8Array) => {
chunks.push(String(chunk));
return true;
}) as typeof process.stdout.write;
try {
assert.equal(await runCompletionCommand(shell, registeredProgram()), 0);
} finally {
process.stdout.write = original;
}
return chunks.join("");
}
test(
"Bash actually completes models test-add, including after sibling command integration",
{
skip: process.platform === "win32",
},
async () => {
const script = await completion("bash");
const output = execFileSync("/bin/bash", ["--noprofile", "--norc"], {
input:
script +
'\nCOMP_WORDS=(omniroute models te)\nCOMP_CWORD=2\n_omniroute\nprintf "%s\\n" "${COMPREPLY[@]}"\n',
encoding: "utf8",
timeout: 5000,
env: { PATH: "/usr/bin:/bin" },
});
assert.equal(output.trim(), "test-add");
}
);
test("the first Zsh models branch exposes test-add rather than shadowing it", async () => {
const script = await completion("zsh");
const first = script.match(/^\s*models\)\s+_arguments '1:subcommand:\(([^)]+)\)'/m);
assert.ok(first, "a model subcommand branch must exist");
assert.ok(first[1].split(" ").includes("test-add"), "the reachable branch must expose test-add");
});
for (const shell of ["bash", "zsh", "fish"]) {
test(`${shell} has one models branch with exactly the registered subcommands`, async () => {
const script = await completion(shell);
const pattern =
shell === "bash"
? /^\s*models\).*compgen -W "([^"]*)"/gm
: shell === "zsh"
? /^\s*models\).*_arguments '1:subcommand:\(([^)]*)\)'/gm
: /^complete .*__fish_seen_subcommand_from models' -a '([^']*)'/gm;
const branches = [...script.matchAll(pattern)];
assert.equal(branches.length, 1);
const expected = registeredProgram()
.commands.find((command) => command.name() === "models")!
.commands.map((command) => command.name());
assert.deepEqual(branches[0][1].split(" "), expected);
});
}
test("Commander printing resolves models registered after completion", async () => {
const chunks: string[] = [];
const original = process.stdout.write;
process.stdout.write = ((chunk: string | Uint8Array) => {
chunks.push(String(chunk));
return true;
}) as typeof process.stdout.write;
try {
await registeredProgram().parseAsync(["completion", "bash"], { from: "user" });
} finally {
process.stdout.write = original;
}
assert.match(chunks.join(""), /test-add/);
});