Skip to content

A local-first multi-agent app development platform, built on PenguinHarness

11.4 KB
Raw Blame
/**
 * The keymap store (lib/shortcuts/store.ts) over an in-memory localStorage mirror.
 *
 * - The keymap starts from the platform's registry defaults; the platform's stored section
 *   overlays them, an explicit null unbinds, and an unknown id or unparseable chord keeps the
 *   default.
 * - Malformed JSON, a foreign shape or a wrong version reads as no overrides; the document is
 *   sanitized to the three platform sections of string-or-null values.
 * - A write stores the override in the platform's section and notifies subscribers; an unbind
 *   is stored as null; a row set back to its default is removed (an emptied section with it);
 *   unknown ids and other platforms' sections survive; a literal Ctrl chord is Mod on Linux;
 *   Reset all clears the current platform only.
 * - Another tab's write to the mirror is re-read (only for its own key); defaults move to the
 *   keys the keyboard layout types.
 * - The account's copy: every edit goes to the persister as the compact document; the server's
 *   document wins over the mirror; a mirror edited here is kept when the server has none;
 *   one account's edit never reaches the next account on the tab; a mirror the server does not
 *   know and this session did not write is cleared.
 */
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { parseChord } from "../src/lib/shortcuts/chord";
import { setPlatformForTests } from "../src/lib/shortcuts/platform";
import {
  KEYBINDINGS_KEY,
  bindingOf,
  configureKeybindingsStoreForTests,
  hydrateFromServer,
  isOverridden,
  keymap,
  keymapVersion,
  noteExternalChange,
  readStored,
  resetAll,
  resetBinding,
  sanitizeStored,
  setBinding,
  setKeybindingsPersister,
  subscribeKeymap,
} from "../src/lib/shortcuts/store";
import type { StoredKeybindings } from "../src/lib/shortcuts/types";
import { memoryStorage } from "./helpers/storage";

let storage = memoryStorage();

beforeEach(() => {
  storage = memoryStorage();
  setPlatformForTests("linux");
  configureKeybindingsStoreForTests({ storage, layout: null });
});

afterEach(() => {
  setPlatformForTests(null);
  configureKeybindingsStoreForTests({ storage: null, layout: null });
});

const stored = (): unknown => JSON.parse(storage.map.get(KEYBINDINGS_KEY) ?? "null");

describe("resolution", () => {
  it("starts from the registry defaults for the platform", () => {
    expect(bindingOf("terminal.close")).toEqual(parseChord("Mod+Alt+Backquote"));
    expect(bindingOf("terminal.toggle")).toEqual({
      ...parseChord("Ctrl+Backquote")!,
      mod: true,
      ctrl: false,
    });
    setPlatformForTests("mac");
    configureKeybindingsStoreForTests({});
    expect(bindingOf("terminal.toggle")).toEqual(parseChord("Ctrl+Backquote"));
    expect(isOverridden("terminal.close")).toBe(false);
  });

  it("overlays the platform's stored section, and an explicit null unbinds", () => {
    storage.map.set(
      KEYBINDINGS_KEY,
      JSON.stringify({
        v: 1,
        linux: { "terminal.close": "Mod+Alt+KeyW", "editor.save": null },
        mac: { "terminal.close": "Mod+KeyE" },
      }),
    );
    configureKeybindingsStoreForTests({});
    expect(bindingOf("terminal.close")).toEqual(parseChord("Mod+Alt+KeyW"));
    expect(bindingOf("editor.save")).toBeNull();
    expect(bindingOf("terminal.toggle")).not.toBeNull();
    expect(isOverridden("terminal.close")).toBe(true);
    expect(isOverridden("editor.save")).toBe(true);
    expect(isOverridden("terminal.toggle")).toBe(false);
  });

  it("ignores an unknown id and an unparseable chord when applying, keeping the default", () => {
    storage.map.set(
      KEYBINDINGS_KEY,
      JSON.stringify({ v: 1, linux: { "future.command": "Mod+KeyZ", "terminal.close": "Cmd-W" } }),
    );
    configureKeybindingsStoreForTests({});
    expect(bindingOf("terminal.close")).toEqual(parseChord("Mod+Alt+Backquote"));
    expect(keymap().has("future.command" as never)).toBe(false);
  });

  it("reads malformed JSON, a foreign shape and a wrong version as no overrides", () => {
    for (const raw of [
      "{not json",
      '"a string"',
      "[]",
      '{"v":2,"linux":{"terminal.close":null}}',
      '{"linux":{"terminal.close":null}}',
    ]) {
      storage.map.set(KEYBINDINGS_KEY, raw);
      configureKeybindingsStoreForTests({});
      expect(bindingOf("terminal.close"), raw).toEqual(parseChord("Mod+Alt+Backquote"));
      expect(readStored(storage), raw).toEqual({ v: 1 });
    }
  });

  it("sanitizes the document shape: only the three sections, only string-or-null values", () => {
    expect(
      sanitizeStored({
        v: 1,
        linux: { a: "Mod+KeyA", b: null, c: 3 },
        ios: { a: "Mod+KeyA" },
        mac: [],
        extra: true,
      }),
    ).toEqual({ v: 1, linux: { a: "Mod+KeyA", b: null } });
  });
});

