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
|
|
|
|
2024-07-29 14:05:22 +03:00
|
|
|
import { expect, type Page } from '@playwright/test';
|
2023-09-17 23:26:06 +03:00
|
|
|
import fs from 'fs-extra';
|
|
|
|
import type { ElectronApplication } from 'playwright';
|
|
|
|
import { _electron as electron } from 'playwright';
|
|
|
|
|
2024-07-29 14:05:22 +03:00
|
|
|
import { test as base, testResultDir } from './playwright';
|
2023-09-17 23:26:06 +03:00
|
|
|
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';
|
|
|
|
|
2024-07-29 14:05:22 +03:00
|
|
|
const getPageId = async (page: Page) => {
|
|
|
|
return page.evaluate(() => {
|
|
|
|
return (window.appInfo as any)?.viewId as string;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const isActivePage = async (page: Page) => {
|
|
|
|
return page.evaluate(async () => {
|
|
|
|
return await (window as any).apis?.ui.isActiveTab();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const getActivePage = async (pages: Page[]) => {
|
|
|
|
for (const page of pages) {
|
|
|
|
if (await isActivePage(page)) {
|
|
|
|
return page;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
2023-05-09 10:30:01 +03:00
|
|
|
export const test = base.extend<{
|
|
|
|
electronApp: ElectronApplication;
|
2024-07-29 14:05:22 +03:00
|
|
|
shell: Page;
|
2023-05-09 10:30:01 +03:00
|
|
|
appInfo: {
|
|
|
|
appPath: string;
|
|
|
|
appData: string;
|
|
|
|
sessionData: string;
|
|
|
|
};
|
2024-07-29 14:05:24 +03:00
|
|
|
views: {
|
|
|
|
getActive: () => Promise<Page>;
|
|
|
|
};
|
2023-07-04 20:47:42 +03:00
|
|
|
router: {
|
|
|
|
goto: (path: RoutePath) => Promise<void>;
|
|
|
|
};
|
2023-05-09 10:30:01 +03:00
|
|
|
}>({
|
2024-07-29 14:05:22 +03:00
|
|
|
shell: async ({ electronApp }, use) => {
|
|
|
|
await expect.poll(() => electronApp.windows().length > 1).toBeTruthy();
|
|
|
|
|
|
|
|
for (const page of electronApp.windows()) {
|
|
|
|
const viewId = await getPageId(page);
|
|
|
|
if (viewId === 'shell') {
|
|
|
|
await use(page);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2023-05-09 10:30:01 +03:00
|
|
|
page: async ({ electronApp }, use) => {
|
2024-07-29 14:05:22 +03:00
|
|
|
await expect
|
|
|
|
.poll(() => {
|
|
|
|
return electronApp.windows().length > 1;
|
|
|
|
})
|
|
|
|
.toBeTruthy();
|
|
|
|
|
|
|
|
const page = await getActivePage(electronApp.windows());
|
|
|
|
|
|
|
|
if (!page) {
|
|
|
|
throw new Error('No active page found');
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait for blocksuite to be loaded
|
|
|
|
await page.waitForSelector('v-line');
|
|
|
|
|
2024-04-15 10:25:30 +03:00
|
|
|
await page.evaluate(() => {
|
|
|
|
window.localStorage.setItem('dismissAiOnboarding', 'true');
|
|
|
|
window.localStorage.setItem('dismissAiOnboardingEdgeless', 'true');
|
|
|
|
window.localStorage.setItem('dismissAiOnboardingLocal', 'true');
|
|
|
|
});
|
2024-07-29 14:05:22 +03:00
|
|
|
|
|
|
|
await page.reload();
|
|
|
|
|
2023-08-29 13:07:05 +03:00
|
|
|
await use(page as Page);
|
2023-05-09 10:30:01 +03:00
|
|
|
},
|
2024-07-29 14:05:24 +03:00
|
|
|
views: async ({ electronApp, page }, use) => {
|
|
|
|
void page; // makes sure page is a dependency
|
|
|
|
await use({
|
|
|
|
getActive: async () => {
|
|
|
|
const view = await getActivePage(electronApp.windows());
|
|
|
|
return view || page;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
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) => {
|
2024-07-29 14:05:22 +03:00
|
|
|
try {
|
|
|
|
// a random id to avoid conflicts between tests
|
|
|
|
const id = generateUUID();
|
|
|
|
const dist = resolve(electronRoot, 'dist');
|
|
|
|
const clonedDist = resolve(electronRoot, 'e2e-dist-' + id);
|
|
|
|
await fs.copy(dist, clonedDist);
|
|
|
|
const packageJson = await fs.readJSON(
|
|
|
|
resolve(electronRoot, 'package.json')
|
|
|
|
);
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
const env: Record<string, string> = {};
|
|
|
|
for (const [key, value] of Object.entries(process.env)) {
|
|
|
|
if (value) {
|
|
|
|
env[key] = value;
|
|
|
|
}
|
2023-09-17 23:26:06 +03:00
|
|
|
}
|
|
|
|
|
2024-07-29 14:05:22 +03:00
|
|
|
env.SKIP_ONBOARDING = '1';
|
2023-09-17 23:26:06 +03:00
|
|
|
|
2024-07-29 14:05:22 +03:00
|
|
|
const electronApp = await electron.launch({
|
|
|
|
args: [clonedDist],
|
|
|
|
env,
|
|
|
|
cwd: clonedDist,
|
|
|
|
recordVideo: {
|
|
|
|
dir: testResultDir,
|
|
|
|
},
|
|
|
|
colorScheme: 'light',
|
|
|
|
});
|
|
|
|
|
|
|
|
await use(electronApp);
|
|
|
|
console.log('Cleaning up...');
|
|
|
|
const pages = electronApp.windows();
|
|
|
|
for (let i = 0; i < pages.length; i++) {
|
|
|
|
const page = pages[i];
|
|
|
|
await page.close();
|
|
|
|
console.log(`Closed page ${i + 1}/${pages.length}`);
|
|
|
|
}
|
|
|
|
await electronApp.close();
|
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);
|
|
|
|
},
|
|
|
|
});
|