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') if (await page.getByText('欢迎使用笔临').isVisible()) { await page.getByRole('button', { name: '跳过' }).click() } } async function createAndOpenBook(page: Page, name = 'P2测试书'): 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 }) } async function ensureChapterEditor(page: Page): Promise { await page.getByRole('button', { name: '+ 新章' }).click() await expect(page.locator('.ProseMirror')).toBeVisible({ timeout: 10_000 }) } test.describe('P2 features', () => { let userDataDir: string test.beforeEach(() => { userDataDir = mkdtempSync(join(tmpdir(), 'bilin-e2e-p2-')) }) test.afterEach(() => { if (userDataDir && existsSync(userDataDir)) { try { rmSync(userDataDir, { recursive: true, force: true, maxRetries: 3, retryDelay: 200 }) } catch { /* ignore */ } } }) test('E2E-P2-REF-01: reference panel drag inserts setting text', async () => { const app = await launchApp(userDataDir) const page = await app.firstWindow() await skipOnboarding(page) await createAndOpenBook(page) await ensureChapterEditor(page) await page.getByTestId('sidebar-tab-setting').click() await page.getByTestId('setting-new').click() await page.getByTestId('setting-name-input').fill('主角林远') await page.locator('.dialog-content select').selectOption('character') await page.getByRole('button', { name: '创建' }).click() await expect(page.getByTestId(/setting-item-/)).toBeVisible({ timeout: 10_000 }) const editor = page.locator('.ProseMirror') await editor.click() await page.getByTestId('open-reference').click() await expect(page.getByTestId('reference-panel')).toBeVisible() const refItem = page.locator('[data-testid^="ref-item-"]').first() await refItem.dragTo(editor) await page.evaluate(() => { const editorEl = document.querySelector('.ProseMirror') if (!editorEl) return const rect = editorEl.getBoundingClientRect() const dt = new DataTransfer() dt.setData('text/plain', '主角林远') editorEl.dispatchEvent( new DragEvent('drop', { bubbles: true, cancelable: true, clientX: rect.left + rect.width / 2, clientY: rect.top + rect.height / 2, dataTransfer: dt }) ) }) await expect(editor).toContainText('主角', { timeout: 5_000 }) await app.close() }) test('E2E-P2-SEARCH-01: global search finds chapter text and jumps', async () => { const app = await launchApp(userDataDir) const page = await app.firstWindow() await skipOnboarding(page) await createAndOpenBook(page) await ensureChapterEditor(page) const unique = '搜索专用词XYZ' const editor = page.locator('.ProseMirror') await editor.click() await editor.pressSequentially(unique) await page.keyboard.press('Control+S') await expect(page.getByText('已保存')).toBeVisible({ timeout: 10_000 }) await page.evaluate(() => window.dispatchEvent(new Event('bilin:e2e-open-search'))) await expect(page.getByTestId('search-modal')).toBeVisible({ timeout: 5_000 }) await page.getByTestId('search-input').fill(unique) await expect(page.locator('.search-result').first()).toBeVisible({ timeout: 10_000 }) await page.keyboard.press('Enter') await expect(page.getByTestId('search-modal')).toBeHidden({ timeout: 5_000 }) await expect(editor).toContainText(unique) await app.close() }) test('E2E-P2-SNAP-01: manual snapshot shows diff', async () => { const app = await launchApp(userDataDir) const page = await app.firstWindow() await skipOnboarding(page) await createAndOpenBook(page) await ensureChapterEditor(page) const editor = page.locator('.ProseMirror') await editor.click() await editor.pressSequentially('版本快照内容A') await page.waitForTimeout(2500) await page.getByTestId('open-version').click() await expect(page.getByTestId('version-modal')).toBeVisible() await page.getByRole('button', { name: '创建快照' }).click() await expect(page.locator('.version-item').first()).toBeVisible({ timeout: 10_000 }) await page.getByRole('button', { name: '取消' }).click() await expect(page.getByTestId('version-modal')).toBeHidden() await editor.click() await editor.press('Control+a') await editor.pressSequentially('版本快照内容B') await page.waitForTimeout(2500) await page.getByTestId('open-version').click() await page.locator('.version-item').first().click() await expect(page.locator('.diff-view')).toContainText('内容') await app.close() }) test('E2E-P2-LAND-01: insert landmark appears in list modal', async () => { const app = await launchApp(userDataDir) const page = await app.firstWindow() await skipOnboarding(page) await createAndOpenBook(page) await ensureChapterEditor(page) await page.evaluate(() => { window.dispatchEvent( new CustomEvent('bilin:e2e-insert-landmark', { detail: { label: '待查伏笔' } }) ) }) await expect(page.locator('.landmark-mark')).toContainText('待查伏笔', { timeout: 5_000 }) await page.evaluate(() => window.dispatchEvent(new Event('bilin:e2e-open-landmarks'))) await expect(page.getByTestId('landmarks-modal')).toBeVisible({ timeout: 5_000 }) await expect(page.locator('[data-testid^="landmark-item-"]')).toContainText('待查伏笔') await app.close() }) test('E2E-P2-WORD-01: word frequency click selects token in editor', async () => { const app = await launchApp(userDataDir) const page = await app.firstWindow() await skipOnboarding(page) await createAndOpenBook(page) await ensureChapterEditor(page) const token = '测' const editor = page.locator('.ProseMirror') await editor.click() await editor.pressSequentially(token.repeat(20)) await page.keyboard.press('Control+S') await expect(page.getByText('已保存')).toBeVisible({ timeout: 10_000 }) await page.getByTestId('panel-tab-wordfreq').click() await expect(page.getByTestId('wordfreq-panel')).toBeVisible() await expect(page.getByTestId(`wordfreq-${token}`)).toBeVisible({ timeout: 10_000 }) await page.getByTestId(`wordfreq-${token}`).click() await expect(page.locator('.editor-content-wrap')).toHaveAttribute('data-highlight-token', token) await app.close() }) })