import path from 'path' import { mkdtempSync, rmSync, existsSync } from 'fs' import { join } from 'path' import { tmpdir } from 'os' import { test, expect, _electron as electron, type Page } from '@playwright/test' const ROOT = path.join(import.meta.dirname, '..') async function launchApp(userDataDir: string) { return electron.launch({ args: [ROOT], env: { ...process.env, BILIN_E2E: '1', BILIN_E2E_USER_DATA: userDataDir } }) } async function skipOnboarding(page: Page): Promise { await page.waitForLoadState('domcontentloaded') await expect(page.locator('#app')).toBeVisible({ timeout: 20_000 }) await expect(page.getByText('欢迎使用笔临')).toBeVisible({ timeout: 15_000 }) await page.getByRole('button', { name: '跳过' }).click() await expect(page.getByText('欢迎使用笔临')).toBeHidden({ timeout: 10_000 }) } async function dismissCockpitIfOpen(page: Page): Promise { const cockpit = page.locator('[data-testid="cockpit-modal"]') try { await cockpit.waitFor({ state: 'visible', timeout: 15_000 }) await cockpit.locator('.modal-close').click() await expect(cockpit).toBeHidden({ timeout: 5000 }) } catch { /* cockpit did not open */ } } async function createAndOpenBook(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) } test.describe('Writing', () => { let userDataDir: string test.beforeEach(() => { userDataDir = mkdtempSync(join(tmpdir(), 'bilin-e2e-write-')) }) test.afterEach(async () => { if (userDataDir && existsSync(userDataDir)) { try { rmSync(userDataDir, { recursive: true, force: true, maxRetries: 3, retryDelay: 200 }) } catch { /* ignore */ } } }) test('auto-save persists content after restart', async () => { const app1 = await launchApp(userDataDir) const page1 = await app1.firstWindow() await skipOnboarding(page1) await createAndOpenBook(page1, '测试书') await dismissCockpitIfOpen(page1) await page1.getByRole('button', { name: '+ 新章' }).click() const editor = page1.locator('.ProseMirror') await expect(editor).toBeVisible({ timeout: 10_000 }) await editor.click() await editor.pressSequentially('林远深吸一口气') await page1.waitForTimeout(3000) await app1.close() const app2 = await launchApp(userDataDir) const page2 = await app2.firstWindow() await expect(page2.getByText('欢迎使用笔临')).toBeHidden({ timeout: 15_000 }) await page2.getByText('测试书').click() await expect(page2.locator('#editor-layout')).toBeVisible({ timeout: 15_000 }) await expect(page2.locator('.ProseMirror')).toContainText('林远深吸一口气', { timeout: 10_000 }) await app2.close() }) })