import { test, expect } from '@playwright/test'; import { _electron as electron } from 'playwright'; import { promises as fs } from 'node:fs'; import nodePath from 'node:path'; import os from 'node:os'; const MAIN_JS = nodePath.join(process.cwd(), 'dist-electron/main/index.js'); async function launch(userData: string) { const app = await electron.launch({ args: ['--user-data-dir=' + userData, MAIN_JS] }); const page = await app.firstWindow(); await page.waitForLoadState('domcontentloaded'); const invoke = (ch: string, args: any) => page.evaluate(({ ch, args }) => (globalThis as any).dsh.invoke(ch, args), { ch, args }); return { app, page, invoke }; } // FR-E5 代码块语言切换:每个代码块顶部语言下拉 -> 切换 language 属性 test('FR-E5:代码块语言下拉切换生效', async () => { const tmp = await fs.mkdtemp(nodePath.join(os.tmpdir(), 'hn-e5-')); const userData = nodePath.join(tmp, 'userdata'); const vault = nodePath.join(tmp, 'vault'); await fs.mkdir(userData, { recursive: true }); await fs.mkdir(vault, { recursive: true }); const a = await launch(userData); await a.invoke('vault:switch', { root: vault }); await a.page.getByRole('button', { name: /新建文档/ }).click(); await a.page.getByPlaceholder(/输入文档标题/).fill('代码块'); await a.page.getByPlaceholder(/输入文档标题/).press('Enter'); await expect(a.page.getByTestId('tabbar').getByText('代码块', { exact: true })).toBeVisible({ timeout: 10000 }); const editor = a.page.locator('.ProseMirror'); await editor.click(); // 插入代码块并输入内容 await a.page.locator('button[title="代码块 Ctrl+Shift+K"]').click(); await a.page.keyboard.type('key: value'); // 代码块顶部应渲染语言下拉 const select = a.page.locator('.ProseMirror select'); await expect(select).toBeVisible({ timeout: 10000 }); // 切换到 YAML -> code 元素带 language-yaml await select.selectOption('yaml'); let cls: string = await a.page.locator('.ProseMirror pre code').evaluate(el => (el as HTMLElement).className); expect(cls).toContain('language-yaml'); // 切换到 JSON -> language-json await select.selectOption('json'); cls = await a.page.locator('.ProseMirror pre code').evaluate(el => (el as HTMLElement).className); expect(cls).toContain('language-json'); // 切换到 TypeScript -> language-typescript await select.selectOption('typescript'); cls = await a.page.locator('.ProseMirror pre code').evaluate(el => (el as HTMLElement).className); expect(cls).toContain('language-typescript'); await a.app.close(); });