Skip to content

Commit d4a0194

Browse files
Leon-SK668steipete
andcommitted
fix(browser): honor explicit inspection timeouts
Co-authored-by: Peter Steinberger <[email protected]>
1 parent 12dff76 commit d4a0194

2 files changed

Lines changed: 99 additions & 10 deletions

File tree

‎extensions/browser/src/cli/browser-cli-inspect.test.ts‎

Lines changed: 73 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ function installInspectSpies() {
9494

9595
describe("browser cli snapshot defaults", () => {
9696
const runBrowserInspect = async (args: string[], withJson = false) => {
97-
const program = new Command();
97+
const program = new Command().enablePositionalOptions();
9898
const browser = program
9999
.command("browser")
100100
.option("--json", "JSON output", false)
@@ -127,20 +127,85 @@ describe("browser cli snapshot defaults", () => {
127127
});
128128

129129
it.each([
130-
{ command: "screenshot", requestPath: "/screenshot", timeout: "30000" },
131-
{ command: "screenshot", requestPath: "/screenshot", timeout: "60000" },
132-
{ command: "snapshot", requestPath: "/snapshot", timeout: "30000" },
133-
{ command: "snapshot", requestPath: "/snapshot", timeout: "60000" },
130+
{
131+
command: "screenshot",
132+
requestPath: "/screenshot",
133+
args: ["screenshot"],
134+
timeout: "30000",
135+
ownerTimeoutMs: undefined,
136+
},
137+
{
138+
command: "screenshot",
139+
requestPath: "/screenshot",
140+
args: ["--timeout", "60000", "screenshot"],
141+
timeout: "60000",
142+
ownerTimeoutMs: 60000,
143+
},
144+
{
145+
command: "screenshot",
146+
requestPath: "/screenshot",
147+
args: ["screenshot", "tab-42", "--timeout", "60000"],
148+
timeout: "60000",
149+
ownerTimeoutMs: 60000,
150+
targetId: "tab-42",
151+
},
152+
{
153+
command: "screenshot",
154+
requestPath: "/screenshot",
155+
args: ["--timeout", "60000", "screenshot", "--timeout", "90000"],
156+
timeout: "90000",
157+
ownerTimeoutMs: 90000,
158+
},
159+
{
160+
command: "snapshot",
161+
requestPath: "/snapshot",
162+
args: ["snapshot"],
163+
timeout: "30000",
164+
ownerTimeoutMs: undefined,
165+
},
166+
{
167+
command: "snapshot",
168+
requestPath: "/snapshot",
169+
args: ["--timeout", "60000", "snapshot"],
170+
timeout: "60000",
171+
ownerTimeoutMs: 60000,
172+
},
173+
{
174+
command: "snapshot",
175+
requestPath: "/snapshot",
176+
args: ["snapshot", "--timeout", "60000"],
177+
timeout: "60000",
178+
ownerTimeoutMs: 60000,
179+
},
180+
{
181+
command: "snapshot",
182+
requestPath: "/snapshot",
183+
args: ["--timeout", "60000", "snapshot", "--timeout", "90000"],
184+
timeout: "90000",
185+
ownerTimeoutMs: 90000,
186+
},
134187
])(
135-
"inherits parent $timeout ms timeout for $command",
136-
async ({ command, requestPath, timeout }) => {
137-
const args = timeout === "30000" ? [command] : ["--timeout", timeout, command];
188+
"keeps the gateway and $command owner on the explicit $timeout ms timeout",
189+
async ({ command, requestPath, args, timeout, ownerTimeoutMs, targetId }) => {
138190
await runBrowserInspect(args, true);
139191

140192
expect(sharedMocks.callBrowserRequest).toHaveBeenLastCalledWith(
141193
expect.objectContaining({ timeout }),
142194
expect.objectContaining({ path: requestPath }),
143195
);
196+
const [, request] = sharedMocks.callBrowserRequest.mock.calls.at(-1) ?? [];
197+
const payload = request as {
198+
query?: { timeoutMs?: number };
199+
body?: { timeoutMs?: number; targetId?: string };
200+
};
201+
const ownerPayload = command === "snapshot" ? payload.query : payload.body;
202+
expect(ownerPayload?.timeoutMs).toBe(ownerTimeoutMs);
203+
if (ownerTimeoutMs === undefined) {
204+
expect(ownerPayload).not.toHaveProperty("timeoutMs");
205+
}
206+
if (targetId !== undefined) {
207+
expect(payload.body?.targetId).toBe(targetId);
208+
}
144209
},
145210
);
146211

‎extensions/browser/src/cli/browser-cli-inspect.ts‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ import {
1010
BROWSER_TAB_REFERENCE_HELP,
1111
callBrowserRequest,
1212
parseBrowserNonNegativeIntegerValue,
13+
parseBrowserPositiveIntegerOption,
1314
parseBrowserPositiveIntegerValue,
1415
type BrowserParentOpts,
1516
} from "./browser-cli-shared.js";
1617
import {
1718
danger,
1819
defaultRuntime,
1920
getRuntimeConfig,
21+
inheritOptionFromParent,
2022
shortenHomePath,
2123
type SnapshotResult,
2224
} from "./core-api.js";
@@ -54,6 +56,22 @@ function parseBrowserChoiceOption<const T extends string>(
5456
return undefined;
5557
}
5658

59+
function resolveBrowserInspectTimeout(
60+
command: Command,
61+
parent: BrowserParentOpts,
62+
value: string | undefined,
63+
): { parent: BrowserParentOpts; timeoutMs?: number } {
64+
const timeout =
65+
command.getOptionValueSource("timeout") === "cli"
66+
? value
67+
: inheritOptionFromParent<string>(command, "timeout", "cli");
68+
if (timeout === undefined) {
69+
return { parent };
70+
}
71+
const timeoutMs = parseBrowserPositiveIntegerOption(timeout, "--timeout");
72+
return { parent: { ...parent, timeout: String(timeoutMs) }, timeoutMs };
73+
}
74+
5775
/** Registers Browser screenshot and snapshot commands. */
5876
export function registerBrowserInspectCommands(
5977
browser: Command,
@@ -72,6 +90,7 @@ export function registerBrowserInspectCommands(
7290
false,
7391
)
7492
.option("--type <png|jpeg>", "Output type (default: png)", "png")
93+
.option("--timeout <ms>", "Timeout in ms")
7594
.action(async (targetId: string | undefined, opts, cmd) => {
7695
const parent = parentOpts(cmd);
7796
const profile = parent?.browserProfile;
@@ -80,7 +99,8 @@ export function registerBrowserInspectCommands(
8099
return;
81100
}
82101
try {
83-
const result = await callBrowserRequest<{ path: string }>(parent, {
102+
const request = resolveBrowserInspectTimeout(cmd, parent, opts.timeout);
103+
const result = await callBrowserRequest<{ path: string }>(request.parent, {
84104
method: "POST",
85105
path: "/screenshot",
86106
query: profile ? { profile } : undefined,
@@ -91,6 +111,7 @@ export function registerBrowserInspectCommands(
91111
element: normalizeOptionalString(opts.element),
92112
labels: Boolean(opts.labels),
93113
type,
114+
...(request.timeoutMs === undefined ? {} : { timeoutMs: request.timeoutMs }),
94115
},
95116
});
96117
if (parent?.json) {
@@ -120,6 +141,7 @@ export function registerBrowserInspectCommands(
120141
.option("--labels", "Include label overlay screenshot with annotations", false)
121142
.option("--urls", "Append discovered link URLs to AI snapshots", false)
122143
.option("--out <path>", "Write snapshot to a file")
144+
.option("--timeout <ms>", "Timeout in ms")
123145
.action(async (opts, cmd: Command) => {
124146
const parent = parentOpts(cmd);
125147
const profile = parent?.browserProfile;
@@ -152,6 +174,7 @@ export function registerBrowserInspectCommands(
152174
return;
153175
}
154176
try {
177+
const request = resolveBrowserInspectTimeout(cmd, parent, opts.timeout);
155178
const query: Record<string, string | number | boolean | undefined> = {
156179
format,
157180
targetId: normalizeOptionalString(opts.targetId),
@@ -165,8 +188,9 @@ export function registerBrowserInspectCommands(
165188
urls: opts.urls ? true : undefined,
166189
mode,
167190
profile,
191+
...(request.timeoutMs === undefined ? {} : { timeoutMs: request.timeoutMs }),
168192
};
169-
const result = await callBrowserRequest<SnapshotResult>(parent, {
193+
const result = await callBrowserRequest<SnapshotResult>(request.parent, {
170194
method: "GET",
171195
path: "/snapshot",
172196
query,

0 commit comments

Comments
 (0)