Open History to see the last commit
1import { EnvironmentId, RuntimeRequestId, ThreadId } from "@t3tools/contracts";
2import { act } from "react";
3import { createRoot } from "react-dom/client";
4import { expect, it, vi } from "vite-plus/test";
5
6vi.mock(import("../ChatMarkdown"), async (importOriginal) => ({
7 ...(await importOriginal()),
8 default: (({ text }: { text: string }) => <p>{text}</p>) as never,
9}));
10
11import { ComposerPendingUserInputPanel } from "./ComposerPendingUserInputPanel";
12
13const threadRef = { environmentId: EnvironmentId.make("env"), threadId: ThreadId.make("thread") };
14
15it("cancels an answer's pending auto-submit on revocation and resumes after a new grant", async () => {
16 vi.useFakeTimers();
17 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
18 const onToggleOption = vi.fn();
19 const onAdvance = vi.fn();
20 const onDismiss = vi.fn();
21 const prompt = {
22 requestId: RuntimeRequestId.make("scoped-answer"),
23 createdAt: "2026-09-05T00:00:00.000Z",
24 dismissible: false,
25 responseCapability: "live" as const,
26 questions: [
27 {
28 id: "approach",
29 header: "Approach",
30 question: "Which approach?",
31 options: [{ label: "Incremental", description: "One module at a time" }],
32 multiSelect: false,
33 },
34 ],
35 };
36 const panel = (disabled: boolean) => (
37 <ComposerPendingUserInputPanel
38 pendingUserInputs={[prompt]}
39 cwd="/repo"
40 threadRef={threadRef}
41 disabled={disabled}
42 respondingRequestIds={[]}
43 answers={{}}
44 questionIndex={0}
45 onToggleOption={onToggleOption}
46 onAdvance={onAdvance}
47 onDismiss={onDismiss}
48 />
49 );
50 const container = document.createElement("div");
51 document.body.append(container);
52 const root = createRoot(container);
53 // A disabled button swallows the click, as it would for a user.
54 const chooseOption = () =>
55 [...container.querySelectorAll("button")]
56 .find((button) => button.textContent?.includes("Incremental"))!
57 .click();
58 try {
59 await act(async () => root.render(panel(false)));
60 await act(async () => chooseOption());
61 expect(onToggleOption).toHaveBeenCalledWith("approach", "Incremental");
62 expect(onAdvance).not.toHaveBeenCalled();
63
64 await act(async () => root.render(panel(true)));
65 await act(async () => {
66 chooseOption();
67 vi.runAllTimers();
68 });
69 expect(onToggleOption).toHaveBeenCalledTimes(1);
70 expect(onAdvance).not.toHaveBeenCalled();
71
72 await act(async () => root.render(panel(false)));
73 await act(async () => {
74 chooseOption();
75 vi.runAllTimers();
76 });
77 expect(onToggleOption).toHaveBeenCalledTimes(2);
78 expect(onAdvance).toHaveBeenCalledTimes(1);
79 } finally {
80 await act(async () => root.unmount());
81 container.remove();
82 vi.unstubAllGlobals();
83 vi.useRealTimers();
84 }
85});