Skip to content

Chrome DevTools for coding agents

fix: report unknown page id before last-page guard in close_page (#3042)

Taras Misiukbdc7e243a8d3
diff --git a/src/McpContext.ts b/src/McpContext.ts
index 818e7c0a..b16d9ea2 100644
--- a/src/McpContext.ts
+++ b/src/McpContext.ts
@@ -346,10 +346,10 @@ export class McpContext implements Context {
return mcpPage;
}
async closePage(pageId: number): Promise<void> {
+ const page = this.getPageById(pageId);
if (this.#mcpPages.size === 1) {
throw new Error(CLOSE_PAGE_ERROR);
}
- const page = this.getPageById(pageId);
this.#mcpPages.delete(page.target);
await page.close();
}
diff --git a/tests/McpContext.test.ts b/tests/McpContext.test.ts
index a1193dc0..8535ebe2 100644
--- a/tests/McpContext.test.ts
+++ b/tests/McpContext.test.ts
@@ -384,6 +384,18 @@ describe('McpContext', () => {
});
});
+ it('reports an unknown page id before the last-page guard on close', async () => {
+ await withMcpContext(async (_response, context) => {
+ const page = context.getSelectedMcpPage();
+ await assert.rejects(context.closePage(99), /No page found/);
+ await assert.rejects(
+ context.closePage(page.id),
+ /The last open page cannot be closed/,
+ );
+ assert.ok(!page.pptrPage.isClosed());
+ });
+ });
+
it('reports the reconnect notice once', async () => {
await withBrowser(async browser => {
const context = await McpContext.from(