describe("writes", () => {
  it("stores an override in the platform section and notifies subscribers", () => {
    let notified = 0;
    const unsubscribe = subscribeKeymap(() => notified++);
    const before = keymapVersion();
    setBinding("terminal.close", parseChord("Mod+Alt+KeyW"));
    expect(bindingOf("terminal.close")).toEqual(parseChord("Mod+Alt+KeyW"));
    expect(stored()).toEqual({ v: 1, linux: { "terminal.close": "Mod+Alt+KeyW" } });
    expect(notified).toBe(1);
    expect(keymapVersion()).toBeGreaterThan(before);
    unsubscribe();
  });

  it("stores an explicit unbind as null", () => {
    setBinding("editor.save", null);
    expect(bindingOf("editor.save")).toBeNull();
    expect(stored()).toEqual({ v: 1, linux: { "editor.save": null } });
  });

  it("removes a row set back to its default, and drops an emptied section", () => {
    setBinding("terminal.close", parseChord("Mod+Alt+KeyW"));
    setBinding("terminal.close", parseChord("Ctrl+Alt+Backquote"));
    expect(isOverridden("terminal.close")).toBe(false);
    expect(stored()).toEqual({ v: 1 });
  });

  it("treats a literal Ctrl chord as Mod on Linux, so Ctrl+` is the terminal toggle's default there", () => {
    setBinding("terminal.toggle", parseChord("Ctrl+Backquote"));
    expect(isOverridden("terminal.toggle")).toBe(false);
  });

  it("preserves unknown ids and the other platforms' sections on write", () => {
    storage.map.set(
      KEYBINDINGS_KEY,
      JSON.stringify({
        v: 1,
        linux: { "future.command": "Mod+KeyZ" },
        mac: { "terminal.close": "Mod+KeyE" },
      }),
    );
    configureKeybindingsStoreForTests({});
    setBinding("editor.save", parseChord("Mod+Shift+KeyS"));
    expect(stored()).toEqual({
      v: 1,
      linux: { "future.command": "Mod+KeyZ", "editor.save": "Mod+Shift+KeyS" },
      mac: { "terminal.close": "Mod+KeyE" },
    });
    resetBinding("editor.save");
    expect(stored()).toEqual({
      v: 1,
      linux: { "future.command": "Mod+KeyZ" },
      mac: { "terminal.close": "Mod+KeyE" },
    });
  });

  it("resetAll clears the current platform's section only", () => {
    storage.map.set(
      KEYBINDINGS_KEY,
      JSON.stringify({ v: 1, linux: { "editor.save": null }, mac: { "editor.save": null } }),
    );
    configureKeybindingsStoreForTests({});
    resetAll();
    expect(stored()).toEqual({ v: 1, mac: { "editor.save": null } });
    expect(bindingOf("editor.save")).toEqual(parseChord("Mod+KeyS"));
  });
});

