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'); const VAULT_SWITCH = 'vault:switch'; const FILE_CREATE = 'file:create'; const DOC_WRITE = 'doc:write'; const DOC_READ = 'doc:read'; // 含嵌套强调、转义、4/3 反引号围栏、GFM 表格对齐、任务列表、数学公式 const KNOWN_MD = [ '# 金标准往返语料', '', '嵌套强调:**外层 *内层* 收尾** 与 ***粗斜***。', '', '转义:写出 \\*不是斜体\\*;代码内 `*不生效*`。', '', '4 反引号围栏包裹 3 反引号:', '', '````md', '```js', 'console.log("hi")', '```', '````', '', '| 左 | 中 | 右 |', '|:--|:-:|--:|', '| a | b | c |', '', '- [x] 已完成', '- [ ] 未完成', '', '行内 $a^2+b^2=c^2$ 与', '', '$$E=mc^2$$', ].join('\n'); 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 }; } test('金标准:建→写→存→重开 字节一致', async () => { const tmp = await fs.mkdtemp(nodePath.join(os.tmpdir(), 'hn-rt-')); 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 a1 = await launch(userData); const ud: string = await a1.app.evaluate(({ app }) => app.getPath('userData')); expect(ud.startsWith(tmp)).toBeTruthy(); // 确认 --user-data-dir 生效,不污染真实数据 await a1.invoke(VAULT_SWITCH, { root: vault }); const created: { path: string } = await a1.invoke(FILE_CREATE, { basePath: '', name: 'roundtrip', isFolder: false }); expect(created.path).toBe('roundtrip.md'); await a1.invoke(DOC_WRITE, { path: 'roundtrip.md', meta: { title: '金标准往返' }, body: KNOWN_MD }); const W1 = await fs.readFile(nodePath.join(vault, 'roundtrip.md'), 'utf8'); await a1.app.close(); // 重开 const a2 = await launch(userData); const read: { meta: any; body: string } = await a2.invoke(DOC_READ, { path: 'roundtrip.md' }); const W2 = await fs.readFile(nodePath.join(vault, 'roundtrip.md'), 'utf8'); await a2.app.close(); expect(read.body).toBe(KNOWN_MD); expect(W2).toBe(W1); });