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
+38 -10
View File
@@ -13,6 +13,8 @@ import {
import { clickSideBarAllPageButton } from '@affine-test/kit/utils/sidebar'; import { clickSideBarAllPageButton } from '@affine-test/kit/utils/sidebar';
import { expect } from '@playwright/test'; import { expect } from '@playwright/test';
const SPLIT_VIEW_READY_TIMEOUT = 30_000;
test('open split view', async ({ page }) => { test('open split view', async ({ page }) => {
await clickNewPageButton(page); await clickNewPageButton(page);
await page.waitForTimeout(500); await page.waitForTimeout(500);
@@ -23,11 +25,20 @@ test('open split view', async ({ page }) => {
.click({ .click({
modifiers: ['ControlOrMeta', 'Alt'], modifiers: ['ControlOrMeta', 'Alt'],
}); });
await expect(page.locator('.doc-title-container')).toHaveCount(2); await expect(page.locator('.doc-title-container')).toHaveCount(2, {
timeout: SPLIT_VIEW_READY_TIMEOUT,
});
// check tab title // check tab title
await expect(page.getByTestId('split-view-label')).toHaveCount(2); await expect(page.getByTestId('split-view-label')).toHaveCount(2, {
await expectTabTitle(page, 0, ['Untitled', 'hi from another page']); timeout: SPLIT_VIEW_READY_TIMEOUT,
});
await expectTabTitle(
page,
0,
['Untitled', 'hi from another page'],
SPLIT_VIEW_READY_TIMEOUT
);
// the first split view should be active // the first split view should be active
await expectActiveTab(page, 0, 0); await expectActiveTab(page, 0, 0);
@@ -53,7 +64,12 @@ test('open split view', async ({ page }) => {
true true
); );
await expectTabTitle(page, 0, ['hi from another page', 'Untitled']); await expectTabTitle(
page,
0,
['hi from another page', 'Untitled'],
SPLIT_VIEW_READY_TIMEOUT
);
}); });
test('open split view in all docs (operations button)', async ({ page }) => { test('open split view in all docs (operations button)', async ({ page }) => {
@@ -65,12 +81,17 @@ test('open split view in all docs (operations button)', async ({ page }) => {
.getByTestId('doc-list-operation-button') .getByTestId('doc-list-operation-button')
.click(); .click();
await page.getByRole('menuitem', { name: 'Open in split view' }).click(); await page.getByRole('menuitem', { name: 'Open in split view' }).click();
await expect(page.getByTestId('split-view-panel')).toHaveCount(2); await expect(page.getByTestId('split-view-panel')).toHaveCount(2, {
timeout: SPLIT_VIEW_READY_TIMEOUT,
});
const targetPage = page.getByTestId('split-view-panel').last(); const targetPage = page.getByTestId('split-view-panel').last();
await expect(targetPage).toHaveAttribute('data-is-active', 'true'); await expect(targetPage).toHaveAttribute('data-is-active', 'true');
await expect(targetPage.locator('.doc-title-container')).toBeVisible(); await expect(targetPage.locator('.doc-title-container')).toBeVisible({
timeout: SPLIT_VIEW_READY_TIMEOUT,
});
await expect(targetPage.locator('.doc-title-container')).toContainText( await expect(targetPage.locator('.doc-title-container')).toContainText(
testTitle testTitle,
{ timeout: SPLIT_VIEW_READY_TIMEOUT }
); );
}); });
@@ -87,7 +108,12 @@ test('open split view in all docs (drag to resize handle)', async ({
const leftResizeHandle = page.getByTestId('resize-handle').first(); const leftResizeHandle = page.getByTestId('resize-handle').first();
await dragTo(page, pageItem, leftResizeHandle, 'center'); await dragTo(page, pageItem, leftResizeHandle, 'center');
await expectTabTitle(page, 0, ['test-page', 'All docs']); await expectTabTitle(
page,
0,
['test-page', 'All docs'],
SPLIT_VIEW_READY_TIMEOUT
);
}); });
test('creating split view by dragging sidebar journals', async ({ page }) => { test('creating split view by dragging sidebar journals', async ({ page }) => {
@@ -95,7 +121,9 @@ test('creating split view by dragging sidebar journals', async ({ page }) => {
const leftResizeHandle = page.getByTestId('resize-handle').first(); const leftResizeHandle = page.getByTestId('resize-handle').first();
await dragTo(page, journalButton, leftResizeHandle, 'center'); await dragTo(page, journalButton, leftResizeHandle, 'center');
await expect(page.getByTestId('split-view-panel')).toHaveCount(2); await expect(page.getByTestId('split-view-panel')).toHaveCount(2, {
timeout: SPLIT_VIEW_READY_TIMEOUT,
});
await expect( await expect(
page page
.getByTestId('split-view-panel') .getByTestId('split-view-panel')
@@ -103,5 +131,5 @@ test('creating split view by dragging sidebar journals', async ({ page }) => {
has: page.locator('[data-is-first="true"]'), has: page.locator('[data-is-first="true"]'),
}) })
.getByTestId('date-today-label') .getByTestId('date-today-label')
).toBeVisible(); ).toBeVisible({ timeout: SPLIT_VIEW_READY_TIMEOUT });
}); });
+79 -23
View File
@@ -13,6 +13,7 @@ import { test as base } from './playwright';
import { removeWithRetry } from './utils/utils'; import { removeWithRetry } from './utils/utils';
const electronRoot = new Package('@affine/electron').path; const electronRoot = new Package('@affine/electron').path;
const initialActivePages = new WeakMap<ElectronApplication, Page>();
const treeKillAsync = (pid: number, signal: NodeJS.Signals) => const treeKillAsync = (pid: number, signal: NodeJS.Signals) =>
new Promise<void>((resolve, reject) => { 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>( const withTimeoutFallback = async <T>(
promise: Promise<T>, promise: Promise<T>,
fallback: T, fallback: T,
@@ -157,6 +167,21 @@ const getShellPage = async (pages: Page[]) => {
return null; 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 ( const waitForElectronPage = async (
electronApp: ElectronApplication, electronApp: ElectronApplication,
label: string, label: string,
@@ -167,7 +192,7 @@ const waitForElectronPage = async (
(process.env.CI && process.platform === 'darwin' ? 25_000 : 20_000); (process.env.CI && process.platform === 'darwin' ? 25_000 : 20_000);
while (Date.now() < deadline) { while (Date.now() < deadline) {
const page = await getPage(electronApp.windows()); const page = await getPage(getElectronPages(electronApp));
if (page) { if (page) {
return page; return page;
} }
@@ -188,14 +213,6 @@ const cleanupElectronApp = async (electronApp: ElectronApplication) => {
} }
electronApp.once('close', () => resolve()); 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 = () => { const killProcess = () => {
try { try {
@@ -205,7 +222,7 @@ const cleanupElectronApp = async (electronApp: ElectronApplication) => {
const closeWithTimeout = async () => { const closeWithTimeout = async () => {
const closeEvent = waitForAppClose(); const closeEvent = waitForAppClose();
const processExit = waitForProcessExit(); const processExit = waitForChildProcessExit(child);
const pid = child.pid; const pid = child.pid;
void electronApp.close().catch(() => {}); void electronApp.close().catch(() => {});
const controller = new AbortController(); const controller = new AbortController();
@@ -243,7 +260,7 @@ const cleanupElectronApp = async (electronApp: ElectronApplication) => {
if (process.env.CI && process.platform === 'linux') { if (process.env.CI && process.platform === 'linux') {
const pid = child.pid; const pid = child.pid;
const closeEvent = waitForAppClose(); const closeEvent = waitForAppClose();
const processExit = waitForProcessExit(); const processExit = waitForChildProcessExit(child);
await Promise.race([ await Promise.race([
Promise.all([ Promise.all([
@@ -273,6 +290,21 @@ const cleanupElectronApp = async (electronApp: ElectronApplication) => {
await closeWithTimeout(); 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<{ export const test = base.extend<{
electronApp: ElectronApplication; electronApp: ElectronApplication;
shell: Page; shell: Page;
@@ -298,11 +330,11 @@ export const test = base.extend<{
await use(shell); await use(shell);
}, },
page: async ({ electronApp }, use) => { page: async ({ electronApp }, use) => {
const page = await waitForElectronPage( const cached = initialActivePages.get(electronApp);
electronApp, const page =
'active page', cached && !cached.isClosed()
getActivePage ? cached
); : await waitForElectronPage(electronApp, 'active page', getActivePage);
await page.waitForSelector('v-line'); await page.waitForSelector('v-line');
@@ -312,7 +344,7 @@ export const test = base.extend<{
void page; void page;
await use({ await use({
getActive: async () => { getActive: async () => {
const view = await getActivePage(electronApp.windows()); const view = await getActivePage(getElectronPages(electronApp));
return view || page; return view || page;
}, },
}); });
@@ -330,6 +362,7 @@ export const test = base.extend<{
electronRoot.join('package.json').value electronRoot.join('package.json').value
); );
packageJson.name = '@affine/electron-test-' + id; packageJson.name = '@affine/electron-test-' + id;
packageJson.productName = 'AFFiNE Test ' + id;
packageJson.main = './main.js'; packageJson.main = './main.js';
await fs.writeJSON(clonedDist + '/package.json', packageJson); await fs.writeJSON(clonedDist + '/package.json', packageJson);
@@ -342,12 +375,35 @@ export const test = base.extend<{
env.DEBUG = 'pw:browser'; env.DEBUG = 'pw:browser';
env.SKIP_ONBOARDING = '1'; env.SKIP_ONBOARDING = '1';
electronApp = await electron.launch({ const launch = () =>
args: [clonedDist], electron.launch({
env, args: [clonedDist],
cwd: clonedDist, env,
colorScheme: 'light', 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); await use(electronApp);
} finally { } finally {
+5 -3
View File
@@ -18,11 +18,13 @@ export async function expectActiveTab(
export async function expectTabTitle( export async function expectTabTitle(
page: Page, page: Page,
index: number, index: number,
title: string | string[] title: string | string[],
timeout?: number
) { ) {
if (typeof title === 'string') { if (typeof title === 'string') {
await expect(page.getByTestId('workbench-tab').nth(index)).toContainText( await expect(page.getByTestId('workbench-tab').nth(index)).toContainText(
title title,
{ timeout }
); );
} else { } else {
for (let i = 0; i < title.length; i++) { for (let i = 0; i < title.length; i++) {
@@ -32,7 +34,7 @@ export async function expectTabTitle(
.nth(index) .nth(index)
.getByTestId('split-view-label') .getByTestId('split-view-label')
.nth(i) .nth(i)
).toContainText(title[i]); ).toContainText(title[i], { timeout });
} }
} }
} }