toolbarShortcuts.test.ts 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 = { type: "select", category: "model", currentValue: "m1", options: [] } as unknown as ZCodeConfigOption;
  22. const modeOption = { type: "select", category: "mode", currentValue: "a", options: [] } as unknown as ZCodeConfigOption;
  23. const thoughtOption = { type: "select", category: "thought_level", currentValue: "low", options: [] } as unknown as ZCodeConfigOption;
  24. const enabledState = {
  25. hasAnyOption: true,
  26. toolbarDisabled: false,
  27. modelMenuDisabled: false,
  28. modelOption,
  29. modeOption,
  30. thoughtOption,
  31. };
  32. describe("composer 工具条热键按生效表解析(spec §12.6)", () => {
  33. it("默认表:Ctrl+M / Ctrl+Shift+M / Ctrl+T 分别命中三个动作", () => {
  34. const effective = resolveEffectiveShortcutBindings(undefined);
  35. expect(resolveToolbarShortcutAction(toolbarEvent({ ctrlKey: true }), effective, enabledState)).toBe(
  36. "openModelMenu",
  37. );
  38. expect(
  39. resolveToolbarShortcutAction(toolbarEvent({ key: "m", code: "KeyM", ctrlKey: true, shiftKey: true }), effective, enabledState),
  40. ).toBe("cycleSessionMode");
  41. expect(
  42. resolveToolbarShortcutAction(toolbarEvent({ key: "t", code: "KeyT", ctrlKey: true }), effective, enabledState),
  43. ).toBe("cycleThoughtLevel");
  44. });
  45. it("修饰精确匹配:Ctrl+Shift+M 不误命中 Ctrl+M(与旧 matchesCtrlShortcut 语义一致)", () => {
  46. const effective = resolveEffectiveShortcutBindings(undefined);
  47. expect(
  48. resolveToolbarShortcutAction(toolbarEvent({ ctrlKey: true, shiftKey: true }), effective, enabledState),
  49. ).toBe("cycleSessionMode");
  50. });
  51. it("改绑后按新键位命中:openModelMenu 改绑 Ctrl+Y", () => {
  52. const effective = resolveEffectiveShortcutBindings({ openModelMenu: ["Ctrl+y"] });
  53. expect(
  54. resolveToolbarShortcutAction(toolbarEvent({ ctrlKey: true }), effective, enabledState),
  55. ).toBeNull();
  56. expect(
  57. resolveToolbarShortcutAction(toolbarEvent({ key: "y", code: "KeyY", ctrlKey: true }), effective, enabledState),
  58. ).toBe("openModelMenu");
  59. });
  60. it("门控保留:disabled / 无选项 / defaultPrevented / repeat 不触发动作", () => {
  61. const effective = resolveEffectiveShortcutBindings(undefined);
  62. const ctrlM = toolbarEvent({ ctrlKey: true });
  63. expect(
  64. resolveToolbarShortcutAction(ctrlM, effective, { ...enabledState, toolbarDisabled: true }),
  65. ).toBeNull();
  66. expect(
  67. resolveToolbarShortcutAction(ctrlM, effective, {
  68. ...enabledState,
  69. modelMenuDisabled: true,
  70. }),
  71. ).toBeNull();
  72. expect(
  73. resolveToolbarShortcutAction(ctrlM, effective, { ...enabledState, modelOption: undefined }),
  74. ).toBeNull();
  75. expect(
  76. resolveToolbarShortcutAction({ ...ctrlM, defaultPrevented: true }, effective, enabledState),
  77. ).toBeNull();
  78. expect(resolveToolbarShortcutAction({ ...ctrlM, repeat: true }, effective, enabledState)).toBeNull();
  79. });
  80. it("mac 语义:显式 Ctrl 命中,Cmd+M 不命中(与旧固定热键一致)", () => {
  81. const effective = resolveEffectiveShortcutBindings(undefined);
  82. const macState = { ...enabledState };
  83. expect(
  84. resolveToolbarShortcutAction(toolbarEvent({ key: "m", code: "KeyM", ctrlKey: true, metaKey: false }), effective, macState),
  85. ).toBe("openModelMenu");
  86. expect(
  87. resolveToolbarShortcutAction(toolbarEvent({ metaKey: true }), effective, macState),
  88. ).toBeNull();
  89. });
  90. });