v4SlashCommands.test.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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("parses /plan as a mode shortcut and keeps only the task argument", () => {
  45. expect(parseV4VisibleSlashCommand("/plan", [])).toEqual({
  46. kind: "planShortcut",
  47. task: "",
  48. displayText: "/plan",
  49. });
  50. expect(parseV4VisibleSlashCommand(" /plan ", [])).toEqual({
  51. kind: "planShortcut",
  52. task: "",
  53. displayText: "/plan",
  54. });
  55. expect(parseV4VisibleSlashCommand(" /PLAN 帮我做 X ", [])).toEqual({
  56. kind: "planShortcut",
  57. task: "帮我做 X",
  58. displayText: "/PLAN 帮我做 X",
  59. });
  60. expect(parseV4VisibleSlashCommand("/planner 做普通任务", [])).toBeNull();
  61. });
  62. it("rejects /plan when attachments or context are present", () => {
  63. expect(
  64. parseV4VisibleSlashCommand("/plan 检查附件", [
  65. { ref: "zcode-artifact://a", fileName: "a.txt", mime: "text/plain", bytes: 1 },
  66. ]),
  67. ).toMatchObject({
  68. kind: "unsupportedPlanShortcut",
  69. task: "检查附件",
  70. });
  71. expect(
  72. parseV4VisibleSlashCommand("/plan 检查网页", [], { contextAttachmentCount: 1 }),
  73. ).toMatchObject({
  74. kind: "unsupportedPlanShortcut",
  75. task: "检查网页",
  76. });
  77. });
  78. it("does not consume commands with web element contexts", () => {
  79. expect(
  80. parseV4VisibleSlashCommand("/goal 修复登录", [], {
  81. contextAttachmentCount: 1,
  82. }),
  83. ).toBeNull();
  84. });
  85. it("keeps queued goal command syntax when edited to objective-only text", () => {
  86. expect(v4QueuedCommandText("sendGoalCommand", "新目标")).toBe("/goal 新目标");
  87. expect(v4QueuedCommandText("sendGoalCommand", "/target 新目标")).toBe("/target 新目标");
  88. expect(v4QueuedCommandText("sendText", "新目标")).toBe("新目标");
  89. });
  90. });
  91. describe("parseSelectionSideSlashCommand", () => {
  92. const enabledCommandNames = ["side", "btw"] as const;
  93. it("parses both aliases and preserves internal whitespace", () => {
  94. expect(
  95. parseSelectionSideSlashCommand(" /SIDE 你好\n继续查一下 ", [], {
  96. enabledCommandNames,
  97. }),
  98. ).toEqual({
  99. command: "side",
  100. text: "你好\n继续查一下",
  101. displayText: "/SIDE 你好\n继续查一下",
  102. });
  103. expect(parseSelectionSideSlashCommand("/btw 请继续", [], { enabledCommandNames })).toEqual({
  104. command: "btw",
  105. text: "请继续",
  106. displayText: "/btw 请继续",
  107. });
  108. });
  109. it("只消费带非空文字参数的完整输入", () => {
  110. expect(parseSelectionSideSlashCommand("/side", [], { enabledCommandNames })).toBeNull();
  111. expect(
  112. parseSelectionSideSlashCommand("先说 /side 你好", [], { enabledCommandNames }),
  113. ).toBeNull();
  114. expect(
  115. parseSelectionSideSlashCommand("/sidekick 你好", [], { enabledCommandNames }),
  116. ).toBeNull();
  117. });
  118. it("存在附件或结构化上下文时不消费 App 命令", () => {
  119. expect(
  120. parseSelectionSideSlashCommand("/side 你好", [{ ref: "zcode-artifact://a" }], {
  121. enabledCommandNames,
  122. }),
  123. ).toBeNull();
  124. expect(
  125. parseSelectionSideSlashCommand("/btw 你好", [], {
  126. contextAttachmentCount: 1,
  127. enabledCommandNames,
  128. }),
  129. ).toBeNull();
  130. });
  131. it("遵守 CLI 同名命令优先的有效 App 命令集合", () => {
  132. expect(
  133. parseSelectionSideSlashCommand("/side 你好", [], { enabledCommandNames: ["btw"] }),
  134. ).toBeNull();
  135. expect(
  136. parseSelectionSideSlashCommand("/btw 你好", [], { enabledCommandNames: ["btw"] }),
  137. ).toMatchObject({ command: "btw", text: "你好" });
  138. });
  139. });