Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

3.1 KB
Raw Blame
import {describe, it} from "node:test";
import * as assert from "node:assert/strict";
import {dispatchPluginGlobalShortcut} from "./globalShortcut";

describe("plugin global shortcuts", () => {
    it("matches secondary bindings and skips disabled commands", () => {
        const calls: string[] = [];
        const plugins = ["first", "second"].map(name => ({name, commands: [{
            langKey: "action", customHotkey: "⌘K", enabled: () => name !== "first",
            globalCallback: () => calls.push(name),
        }]}));
        const keymap = Object.fromEntries(plugins.map(plugin => [plugin.name, {action: {
            custom: "⌘K", bindings: {version: 1 as const, keys: ["⌘K", "⌘L"],
                priority: {"system:⌘L": plugin.name === "first" ? 10 : 0}},
        }}]));
        assert.equal(dispatchPluginGlobalShortcut(plugins, "⌘L", keymap), true);
        assert.deepEqual(calls, ["second"]);
    });
    it("uses stable command IDs after reload and ignores legacy priority settings", () => {
        for (const order of [["second", "first"], ["first", "second"]]) {
            const calls: string[] = [];
            const plugins = order.map(name => ({name, commands: [{
                langKey: "action", customHotkey: "⌘L", globalCallback: () => calls.push(name),
            }]}));
            const keymap = Object.fromEntries(plugins.map(plugin => [plugin.name, {action: {
                custom: "⌘L", bindings: {version: 1 as const, keys: ["⌘L"],
                    priority: {"system:⌘L": plugin.name === "second" ? 100 : 0}},
            }}]));
            assert.equal(dispatchPluginGlobalShortcut(plugins, "⌘L", keymap), true);
            assert.deepEqual(calls, ["first"]);
        }
    });
    it("runs only the first matching command", () => {
        const calls: string[] = [];
        const plugins = ["first", "second"].map(name => ({
            commands: [{
                customHotkey: "⌘A",
                globalCallback: () => calls.push(name),
            }],
        }));

        assert.equal(dispatchPluginGlobalShortcut(plugins, "⌘A"), true);
        assert.deepEqual(calls, ["first"]);
    });

    it("ignores commands that do not match", () => {
        let calls = 0;
        const plugins = [{commands: [{customHotkey: "⌘A", globalCallback: () => calls++}]}];

        assert.equal(dispatchPluginGlobalShortcut(plugins, "⌘B"), false);
        assert.equal(calls, 0);
    });

    it("prefers execute and passes the global context", () => {
        const calls: string[] = [];
        let receivedContext: ICommandContext | undefined;
        const plugins = [{commands: [{
            customHotkey: "⌘A",
            execute: (context: ICommandContext) => {
                calls.push("execute");
                receivedContext = context;
            },
            globalCallback: () => calls.push("global"),
        }]}];

        assert.equal(dispatchPluginGlobalShortcut(plugins, "⌘A"), true);
        assert.deepEqual(calls, ["execute"]);
        assert.deepEqual(receivedContext, {source: "globalShortcut", focus: "global"});
    });
});