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 }); const clip = () => app.evaluate(({ clipboard }) => clipboard.readText()); return { app, page, invoke, clip }; } async function openDocWithCode(a: any) { await a.invoke('vault:switch', { root: a.v }); await a.page.getByRole('button', { name: /新建文档/ }).click(); await a.page.getByPlaceholder(/输入文档标题/).fill('CodeCopy'); await a.page.getByPlaceholder(/输入文档标题/).press('Enter'); await expect(a.page.getByTestId('tabbar').getByText('CodeCopy', { exact: true })).toBeVisible({ timeout: 10000 }); const ed = a.page.locator('.ProseMirror'); await ed.click(); await a.page.locator('button[title="代码块 Ctrl+Shift+K"]').click(); await a.page.keyboard.type('sudo apt-get update'); } // 复制代码块内容不应附带 ``` 围栏 test('代码块顶部“复制”按钮复制纯内容(无围栏)', async () => { const tmp = await fs.mkdtemp(nodePath.join(os.tmpdir(), 'hn-cc1-')); const ud = nodePath.join(tmp, 'ud'); const v = nodePath.join(tmp, 'v'); await fs.mkdir(ud, { recursive: true }); await fs.mkdir(v, { recursive: true }); const a = await launch(ud); a.v = v; await openDocWithCode(a); await a.page.locator('.ProseMirror button').filter({ hasText: /复制/ }).first().click(); await a.page.waitForTimeout(200); const text = await a.clip(); console.log('CLIP_BUTTON', JSON.stringify(text)); expect(text.trim()).toBe('sudo apt-get update'); await a.app.close(); }); // Ctrl+C 选中代码块内容复制也不应带围栏 test('代码块内 Ctrl+C 复制纯内容(无围栏)', async () => { const tmp = await fs.mkdtemp(nodePath.join(os.tmpdir(), 'hn-cc2-')); const ud = nodePath.join(tmp, 'ud'); const v = nodePath.join(tmp, 'v'); await fs.mkdir(ud, { recursive: true }); await fs.mkdir(v, { recursive: true }); const a = await launch(ud); a.v = v; await openDocWithCode(a); const code = a.page.locator('.ProseMirror pre code'); await code.click(); // 选中代码块内全部内容 await a.page.keyboard.press('Control+a'); await a.page.keyboard.press('Control+c'); await a.page.waitForTimeout(200); const text = await a.clip(); console.log('CLIP_CTRLC', JSON.stringify(text)); expect(text.trim()).toBe('sudo apt-get update'); await a.app.close(); });