diff --git a/electron/e2e/sso.e2e.ts b/electron/e2e/sso.e2e.ts index 024dee44a..b2ef1bdb0 100644 --- a/electron/e2e/sso.e2e.ts +++ b/electron/e2e/sso.e2e.ts @@ -3,28 +3,52 @@ // the mock account's opens Tsinghua's login page in a web view, after the // confirmation, and the page's script fills in learnX's form there. Nothing // is sent to Tsinghua: the made-up account is never submitted. Needs -// id.tsinghua.edu.cn, which CI's runners abroad can't always reach: tagged -// @network, and skipped (not failed) when it doesn't answer, whether before -// the test or when the web view loads it. +// learn.tsinghua.edu.cn, where the web view starts, and id.tsinghua.edu.cn, +// where it lands, which CI's runners abroad can't always reach: tagged +// @network, and skipped (not failed) when they don't answer, whether before +// the test or while the web view loads them. import type { Page } from '@playwright/test'; import { expect, test } from './fixtures.ts'; import { exact } from './maestroEnv.ts'; const NOBODY = 'learnx-e2e-nobody'; -const SSO = 'https://id.tsinghua.edu.cn/'; +/** The sites the web view loads, in order (SSO.tsx's SSO_LOGIN_URL). */ +const SITES = ['https://learn.tsinghua.edu.cn/', 'https://id.tsinghua.edu.cn/']; -/** Whether Tsinghua's login site answers within a few seconds. */ -const reachable = async () => { +/** Whether `url` is on one of Tsinghua's sites. */ +const isTsinghua = (url: string) => { try { - await fetch(SSO, { method: 'HEAD', signal: AbortSignal.timeout(10_000) }); - return true; + return new URL(url).hostname.endsWith('.tsinghua.edu.cn'); } catch { return false; } }; +/** + * Why one of the sites doesn't answer within a few seconds, as the site and + * the error's code (ECONNRESET, UND_ERR_CONNECT_TIMEOUT, TimeoutError...), + * or null when all of them answer, whatever their status. + */ +const unreachable = async () => { + for (const site of SITES) { + try { + await fetch(site, { + method: 'HEAD', + signal: AbortSignal.timeout(10_000), + }); + } catch (error) { + const { cause, name } = error as Error & { + cause?: { code?: string }; + }; + return `${site}: ${cause?.code ?? name}`; + } + } + return null; +}; + test.beforeEach(async () => { - test.skip(!(await reachable()), `${SSO} doesn't answer from here`); + const reason = await unreachable(); + test.skip(reason !== null, `Doesn't answer from here: ${reason}`); }); /** @@ -43,34 +67,57 @@ const guest = (window: Page, script: string) => } }, script); -interface LoadFailure { - code: number; - description: string; - url: string; +/** The web view's main frame loads, as recordLoads keeps them. */ +interface Loads { + /** The page it last started to load. */ + url: string | null; + /** Whether that page got as far as `dom-ready`. */ + ready: boolean; + /** Its failed loads. */ + failures: { code: number; description: string; url: string }[]; } /** - * Keeps the web view's failed main frame loads in the page, from the - * `did-fail-load` events Electron sends the . They don't bubble, so - * a capturing listener on the document hears them. Call it before the web - * view opens. + * Keeps the web view's main frame loads in the page, from the events + * Electron sends the . They don't bubble, so capturing listeners on + * the document hear them. Call it before the web view opens. */ -const recordLoadFailures = (window: Page) => +const recordLoads = (window: Page) => window.evaluate(() => { - const failures: LoadFailure[] = []; - Object.assign(globalThis, { e2eLoadFailures: failures }); + const loads: Loads = { url: null, ready: false, failures: [] }; + Object.assign(globalThis, { e2eLoads: loads }); + type WebviewEvent = Event & { + url: string; + isMainFrame: boolean; + errorCode: number; + errorDescription: string; + validatedURL: string; + }; + document.addEventListener( + 'did-start-navigation', + event => { + const { url, isMainFrame } = event as WebviewEvent; + if (isMainFrame) { + loads.url = url; + loads.ready = false; + } + }, + true, + ); + document.addEventListener( + 'dom-ready', + () => { + loads.ready = true; + }, + true, + ); document.addEventListener( 'did-fail-load', event => { const { errorCode, errorDescription, validatedURL, isMainFrame } = - event as Event & { - errorCode: number; - errorDescription: string; - validatedURL: string; - isMainFrame: boolean; - }; + event as WebviewEvent; if (isMainFrame) { - failures.push({ + loads.failures.push({ code: errorCode, description: errorDescription, url: validatedURL, @@ -81,6 +128,11 @@ const recordLoadFailures = (window: Page) => ); }); +const loadsSoFar = (window: Page) => + window.evaluate( + () => (globalThis as unknown as { e2eLoads: Loads }).e2eLoads, + ); + /** * Whether a Chromium net error is the network's: a time out (-7), a changed * network (-21), a connection or host name error (-100 to -199) or an empty @@ -94,40 +146,47 @@ const isNetworkError = (code: number) => (code <= -100 && code >= -199); /** - * Waits for the web view to show Tsinghua's login page. When the web view - * couldn't load it for the network's sake (recordLoadFailures), the site - * stopped answering after the check before the test, so this skips as that - * check does. + * Waits for the web view to show Tsinghua's login page. The sites can stop + * answering after the check before the test, so this skips as that check + * does when the web view's load of one of them fails with a network error, + * or is still loading one of them when the wait ends. */ const expectSSOPage = async (window: Page) => { - let failure: LoadFailure | undefined; - await expect - .poll( - async () => { - const failures = await window.evaluate( - () => - (globalThis as unknown as { e2eLoadFailures: LoadFailure[] }) - .e2eLoadFailures, - ); - failure = failures.find( - ({ code, url }) => url.startsWith(SSO) && isNetworkError(code), - ); - return failure ? 'failed' : guest(window, 'location.hostname'); - }, - { timeout: 30_000 }, - ) - .toMatch(/^(?:id\.tsinghua\.edu\.cn|failed)$/); - test.skip( - failure !== undefined, - `${SSO} stopped answering: ${failure?.description} (${failure?.code})`, - ); + const networkFailure = ({ failures }: Loads) => + failures.find(({ code, url }) => isTsinghua(url) && isNetworkError(code)); + let reason: string | undefined; + try { + await expect + .poll( + async () => + networkFailure(await loadsSoFar(window)) + ? 'failed' + : guest(window, 'location.hostname'), + { timeout: 30_000 }, + ) + .toMatch(/^(?:id\.tsinghua\.edu\.cn|failed)$/); + } catch (error) { + // A failure may land just as the wait ends. + const loads = await loadsSoFar(window); + if (networkFailure(loads) === undefined) { + if (loads.ready || loads.url === null || !isTsinghua(loads.url)) { + throw error; + } + reason = `${loads.url}: still loading after 30 s`; + } + } + const failure = networkFailure(await loadsSoFar(window)); + if (failure) { + reason = `${failure.url}: ${failure.description} (${failure.code})`; + } + test.skip(reason !== undefined, `Stopped answering: ${reason}`); }; const fillLogin = async (window: Page) => { await window.getByTestId('welcome-continue').click(); await window.getByTestId('login-username').fill(NOBODY); await window.getByTestId('login-password').fill('not-a-password'); - await recordLoadFailures(window); + await recordLoads(window); }; test(