diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fdb163f..68be872 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -66,6 +66,23 @@ jobs: run: npm run test:coverage env: FIREFOX_PATH: ${{ steps.setup-firefox.outputs.firefox-path }} + DEBUG: ${{ matrix.os == 'windows-latest' && 'firefox-mcp' || '' }} + + - name: Repeat restricted session tests + if: ${{ always() && matrix.os == 'windows-latest' }} + shell: bash + env: + FIREFOX_PATH: ${{ steps.setup-firefox.outputs.firefox-path }} + DEBUG: firefox-mcp + run: | + failures=0 + for i in $(seq 1 20); do + echo "::group::Iteration $i" + npx vitest run tests/integration/mcp/restricted-session.test.ts tests/integration/mcp/restricted-session-restart.test.ts || { failures=$((failures + 1)); echo "::warning::Iteration $i failed"; } + echo "::endgroup::" + done + echo "Failed iterations: $failures / 20" + [ "$failures" -eq 0 ] - name: Upload coverage reports to Codecov if: >- diff --git a/src/firefox/bidi.ts b/src/firefox/bidi.ts index 051d0fe..c1c679a 100644 --- a/src/firefox/bidi.ts +++ b/src/firefox/bidi.ts @@ -140,14 +140,16 @@ export class BiDiFacade extends EventEmitter { * depends on a Firefox pref. A restricted session rejects browser.getUserContexts. */ async detectRestrictedUserContext(userContext: string): Promise { + const start = Date.now(); try { await this.sendCommand('browser.getUserContexts', {}); + logDebug(`browser.getUserContexts succeeded after ${Date.now() - start}ms`); } catch (error) { - if (error instanceof RestrictedSessionError) { - this.userContext = userContext; - } else { - logDebug('Unable to check if the session is restricted', error); + logDebug(`browser.getUserContexts failed after ${Date.now() - start}ms`, error); + if (!(error instanceof RestrictedSessionError)) { + throw error; } + this.userContext = userContext; } } @@ -183,12 +185,21 @@ export class BiDiFacade extends EventEmitter { params: FirefoxCommands[T]['params'] = {}, { timeout = 10000 }: { timeout?: number } = {} ): Promise { + const start = Date.now(); const bidi = await this.driver.getBidi(); // bidi.socket is a Node.js `ws` WebSocket (EventEmitter-style), but typed as browser WebSocket const ws = bidi.socket as any; + if (ws.readyState !== 1) { + logDebug( + `${method}: getBidi took ${Date.now() - start}ms, WebSocket readyState ${ws.readyState}` + ); + } // Wait for WebSocket to be ready before sending await this.waitForWebSocketOpen(ws); + if (Date.now() - start > 500) { + logDebug(`${method}: WebSocket ready after ${Date.now() - start}ms`); + } const id = this.nextCommandId++; diff --git a/tests/helpers/mcp.ts b/tests/helpers/mcp.ts index c228d0b..1adcb5b 100644 --- a/tests/helpers/mcp.ts +++ b/tests/helpers/mcp.ts @@ -70,7 +70,7 @@ export async function startMcpServer(serverArgs: string[] = []): Promise