chore: improve test stability

This commit is contained in:
DarkSky
2026-07-07 12:26:56 +08:00
parent fe2a4db76b
commit 9f8e5c0eb3
3 changed files with 122 additions and 36 deletions
+79 -23
View File
@@ -13,6 +13,7 @@ import { test as base } from './playwright';
import { removeWithRetry } from './utils/utils';
const electronRoot = new Package('@affine/electron').path;
const initialActivePages = new WeakMap<ElectronApplication, Page>();
const treeKillAsync = (pid: number, signal: NodeJS.Signals) =>
new Promise<void>((resolve, reject) => {
@@ -73,6 +74,15 @@ const releaseChildProcessHandles = (child: ChildProcess) => {
}
};
const waitForChildProcessExit = (child: ChildProcess) =>
new Promise<void>(resolve => {
if (child.exitCode !== null || child.signalCode !== null) {
resolve();
return;
}
child.once('exit', () => resolve());
});
const withTimeoutFallback = async <T>(
promise: Promise<T>,
fallback: T,
@@ -157,6 +167,21 @@ const getShellPage = async (pages: Page[]) => {
return null;
};
const getElectronPages = (electronApp: ElectronApplication) => {
const pages = new Set<Page>();
for (const page of electronApp.windows()) {
if (!page.isClosed()) {
pages.add(page);
}
}
for (const page of electronApp.context().pages()) {
if (!page.isClosed()) {
pages.add(page);
}
}
return [...pages];
};
const waitForElectronPage = async (
electronApp: ElectronApplication,
label: string,
@@ -167,7 +192,7 @@ const waitForElectronPage = async (
(process.env.CI && process.platform === 'darwin' ? 25_000 : 20_000);
while (Date.now() < deadline) {
const page = await getPage(electronApp.windows());
const page = await getPage(getElectronPages(electronApp));
if (page) {
return page;
}
@@ -188,14 +213,6 @@ const cleanupElectronApp = async (electronApp: ElectronApplication) => {
}
electronApp.once('close', () => resolve());
});
const waitForProcessExit = () =>
new Promise<void>(resolve => {
if (child.exitCode !== null || child.signalCode !== null) {
resolve();
return;
}
child.once('exit', () => resolve());
});
const killProcess = () => {
try {
@@ -205,7 +222,7 @@ const cleanupElectronApp = async (electronApp: ElectronApplication) => {
const closeWithTimeout = async () => {
const closeEvent = waitForAppClose();
const processExit = waitForProcessExit();
const processExit = waitForChildProcessExit(child);
const pid = child.pid;
void electronApp.close().catch(() => {});
const controller = new AbortController();
@@ -243,7 +260,7 @@ const cleanupElectronApp = async (electronApp: ElectronApplication) => {
if (process.env.CI && process.platform === 'linux') {
const pid = child.pid;
const closeEvent = waitForAppClose();
const processExit = waitForProcessExit();
const processExit = waitForChildProcessExit(child);
await Promise.race([
Promise.all([
@@ -273,6 +290,21 @@ const cleanupElectronApp = async (electronApp: ElectronApplication) => {
await closeWithTimeout();
};
const forceKillElectronApp = async (electronApp: ElectronApplication) => {
const child = electronApp.process();
const pid = child.pid;
if (pid !== undefined) {
await treeKillAsync(pid, 'SIGKILL').catch(() => child.kill());
} else {
child.kill();
}
await Promise.race([waitForChildProcessExit(child), setTimeout(5_000)]).catch(
() => {}
);
releaseChildProcessHandles(child);
};
export const test = base.extend<{
electronApp: ElectronApplication;
shell: Page;
@@ -298,11 +330,11 @@ export const test = base.extend<{
await use(shell);
},
page: async ({ electronApp }, use) => {
const page = await waitForElectronPage(
electronApp,
'active page',
getActivePage
);
const cached = initialActivePages.get(electronApp);
const page =
cached && !cached.isClosed()
? cached
: await waitForElectronPage(electronApp, 'active page', getActivePage);
await page.waitForSelector('v-line');
@@ -312,7 +344,7 @@ export const test = base.extend<{
void page;
await use({
getActive: async () => {
const view = await getActivePage(electronApp.windows());
const view = await getActivePage(getElectronPages(electronApp));
return view || page;
},
});
@@ -330,6 +362,7 @@ export const test = base.extend<{
electronRoot.join('package.json').value
);
packageJson.name = '@affine/electron-test-' + id;
packageJson.productName = 'AFFiNE Test ' + id;
packageJson.main = './main.js';
await fs.writeJSON(clonedDist + '/package.json', packageJson);
@@ -342,12 +375,35 @@ export const test = base.extend<{
env.DEBUG = 'pw:browser';
env.SKIP_ONBOARDING = '1';
electronApp = await electron.launch({
args: [clonedDist],
env,
cwd: clonedDist,
colorScheme: 'light',
});
const launch = () =>
electron.launch({
args: [clonedDist],
env,
cwd: clonedDist,
colorScheme: 'light',
});
for (let attempt = 0; attempt < 2; attempt++) {
electronApp = await launch();
try {
const page = await waitForElectronPage(
electronApp,
'active page',
getActivePage
);
initialActivePages.set(electronApp, page);
break;
} catch (error) {
if (attempt > 0) {
throw error;
}
await forceKillElectronApp(electronApp);
electronApp = undefined;
}
}
if (!electronApp) {
throw new Error('Failed to launch electron app');
}
await use(electronApp);
} finally {
+5 -3
View File
@@ -18,11 +18,13 @@ export async function expectActiveTab(
export async function expectTabTitle(
page: Page,
index: number,
title: string | string[]
title: string | string[],
timeout?: number
) {
if (typeof title === 'string') {
await expect(page.getByTestId('workbench-tab').nth(index)).toContainText(
title
title,
{ timeout }
);
} else {
for (let i = 0; i < title.length; i++) {
@@ -32,7 +34,7 @@ export async function expectTabTitle(
.nth(index)
.getByTestId('split-view-label')
.nth(i)
).toContainText(title[i]);
).toContainText(title[i], { timeout });
}
}
}