command.test.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { describe, expect, test } from "bun:test"
  2. import { commandPaletteOptions, resolveKeybindOption, upsertCommandRegistration, type CommandOption } from "./command"
  3. const paletteOptions: CommandOption[] = [
  4. { id: "settings.open", title: "Open settings" },
  5. { id: "session.undo", title: "Undo" },
  6. { id: "file.open", title: "Open file" },
  7. { id: "hidden", title: "Hidden", hidden: true },
  8. { id: "disabled", title: "Disabled", disabled: true },
  9. ]
  10. describe("commandPaletteOptions", () => {
  11. test("keeps visible enabled commands", () => {
  12. expect(commandPaletteOptions(paletteOptions).map((option) => option.id)).toEqual(["settings.open", "session.undo"])
  13. })
  14. })
  15. describe("upsertCommandRegistration", () => {
  16. test("replaces keyed registrations", () => {
  17. const one = () => [{ id: "one", title: "One" }]
  18. const two = () => [{ id: "two", title: "Two" }]
  19. const next = upsertCommandRegistration([{ key: "layout", options: one }], { key: "layout", options: two })
  20. expect(next).toHaveLength(1)
  21. expect(next[0]?.options).toBe(two)
  22. })
  23. test("keeps unkeyed registrations additive", () => {
  24. const one = () => [{ id: "one", title: "One" }]
  25. const two = () => [{ id: "two", title: "Two" }]
  26. const next = upsertCommandRegistration([{ options: one }], { options: two })
  27. expect(next).toHaveLength(2)
  28. expect(next[0]?.options).toBe(two)
  29. expect(next[1]?.options).toBe(one)
  30. })
  31. })
  32. describe("resolveKeybindOption", () => {
  33. test("prefers a matching contextual command over the global fallback", () => {
  34. const fallback = { id: "tab.close", title: "Close tab" }
  35. const contextual = { id: "terminal.close", title: "Close terminal", when: () => true }
  36. expect(resolveKeybindOption([fallback, contextual], new KeyboardEvent("keydown"))).toBe(contextual)
  37. })
  38. test("uses the global fallback outside the command context", () => {
  39. const fallback = { id: "tab.close", title: "Close tab" }
  40. const contextual = { id: "terminal.close", title: "Close terminal", when: () => false }
  41. expect(resolveKeybindOption([fallback, contextual], new KeyboardEvent("keydown"))).toBe(fallback)
  42. })
  43. })