From f25f3df2abfaad084fdd69e4c729ed64e159b20a Mon Sep 17 00:00:00 2001 From: Nikolay Vitkov <34244704+Lightning00Blade@users.noreply.github.com> Date: Thu, 22 Oct 2026 12:12:48 +0000 Subject: [PATCH] fix(network): reset page network emulation before applying URL restrictions (#3312) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Call emulateNetworkConditions(null) in McpPage.clearNetworkConditions() and await it before browser.restrictNetwork() in McpContext.syncNetworkRestrictions() so emulated offline/throttled state (such as navigator.onLine) is cleared when URL restrictions are hot-reloaded. --- Stack created with GitHub Stacks CLI • Give Feedback 💬 --- src/McpContext.ts | 10 +++++----- src/McpPage.ts | 3 ++- tests/McpContext.test.ts | 12 ++++++++++-- tests/McpPage.test.ts | 10 ++++++++-- 4 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/McpContext.ts b/src/McpContext.ts index 19103716..fa15ff67 100644 --- a/src/McpContext.ts +++ b/src/McpContext.ts @@ -251,17 +251,17 @@ export class McpContext implements Context, BrowserlessContext { */ async syncNetworkRestrictions(): Promise { const {blockedUrlPattern, allowedUrlPattern} = this.#config.resolved; + if (blockedUrlPattern || allowedUrlPattern) { + for (const mcpPage of this.#allMcpPages()) { + await mcpPage.clearNetworkConditions(); + } + } if (this.#browser) { await this.#browser.restrictNetwork({ blocklist: blockedUrlPattern, allowlist: allowedUrlPattern, }); } - if (blockedUrlPattern || allowedUrlPattern) { - for (const mcpPage of this.#allMcpPages()) { - mcpPage.clearNetworkConditions(); - } - } } /** diff --git a/src/McpPage.ts b/src/McpPage.ts index bb8ff94a..4d74d91d 100644 --- a/src/McpPage.ts +++ b/src/McpPage.ts @@ -1337,10 +1337,11 @@ export class McpPage implements ContextPage { } } - clearNetworkConditions(): void { + async clearNetworkConditions(): Promise { if (!this.emulationSettings.networkConditions) { return; } + await this.#pptrPage?.emulateNetworkConditions(null); delete this.emulationSettings.networkConditions; this.updateTimeouts(); } diff --git a/tests/McpContext.test.ts b/tests/McpContext.test.ts index 5ddd2b78..9ed5c124 100644 --- a/tests/McpContext.test.ts +++ b/tests/McpContext.test.ts @@ -1747,11 +1747,15 @@ describe('McpContext', () => { await updateConfig(config, context, { blockedUrlPattern: ['https://blocked.example.com/*'], }); + sinon.assert.calledOnceWithExactly(clearNetworkConditionsSpy); sinon.assert.calledOnceWithExactly(browser.restrictNetwork, { blocklist: ['https://blocked.example.com/*'], allowlist: undefined, }); - sinon.assert.calledOnceWithExactly(clearNetworkConditionsSpy); + sinon.assert.callOrder( + clearNetworkConditionsSpy, + browser.restrictNetwork, + ); browser.restrictNetwork.resetHistory(); clearNetworkConditionsSpy.resetHistory(); @@ -1765,11 +1769,15 @@ describe('McpContext', () => { blockedUrlPattern: undefined, allowedUrlPattern: ['https://allowed.example.com/*'], }); + sinon.assert.calledOnceWithExactly(clearNetworkConditionsSpy); sinon.assert.calledOnceWithExactly(browser.restrictNetwork, { blocklist: undefined, allowlist: ['https://allowed.example.com/*'], }); - sinon.assert.calledOnceWithExactly(clearNetworkConditionsSpy); + sinon.assert.callOrder( + clearNetworkConditionsSpy, + browser.restrictNetwork, + ); browser.restrictNetwork.resetHistory(); clearNetworkConditionsSpy.resetHistory(); diff --git a/tests/McpPage.test.ts b/tests/McpPage.test.ts index 4a6d8088..67867d82 100644 --- a/tests/McpPage.test.ts +++ b/tests/McpPage.test.ts @@ -1015,6 +1015,7 @@ describe('McpPage', () => { networkConditions: 'Slow 3G', cpuThrottlingRate: 2, }); + pptrPage.emulateNetworkConditions.resetHistory(); pptrPage.setDefaultTimeout.resetHistory(); pptrPage.setDefaultNavigationTimeout.resetHistory(); config.commit( @@ -1023,10 +1024,14 @@ describe('McpPage', () => { }), ); - mcpPage.clearNetworkConditions(); + await mcpPage.clearNetworkConditions(); assert.strictEqual(mcpPage.networkConditions, null); assert.strictEqual(mcpPage.cpuThrottlingRate, 2); + sinon.assert.calledOnceWithExactly( + pptrPage.emulateNetworkConditions, + null, + ); sinon.assert.calledOnceWithExactly(pptrPage.setDefaultTimeout, 10_000); sinon.assert.calledOnceWithExactly( pptrPage.setDefaultNavigationTimeout, @@ -1040,8 +1045,9 @@ describe('McpPage', () => { pptrPage.setDefaultTimeout.resetHistory(); pptrPage.setDefaultNavigationTimeout.resetHistory(); - mcpPage.clearNetworkConditions(); + await mcpPage.clearNetworkConditions(); + sinon.assert.notCalled(pptrPage.emulateNetworkConditions); sinon.assert.notCalled(pptrPage.setDefaultTimeout); sinon.assert.notCalled(pptrPage.setDefaultNavigationTimeout); });