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"]') if (await cockpit.isVisible({ timeout: 3000 }).catch(() => false)) { await page.locator('[data-testid="cockpit-modal"] .modal-close').click() await expect(cockpit).toBeHidden({ timeout: 5000 }) } } async function ensureChapterEditor(page: Page): Promise { const items = page.locator('.chapter-item') if ((await items.count()) === 0) { await page.getByRole('button', { name: '+ 新章' }).click() } else { await items.first().click() } await expect(page.locator('.ProseMirror')).toBeVisible({ timeout: 10_000 }) } 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('Cockpit & Knowledge P5', () => { let userDataDir: string test.beforeEach(() => { userDataDir = mkdtempSync(join(tmpdir(), 'bilin-e2e-p5-')) }) test.afterEach(async () => { if (userDataDir && existsSync(userDataDir)) { try { rmSync(userDataDir, { recursive: true, force: true, maxRetries: 3, retryDelay: 200 }) } catch { /* ignore */ } } }) test('E2E-COCK-01: cockpit modal shows stats', async () => { const app = await launchApp(userDataDir) const page = await app.firstWindow() await skipOnboarding(page) await createAndOpenBook(page, '驾驶舱书') await page.locator('[data-testid="topbar-cockpit"]').click() await expect(page.locator('[data-testid="cockpit-modal"]')).toBeVisible() await expect(page.locator('[data-testid="cockpit-total-words"]')).toBeVisible() await app.close() }) test('E2E-COCK-02: first open shows cockpit once', async () => { const app1 = await launchApp(userDataDir) const page1 = await app1.firstWindow() await skipOnboarding(page1) await page1.getByRole('button', { name: /新建书籍/ }).first().click() await page1.locator('.dialog-content input').first().fill('首次驾驶舱') await page1.getByRole('button', { name: '创建' }).click() await expect(page1.locator('[data-testid="cockpit-modal"]')).toBeVisible({ timeout: 15_000 }) await page1.locator('[data-testid="cockpit-modal"] .modal-close').click() 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('[data-testid="cockpit-modal"]')).toBeHidden({ timeout: 3000 }) await app2.close() }) test('E2E-PUB-01: publish ready increases stock count', async () => { const app = await launchApp(userDataDir) const page = await app.firstWindow() await skipOnboarding(page) await createAndOpenBook(page, '发布书') await ensureChapterEditor(page) const chapterItem = page.locator('.chapter-item').first() await expect(chapterItem).toBeVisible() const chapterId = await chapterItem.getAttribute('data-testid') const id = chapterId?.replace('chapter-item-', '') ?? '' await page.locator(`[data-testid="chapter-publish-${id}"]`).click() await page.locator('[data-testid="topbar-cockpit"]').click() await expect(page.locator('[data-testid="cockpit-stock-count"]')).toContainText('1') await app.close() }) test('E2E-KNOW-01: create approve knowledge entry', async () => { const app = await launchApp(userDataDir) const page = await app.firstWindow() await skipOnboarding(page) await createAndOpenBook(page, '知识库书') await page.locator('[data-testid="panel-tab-knowledge"]').click() await page.locator('[data-testid="knowledge-new"]').click() await page.locator('[data-testid="knowledge-title-input"]').fill('测灵石异常') await page.locator('[data-testid="knowledge-save"]').click() await page.locator('[data-testid="knowledge-tab-pending"]').click() await expect(page.locator('.kb-item')).toContainText('测灵石异常') const approveBtn = page.locator('[data-testid^="knowledge-approve-"]').first() await approveBtn.click() await page.locator('[data-testid="knowledge-tab-all"]').click() await expect(page.locator('.kb-item')).toContainText('测灵石异常') await app.close() }) test('E2E-BRIDGE-01: new chapter triggers bridge modal', 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('上一章结尾内容,林远站在演武场中央。') await page.keyboard.press('Control+s') await expect(page.getByText('已保存')).toBeVisible({ timeout: 15_000 }) await dismissCockpitIfOpen(page) await page.getByRole('button', { name: '+ 新章' }).click() await dismissCockpitIfOpen(page) await expect(page.locator('[data-testid="bridge-modal"]')).toBeVisible({ timeout: 10_000 }) await expect(page.locator('[data-testid="bridge-previous-tail"]')).toBeVisible({ timeout: 60_000 }) await expect(page.locator('[data-testid="bridge-previous-tail"]')).not.toHaveText('(无)') await app.close() }) test('E2E-STOCK-01: low stock warning in statusbar', async () => { const app = await launchApp(userDataDir) const page = await app.firstWindow() await skipOnboarding(page) await createAndOpenBook(page, '存稿书') await page.locator('.top-btn').filter({ hasText: '⚙' }).click() await page.locator('[data-testid="settings-update-schedule"]').selectOption('daily') await page.locator('[data-testid="settings-stock-threshold"]').fill('3') await page.locator('[data-testid="settings-stock-threshold"]').blur() await page.waitForTimeout(500) await page.getByText('存稿书').click() await expect(page.locator('#editor-layout')).toBeVisible({ timeout: 10_000 }) await expect(page.locator('[data-testid="status-stock-warning"]')).toBeVisible({ timeout: 10_000 }) await app.close() }) test('E2E-BRIDGE-02: bridge shows AI suggestion', async () => { test.setTimeout(300_000) const app = await launchApp(userDataDir) const page = await app.firstWindow() await skipOnboarding(page) await createAndOpenBook(page, 'AI衔接书') await ensureChapterEditor(page) const editor = page.locator('.ProseMirror') await editor.click() await editor.pressSequentially('林远站在演武场中央,众人目光汇聚,他知道从这一刻起一切都将不同。') await page.keyboard.press('Control+s') await page.waitForTimeout(1000) await dismissCockpitIfOpen(page) await page.getByRole('button', { name: '+ 新章' }).click() await dismissCockpitIfOpen(page) await expect(page.locator('[data-testid="bridge-modal"]')).toBeVisible({ timeout: 15_000 }) await expect(page.locator('[data-testid="bridge-ai-suggestion"]')).not.toBeEmpty({ timeout: 240_000 }) await app.close() }) })