import path from 'path' import { _electron as electron, type ElectronApplication, type Page, expect } from '@playwright/test' export const ROOT = path.join(import.meta.dirname, '..') export async function launchApp( userDataDir: string, extraEnv: Record = {} ): Promise { return electron.launch({ args: [ROOT], env: { ...process.env, BILIN_E2E: '1', BILIN_E2E_USER_DATA: userDataDir, ...extraEnv } }) } export async function skipOnboarding(page: Page): Promise { await page.waitForLoadState('domcontentloaded') await expect(page.locator('#app')).toBeVisible({ timeout: 20_000 }) if (await page.getByText('欢迎使用笔临').isVisible()) { await page.getByRole('button', { name: '跳过' }).click() } } export async function dismissCockpitIfOpen(page: Page): Promise { const cockpit = page.locator('[data-testid="cockpit-modal"]') if (await cockpit.isVisible({ timeout: 3000 }).catch(() => false)) { await page.locator('[data-testid="cockpit-modal"] .modal-close').click() await expect(cockpit).toBeHidden({ timeout: 5000 }) } } export async function createBookAndOpenEditor(page: Page, name: string): Promise { await page.getByRole('button', { name: /新建书籍/ }).first().click() await page.locator('.dialog-content input').first().fill(name) await page.getByRole('button', { name: '创建' }).click() await expect(page.locator('#editor-layout')).toBeVisible({ timeout: 15_000 }) await dismissCockpitIfOpen(page) } export async function ensureChapterEditor(page: Page): Promise { const editor = page.locator('.ProseMirror') if (await editor.isVisible().catch(() => false)) return await page.getByRole('button', { name: '+ 新章' }).click() await expect(editor).toBeVisible({ timeout: 10_000 }) } export async function openInspirationModal(page: Page): Promise { await page.keyboard.press('Control+Shift+I') const modal = page.getByTestId('inspiration-modal') if (!(await modal.isVisible().catch(() => false))) { await page.evaluate(() => window.dispatchEvent(new Event('bilin:e2e-capture-inspiration'))) } await expect(modal).toBeVisible({ timeout: 10_000 }) } export async function openExportModal(page: Page): Promise { await page.keyboard.press('Control+Shift+E') const modal = page.getByTestId('export-modal') if (!(await modal.isVisible().catch(() => false))) { await page.getByTestId('open-export').click() } await expect(modal).toBeVisible({ timeout: 10_000 }) } export async function openBookSettings(page: Page): Promise { await page.getByTestId('panel-tab-book-settings').click() await expect(page.getByTestId('book-settings-tab')).toBeVisible({ timeout: 10_000 }) } export async function addCharacterSetting(page: Page, name: string): Promise { await page.getByTestId('sidebar-tab-setting').click() await page.getByTestId('setting-new').click() await page.getByTestId('setting-name-input').fill(name) await page.locator('.dialog-content select').selectOption('character') await page.getByRole('button', { name: '创建' }).click() await expect(page.getByTestId(/setting-item-/).filter({ hasText: name })).toBeVisible({ timeout: 10_000 }) }