feat: 实现笔临 P0/P1 Electron 桌面应用 v0.1.0

交付写作核心(TipTap、自动保存、分卷分章)、Onboarding、7 主题与双语 i18n、快捷键设置;主进程使用 node:sqlite;补全 Vitest 与 Playwright E2E;统一 design/ 目录并移除 desigin 拼写错误路径。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-07-06 10:39:20 +08:00
parent a06038035b
commit 011bd6d4ca
70 changed files with 14245 additions and 84 deletions
+10
View File
@@ -0,0 +1,10 @@
import { execSync } from 'child_process'
import { resolve } from 'path'
export default async function globalSetup(): Promise<void> {
execSync('npm run build', {
cwd: resolve(import.meta.dirname, '..'),
stdio: 'inherit',
env: process.env
})
}
+79
View File
@@ -0,0 +1,79 @@
import path from 'path'
import { mkdtempSync, rmSync, readFileSync, existsSync } from 'fs'
import { join } from 'path'
import { tmpdir } from 'os'
import { test, expect, _electron as electron, type ElectronApplication, type Page } from '@playwright/test'
const ROOT = path.join(import.meta.dirname, '..')
function createUserDataDir(): string {
return mkdtempSync(join(tmpdir(), 'bilin-e2e-'))
}
async function launchApp(userDataDir: string): Promise<ElectronApplication> {
return electron.launch({
args: [ROOT],
env: {
...process.env,
BILIN_E2E: '1',
BILIN_E2E_USER_DATA: userDataDir
}
})
}
async function waitForApp(page: Page): Promise<void> {
await page.waitForLoadState('domcontentloaded')
await expect(page.locator('#app')).toBeVisible({ timeout: 20_000 })
}
async function completeOnboarding(page: Page, penName: string, withSample = false): Promise<void> {
await waitForApp(page)
await expect(page.getByText('欢迎使用笔临')).toBeVisible({ timeout: 15_000 })
for (let i = 0; i < 4; i++) {
await page.getByTestId('onboarding-next').click()
}
await page.getByTestId('onboarding-pen-name').fill(penName)
if (!withSample) {
await page.getByText('创建示例书籍').click()
}
await page.getByTestId('onboarding-start').click()
await expect(page.getByText('欢迎使用笔临')).toBeHidden({ timeout: 10_000 })
}
test.describe('Onboarding', () => {
let userDataDir: string
test.beforeEach(() => {
userDataDir = createUserDataDir()
})
test.afterEach(async () => {
if (userDataDir && existsSync(userDataDir)) {
try {
rmSync(userDataDir, { recursive: true, force: true, maxRetries: 3, retryDelay: 200 })
} catch {
/* electron may still release file handles */
}
}
})
test('E2E-ONBOARD-02: complete onboarding saves pen name after restart', async () => {
const app1 = await launchApp(userDataDir)
const page1 = await app1.firstWindow()
await completeOnboarding(page1, '山海', false)
await app1.close()
const settingsPath = join(userDataDir, 'global_settings.json')
expect(existsSync(settingsPath)).toBe(true)
const saved = JSON.parse(readFileSync(settingsPath, 'utf-8')) as { penName: string; onboardingCompleted: boolean }
expect(saved.penName).toBe('山海')
expect(saved.onboardingCompleted).toBe(true)
const app2 = await launchApp(userDataDir)
const page2 = await app2.firstWindow()
await expect(page2.getByText('欢迎使用笔临')).toBeHidden({ timeout: 15_000 })
await page2.getByRole('button', { name: /系统设置/ }).click()
await expect(page2.getByTestId('settings-pen-name')).toHaveValue('山海')
await app2.close()
})
})
+87
View File
@@ -0,0 +1,87 @@
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')
await expect(page.locator('#app')).toBeVisible({ timeout: 20_000 })
if (await page.getByText('欢迎使用笔临').isVisible()) {
await page.getByRole('button', { name: '跳过' }).click()
await expect(page.getByText('欢迎使用笔临')).toBeHidden({ timeout: 10_000 })
}
}
async function openSettings(page: Page): Promise<void> {
await page.getByRole('button', { name: /系统设置/ }).click()
await expect(page.locator('#settings-page')).toBeVisible()
}
test.describe('Settings', () => {
let userDataDir: string
test.beforeEach(() => {
userDataDir = mkdtempSync(join(tmpdir(), 'bilin-e2e-settings-'))
})
test.afterEach(() => {
if (userDataDir && existsSync(userDataDir)) {
try {
rmSync(userDataDir, { recursive: true, force: true, maxRetries: 3, retryDelay: 200 })
} catch {
/* ignore */
}
}
})
test('E2E-THEME: all 7 themes apply data-theme attribute', async () => {
const themes = ['default', 'bamboo', 'moonlit', 'ricepaper', 'mist', 'teagarden', 'twilight'] as const
const app = await launchApp(userDataDir)
const page = await app.firstWindow()
await skipOnboarding(page)
await openSettings(page)
for (const theme of themes) {
await page.getByTestId('settings-theme').selectOption(theme)
const attr = await page.evaluate(() => document.documentElement.getAttribute('data-theme'))
if (theme === 'default') {
expect(attr).toBeNull()
} else {
expect(attr).toBe(theme)
}
}
await app.close()
})
test('E2E-I18N: language switch updates UI text', async () => {
const app = await launchApp(userDataDir)
const page = await app.firstWindow()
await skipOnboarding(page)
await openSettings(page)
await expect(page.getByRole('heading', { name: '系统设置' })).toBeVisible()
await page.getByTestId('settings-language').selectOption('en')
await expect(page.getByRole('heading', { name: 'Settings' })).toBeVisible({ timeout: 5_000 })
await expect(page.locator('.settings-nav-item.active')).toHaveText('General')
await page.getByTestId('settings-language').selectOption('zh-CN')
await expect(page.getByText('通用')).toBeVisible({ timeout: 5_000 })
await app.close()
})
})
+82
View File
@@ -0,0 +1,82 @@
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')
await expect(page.locator('#app')).toBeVisible({ timeout: 20_000 })
if (await page.getByText('欢迎使用笔临').isVisible()) {
await page.getByRole('button', { name: '跳过' }).click()
await expect(page.getByText('欢迎使用笔临')).toBeHidden({ timeout: 10_000 })
}
}
async function createBookAndOpenEditor(page: Page, name: string): Promise<void> {
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 page.getByRole('button', { name: '+ 新章' }).click()
await expect(page.locator('.ProseMirror')).toBeVisible({ timeout: 10_000 })
}
test.describe('Shortcuts', () => {
let userDataDir: string
test.beforeEach(() => {
userDataDir = mkdtempSync(join(tmpdir(), 'bilin-e2e-shortcut-'))
})
test.afterEach(() => {
if (userDataDir && existsSync(userDataDir)) {
try {
rmSync(userDataDir, { recursive: true, force: true, maxRetries: 3, retryDelay: 200 })
} catch {
/* ignore */
}
}
})
test('E2E-SHORTCUT: Ctrl+S saves, chapter/tab actions work, bindings visible', async () => {
const app = await launchApp(userDataDir)
const page = await app.firstWindow()
await skipOnboarding(page)
await createBookAndOpenEditor(page, '快捷键测试')
const editor = page.locator('.ProseMirror')
await editor.click()
await editor.pressSequentially('快捷键保存测试')
await page.keyboard.press('Control+S')
await expect(page.getByText('已保存')).toBeVisible({ timeout: 10_000 })
const chapterCountBefore = await page.locator('.chapter-item').count()
await page.getByRole('button', { name: '+ 新章' }).click()
await expect(page.locator('.chapter-item')).toHaveCount(chapterCountBefore + 1, { timeout: 10_000 })
await page.locator('.tab.active').locator('span').last().click()
await expect(page.locator('#editor-layout')).toBeHidden({ timeout: 10_000 })
await expect(page.locator('#home-page')).toBeVisible()
await page.getByRole('button', { name: /系统设置/ }).click()
await page.locator('.settings-nav-item').filter({ hasText: /快捷键|Shortcuts/ }).click()
await expect(page.getByTestId('shortcut-saveChapter')).toContainText('Ctrl+S')
await expect(page.getByTestId('shortcut-newChapter')).toContainText('Ctrl+Shift+N')
await app.close()
})
})
+75
View File
@@ -0,0 +1,75 @@
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')
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 createAndOpenBook(page: Page, name: string): Promise<void> {
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 })
}
test.describe('Writing', () => {
let userDataDir: string
test.beforeEach(() => {
userDataDir = mkdtempSync(join(tmpdir(), 'bilin-e2e-write-'))
})
test.afterEach(async () => {
if (userDataDir && existsSync(userDataDir)) {
try {
rmSync(userDataDir, { recursive: true, force: true, maxRetries: 3, retryDelay: 200 })
} catch {
/* ignore */
}
}
})
test('auto-save persists content after restart', async () => {
const app1 = await launchApp(userDataDir)
const page1 = await app1.firstWindow()
await skipOnboarding(page1)
await createAndOpenBook(page1, '测试书')
await page1.getByRole('button', { name: '+ 新章' }).click()
const editor = page1.locator('.ProseMirror')
await expect(editor).toBeVisible({ timeout: 10_000 })
await editor.click()
await editor.pressSequentially('林远深吸一口气')
await page1.waitForTimeout(3000)
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('.ProseMirror')).toContainText('林远深吸一口气', { timeout: 10_000 })
await app2.close()
})
})