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

Fixes #2991

On a session with a single open page, calling `close_page` with a pageId
that doesn't exist returned "The last open page cannot be closed". That
points at the wrong problem.

`McpContext.closePage` now looks the page up first and only then applies
the last-page guard. An unknown id fails with "No page found". Closing
the real last page behaves as before.

Added a test to tests/McpContext.test.ts. It fails on main and passes
with this change.
This commit is contained in:
Taras Misiuk authored and GitHub committed 2026-10-13 16:54:53 +00:00
1 parent 274cd4cdba
commit bdc7e243a8
2 files changed
+13 -1

No files matched your search

+1 -1
View File
@@ -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();
}
+12
View File
@@ -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(