Files
MyNote/apps/desktop/tests/e2e/keymap-enhanced.spec.ts
T

90 lines
3.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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-K7:Ctrl+F 呼出查找替换面板(拦截宿主查找)
test('FR-K7:Ctrl+F 呼出查找替换', async () => {
const tmp = await fs.mkdtemp(nodePath.join(os.tmpdir(), 'hn-k7-'));
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, 'K7测试');
// 查找替换面板初始不可见
await expect(a.page.getByPlaceholder('查找')).not.toBeVisible();
// Ctrl+F 呼出
await a.page.locator('.ProseMirror').click();
await a.page.keyboard.press('Control+f');
await expect(a.page.getByPlaceholder('查找')).toBeVisible({ timeout: 5000 });
// 再次 Ctrl+F 关闭
await a.page.keyboard.press('Control+f');
await expect(a.page.getByPlaceholder('查找')).not.toBeVisible({ timeout: 5000 });
await a.app.close();
});
// FR-K6:快捷键速查面板从 store 动态渲染(验证多作用域绑定 + Mac 适配 formatCombo)
test('FR-K6:快捷键速查面板显示绑定', async () => {
const tmp = await fs.mkdtemp(nodePath.join(os.tmpdir(), 'hn-k6-'));
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 });
// 验证 keymap store 包含多作用域绑定(FR-K1/K3)
const result = await a.page.evaluate(() => {
const store = (window as any).__KEYMAP_STORE__;
if (!store) return { error: 'no keymap store' };
const bindings = store.getState().bindings;
const scopes = [...new Set(bindings.map((b: any) => b.scope))];
return {
scopes,
hasEditor: bindings.some((b: any) => b.scope === 'editor'),
hasGlobal: bindings.some((b: any) => b.scope === 'global'),
hasApp: bindings.some((b: any) => b.scope === 'app'),
bindingCount: bindings.length,
conflictCount: store.getState().conflictCount(),
hasFind: bindings.some((b: any) => b.id === 'find'),
hasBold: bindings.some((b: any) => b.id === 'bold'),
};
});
console.log('KEYMAP_STORE', JSON.stringify(result));
expect(result.hasEditor).toBe(true);
expect(result.hasGlobal).toBe(true);
expect(result.hasApp).toBe(true);
expect(result.bindingCount).toBeGreaterThanOrEqual(10);
expect(result.conflictCount).toBe(0);
expect(result.hasFind).toBe(true);
expect(result.hasBold).toBe(true);
await a.app.close();
});