Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions .artifacts/1842/state-coverage.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
144 changes: 144 additions & 0 deletions e2e/search-float-badge-surface.spec.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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<void> {
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<void> {
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<Record<string, string>> = [];
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<void> {
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");
});
49 changes: 48 additions & 1 deletion src/__tests__/lib/localHotkeys.test.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -44,6 +45,9 @@ function event(
}

describe("localHotkeys —— 局部直选读面", () => {
beforeEach(() => resetSurfaces());
afterEach(() => resetSurfaces());

it("默认面(回落):⌘3 → 3", () => {
expect(
localPickDigit(DOC_ID, event("3", { meta: true }, "Digit3"), []),
Expand All @@ -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(
Expand Down
12 changes: 9 additions & 3 deletions src/lib/keybindings/localHotkeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[],
Expand All @@ -44,8 +49,9 @@ export function getLocalSpec(
return resolveShortcut(id, items);
}
const surface = getSurfaceItems<KeybindingSpec>("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());
}

/**
Expand Down
Loading