Skip to content

Commit dbcdf55

Browse files
robertyingclaude
andauthored
Run the Electron E2E tests without internet access (#1808)
The SSO tests serve a saved login page at id.tsinghua.edu.cn's URLs, and the file and download tests serve the mock account's files from mock-files/, through an https handler on the web views' session. The SSO reachability pre-check and network skips are removed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent e82dfc6 commit dbcdf55

7 files changed

Lines changed: 152 additions & 206 deletions

File tree

‎electron/e2e/downloads.e2e.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// the flow does) and deletes them.
44
import fs from 'node:fs';
55
import path from 'node:path';
6-
import { expect, test } from './fixtures.ts';
6+
import { expect, mockFilesDir, test } from './fixtures.ts';
77
import { exact, text } from './maestroEnv.ts';
88
import {
99
byId,
@@ -14,6 +14,11 @@ import {
1414
shown,
1515
} from './steps.ts';
1616

17+
// The mock account's files come from this checkout, not GitHub.
18+
test.beforeEach(async ({ main }) => {
19+
await main.serveSites({ mockFilesDir });
20+
});
21+
1722
test('downloads a file, exports it, then deletes it', async ({
1823
window,
1924
main,

‎electron/e2e/files.e2e.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@
66
import fs from 'node:fs';
77
import path from 'node:path';
88
import type { ElectronApplication } from '@playwright/test';
9-
import { expect, test } from './fixtures.ts';
9+
import { expect, mockFilesDir, test } from './fixtures.ts';
1010
import { exact, text } from './maestroEnv.ts';
1111
import { byId, card, goBack, openCard, openTab, shown } from './steps.ts';
1212

13+
// The mock account's files come from this checkout, not GitHub.
14+
test.beforeEach(async ({ main }) => {
15+
await main.serveSites({ mockFilesDir });
16+
});
17+
1318
/** The URLs of the window's frames: the PDF preview is one of them. */
1419
const frameUrls = (app: ElectronApplication) =>
1520
app.evaluate(({ BrowserWindow }) =>

‎electron/e2e/fixtures.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ import { mainProcess, type MainProcess } from './mainProcess.ts';
2929
const electronDir = path.join(import.meta.dirname, '..');
3030
const root = path.join(electronDir, '..');
3131

32+
/** The mock account's files (`main.serveSites`). */
33+
export const mockFilesDir = path.join(root, 'mock-files');
34+
3235
/** The packaged app under test, if any (LEARNX_E2E_APP). */
3336
export const packagedApp = process.env.LEARNX_E2E_APP;
3437

‎electron/e2e/mainProcess.ts‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
// for the test to read back. The fixtures install them before a test starts,
66
// so nothing reaches the system.
77
import type { ElectronApplication } from '@playwright/test';
8+
import { MOCK_FILES } from '../files.ts';
9+
import { readShared } from '../shared.ts';
810

911
/** What the stand-ins recorded, in the main process. */
1012
export interface MainRecord {
@@ -149,6 +151,59 @@ export const mainProcess = (app: ElectronApplication) => ({
149151
answers,
150152
),
151153

154+
/**
155+
* Serves, in the web views' session (whose requests the app's downloads
156+
* use too), sites a test would otherwise reach over the internet, at their
157+
* own https URLs, so the app's host checks and injected scripts run as on
158+
* the real sites:
159+
* - `loginPage`: every page of Tsinghua's login site (id.tsinghua.edu.cn);
160+
* - `mockFilesDir`: the mock account's files (MOCK_FILES in
161+
* src/data/mock.ts), from this checkout's `mock-files`.
162+
*
163+
* Every other request goes out as usual, but through this handler, which
164+
* DevTools' `Fetch` doesn't see: loadHtml (webviews.ts) can't answer a
165+
* load then, so only tests that need it call it.
166+
*/
167+
serveSites: (sites: { loginPage?: string; mockFilesDir?: string }) =>
168+
app.evaluate(
169+
({ net, session }, given) => {
170+
const fs = process.getBuiltinModule('node:fs');
171+
const path = process.getBuiltinModule('node:path');
172+
session
173+
.fromPartition(given.partition)
174+
.protocol.handle('https', request => {
175+
const url = new URL(request.url);
176+
const mockFile = `${url.origin}${url.pathname}`.startsWith(
177+
given.mockFiles,
178+
)
179+
? decodeURIComponent(
180+
url.pathname.slice(new URL(given.mockFiles).pathname.length),
181+
)
182+
: undefined;
183+
if (url.hostname === 'id.tsinghua.edu.cn' && given.loginPage) {
184+
return new Response(given.loginPage, {
185+
headers: { 'content-type': 'text/html; charset=utf-8' },
186+
});
187+
}
188+
if (mockFile !== undefined && given.mockFilesDir) {
189+
const file = path.join(given.mockFilesDir, mockFile);
190+
return file.startsWith(given.mockFilesDir + path.sep) &&
191+
fs.statSync(file, { throwIfNoEntry: false })?.isFile()
192+
? new Response(fs.readFileSync(file), {
193+
headers: { 'content-type': 'application/octet-stream' },
194+
})
195+
: new Response('Not found', { status: 404 });
196+
}
197+
return net.fetch(request, { bypassCustomProtocolHandlers: true });
198+
});
199+
},
200+
{
201+
...sites,
202+
mockFiles: MOCK_FILES,
203+
partition: readShared('webviewPartition.json').partition,
204+
},
205+
),
206+
152207
/** What the stand-ins have recorded so far. */
153208
record: () =>
154209
app.evaluate(() => {

0 commit comments

Comments
 (0)