Files
bilin/e2e/auto-writing.spec.ts
bing 8e5045b882 fix(test): stabilize E2E cockpit flow and add LM Studio novel integration test
Harden cockpit dismiss timing and global settings navigation in E2E helpers, tune short-chapter AI prompts for local models, fix electron-updater CJS loading, and add a 10-chapter auto-writing integration test against LM Studio.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-09 14:03:19 +08:00

155 lines
6.0 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 dismissCockpitIfOpen(page: Page): Promise<void> {
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 ensureChapterEditor(page: Page): Promise<void> {
await dismissCockpitIfOpen(page)
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 dismissCockpitIfOpen(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 })
}
async function openAutoPanel(page: Page): Promise<void> {
await page.getByTestId('panel-tab-ai').click()
await page.getByTestId('ai-session-new').click()
await expect(page.getByTestId('ai-mode-tab-auto')).toBeEnabled({ timeout: 10_000 })
await page.getByTestId('ai-mode-tab-auto').click()
await expect(page.getByTestId('auto-panel')).toBeVisible({ timeout: 10_000 })
}
async function planAutoScenes(page: Page): Promise<void> {
await page.getByTestId('auto-goal-input').fill('林远通过入门考核,展现天赋震惊众人')
const wordInputs = page.locator('.auto-word-range input')
await wordInputs.nth(0).fill('180')
await wordInputs.nth(1).fill('220')
await page.getByTestId('auto-plan-scenes').click()
await expect(page.getByTestId('auto-start-generate')).toBeVisible({ timeout: 360_000 })
}
async function generateOneAutoScene(page: Page): Promise<void> {
await page.getByTestId('auto-start-generate').click()
const naming0 = page.getByTestId('naming-option-0')
const finishBtn = page.getByTestId('auto-finish-chapter')
await expect(naming0.or(finishBtn).or(page.getByTestId('auto-start-generate'))).toBeVisible({
timeout: 240_000
})
if (await naming0.isVisible()) {
await naming0.click()
await expect(finishBtn.or(page.getByTestId('auto-start-generate'))).toBeVisible({
timeout: 240_000
})
}
}
test.describe('Auto writing', () => {
test.describe.configure({ retries: 1 })
let userDataDir: string
test.beforeEach(() => {
userDataDir = mkdtempSync(join(tmpdir(), 'bilin-e2e-auto-'))
})
test.afterEach(() => {
if (userDataDir && existsSync(userDataDir)) {
try {
rmSync(userDataDir, { recursive: true, force: true, maxRetries: 3, retryDelay: 200 })
} catch {
/* ignore */
}
}
})
test('E2E-AUTO-03: auto 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 dismissCockpitIfOpen(page)
await page.getByTestId('panel-tab-ai').click()
await expect(page.getByTestId('ai-mode-tab-auto')).toBeDisabled({ timeout: 10_000 })
await app.close()
})
test('E2E-AUTO-01: auto flow creates chapter', async () => {
test.setTimeout(600_000)
const app = await launchApp(userDataDir)
const page = await app.firstWindow()
await skipOnboarding(page)
await openBookWithContent(page)
await openAutoPanel(page)
await planAutoScenes(page)
await generateOneAutoScene(page)
await expect(page.getByTestId('auto-finish-chapter')).toBeVisible({ timeout: 30_000 })
await page.getByTestId('auto-finish-chapter').click()
await expect(page.locator('.ProseMirror')).not.toBeEmpty({ timeout: 30_000 })
await app.close()
})
test('E2E-AUTO-02: pause and handoff to interactive', async () => {
test.setTimeout(600_000)
const app = await launchApp(userDataDir)
const page = await app.firstWindow()
await skipOnboarding(page)
await openBookWithContent(page)
await openAutoPanel(page)
await planAutoScenes(page)
await page.getByTestId('auto-start-generate').click()
await expect(page.getByTestId('auto-pause')).toBeEnabled({ timeout: 15_000 })
await page.waitForTimeout(2_000)
await page.getByTestId('auto-pause').click()
await expect(page.getByTestId('auto-handoff-interactive')).toBeVisible({ timeout: 10_000 })
await page.getByTestId('auto-handoff-interactive').click()
await expect(page.getByTestId('interactive-panel')).toBeVisible({ timeout: 15_000 })
const acceptBtn = page.getByTestId('interactive-accept-scene')
const plotA = page.getByTestId('plot-option-A')
const startBtn = page.getByTestId('interactive-start')
await expect(acceptBtn.or(plotA).or(startBtn)).toBeVisible({ timeout: 120_000 })
await app.close()
})
})