mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-11-19 04:41:03 +00:00
Compare commits
4 Commits
fix/plugin
...
fix/cli-li
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3ee7ddb512 | ||
|
|
4b9062d5eb | ||
|
|
3f7b20170f | ||
|
|
ad6fe67161 |
@@ -4,6 +4,7 @@ import { setTimeout as sleep } from "node:timers/promises";
|
||||
import { apiFetch, getBaseUrl } from "../api.mjs";
|
||||
import { emit } from "../output.mjs";
|
||||
import { t } from "../i18n.mjs";
|
||||
import { parsePositiveInt } from "../utils/positiveInteger.mjs";
|
||||
|
||||
function fmtTs(v) {
|
||||
if (!v) return "-";
|
||||
@@ -97,7 +98,7 @@ export function registerBatches(program) {
|
||||
batches
|
||||
.command("list")
|
||||
.option("--status <s>", t("batches.list.status"))
|
||||
.option("--limit <n>", t("batches.list.limit"), parseInt, 50)
|
||||
.option("--limit <n>", t("batches.list.limit"), parsePositiveInt, 50)
|
||||
.action(async (opts, cmd) => {
|
||||
const params = new URLSearchParams({ limit: String(opts.limit) });
|
||||
if (opts.status) params.set("status", opts.status);
|
||||
|
||||
@@ -4,6 +4,7 @@ import { createInterface } from "node:readline";
|
||||
import { apiFetch, getBaseUrl } from "../api.mjs";
|
||||
import { emit } from "../output.mjs";
|
||||
import { t } from "../i18n.mjs";
|
||||
import { parsePositiveInt } from "../utils/positiveInteger.mjs";
|
||||
|
||||
function fmtTs(v) {
|
||||
if (!v) return "-";
|
||||
@@ -49,7 +50,7 @@ export function registerFiles(program) {
|
||||
files
|
||||
.command("list")
|
||||
.option("--purpose <p>", t("files.list.purpose"))
|
||||
.option("--limit <n>", t("files.list.limit"), parseInt, 100)
|
||||
.option("--limit <n>", t("files.list.limit"), parsePositiveInt, 100)
|
||||
.action(async (opts, cmd) => {
|
||||
const params = new URLSearchParams({ limit: String(opts.limit) });
|
||||
if (opts.purpose) params.set("purpose", opts.purpose);
|
||||
|
||||
@@ -13,6 +13,7 @@ import {
|
||||
import { encryptCredential } from "../encryption.mjs";
|
||||
import { openOmniRouteDb } from "../sqlite.mjs";
|
||||
import { t } from "../i18n.mjs";
|
||||
import { parsePositiveInt } from "../utils/positiveInteger.mjs";
|
||||
import { registerProviderCrud } from "./provider-crud.mjs";
|
||||
|
||||
function publicConnection(connection) {
|
||||
@@ -824,7 +825,7 @@ export function extendProvidersMetrics(providers) {
|
||||
.option("--period <p>", t("providers.metrics.period"), "24h")
|
||||
.option("--metric <m>", t("providers.metrics.metric"))
|
||||
.option("--sort-by <field>", t("providers.metrics.sort"))
|
||||
.option("--limit <n>", t("providers.metrics.limit"), parseInt, 50)
|
||||
.option("--limit <n>", t("providers.metrics.limit"), parsePositiveInt, 50)
|
||||
.option("--watch", t("providers.metrics.watch"))
|
||||
.option("--compare <list>", t("providers.metrics.compare"))
|
||||
.action(runProvidersMetrics);
|
||||
|
||||
@@ -12,7 +12,8 @@
|
||||
"dryRun": "[dry-run] would {action}",
|
||||
"cancelled": "Cancelled.",
|
||||
"jsonOpt": "Output as JSON",
|
||||
"yesOpt": "Skip confirmation prompt"
|
||||
"yesOpt": "Skip confirmation prompt",
|
||||
"positiveIntegerRequired": "Limit must be a positive integer."
|
||||
},
|
||||
"setup": {
|
||||
"title": "OmniRoute Setup",
|
||||
|
||||
18
bin/cli/utils/positiveInteger.mjs
Normal file
18
bin/cli/utils/positiveInteger.mjs
Normal file
@@ -0,0 +1,18 @@
|
||||
import { z } from "zod";
|
||||
import { InvalidArgumentError } from "commander";
|
||||
import { t } from "../i18n.mjs";
|
||||
|
||||
const positiveIntSchema = z
|
||||
.string()
|
||||
.trim()
|
||||
.regex(/^[0-9]+$/)
|
||||
.transform(Number)
|
||||
.pipe(z.number().int().positive().max(Number.MAX_SAFE_INTEGER));
|
||||
|
||||
export function parsePositiveInt(value) {
|
||||
const result = positiveIntSchema.safeParse(String(value ?? ""));
|
||||
if (!result.success) {
|
||||
throw new InvalidArgumentError(t("common.positiveIntegerRequired"));
|
||||
}
|
||||
return result.data;
|
||||
}
|
||||
1
changelog.d/fixes/18263-cli-limit-validation.md
Normal file
1
changelog.d/fixes/18263-cli-limit-validation.md
Normal file
@@ -0,0 +1 @@
|
||||
- **fix(cli):** Parse positive safe integer `--limit` values correctly for provider metrics, batch lists, and file lists instead of passing Commander's previous value to `parseInt` as a radix ([#18263](https://github.com/cheaper-inference/OmniRoute/pull/18263)) — thanks @mcspanky802.
|
||||
115
tests/unit/cli-limit-parsing.test.ts
Normal file
115
tests/unit/cli-limit-parsing.test.ts
Normal file
@@ -0,0 +1,115 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { describe, it } from "node:test";
|
||||
import { Command } from "commander";
|
||||
import { registerBatches } from "../../bin/cli/commands/batches.mjs";
|
||||
import { registerFiles } from "../../bin/cli/commands/files.mjs";
|
||||
import { extendProvidersMetrics } from "../../bin/cli/commands/providers.mjs";
|
||||
|
||||
function findSubcommand(root: Command, path: string[]): Command {
|
||||
let current: Command | undefined = root;
|
||||
for (const segment of path) {
|
||||
current = current?.commands.find((c) => c.name() === segment);
|
||||
assert.ok(current, `Expected subcommand "${segment}" to exist`);
|
||||
}
|
||||
return current;
|
||||
}
|
||||
|
||||
function buildProgramWithCapture(path: string[]) {
|
||||
const program = new Command();
|
||||
program.exitOverride();
|
||||
program.configureOutput({
|
||||
writeOut: () => {},
|
||||
writeErr: () => {},
|
||||
});
|
||||
|
||||
const providers = program.command("providers");
|
||||
extendProvidersMetrics(providers);
|
||||
registerBatches(program);
|
||||
registerFiles(program);
|
||||
|
||||
const target = findSubcommand(program, path);
|
||||
let capturedOpts: Record<string, unknown> | null = null;
|
||||
target.action((opts: Record<string, unknown>) => {
|
||||
capturedOpts = opts;
|
||||
});
|
||||
|
||||
return {
|
||||
parse: async (extraArgs: string[]) => {
|
||||
capturedOpts = null;
|
||||
await program.parseAsync(["node", "omniroute", ...path, ...extraArgs]);
|
||||
return capturedOpts;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const TARGET_COMMANDS: Array<{
|
||||
label: string;
|
||||
path: string[];
|
||||
defaultLimit: number;
|
||||
}> = [
|
||||
{ label: "providers metrics", path: ["providers", "metrics"], defaultLimit: 50 },
|
||||
{ label: "batches list", path: ["batches", "list"], defaultLimit: 50 },
|
||||
{ label: "files list", path: ["files", "list"], defaultLimit: 100 },
|
||||
];
|
||||
|
||||
describe("CLI --limit positive integer parsing regression", () => {
|
||||
for (const { label, path, defaultLimit } of TARGET_COMMANDS) {
|
||||
describe(label, () => {
|
||||
it(`uses default limit (${defaultLimit}) when --limit is omitted`, async () => {
|
||||
const { parse } = buildProgramWithCapture(path);
|
||||
const opts = await parse([]);
|
||||
assert.ok(opts);
|
||||
assert.equal(opts.limit, defaultLimit);
|
||||
});
|
||||
|
||||
it("parses explicit valid positive integer --limit without radix corruption", async () => {
|
||||
const { parse } = buildProgramWithCapture(path);
|
||||
for (const [raw, expected] of [
|
||||
["1", 1],
|
||||
["10", 10],
|
||||
["25", 25],
|
||||
["250", 250],
|
||||
[String(Number.MAX_SAFE_INTEGER), Number.MAX_SAFE_INTEGER],
|
||||
] as const) {
|
||||
const opts = await parse(["--limit", raw]);
|
||||
assert.ok(opts);
|
||||
assert.equal(opts.limit, expected);
|
||||
assert.equal(Number.isNaN(opts.limit), false);
|
||||
}
|
||||
});
|
||||
|
||||
it("rejects zero, negative, malformed, and unsafe integer --limit values", async () => {
|
||||
const { parse } = buildProgramWithCapture(path);
|
||||
const invalidValues = [
|
||||
"0",
|
||||
"00",
|
||||
"-1",
|
||||
"-50",
|
||||
"1.5",
|
||||
"10abc",
|
||||
"abc",
|
||||
"",
|
||||
" ",
|
||||
"NaN",
|
||||
"Infinity",
|
||||
"1e5",
|
||||
"0x10",
|
||||
"9007199254740992",
|
||||
"99999999999999999999",
|
||||
];
|
||||
|
||||
for (const bad of invalidValues) {
|
||||
await assert.rejects(
|
||||
() => parse(["--limit", bad]),
|
||||
(err: unknown) => {
|
||||
assert.ok(err instanceof Error);
|
||||
assert.match(err.message, /positive integer/i);
|
||||
return true;
|
||||
},
|
||||
`Expected "${bad}" to be rejected for ${label}`
|
||||
);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user