Respect reserved keybindings when registering extensions

This commit is contained in:
Richard Gill 2026-01-18 13:01:12 +00:00 committed by Mario Zechner
parent c63f33d83b
commit 54c33f2ade
3 changed files with 179 additions and 29 deletions

View file

@ -9,6 +9,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { AuthStorage } from "../src/core/auth-storage.js";
import { discoverAndLoadExtensions } from "../src/core/extensions/loader.js";
import { ExtensionRunner } from "../src/core/extensions/runner.js";
import { DEFAULT_KEYBINDINGS, type KeyId } from "../src/core/keybindings.js";
import { ModelRegistry } from "../src/core/model-registry.js";
import { SessionManager } from "../src/core/session-manager.js";
@ -47,7 +48,7 @@ describe("ExtensionRunner", () => {
const result = await discoverAndLoadExtensions([], tempDir, tempDir);
const runner = new ExtensionRunner(result.extensions, result.runtime, tempDir, sessionManager, modelRegistry);
const shortcuts = runner.getShortcuts();
const shortcuts = runner.getShortcuts(DEFAULT_KEYBINDINGS);
expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining("conflicts with built-in"));
expect(shortcuts.has("ctrl+c")).toBe(false);
@ -55,6 +56,125 @@ describe("ExtensionRunner", () => {
warnSpy.mockRestore();
});
it("allows a shortcut when the reserved set no longer contains the default key", async () => {
const extCode = `
export default function(pi) {
pi.registerShortcut("ctrl+p", {
description: "Uses freed default",
handler: async () => {},
});
}
`;
fs.writeFileSync(path.join(extensionsDir, "rebinding.ts"), extCode);
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
const result = await discoverAndLoadExtensions([], tempDir, tempDir);
const runner = new ExtensionRunner(result.extensions, result.runtime, tempDir, sessionManager, modelRegistry);
const keybindings = { ...DEFAULT_KEYBINDINGS, cycleModelForward: "ctrl+n" as KeyId };
const shortcuts = runner.getShortcuts(keybindings);
expect(shortcuts.has("ctrl+p")).toBe(true);
expect(warnSpy).not.toHaveBeenCalledWith(expect.stringContaining("conflicts with built-in"));
warnSpy.mockRestore();
});
it("warns but allows when extension uses non-reserved built-in shortcut", async () => {
const extCode = `
export default function(pi) {
pi.registerShortcut("ctrl+v", {
description: "Overrides non-reserved",
handler: async () => {},
});
}
`;
fs.writeFileSync(path.join(extensionsDir, "non-reserved.ts"), extCode);
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
const result = await discoverAndLoadExtensions([], tempDir, tempDir);
const runner = new ExtensionRunner(result.extensions, result.runtime, tempDir, sessionManager, modelRegistry);
const shortcuts = runner.getShortcuts(DEFAULT_KEYBINDINGS);
expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining("built-in shortcut for pasteImage"));
expect(shortcuts.has("ctrl+v")).toBe(true);
warnSpy.mockRestore();
});
it("blocks shortcuts for reserved actions even when rebound", async () => {
const extCode = `
export default function(pi) {
pi.registerShortcut("ctrl+x", {
description: "Conflicts with rebound reserved",
handler: async () => {},
});
}
`;
fs.writeFileSync(path.join(extensionsDir, "rebound-reserved.ts"), extCode);
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
const result = await discoverAndLoadExtensions([], tempDir, tempDir);
const runner = new ExtensionRunner(result.extensions, result.runtime, tempDir, sessionManager, modelRegistry);
const keybindings = { ...DEFAULT_KEYBINDINGS, interrupt: "ctrl+x" as KeyId };
const shortcuts = runner.getShortcuts(keybindings);
expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining("conflicts with built-in"));
expect(shortcuts.has("ctrl+x")).toBe(false);
warnSpy.mockRestore();
});
it("blocks shortcuts when reserved action has multiple keys", async () => {
const extCode = `
export default function(pi) {
pi.registerShortcut("ctrl+y", {
description: "Conflicts with multi-key reserved",
handler: async () => {},
});
}
`;
fs.writeFileSync(path.join(extensionsDir, "multi-reserved.ts"), extCode);
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
const result = await discoverAndLoadExtensions([], tempDir, tempDir);
const runner = new ExtensionRunner(result.extensions, result.runtime, tempDir, sessionManager, modelRegistry);
const keybindings = { ...DEFAULT_KEYBINDINGS, clear: ["ctrl+x", "ctrl+y"] as KeyId[] };
const shortcuts = runner.getShortcuts(keybindings);
expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining("conflicts with built-in"));
expect(shortcuts.has("ctrl+y")).toBe(false);
warnSpy.mockRestore();
});
it("warns but allows when non-reserved action has multiple keys", async () => {
const extCode = `
export default function(pi) {
pi.registerShortcut("ctrl+y", {
description: "Overrides multi-key non-reserved",
handler: async () => {},
});
}
`;
fs.writeFileSync(path.join(extensionsDir, "multi-non-reserved.ts"), extCode);
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
const result = await discoverAndLoadExtensions([], tempDir, tempDir);
const runner = new ExtensionRunner(result.extensions, result.runtime, tempDir, sessionManager, modelRegistry);
const keybindings = { ...DEFAULT_KEYBINDINGS, pasteImage: ["ctrl+x", "ctrl+y"] as KeyId[] };
const shortcuts = runner.getShortcuts(keybindings);
expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining("built-in shortcut for pasteImage"));
expect(shortcuts.has("ctrl+y")).toBe(true);
warnSpy.mockRestore();
});
it("warns when two extensions register same shortcut", async () => {
// Use a non-reserved shortcut
const extCode1 = `
@ -80,7 +200,7 @@ describe("ExtensionRunner", () => {
const result = await discoverAndLoadExtensions([], tempDir, tempDir);
const runner = new ExtensionRunner(result.extensions, result.runtime, tempDir, sessionManager, modelRegistry);
const shortcuts = runner.getShortcuts();
const shortcuts = runner.getShortcuts(DEFAULT_KEYBINDINGS);
expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining("shortcut conflict"));
// Last one wins