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 }; } async function newDoc(a: { page: any; invoke: any }, title: string) { await a.page.getByRole('button', { name: /新建文档/ }).click(); await a.page.getByPlaceholder(/输入文档标题/).fill(title); await a.page.getByPlaceholder(/输入文档标题/).press('Enter'); await expect(a.page.getByTestId('tabbar').getByText(title, { exact: true })).toBeVisible({ timeout: 10000 }); } // FR-F7:行内重命名失焦提交(onBlur 而非取消) test('FR-F7:行内重命名失焦提交', async () => { const tmp = await fs.mkdtemp(nodePath.join(os.tmpdir(), 'hn-f7-')); 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 newDoc(a, '原名文档'); await a.page.waitForTimeout(500); const node = a.page.getByTestId('filetree').getByText('原名文档', { exact: true }); await node.hover(); await a.page.getByTitle('重命名').click(); const input = a.page.getByTestId('filetree').locator('input'); await input.fill('重命名后'); await a.page.locator('body').click(); await a.page.waitForTimeout(500); await expect(a.page.getByTestId('filetree').getByText('重命名后', { exact: true })).toBeVisible({ timeout: 5000 }); await a.app.close(); }); // FR-F8:删除后从树中移除 test('FR-F8:删除文件后从树中移除', async () => { const tmp = await fs.mkdtemp(nodePath.join(os.tmpdir(), 'hn-f8-')); 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 newDoc(a, '待删文档'); await a.page.waitForTimeout(500); const node = a.page.getByTestId('filetree').getByText('待删文档', { exact: true }); await node.hover(); await a.page.getByTestId('filetree').getByTitle('删除').click(); await a.page.getByRole('button', { name: '确认删除' }).click(); await a.page.waitForTimeout(500); await expect(a.page.getByTestId('filetree').getByText('待删文档', { exact: true })).toHaveCount(0, { timeout: 5000 }); await a.app.close(); }); // FR-F3:文件夹悬停显示新建文件夹按钮 test('FR-F3:文件夹悬停显示新建文件夹按钮', async () => { const tmp = await fs.mkdtemp(nodePath.join(os.tmpdir(), 'hn-f3-')); 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.invoke('file:create', { basePath: '', name: '测试文件夹', isFolder: true }); await a.page.getByTitle('刷新').click(); await a.page.waitForTimeout(500); const folder = a.page.getByTestId('filetree').getByText('测试文件夹', { exact: true }); await folder.hover(); await expect(a.page.getByTitle('新建文件夹')).toBeVisible({ timeout: 3000 }); await a.app.close(); });