From 9d9ceb8235b5995d71e720a5de7cadab5999aad4 Mon Sep 17 00:00:00 2001 From: lqtdys Date: Wed, 2 Dec 2026 19:05:49 +0800 Subject: [PATCH] =?UTF-8?q?fix(search):=20=E5=B1=80=E9=83=A8=E9=94=AE?= =?UTF-8?q?=E4=BD=8D=E9=9D=A2=E7=BC=BA=E6=9D=A1=E7=9B=AE=E6=97=B6=E5=BE=BD?= =?UTF-8?q?=E6=A0=87=E5=9B=9E=E8=90=BD=E4=BB=A3=E7=A0=81=E9=BB=98=E8=AE=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fixes #1842 --- .artifacts/1842/state-coverage.json | 33 ++++++ e2e/search-float-badge-surface.spec.ts | 144 +++++++++++++++++++++++++ src/__tests__/lib/localHotkeys.test.ts | 49 ++++++++- src/lib/keybindings/localHotkeys.ts | 12 ++- 4 files changed, 234 insertions(+), 4 deletions(-) create mode 100644 .artifacts/1842/state-coverage.json create mode 100644 e2e/search-float-badge-surface.spec.ts diff --git a/.artifacts/1842/state-coverage.json b/.artifacts/1842/state-coverage.json new file mode 100644 index 00000000..6853613a --- /dev/null +++ b/.artifacts/1842/state-coverage.json @@ -0,0 +1,33 @@ +{ + "task": "1842-search-badge-surface-fallback", + "states": [ + { + "state": "01-stale-surface-badges", + "screenshot": ".artifacts/1842/01-stale-surface-badges.png", + "assertion": "passed", + "recorded_at": "2026-12-02T11:04:10.441Z", + "runner": "unknown@unknown" + }, + { + "state": "02-selection-moved", + "screenshot": ".artifacts/1842/02-selection-moved.png", + "assertion": "passed", + "recorded_at": "2026-12-02T11:04:10.474Z", + "runner": "unknown@unknown" + }, + { + "state": "04-empty-query-no-results", + "screenshot": ".artifacts/1842/04-empty-query-no-results.png", + "assertion": "passed", + "recorded_at": "2026-12-02T11:04:10.501Z", + "runner": "unknown@unknown" + }, + { + "state": "03-remapped-alt-badges", + "screenshot": ".artifacts/1842/03-remapped-alt-badges.png", + "assertion": "passed", + "recorded_at": "2026-12-02T11:04:11.551Z", + "runner": "unknown@unknown" + } + ] +} diff --git a/e2e/search-float-badge-surface.spec.ts b/e2e/search-float-badge-surface.spec.ts new file mode 100644 index 00000000..73b1bc16 --- /dev/null +++ b/e2e/search-float-badge-surface.spec.ts @@ -0,0 +1,144 @@ +import { test, expect, type Page } from "@playwright/test"; +import { mkdirSync, readFileSync, writeFileSync } from "node:fs"; +import path from "node:path"; + +/** + * #1842 搜索结果行尾快捷键徽标:面已加载但缺 `local-search-row-pick` 时仍显示 ⌘N。 + * + * 已安装用户的磁盘默认 keybindings.json 是 #1785 之前的旧快照(Rust materialize + * 磁盘优先、不回填新条目),面非空却缺该 id;旧实现返回 null → 徽标空。 + * 这里用 `window.__biaoyue_keybindings__` 在 app 运行前注入面,复现真机面。 + */ + +const DOC = "# 属性面板\n\n属性一\n\n属性二\n\n属性三\n"; + +const RESOURCES = path.resolve( + process.cwd(), + "src-tauri/src/resources/keybindings.default.json", +); + +type KeybindingItem = { + id: string; + keys: { global?: string; local?: string }; + command: string; + kind: "chord" | "hold"; + family: string; + label: string; + context?: string; + core: boolean; + implemented: boolean; + remappable: boolean; +}; + +function defaultItems(): KeybindingItem[] { + return ( + JSON.parse(readFileSync(RESOURCES, "utf8")) as { items: KeybindingItem[] } + ).items; +} + +async function boot(page: Page, items: KeybindingItem[]): Promise { + await page.addInitScript({ path: "./e2e/mocks/tauri-mock.js" }); + await page.addInitScript((kb) => { + ( + window as unknown as { __biaoyue_keybindings__: KeybindingItem[] } + ).__biaoyue_keybindings__ = kb; + }, items); + await page.goto("/", { waitUntil: "networkidle" }); + await page.waitForSelector('[data-testid="pane-header"]', { timeout: 15000 }); +} + +async function openDoc(page: Page): Promise { + await page.evaluate((content) => { + ( + window as unknown as { + __biaoyue_openFile__: (p: string, c: string, t?: string) => boolean; + } + ).__biaoyue_openFile__("/Users/test/badge.md", content, "badge.md"); + }, DOC); +} + +const ART_1842 = path.resolve(process.cwd(), ".artifacts/1842"); + +/** ui-surface 证据:截图 + 按 state 去重合并进 state-coverage.json。 */ +async function recordState1842(page: Page, state: string): Promise { + mkdirSync(ART_1842, { recursive: true }); + const file = path.join(ART_1842, `${state}.png`); + await page.screenshot({ path: file }); + const manifest = path.join(ART_1842, "state-coverage.json"); + let states: Array> = []; + try { + states = JSON.parse(readFileSync(manifest, "utf8")).states ?? []; + } catch { + states = []; + } + states = states.filter((s) => s.state !== state); + states.push({ + state, + screenshot: path.relative(process.cwd(), file), + assertion: "passed", + recorded_at: new Date().toISOString(), + runner: `${process.env.USER ?? "unknown"}@${process.env.HOSTNAME ?? "unknown"}`, + }); + writeFileSync( + manifest, + `${JSON.stringify({ task: "1842-search-badge-surface-fallback", states }, null, 2)}\n`, + ); +} + +async function openSearch(page: Page): Promise { + await page.keyboard.press("Meta+f"); + const input = page.locator('[data-testid="search-float-input"]'); + await expect(input).toBeFocused(); + await input.fill("属性"); + await expect(page.locator('[data-testid="search-float-hit"]')).toHaveCount(4); +} + +test("面非空缺 local-search-row-pick:选中行 ↵、其余可见行 ⌘N(回落代码默认)", async ({ + page, +}) => { + const stale = defaultItems().filter( + (item) => item.id !== "local-search-row-pick", + ); + await boot(page, stale); + await openDoc(page); + await openSearch(page); + + const rows = page.locator('[data-testid="search-float-hit"]'); + await expect(rows.nth(0).locator(".search-float-key")).toHaveText("↵"); + await expect(rows.nth(1).locator(".search-float-key")).toHaveText("⌘2"); + await expect(rows.nth(2).locator(".search-float-key")).toHaveText("⌘3"); + await expect(rows.nth(3).locator(".search-float-key")).toHaveText("⌘4"); + await recordState1842(page, "01-stale-surface-badges"); + + await page.keyboard.press("ArrowDown"); + await expect(rows.nth(1).locator(".search-float-key")).toHaveText("↵"); + await expect(rows.nth(0).locator(".search-float-key")).toHaveText("⌘1"); + await recordState1842(page, "02-selection-moved"); + + await page.locator('[data-testid="search-float-input"]').fill(""); + await expect(page.locator('[data-testid="search-float-hit"]')).toHaveCount(0); + await expect( + page.locator('[data-testid="search-float-results"]'), + ).toBeHidden(); + await recordState1842(page, "04-empty-query-no-results"); +}); + +test("面含该 id 且重映射 ⌥1–9:徽标显示 ⌥ 前缀(回落不覆盖面项)", async ({ + page, +}) => { + const remapped = defaultItems().map((item) => + item.id === "local-search-row-pick" + ? { ...item, keys: { local: "⌥1–9" } } + : item, + ); + await boot(page, remapped); + await openDoc(page); + await openSearch(page); + + const rows = page.locator('[data-testid="search-float-hit"]'); + await expect(rows.nth(0).locator(".search-float-key")).toHaveText("↵"); + await expect(rows.nth(1).locator(".search-float-key")).toHaveText("⌥2"); + await expect(rows.nth(2).locator(".search-float-key")).toHaveText("⌥3"); + await expect(rows.nth(3).locator(".search-float-key")).toHaveText("⌥4"); + await recordState1842(page, "03-remapped-alt-badges"); +}); diff --git a/src/__tests__/lib/localHotkeys.test.ts b/src/__tests__/lib/localHotkeys.test.ts index 01abbf2b..07d0f20a 100644 --- a/src/__tests__/lib/localHotkeys.test.ts +++ b/src/__tests__/lib/localHotkeys.test.ts @@ -1,5 +1,6 @@ -import { describe, expect, it } from "vitest"; +import { afterEach, beforeEach, describe, expect, it } from "vitest"; import type { KeybindingSpec } from "../../lib/dataSurfaces/loader"; +import { resetSurfaces, setSurfaces } from "../../lib/dataSurfaces/store"; import { getLocalSpec, localDigitLabel, @@ -44,6 +45,9 @@ function event( } describe("localHotkeys —— 局部直选读面", () => { + beforeEach(() => resetSurfaces()); + afterEach(() => resetSurfaces()); + it("默认面(回落):⌘3 → 3", () => { expect( localPickDigit(DOC_ID, event("3", { meta: true }, "Digit3"), []), @@ -70,6 +74,49 @@ describe("localHotkeys —— 局部直选读面", () => { ).toBe(1); }); + it("面非空但缺该 id:回落代码默认(⌘1–9),不再返回 null", () => { + // 已安装用户磁盘上的旧 keybindings.json 缺 #1785 后新增的 local id, + // 面已加载但不含该条目——此时仍应回落代码默认,而不是徽标空。 + const stale: KeybindingSpec[] = [ + { + id: "local-doc-menu-pick", + keys: { local: "⌘1–9" }, + command: "doc-menu-pick", + kind: "chord", + family: "local", + label: "文档链接菜单直选", + context: "属性面板 [[ 文档菜单打开", + core: false, + implemented: true, + remappable: true, + }, + ]; + setSurfaces([{ id: "keybindings", items: stale, warnings: [] }]); + expect(getLocalSpec("local-search-row-pick")).not.toBeNull(); + expect(getLocalSpec("local-search-row-pick")?.keys.local).toBe("⌘1–9"); + expect(localDigitLabel("local-search-row-pick", 0)).toBe("⌘1"); + }); + + it("面含该 id:尊重面项(重映射 ⌥1–9 不被回落覆盖)", () => { + const remapped: KeybindingSpec[] = [ + { + id: "local-search-row-pick", + keys: { local: "⌥1–9" }, + command: "search-row-pick", + kind: "chord", + family: "local", + label: "搜索结果直选", + context: "搜索浮窗打开且焦点在浮窗内", + core: false, + implemented: true, + remappable: true, + }, + ]; + setSurfaces([{ id: "keybindings", items: remapped, warnings: [] }]); + expect(getLocalSpec("local-search-row-pick")?.keys.local).toBe("⌥1–9"); + expect(localDigitLabel("local-search-row-pick", 1)).toBe("⌥2"); + }); + it("未命中 / 非数字键体返回 null", () => { const items = [spec("⌥1–9")]; expect( diff --git a/src/lib/keybindings/localHotkeys.ts b/src/lib/keybindings/localHotkeys.ts index 814e5af6..38519f49 100644 --- a/src/lib/keybindings/localHotkeys.ts +++ b/src/lib/keybindings/localHotkeys.ts @@ -35,7 +35,12 @@ function fallbackSpecs(): KeybindingSpec[] { })); } -/** 按 local id 取键位项;面非空时从面取(缺 id 返回 null),否则回落代码默认集。 */ +/** + * 按 local id 取键位项。 + * + * 面非空但缺该 id(旧磁盘快照缺新条目)时按 id 回落代码默认——面是整体加载的, + * 缺条目不代表用户重映射过该 id,只是快照陈旧;此时返回 null 会让徽标空白。 + */ export function getLocalSpec( id: string, items?: readonly KeybindingSpec[], @@ -44,8 +49,9 @@ export function getLocalSpec( return resolveShortcut(id, items); } const surface = getSurfaceItems("keybindings"); - const source = surface.length > 0 ? surface : fallbackSpecs(); - return resolveShortcut(id, source); + const fromSurface = resolveShortcut(id, surface); + if (fromSurface !== null) return fromSurface; + return resolveShortcut(id, fallbackSpecs()); } /**