v4SlashCommands.test.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. parseSelectionSideSlashCommand,
  4. parseV4VisibleSlashCommand,
  5. v4QueuedCommandText,
  6. } from "@/v4/slashCommands.js";
  7. describe("parseV4VisibleSlashCommand", () => {
  8. it("parses /goal and /target into a goal command objective", () => {
  9. expect(parseV4VisibleSlashCommand("/goal 修复登录", [])).toMatchObject({
  10. kind: "sendGoalCommand",
  11. objective: "修复登录",
  12. displayText: "/goal 修复登录",
  13. });
  14. expect(parseV4VisibleSlashCommand(" /target 修复登录 ", [])).toMatchObject({
  15. kind: "sendGoalCommand",
  16. objective: "修复登录",
  17. displayText: "/target 修复登录",
  18. });
  19. });
  20. it("strips /goal replace from the objective but preserves display text", () => {
  21. expect(parseV4VisibleSlashCommand("/goal replace 新目标", [])).toMatchObject({
  22. kind: "sendGoalCommand",
  23. objective: "新目标",
  24. displayText: "/goal replace 新目标",
  25. });
  26. });
  27. it("maps the CLI /goal resume syntax to resumeGoal", () => {
  28. expect(parseV4VisibleSlashCommand("/goal resume", [])).toEqual({
  29. kind: "resumeGoal",
  30. displayText: "/goal resume",
  31. });
  32. });
  33. it("does not consume the removed UI-only /resume-goal alias", () => {
  34. expect(parseV4VisibleSlashCommand("/resume-goal", [])).toBeNull();
  35. expect(parseV4VisibleSlashCommand("/resumegoal", [])).toBeNull();
  36. });
  37. it("does not consume commands with attachments", () => {
  38. expect(
  39. parseV4VisibleSlashCommand("/goal 修复登录", [
  40. { ref: "zcode-artifact://a", fileName: "a.txt", mime: "text/plain", bytes: 1 },
  41. ]),
  42. ).toBeNull();
  43. });
  44. it.each(["/plan", " /PLAN 帮我做 X ", "/planner 做普通任务"])(
  45. "does not interpret %s as a send-time command",
  46. (text) => {
  47. expect(parseV4VisibleSlashCommand(text)).toBeNull();
  48. expect(parseV4VisibleSlashCommand(text, [{ ref: "zcode-artifact://a" }])).toBeNull();
  49. expect(parseV4VisibleSlashCommand(text, [], { contextAttachmentCount: 1 })).toBeNull();
  50. },
  51. );
  52. it("does not consume commands with web element contexts", () => {
  53. expect(
  54. parseV4VisibleSlashCommand("/goal 修复登录", [], {
  55. contextAttachmentCount: 1,
  56. }),
  57. ).toBeNull();
  58. });
  59. it("keeps queued goal command syntax when edited to objective-only text", () => {
  60. expect(v4QueuedCommandText("sendGoalCommand", "新目标")).toBe("/goal 新目标");
  61. expect(v4QueuedCommandText("sendGoalCommand", "/target 新目标")).toBe("/target 新目标");
  62. expect(v4QueuedCommandText("sendText", "新目标")).toBe("新目标");
  63. });
  64. });
  65. describe("parseSelectionSideSlashCommand", () => {
  66. const enabledCommandNames = ["side", "btw"] as const;
  67. it("parses both aliases and preserves internal whitespace", () => {
  68. expect(
  69. parseSelectionSideSlashCommand(" /SIDE 你好\n继续查一下 ", [], {
  70. enabledCommandNames,
  71. }),
  72. ).toEqual({
  73. command: "side",
  74. text: "你好\n继续查一下",
  75. displayText: "/SIDE 你好\n继续查一下",
  76. });
  77. expect(parseSelectionSideSlashCommand("/btw 请继续", [], { enabledCommandNames })).toEqual({
  78. command: "btw",
  79. text: "请继续",
  80. displayText: "/btw 请继续",
  81. });
  82. });
  83. it("只消费带非空文字参数的完整输入", () => {
  84. expect(parseSelectionSideSlashCommand("/side", [], { enabledCommandNames })).toBeNull();
  85. expect(
  86. parseSelectionSideSlashCommand("先说 /side 你好", [], { enabledCommandNames }),
  87. ).toBeNull();
  88. expect(
  89. parseSelectionSideSlashCommand("/sidekick 你好", [], { enabledCommandNames }),
  90. ).toBeNull();
  91. });
  92. it("存在附件或结构化上下文时不消费 App 命令", () => {
  93. expect(
  94. parseSelectionSideSlashCommand("/side 你好", [{ ref: "zcode-artifact://a" }], {
  95. enabledCommandNames,
  96. }),
  97. ).toBeNull();
  98. expect(
  99. parseSelectionSideSlashCommand("/btw 你好", [], {
  100. contextAttachmentCount: 1,
  101. enabledCommandNames,
  102. }),
  103. ).toBeNull();
  104. });
  105. it("遵守 CLI 同名命令优先的有效 App 命令集合", () => {
  106. expect(
  107. parseSelectionSideSlashCommand("/side 你好", [], { enabledCommandNames: ["btw"] }),
  108. ).toBeNull();
  109. expect(
  110. parseSelectionSideSlashCommand("/btw 你好", [], { enabledCommandNames: ["btw"] }),
  111. ).toMatchObject({ command: "btw", text: "你好" });
  112. });
  113. });