mirror of
https://github.com/openclaw/openclaw.git
synced 2026-06-03 21:44:05 +00:00
fix(browser): centralize cli index parsing
This commit is contained in:
@@ -179,6 +179,12 @@ describe("browser cli snapshot defaults", () => {
|
||||
expect(params?.query?.depth).toBe(0);
|
||||
});
|
||||
|
||||
it("accepts signed decimal snapshot numeric options", async () => {
|
||||
const params = await runSnapshot(["--limit", "+10", "--depth", "+0"]);
|
||||
expect(params?.query?.limit).toBe(10);
|
||||
expect(params?.query?.depth).toBe(0);
|
||||
});
|
||||
|
||||
it("sends screenshot request with trimmed target id and jpeg type", async () => {
|
||||
const params = await runBrowserInspect(["screenshot", " tab-1 ", "--type", "jpeg"], true);
|
||||
expect(params?.path).toBe("/screenshot");
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
import fs from "node:fs/promises";
|
||||
import type { Command } from "commander";
|
||||
import { normalizeOptionalString } from "openclaw/plugin-sdk/string-coerce-runtime";
|
||||
import { callBrowserRequest, type BrowserParentOpts } from "./browser-cli-shared.js";
|
||||
import {
|
||||
callBrowserRequest,
|
||||
parseBrowserNonNegativeIntegerValue,
|
||||
parseBrowserPositiveIntegerValue,
|
||||
type BrowserParentOpts,
|
||||
} from "./browser-cli-shared.js";
|
||||
import {
|
||||
danger,
|
||||
defaultRuntime,
|
||||
@@ -18,9 +23,11 @@ function parseOptionalIntegerOption(
|
||||
if (value === undefined) {
|
||||
return undefined;
|
||||
}
|
||||
const raw = value.trim();
|
||||
const parsed = /^\d+$/.test(raw) ? Number(raw) : Number.NaN;
|
||||
if (!Number.isSafeInteger(parsed) || parsed < opts.min) {
|
||||
const parsed =
|
||||
opts.min === 0
|
||||
? parseBrowserNonNegativeIntegerValue(value)
|
||||
: parseBrowserPositiveIntegerValue(value);
|
||||
if (parsed === undefined || parsed < opts.min) {
|
||||
defaultRuntime.error(danger(`Invalid ${label}: must be an integer >= ${opts.min}`));
|
||||
defaultRuntime.exit(1);
|
||||
return undefined;
|
||||
|
||||
@@ -285,6 +285,21 @@ describe("browser manage output", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it("accepts signed decimal tab indexes", async () => {
|
||||
const program = createBrowserManageProgram();
|
||||
|
||||
await program.parseAsync(["browser", "tab", "select", "+2"], { from: "user" });
|
||||
|
||||
expect(getBrowserManageCallBrowserRequestMock()).toHaveBeenCalledWith(
|
||||
expect.anything(),
|
||||
expect.objectContaining({
|
||||
path: "/tabs/action",
|
||||
body: { action: "select", index: 1 },
|
||||
}),
|
||||
expect.anything(),
|
||||
);
|
||||
});
|
||||
|
||||
it("prints a readable browser doctor report", async () => {
|
||||
getBrowserManageCallBrowserRequestMock().mockImplementation(async (_opts: unknown, req) => {
|
||||
if (req.path === "/") {
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
import type { Command } from "commander";
|
||||
import { runCommandWithRuntime } from "../core-api.js";
|
||||
import { callBrowserRequest, type BrowserParentOpts } from "./browser-cli-shared.js";
|
||||
import {
|
||||
callBrowserRequest,
|
||||
parseBrowserPositiveIntegerValue,
|
||||
type BrowserParentOpts,
|
||||
} from "./browser-cli-shared.js";
|
||||
import {
|
||||
danger,
|
||||
defaultRuntime,
|
||||
@@ -113,8 +117,7 @@ function runBrowserCommand(action: () => Promise<void>) {
|
||||
}
|
||||
|
||||
function parseTabIndex(value: string): number {
|
||||
const trimmed = value.trim();
|
||||
return /^\d+$/.test(trimmed) ? Number(trimmed) : Number.NaN;
|
||||
return parseBrowserPositiveIntegerValue(value) ?? Number.NaN;
|
||||
}
|
||||
|
||||
function logBrowserTabs(tabs: BrowserTab[], json?: boolean) {
|
||||
|
||||
Reference in New Issue
Block a user