| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- import { createElement, type ComponentProps } from "react";
- import { renderToStaticMarkup } from "react-dom/server";
- import { afterEach, describe, expect, it, vi } from "vitest";
- import type {
- CommandEnvelope,
- ConversationSnapshot,
- } from "@zcode/shared/zcode-protocol-v4";
- import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
- import type { ElicitationDialog } from "@/ElicitationDialog.js";
- import { V4InteractionDialogs } from "@/v4/V4InteractionDialogs.js";
- type ElicitationDialogProps = ComponentProps<typeof ElicitationDialog>;
- const captured = vi.hoisted(() => ({
- elicitationProps: null as ElicitationDialogProps | null,
- sendCommand: vi.fn(),
- }));
- vi.mock("@/ElicitationDialog.js", async () => {
- const React = await import("react");
- return {
- ElicitationDialog: (props: ElicitationDialogProps) => {
- captured.elicitationProps = props;
- return React.createElement("div", { "data-testid": "mock-elicitation" });
- },
- };
- });
- vi.mock("@/v4/V4ConversationContext.js", () => ({
- useV4Conversation: () => ({
- sendCommand: captured.sendCommand,
- }),
- }));
- vi.mock("@/hooks/useTaskNotifications.js", () => ({
- usePendingInteractionTaskNotifications: () => {},
- }));
- function makePlanSnapshot(): ConversationSnapshot {
- return {
- sessionId: "session-plan",
- pendingInteractions: [
- {
- interactionId: "plan-request-1",
- kind: "userInput",
- anchorRowId: 5,
- createdAt: 300,
- payload: {
- kind: "userInput",
- prompt: "Review this implementation plan.",
- freeText: true,
- toolName: "ExitPlanMode",
- toolCallId: "exit-plan-1",
- traceId: "trace-plan",
- schema: { interaction: "plan_approval", toolName: "ExitPlanMode" },
- questions: [
- {
- question: "Review this implementation plan.",
- header: "Plan",
- options: [{ value: "approve", label: "Approve" }],
- },
- ],
- },
- },
- ],
- } as ConversationSnapshot;
- }
- function makeAskUserSnapshot(): ConversationSnapshot {
- const snapshot = makePlanSnapshot();
- const pending = snapshot.pendingInteractions[0];
- if (!pending || pending.payload.kind !== "userInput") return snapshot;
- return {
- ...snapshot,
- pendingInteractions: [
- {
- ...pending,
- interactionId: "ask-request-1",
- payload: {
- ...pending.payload,
- toolName: "AskUserQuestion",
- toolCallId: "ask-user-1",
- schema: { toolName: "AskUserQuestion" },
- },
- },
- ],
- };
- }
- async function flushMicrotasks(): Promise<void> {
- for (let index = 0; index < 8; index += 1) {
- await Promise.resolve();
- }
- }
- afterEach(() => {
- captured.elicitationProps = null;
- vi.clearAllMocks();
- });
- describe("V4 mobile plan interaction acknowledgement", () => {
- it("reports an accepted ExitPlanMode response to the owning pane", async () => {
- captured.sendCommand.mockImplementation(
- async (envelope: CommandEnvelope) => ({
- commandId: envelope.commandId,
- revisionAtDecision: 1,
- status: "accepted" as const,
- }),
- );
- const onPlanInteractionAccepted = vi.fn();
- renderToStaticMarkup(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(V4InteractionDialogs, {
- sessionId: "session-plan",
- workspacePath: "/workspace",
- snapshot: makePlanSnapshot(),
- onPlanInteractionAccepted,
- }),
- ),
- );
- expect(captured.elicitationProps).not.toBeNull();
- captured.elicitationProps?.onRespond("plan-request-1", "accept", {
- answer: "approve",
- });
- await flushMicrotasks();
- expect(onPlanInteractionAccepted).toHaveBeenCalledTimes(1);
- expect(onPlanInteractionAccepted).toHaveBeenCalledWith("plan-request-1");
- });
- it.each(["duplicate", "noop"] as const)(
- "reports a %s ExitPlanMode response for authoritative reconciliation",
- async (status) => {
- captured.sendCommand.mockImplementation(
- async (envelope: CommandEnvelope) => ({
- commandId: envelope.commandId,
- revisionAtDecision: 1,
- status,
- }),
- );
- const onPlanInteractionAccepted = vi.fn();
- renderToStaticMarkup(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(V4InteractionDialogs, {
- sessionId: "session-plan",
- workspacePath: "/workspace",
- snapshot: makePlanSnapshot(),
- onPlanInteractionAccepted,
- }),
- ),
- );
- captured.elicitationProps?.onRespond("plan-request-1", "accept", {
- answer: "approve",
- });
- await flushMicrotasks();
- expect(onPlanInteractionAccepted).toHaveBeenCalledWith("plan-request-1");
- },
- );
- it("does not report rejected ExitPlanMode responses", async () => {
- captured.sendCommand.mockImplementation(
- async (envelope: CommandEnvelope) => ({
- commandId: envelope.commandId,
- reasonCode: "guard.interactionRejected",
- revisionAtDecision: 1,
- status: "rejected" as const,
- }),
- );
- const onPlanInteractionAccepted = vi.fn();
- renderToStaticMarkup(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(V4InteractionDialogs, {
- sessionId: "session-plan",
- workspacePath: "/workspace",
- snapshot: makePlanSnapshot(),
- onPlanInteractionAccepted,
- }),
- ),
- );
- captured.elicitationProps?.onRespond("plan-request-1", "accept", {
- answer: "approve",
- });
- await flushMicrotasks();
- expect(onPlanInteractionAccepted).not.toHaveBeenCalled();
- });
- it("does not report accepted AskUserQuestion responses as plan interactions", async () => {
- captured.sendCommand.mockImplementation(
- async (envelope: CommandEnvelope) => ({
- commandId: envelope.commandId,
- revisionAtDecision: 1,
- status: "accepted" as const,
- }),
- );
- const onPlanInteractionAccepted = vi.fn();
- renderToStaticMarkup(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(V4InteractionDialogs, {
- sessionId: "session-plan",
- workspacePath: "/workspace",
- snapshot: makeAskUserSnapshot(),
- onPlanInteractionAccepted,
- }),
- ),
- );
- captured.elicitationProps?.onRespond("ask-request-1", "accept", {
- answer: "safe",
- });
- await flushMicrotasks();
- expect(onPlanInteractionAccepted).not.toHaveBeenCalled();
- });
- });
|