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 logs P5.2', () => { let userDataDir: string test.beforeEach(() => { userDataDir = mkdtempSync(join(tmpdir(), 'bilin-e2e-log-')) }) test.afterEach(async () => { if (userDataDir && existsSync(userDataDir)) { try { rmSync(userDataDir, { recursive: true, force: true, maxRetries: 3, retryDelay: 200 }) } catch { /* ignore */ } } }) test('E2E-LOG-01: typing updates today progress in cockpit', async () => { const app = await launchApp(userDataDir) const page = await app.firstWindow() await skipOnboarding(page) await createAndOpenBook(page, '日志测试书') await page.getByTestId('topbar-settings').click() await expect(page.locator('#settings-page')).toBeVisible({ timeout: 10_000 }) await page.locator('[data-testid="settings-daily-word-goal"]').fill('100') await page.getByRole('button', { name: '日志测试书' }).click() 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 }) await page.locator('.ProseMirror').click() await page.keyboard.type('这是一段用于测试写作日志累计的示例文字内容,需要足够长才能触发字数统计与日志记录。') await page.waitForTimeout(1500) await page.locator('[data-testid="topbar-cockpit"]').click() await expect(page.locator('[data-testid="cockpit-modal"]')).toBeVisible() await expect(page.locator('[data-testid="cockpit-today-progress"]')).toBeVisible() await expect(page.locator('[data-testid="cockpit-heatmap"]')).toBeVisible() await app.close() }) test('E2E-LOG-02: status bar shows daily goal when configured', async () => { const app = await launchApp(userDataDir) const page = await app.firstWindow() await skipOnboarding(page) await createAndOpenBook(page, '状态栏测试') await page.getByTestId('topbar-settings').click() await expect(page.locator('#settings-page')).toBeVisible({ timeout: 10_000 }) await page.locator('[data-testid="settings-daily-word-goal"]').fill('500') await page.getByRole('button', { name: '状态栏测试' }).click() 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('[data-testid="status-daily-goal"]')).toBeVisible({ timeout: 10_000 }) await app.close() }) })