toolbarShortcuts.test.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import { describe, expect, it } from "vitest";
  2. import { resolveEffectiveShortcutBindings } from "../src/shortcuts/bindings.js";
  3. import { resolveToolbarShortcutAction } from "../src/v4/composer/toolbarShortcuts.js";
  4. import type { ZCodeConfigOption } from "@zcode/shared";
  5. /** 构造工具条关心的键盘事件字段子集。 */
  6. function toolbarEvent(patch: Partial<Parameters<typeof resolveToolbarShortcutAction>[0]> = {}) {
  7. return {
  8. key: "m",
  9. code: "KeyM",
  10. ctrlKey: false,
  11. metaKey: false,
  12. shiftKey: false,
  13. altKey: false,
  14. defaultPrevented: false,
  15. repeat: false,
  16. isComposing: false,
  17. ...patch,
  18. };
  19. }
  20. /** model 类选项(category=model → 归 Ctrl+M 动作)。 */
  21. const modelOption = {
  22. type: "select",
  23. category: "model",
  24. currentValue: "m1",
  25. options: [],
  26. } as unknown as ZCodeConfigOption;
  27. const modeOption = {
  28. type: "select",
  29. category: "mode",
  30. currentValue: "a",
  31. options: [],
  32. } as unknown as ZCodeConfigOption;
  33. const thoughtOption = {
  34. type: "select",
  35. category: "thought_level",
  36. currentValue: "low",
  37. options: [],
  38. } as unknown as ZCodeConfigOption;
  39. const enabledState = {
  40. hasAnyOption: true,
  41. toolbarDisabled: false,
  42. modelMenuDisabled: false,
  43. modelOption,
  44. modeOption,
  45. thoughtOption,
  46. };
  47. describe("composer 工具条热键按生效表解析(spec §12.6)", () => {
  48. it("默认表:Ctrl+M / Ctrl+Shift+M / Ctrl+T 分别命中三个动作", () => {
  49. const effective = resolveEffectiveShortcutBindings(undefined);
  50. expect(
  51. resolveToolbarShortcutAction(toolbarEvent({ ctrlKey: true }), effective, enabledState),
  52. ).toBe("openModelMenu");
  53. expect(
  54. resolveToolbarShortcutAction(
  55. toolbarEvent({ key: "m", code: "KeyM", ctrlKey: true, shiftKey: true }),
  56. effective,
  57. enabledState,
  58. ),
  59. ).toBe("cycleSessionMode");
  60. expect(
  61. resolveToolbarShortcutAction(
  62. toolbarEvent({ key: "t", code: "KeyT", ctrlKey: true }),
  63. effective,
  64. enabledState,
  65. ),
  66. ).toBe("cycleThoughtLevel");
  67. });
  68. it("改绑后按新键位命中:openModelMenu 改绑 Ctrl+Y", () => {
  69. const effective = resolveEffectiveShortcutBindings({ openModelMenu: ["Ctrl+y"] });
  70. expect(
  71. resolveToolbarShortcutAction(toolbarEvent({ ctrlKey: true }), effective, enabledState),
  72. ).toBeNull();
  73. expect(
  74. resolveToolbarShortcutAction(
  75. toolbarEvent({ key: "y", code: "KeyY", ctrlKey: true }),
  76. effective,
  77. enabledState,
  78. ),
  79. ).toBe("openModelMenu");
  80. });
  81. it("门控保留:disabled / 无选项 / defaultPrevented / repeat 不触发动作", () => {
  82. const effective = resolveEffectiveShortcutBindings(undefined);
  83. const ctrlM = toolbarEvent({ ctrlKey: true });
  84. expect(
  85. resolveToolbarShortcutAction(ctrlM, effective, { ...enabledState, toolbarDisabled: true }),
  86. ).toBeNull();
  87. expect(
  88. resolveToolbarShortcutAction(ctrlM, effective, {
  89. ...enabledState,
  90. modelMenuDisabled: true,
  91. }),
  92. ).toBeNull();
  93. expect(
  94. resolveToolbarShortcutAction(ctrlM, effective, { ...enabledState, modelOption: undefined }),
  95. ).toBeNull();
  96. expect(
  97. resolveToolbarShortcutAction({ ...ctrlM, defaultPrevented: true }, effective, enabledState),
  98. ).toBeNull();
  99. expect(
  100. resolveToolbarShortcutAction({ ...ctrlM, repeat: true }, effective, enabledState),
  101. ).toBeNull();
  102. });
  103. it("mac 语义:显式 Ctrl 命中,Cmd+M 不命中(与旧固定热键一致)", () => {
  104. const effective = resolveEffectiveShortcutBindings(undefined);
  105. const macState = { ...enabledState };
  106. expect(
  107. resolveToolbarShortcutAction(
  108. toolbarEvent({ key: "m", code: "KeyM", ctrlKey: true, metaKey: false }),
  109. effective,
  110. macState,
  111. ),
  112. ).toBe("openModelMenu");
  113. expect(
  114. resolveToolbarShortcutAction(toolbarEvent({ metaKey: true }), effective, macState),
  115. ).toBeNull();
  116. });
  117. });