v4InteractionDialogs.test.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. import { createElement } from "react";
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { describe, expect, it, vi } from "vitest";
  4. import { TID_V4_USER_INPUT_DIALOG } from "@zcode/shared";
  5. import type { ConversationSnapshot } from "@zcode/shared/zcode-protocol-v4";
  6. import { ZCodeIntlProvider } from "../src/i18n/IntlProvider.js";
  7. import {
  8. buildV4ElicitationProgressKey,
  9. createInteractionAutoResolutionIntentTracker,
  10. getCurrentSessionInteractionSnapshot,
  11. resolveV4ElicitationRequest,
  12. V4InteractionDialogs,
  13. } from "../src/v4/V4InteractionDialogs.js";
  14. vi.mock("@/v4/V4ConversationContext.js", () => ({
  15. useV4Conversation: () => ({
  16. sendCommand: vi.fn(),
  17. }),
  18. }));
  19. describe("V4InteractionDialogs", () => {
  20. it("ignores a stale snapshot while switching back to another task", () => {
  21. const staleSnapshot = {
  22. sessionId: "task-b",
  23. pendingInteractions: [],
  24. } as ConversationSnapshot;
  25. expect(getCurrentSessionInteractionSnapshot("task-a", staleSnapshot)).toBeNull();
  26. expect(getCurrentSessionInteractionSnapshot("task-b", staleSnapshot)).toBe(staleSnapshot);
  27. });
  28. it("uses Bot question progress for the same v4 elicitation request", () => {
  29. const projected = {
  30. type: "elicitation_request" as const,
  31. taskId: "sess-1",
  32. traceId: "trace-1",
  33. requestId: "ask-1",
  34. message: "第一题",
  35. options: [],
  36. currentQuestionIndex: 0,
  37. questions: [],
  38. };
  39. const botProgress = {
  40. ...projected,
  41. message: "第二题",
  42. currentQuestionIndex: 1,
  43. answerDrafts: { answer_0: ["first-answer"] },
  44. };
  45. expect(resolveV4ElicitationRequest(projected, botProgress)).toBe(botProgress);
  46. expect(buildV4ElicitationProgressKey(botProgress)).toBe(
  47. 'ask-1:1:{"answer_0":["first-answer"]}',
  48. );
  49. expect(
  50. resolveV4ElicitationRequest(projected, {
  51. ...botProgress,
  52. requestId: "ask-other",
  53. }),
  54. ).toBe(projected);
  55. });
  56. it("remembers early user input and consumes the snooze intent exactly once", () => {
  57. const tracker = createInteractionAutoResolutionIntentTracker();
  58. tracker.markInteracted("ask-1");
  59. expect(tracker.consumeSnooze("ask-1", false)).toBe(false);
  60. expect(tracker.consumeSnooze("ask-1", true)).toBe(true);
  61. expect(tracker.consumeSnooze("ask-1", true)).toBe(false);
  62. tracker.releaseSnooze("ask-1");
  63. expect(tracker.consumeSnooze("ask-1", true)).toBe(true);
  64. expect(tracker.consumeSnooze("other", true)).toBe(false);
  65. });
  66. it("does not downgrade workspaceHookReview to a generic interaction dialog", () => {
  67. const snapshot = {
  68. sessionId: "sess-1",
  69. pendingInteractions: [
  70. {
  71. interactionId: "workspace-review-1",
  72. kind: "workspaceHookReview",
  73. anchorRowId: null,
  74. createdAt: 300,
  75. payload: {
  76. kind: "workspaceHookReview",
  77. reviewFlowId: "flow-1",
  78. generation: 1,
  79. interactionId: "workspace-review-1",
  80. sessionId: "sess-1",
  81. taskId: "sess-1",
  82. runId: "run-1",
  83. workspaceIdentity: "/workspace",
  84. workspaceLabel: "workspace",
  85. bundleDigest: "a".repeat(64),
  86. createdAt: 300,
  87. deadlineAt: 600_300,
  88. sourceFiles: [],
  89. summary: { eventCount: 0, hookCount: 0, pendingCount: 0 },
  90. items: [],
  91. warningCode: "workspace_hooks_execute_code",
  92. },
  93. },
  94. ],
  95. } as ConversationSnapshot;
  96. const html = renderToStaticMarkup(
  97. createElement(
  98. ZCodeIntlProvider,
  99. { initialLocale: "zh-CN" },
  100. createElement(V4InteractionDialogs, {
  101. sessionId: "sess-1",
  102. workspacePath: "/workspace",
  103. snapshot,
  104. }),
  105. ),
  106. );
  107. expect(html).toBe("");
  108. });
  109. it("renders the first generic interaction after a workspaceHookReview", () => {
  110. const snapshot = {
  111. sessionId: "sess-1",
  112. pendingInteractions: [
  113. {
  114. interactionId: "workspace-review-1",
  115. kind: "workspaceHookReview",
  116. anchorRowId: null,
  117. createdAt: 300,
  118. payload: {
  119. kind: "workspaceHookReview",
  120. reviewFlowId: "flow-1",
  121. generation: 1,
  122. interactionId: "workspace-review-1",
  123. sessionId: "sess-1",
  124. taskId: "sess-1",
  125. runId: "run-1",
  126. workspaceIdentity: "/workspace",
  127. workspaceLabel: "workspace",
  128. bundleDigest: "a".repeat(64),
  129. createdAt: 300,
  130. deadlineAt: 600_300,
  131. sourceFiles: [],
  132. summary: { eventCount: 0, hookCount: 0, pendingCount: 0 },
  133. items: [],
  134. warningCode: "workspace_hooks_execute_code",
  135. },
  136. },
  137. {
  138. interactionId: "plan-1",
  139. kind: "userInput",
  140. anchorRowId: 5,
  141. createdAt: 301,
  142. payload: {
  143. kind: "userInput",
  144. prompt: "请确认计划",
  145. freeText: true,
  146. toolName: "ExitPlanMode",
  147. toolCallId: "tool-plan",
  148. traceId: "trace-plan",
  149. schema: { toolName: "ExitPlanMode" },
  150. questions: [
  151. {
  152. question: "是否执行该计划?",
  153. header: "计划",
  154. options: [
  155. { value: "approve", label: "执行", description: "执行当前计划" },
  156. { value: "feedback", label: "反馈", description: "返回修改意见" },
  157. ],
  158. },
  159. ],
  160. },
  161. },
  162. ],
  163. } as ConversationSnapshot;
  164. const html = renderToStaticMarkup(
  165. createElement(
  166. ZCodeIntlProvider,
  167. { initialLocale: "zh-CN" },
  168. createElement(V4InteractionDialogs, {
  169. sessionId: "sess-1",
  170. workspacePath: "/workspace",
  171. snapshot,
  172. }),
  173. ),
  174. );
  175. expect(html).toContain("是否执行该计划?");
  176. expect(html).toContain("执行当前计划");
  177. });
  178. it("renders structured userInput interactions with ElicitationDialog", () => {
  179. const snapshot = {
  180. sessionId: "sess-1",
  181. pendingInteractions: [
  182. {
  183. interactionId: "ask-1",
  184. kind: "userInput",
  185. anchorRowId: 5,
  186. createdAt: 300,
  187. payload: {
  188. kind: "userInput",
  189. prompt: "请选择",
  190. freeText: true,
  191. toolName: "AskUserQuestion",
  192. toolCallId: "tool-ask",
  193. traceId: "trace-ask",
  194. schema: { toolName: "AskUserQuestion" },
  195. questions: [
  196. {
  197. question: "你想怎么做?",
  198. header: "方案",
  199. options: [
  200. { value: "fast", label: "快速", description: "先做最小修复" },
  201. { value: "safe", label: "稳妥", description: "补齐测试和文档" },
  202. ],
  203. },
  204. ],
  205. },
  206. },
  207. ],
  208. } as ConversationSnapshot;
  209. const html = renderToStaticMarkup(
  210. createElement(
  211. ZCodeIntlProvider,
  212. { initialLocale: "zh-CN" },
  213. createElement(V4InteractionDialogs, {
  214. sessionId: "sess-1",
  215. workspacePath: "/workspace",
  216. snapshot,
  217. }),
  218. ),
  219. );
  220. expect(html).toContain("你想怎么做?");
  221. expect(html).toContain("快速");
  222. expect(html).toContain("先做最小修复");
  223. expect(html).not.toContain(`data-testid="${TID_V4_USER_INPUT_DIALOG}"`);
  224. });
  225. });