2023-05-09 10:30:01 +03:00
|
|
|
import crypto from 'node:crypto';
|
2023-05-12 04:51:13 +03:00
|
|
|
import { join, resolve } from 'node:path';
|
2023-05-09 10:30:01 +03:00
|
|
|
|
2023-09-17 23:26:06 +03:00
|
|
|
import type { Page } from '@playwright/test';
|
|
|
|
import fs from 'fs-extra';
|
|
|
|
import type { ElectronApplication } from 'playwright';
|
|
|
|
import { _electron as electron } from 'playwright';
|
|
|
|
|
2023-05-12 04:51:13 +03:00
|
|
|
import {
|
|
|
|
enableCoverage,
|
|
|
|
istanbulTempDir,
|
|
|
|
test as base,
|
2023-05-15 09:13:30 +03:00
|
|
|
testResultDir,
|
2023-09-17 23:26:06 +03:00
|
|
|
} from './playwright';
|
|
|
|
import { removeWithRetry } from './utils/utils';
|
2023-05-09 10:30:01 +03:00
|
|
|
|
2023-09-29 06:02:26 +03:00
|
|
|
const projectRoot = join(__dirname, '..', '..');
|
2023-10-18 18:30:08 +03:00
|
|
|
const electronRoot = join(projectRoot, 'packages/frontend/electron');
|
2023-09-07 20:41:27 +03:00
|
|
|
|
2023-05-09 10:30:01 +03:00
|
|
|
function generateUUID() {
|
|
|
|
return crypto.randomUUID();
|
|
|
|
}
|
|
|
|
|
2023-07-04 20:47:42 +03:00
|
|
|
type RoutePath = 'setting';
|
|
|
|
|
2023-05-09 10:30:01 +03:00
|
|
|
export const test = base.extend<{
|
|
|
|
electronApp: ElectronApplication;
|
|
|
|
appInfo: {
|
|
|
|
appPath: string;
|
|
|
|
appData: string;
|
|
|
|
sessionData: string;
|
|
|
|
};
|
2023-07-04 20:47:42 +03:00
|
|
|
router: {
|
|
|
|
goto: (path: RoutePath) => Promise<void>;
|
|
|
|
};
|
2023-05-09 10:30:01 +03:00
|
|
|
}>({
|
|
|
|
page: async ({ electronApp }, use) => {
|
|
|
|
const page = await electronApp.firstWindow();
|
2023-09-17 23:26:06 +03:00
|
|
|
// wait for blocksuite to be loaded
|
2023-05-10 06:04:36 +03:00
|
|
|
await page.waitForSelector('v-line');
|
2023-05-12 04:51:13 +03:00
|
|
|
if (enableCoverage) {
|
|
|
|
await fs.promises.mkdir(istanbulTempDir, { recursive: true });
|
|
|
|
await page.exposeFunction(
|
|
|
|
'collectIstanbulCoverage',
|
|
|
|
(coverageJSON?: string) => {
|
|
|
|
if (coverageJSON)
|
|
|
|
fs.writeFileSync(
|
|
|
|
join(
|
|
|
|
istanbulTempDir,
|
|
|
|
`playwright_coverage_${generateUUID()}.json`
|
|
|
|
),
|
|
|
|
coverageJSON
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2023-08-29 13:07:05 +03:00
|
|
|
await use(page as Page);
|
2023-05-12 04:51:13 +03:00
|
|
|
if (enableCoverage) {
|
|
|
|
await page.evaluate(() =>
|
|
|
|
// @ts-expect-error
|
|
|
|
window.collectIstanbulCoverage(JSON.stringify(window.__coverage__))
|
|
|
|
);
|
|
|
|
}
|
2023-05-09 10:30:01 +03:00
|
|
|
await page.close();
|
|
|
|
},
|
2023-09-17 23:26:06 +03:00
|
|
|
// eslint-disable-next-line no-empty-pattern
|
2023-05-09 10:30:01 +03:00
|
|
|
electronApp: async ({}, use) => {
|
|
|
|
// a random id to avoid conflicts between tests
|
|
|
|
const id = generateUUID();
|
2023-05-10 13:38:23 +03:00
|
|
|
const ext = process.platform === 'win32' ? '.cmd' : '';
|
2023-09-17 23:26:06 +03:00
|
|
|
const dist = resolve(electronRoot, 'dist');
|
|
|
|
const clonedDist = resolve(electronRoot, 'e2e-dist-' + id);
|
2023-06-13 05:01:43 +03:00
|
|
|
await fs.copy(dist, clonedDist);
|
|
|
|
const packageJson = await fs.readJSON(
|
2023-09-17 23:26:06 +03:00
|
|
|
resolve(electronRoot, 'package.json')
|
2023-06-13 05:01:43 +03:00
|
|
|
);
|
|
|
|
// overwrite the app name
|
|
|
|
packageJson.name = 'affine-test-' + id;
|
|
|
|
// overwrite the path to the main script
|
|
|
|
packageJson.main = './main.js';
|
|
|
|
// write to the cloned dist
|
|
|
|
await fs.writeJSON(resolve(clonedDist, 'package.json'), packageJson);
|
|
|
|
|
2023-09-17 23:26:06 +03:00
|
|
|
const env: Record<string, string> = {};
|
|
|
|
for (const [key, value] of Object.entries(process.env)) {
|
|
|
|
if (value) {
|
|
|
|
env[key] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (process.env.DEV_SERVER_URL) {
|
|
|
|
env.DEV_SERVER_URL = process.env.DEV_SERVER_URL;
|
|
|
|
}
|
|
|
|
|
2023-12-19 10:17:54 +03:00
|
|
|
env.SKIP_ONBOARDING = '1';
|
|
|
|
|
2023-05-09 10:30:01 +03:00
|
|
|
const electronApp = await electron.launch({
|
2023-06-13 05:01:43 +03:00
|
|
|
args: [clonedDist],
|
2023-09-17 23:26:06 +03:00
|
|
|
env,
|
2023-05-10 13:38:23 +03:00
|
|
|
executablePath: resolve(
|
2023-09-29 06:02:26 +03:00
|
|
|
projectRoot,
|
2023-05-10 13:38:23 +03:00
|
|
|
'node_modules',
|
|
|
|
'.bin',
|
|
|
|
`electron${ext}`
|
|
|
|
),
|
2023-09-17 23:26:06 +03:00
|
|
|
cwd: clonedDist,
|
2023-05-15 09:13:30 +03:00
|
|
|
recordVideo: {
|
|
|
|
dir: testResultDir,
|
|
|
|
},
|
2023-05-09 10:30:01 +03:00
|
|
|
colorScheme: 'light',
|
|
|
|
});
|
|
|
|
await use(electronApp);
|
2023-06-13 05:01:43 +03:00
|
|
|
try {
|
2023-09-07 20:41:27 +03:00
|
|
|
await removeWithRetry(clonedDist);
|
2023-06-13 05:01:43 +03:00
|
|
|
} catch (error) {
|
|
|
|
console.log(error);
|
|
|
|
}
|
2023-05-09 10:30:01 +03:00
|
|
|
},
|
|
|
|
appInfo: async ({ electronApp }, use) => {
|
|
|
|
const appInfo = await electronApp.evaluate(async ({ app }) => {
|
|
|
|
return {
|
|
|
|
appPath: app.getAppPath(),
|
|
|
|
appData: app.getPath('appData'),
|
|
|
|
sessionData: app.getPath('sessionData'),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
await use(appInfo);
|
|
|
|
},
|
|
|
|
});
|