Files
bilin/e2e/interactive-writing.spec.ts
T
2026-07-06 17:46:16 +08:00

140 lines
5.9 KiB
TypeScript

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<void> {
await page.waitForLoadState('domcontentloaded')
if (await page.getByText('欢迎使用笔临').isVisible()) {
await page.getByRole('button', { name: '跳过' }).click()
}
}
async function ensureChapterEditor(page: Page): Promise<void> {
await page.getByRole('button', { name: '+ 新章' }).click()
await expect(page.locator('.ProseMirror')).toBeVisible({ timeout: 10_000 })
}
async function openBookWithContent(page: Page): Promise<void> {
await page.getByRole('button', { name: /新建书籍/ }).first().click()
await page.locator('.dialog-content input').first().fill('交互测试书')
await page.getByRole('button', { name: '创建' }).click()
await expect(page.locator('#editor-layout')).toBeVisible({ timeout: 15_000 })
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 })
}
test.describe('Interactive writing', () => {
let userDataDir: string
test.beforeEach(() => {
userDataDir = mkdtempSync(join(tmpdir(), 'bilin-e2e-int-'))
})
test.afterEach(() => {
if (userDataDir && existsSync(userDataDir)) {
try {
rmSync(userDataDir, { recursive: true, force: true, maxRetries: 3, retryDelay: 200 })
} catch {
/* ignore */
}
}
})
test('E2E-INT-02: interactive tab disabled on empty book', async () => {
const app = await launchApp(userDataDir)
const page = await app.firstWindow()
await skipOnboarding(page)
await page.getByRole('button', { name: /新建书籍/ }).first().click()
await page.locator('.dialog-content input').first().fill('空书')
await page.getByRole('button', { name: '创建' }).click()
await expect(page.locator('#editor-layout')).toBeVisible({ timeout: 15_000 })
await page.getByTestId('panel-tab-ai').click()
await expect(page.getByTestId('ai-mode-tab-interactive')).toBeDisabled({ timeout: 10_000 })
await app.close()
})
test('E2E-INT-01: interactive flow creates chapter', async () => {
test.setTimeout(300_000)
const app = await launchApp(userDataDir)
const page = await app.firstWindow()
await skipOnboarding(page)
await openBookWithContent(page)
await page.getByTestId('panel-tab-ai').click()
await page.getByTestId('ai-session-new').click()
await expect(page.getByTestId('ai-mode-tab-interactive')).toBeEnabled({ timeout: 10_000 })
await page.getByTestId('ai-mode-tab-interactive').click()
await expect(page.getByTestId('interactive-panel')).toBeVisible({ timeout: 10_000 })
await page.getByTestId('interactive-start').click()
await expect(page.getByTestId('plot-option-A')).toBeVisible({ timeout: 120_000 })
await page.getByTestId('plot-option-A').click()
await page.getByTestId('interactive-confirm-plot').click()
const naming0 = page.getByTestId('naming-option-0')
const acceptBtn = page.getByTestId('interactive-accept-scene')
await expect(naming0.or(acceptBtn)).toBeVisible({ timeout: 120_000 })
if (await naming0.isVisible()) {
await naming0.click()
await expect(acceptBtn).toBeVisible({ timeout: 120_000 })
}
await acceptBtn.click()
const finishBtn = page.getByTestId('interactive-finish-chapter')
await expect(finishBtn).toBeVisible({ timeout: 120_000 })
await finishBtn.click()
await expect(page.locator('.ProseMirror')).not.toBeEmpty({ timeout: 30_000 })
await app.close()
})
test('E2E-INT-03: restores flow after restart', async () => {
test.setTimeout(300_000)
const app1 = await launchApp(userDataDir)
const page1 = await app1.firstWindow()
await skipOnboarding(page1)
await openBookWithContent(page1)
await page1.getByTestId('panel-tab-ai').click()
await page1.getByTestId('ai-session-new').click()
const sessionValue = await page1.getByTestId('ai-session-select').inputValue()
expect(sessionValue).not.toBe('')
await page1.getByTestId('ai-mode-tab-interactive').click()
await page1.getByTestId('interactive-start').click()
await expect(page1.getByTestId('plot-option-A')).toBeVisible({ timeout: 120_000 })
await page1.getByTestId('plot-option-A').click()
await page1.getByTestId('interactive-confirm-plot').click()
const naming0 = page1.getByTestId('naming-option-0')
const acceptBtn = page1.getByTestId('interactive-accept-scene')
await expect(naming0.or(acceptBtn)).toBeVisible({ timeout: 120_000 })
if (await naming0.isVisible()) {
await naming0.click()
await expect(acceptBtn).toBeVisible({ timeout: 120_000 })
}
await expect(acceptBtn).toBeVisible()
await app1.close()
const app2 = await launchApp(userDataDir)
const page2 = await app2.firstWindow()
await skipOnboarding(page2)
await page2.getByText('交互测试书').click()
await expect(page2.locator('#editor-layout')).toBeVisible({ timeout: 15_000 })
await page2.getByTestId('panel-tab-ai').click()
await expect(page2.getByTestId('ai-session-select')).not.toHaveValue('', { timeout: 10_000 })
await page2.getByTestId('ai-session-select').selectOption(sessionValue)
await page2.getByTestId('ai-mode-tab-interactive').click()
await expect(page2.getByTestId('interactive-accept-scene')).toBeVisible({ timeout: 30_000 })
await app2.close()
})
})