describe("external changes and the keyboard layout", () => {
  it("re-reads the mirror when another tab wrote it, and only for its own key", () => {
    expect(bindingOf("editor.save")).toEqual(parseChord("Mod+KeyS"));
    storage.map.set(KEYBINDINGS_KEY, JSON.stringify({ v: 1, linux: { "editor.save": null } }));
    expect(bindingOf("editor.save")).toEqual(parseChord("Mod+KeyS")); // still cached
    noteExternalChange("penguin.theme");
    expect(bindingOf("editor.save")).toEqual(parseChord("Mod+KeyS"));
    noteExternalChange(KEYBINDINGS_KEY);
    expect(bindingOf("editor.save")).toBeNull();
    storage.map.delete(KEYBINDINGS_KEY);
    noteExternalChange(null); // a whole-store clear
    expect(bindingOf("editor.save")).toEqual(parseChord("Mod+KeyS"));
  });

  it("relocates defaults to the layout's keys and treats the relocated chord as the default", () => {
    // US-Dvorak types p on the physical R key and r on the P key.
    const dvorak = new Map([
      ["KeyP", "r"],
      ["KeyR", "p"],
    ]);
    configureKeybindingsStoreForTests({ layout: dvorak });
    expect(bindingOf("palette.toggle")?.code).toBe("KeyR");
    // The user records the key labelled P (physically KeyR): that is the default, not an override.
    setBinding("palette.toggle", parseChord("Mod+Alt+KeyR"));
    expect(isOverridden("palette.toggle")).toBe(false);
    // A stored user chord is physical and does not move.
    setBinding("palette.toggle", parseChord("Mod+Alt+KeyP"));
    expect(bindingOf("palette.toggle")?.code).toBe("KeyP");
    expect(stored()).toEqual({ v: 1, linux: { "palette.toggle": "Mod+Alt+KeyP" } });
  });
});

describe("the account's copy", () => {
  it("carries every edit to the persister as the compact document, and nothing before one is installed", () => {
    const sent: StoredKeybindings[] = [];
    setBinding("editor.save", null);
    setKeybindingsPersister((doc) => sent.push(doc));
    setBinding("terminal.close", parseChord("Mod+Alt+KeyW"));
    resetBinding("editor.save");
    expect(sent).toEqual([
      { v: 1, linux: { "editor.save": null, "terminal.close": "Mod+Alt+KeyW" } },
      { v: 1, linux: { "terminal.close": "Mod+Alt+KeyW" } },
    ]);
    setKeybindingsPersister(null);
    resetAll();
    expect(sent).toHaveLength(2);
  });

  it("applies the server document over the mirror, even over an edit made this session", () => {
    setBinding("editor.save", null);
    hydrateFromServer({ v: 1, linux: { "terminal.close": "Mod+Alt+KeyW" }, mac: {} });
    expect(bindingOf("editor.save")).toEqual(parseChord("Mod+KeyS"));
    expect(bindingOf("terminal.close")).toEqual(parseChord("Mod+Alt+KeyW"));
    expect(stored()).toEqual({ v: 1, linux: { "terminal.close": "Mod+Alt+KeyW" } });
  });

  it("keeps the mirror when the server has no copy but this account edited it here, sending nothing again", () => {
    const sent: StoredKeybindings[] = [];
    setKeybindingsPersister((doc) => sent.push(doc));
    setBinding("editor.save", null);
    hydrateFromServer(undefined);
    expect(sent).toEqual([{ v: 1, linux: { "editor.save": null } }]);
    expect(bindingOf("editor.save")).toBeNull();
    expect(stored()).toEqual({ v: 1, linux: { "editor.save": null } });
  });

  it("does not carry one account's edit into the next account signed in on the same tab", () => {
    const first: StoredKeybindings[] = [];
    setKeybindingsPersister((doc) => first.push(doc));
    setBinding("editor.save", null); // account A edits, then signs out (no reload)
    setKeybindingsPersister(null);
    const second: StoredKeybindings[] = [];
    setKeybindingsPersister((doc) => second.push(doc)); // account B signs in
    hydrateFromServer(undefined); // B has no keybindings
    expect(first).toHaveLength(1);
    expect(second).toEqual([]);
    expect(storage.map.has(KEYBINDINGS_KEY)).toBe(false);
    expect(bindingOf("editor.save")).toEqual(parseChord("Mod+KeyS"));
  });

  it("clears a mirror the server knows nothing about when this session did not write it", () => {
    storage.map.set(KEYBINDINGS_KEY, JSON.stringify({ v: 1, linux: { "editor.save": null } }));
    configureKeybindingsStoreForTests({});
    expect(bindingOf("editor.save")).toBeNull();
    hydrateFromServer(undefined);
    expect(storage.map.has(KEYBINDINGS_KEY)).toBe(false);
    expect(bindingOf("editor.save")).toEqual(parseChord("Mod+KeyS"));
  });
});