v4PlanInteractionAcknowledgement.test.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. import { createElement, type ComponentProps } from "react";
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { afterEach, describe, expect, it, vi } from "vitest";
  4. import type {
  5. CommandEnvelope,
  6. ConversationSnapshot,
  7. } from "@zcode/shared/zcode-protocol-v4";
  8. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  9. import type { ElicitationDialog } from "@/ElicitationDialog.js";
  10. import { V4InteractionDialogs } from "@/v4/V4InteractionDialogs.js";
  11. type ElicitationDialogProps = ComponentProps<typeof ElicitationDialog>;
  12. const captured = vi.hoisted(() => ({
  13. elicitationProps: null as ElicitationDialogProps | null,
  14. sendCommand: vi.fn(),
  15. }));
  16. vi.mock("@/ElicitationDialog.js", async () => {
  17. const React = await import("react");
  18. return {
  19. ElicitationDialog: (props: ElicitationDialogProps) => {
  20. captured.elicitationProps = props;
  21. return React.createElement("div", { "data-testid": "mock-elicitation" });
  22. },
  23. };
  24. });
  25. vi.mock("@/v4/V4ConversationContext.js", () => ({
  26. useV4Conversation: () => ({
  27. sendCommand: captured.sendCommand,
  28. }),
  29. }));
  30. vi.mock("@/hooks/useTaskNotifications.js", () => ({
  31. usePendingInteractionTaskNotifications: () => {},
  32. }));
  33. function makePlanSnapshot(): ConversationSnapshot {
  34. return {
  35. sessionId: "session-plan",
  36. pendingInteractions: [
  37. {
  38. interactionId: "plan-request-1",
  39. kind: "userInput",
  40. anchorRowId: 5,
  41. createdAt: 300,
  42. payload: {
  43. kind: "userInput",
  44. prompt: "Review this implementation plan.",
  45. freeText: true,
  46. toolName: "ExitPlanMode",
  47. toolCallId: "exit-plan-1",
  48. traceId: "trace-plan",
  49. schema: { interaction: "plan_approval", toolName: "ExitPlanMode" },
  50. questions: [
  51. {
  52. question: "Review this implementation plan.",
  53. header: "Plan",
  54. options: [{ value: "approve", label: "Approve" }],
  55. },
  56. ],
  57. },
  58. },
  59. ],
  60. } as ConversationSnapshot;
  61. }
  62. function makeAskUserSnapshot(): ConversationSnapshot {
  63. const snapshot = makePlanSnapshot();
  64. const pending = snapshot.pendingInteractions[0];
  65. if (!pending || pending.payload.kind !== "userInput") return snapshot;
  66. return {
  67. ...snapshot,
  68. pendingInteractions: [
  69. {
  70. ...pending,
  71. interactionId: "ask-request-1",
  72. payload: {
  73. ...pending.payload,
  74. toolName: "AskUserQuestion",
  75. toolCallId: "ask-user-1",
  76. schema: { toolName: "AskUserQuestion" },
  77. },
  78. },
  79. ],
  80. };
  81. }
  82. async function flushMicrotasks(): Promise<void> {
  83. for (let index = 0; index < 8; index += 1) {
  84. await Promise.resolve();
  85. }
  86. }
  87. afterEach(() => {
  88. captured.elicitationProps = null;
  89. vi.clearAllMocks();
  90. });
  91. describe("V4 mobile plan interaction acknowledgement", () => {
  92. it("reports an accepted ExitPlanMode response to the owning pane", async () => {
  93. captured.sendCommand.mockImplementation(
  94. async (envelope: CommandEnvelope) => ({
  95. commandId: envelope.commandId,
  96. revisionAtDecision: 1,
  97. status: "accepted" as const,
  98. }),
  99. );
  100. const onPlanInteractionAccepted = vi.fn();
  101. renderToStaticMarkup(
  102. createElement(
  103. ZCodeIntlProvider,
  104. { initialLocale: "zh-CN" },
  105. createElement(V4InteractionDialogs, {
  106. sessionId: "session-plan",
  107. workspacePath: "/workspace",
  108. snapshot: makePlanSnapshot(),
  109. onPlanInteractionAccepted,
  110. }),
  111. ),
  112. );
  113. expect(captured.elicitationProps).not.toBeNull();
  114. captured.elicitationProps?.onRespond("plan-request-1", "accept", {
  115. answer: "approve",
  116. });
  117. await flushMicrotasks();
  118. expect(onPlanInteractionAccepted).toHaveBeenCalledTimes(1);
  119. expect(onPlanInteractionAccepted).toHaveBeenCalledWith("plan-request-1");
  120. });
  121. it.each(["duplicate", "noop"] as const)(
  122. "reports a %s ExitPlanMode response for authoritative reconciliation",
  123. async (status) => {
  124. captured.sendCommand.mockImplementation(
  125. async (envelope: CommandEnvelope) => ({
  126. commandId: envelope.commandId,
  127. revisionAtDecision: 1,
  128. status,
  129. }),
  130. );
  131. const onPlanInteractionAccepted = vi.fn();
  132. renderToStaticMarkup(
  133. createElement(
  134. ZCodeIntlProvider,
  135. { initialLocale: "zh-CN" },
  136. createElement(V4InteractionDialogs, {
  137. sessionId: "session-plan",
  138. workspacePath: "/workspace",
  139. snapshot: makePlanSnapshot(),
  140. onPlanInteractionAccepted,
  141. }),
  142. ),
  143. );
  144. captured.elicitationProps?.onRespond("plan-request-1", "accept", {
  145. answer: "approve",
  146. });
  147. await flushMicrotasks();
  148. expect(onPlanInteractionAccepted).toHaveBeenCalledWith("plan-request-1");
  149. },
  150. );
  151. it("does not report rejected ExitPlanMode responses", async () => {
  152. captured.sendCommand.mockImplementation(
  153. async (envelope: CommandEnvelope) => ({
  154. commandId: envelope.commandId,
  155. reasonCode: "guard.interactionRejected",
  156. revisionAtDecision: 1,
  157. status: "rejected" as const,
  158. }),
  159. );
  160. const onPlanInteractionAccepted = vi.fn();
  161. renderToStaticMarkup(
  162. createElement(
  163. ZCodeIntlProvider,
  164. { initialLocale: "zh-CN" },
  165. createElement(V4InteractionDialogs, {
  166. sessionId: "session-plan",
  167. workspacePath: "/workspace",
  168. snapshot: makePlanSnapshot(),
  169. onPlanInteractionAccepted,
  170. }),
  171. ),
  172. );
  173. captured.elicitationProps?.onRespond("plan-request-1", "accept", {
  174. answer: "approve",
  175. });
  176. await flushMicrotasks();
  177. expect(onPlanInteractionAccepted).not.toHaveBeenCalled();
  178. });
  179. it("does not report accepted AskUserQuestion responses as plan interactions", async () => {
  180. captured.sendCommand.mockImplementation(
  181. async (envelope: CommandEnvelope) => ({
  182. commandId: envelope.commandId,
  183. revisionAtDecision: 1,
  184. status: "accepted" as const,
  185. }),
  186. );
  187. const onPlanInteractionAccepted = vi.fn();
  188. renderToStaticMarkup(
  189. createElement(
  190. ZCodeIntlProvider,
  191. { initialLocale: "zh-CN" },
  192. createElement(V4InteractionDialogs, {
  193. sessionId: "session-plan",
  194. workspacePath: "/workspace",
  195. snapshot: makeAskUserSnapshot(),
  196. onPlanInteractionAccepted,
  197. }),
  198. ),
  199. );
  200. captured.elicitationProps?.onRespond("ask-request-1", "accept", {
  201. answer: "safe",
  202. });
  203. await flushMicrotasks();
  204. expect(onPlanInteractionAccepted).not.toHaveBeenCalled();
  205. });
  206. });