Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ComposerPendingUserInputPanel.navigation.browser.test.tsx

133 lines4.3 KB

1import { EnvironmentId, RuntimeRequestId, ThreadId } from "@t3tools/contracts";
2import { act, useState } from "react";
3import { createRoot, type Root } from "react-dom/client";
4import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";
5​
6import {
7 togglePendingUserInputOptionSelection,
8 type PendingUserInputDraftAnswer,
9} from "../../pendingUserInput";
10​
11vi.mock(import("../ChatMarkdown"), async (importOriginal) => ({
12 ...(await importOriginal()),
13 default: (({ text }: { text: string }) => <p>{text}</p>) as never,
14}));
15​
16import { ComposerPendingUserInputPanel } from "./ComposerPendingUserInputPanel";
17​
18const threadRef = { environmentId: EnvironmentId.make("env"), threadId: ThreadId.make("thread") };
19​
20const prompt = {
21 requestId: RuntimeRequestId.make("question-navigation"),
22 createdAt: "2026-10-09T00:00:00.000Z",
23 dismissible: false,
24 responseCapability: "live" as const,
25 questions: [0, 1, 2].map((index) => ({
26 id: `question-${index}`,
27 header: `Question ${index + 1}`,
28 question: `Choose for question ${index + 1}`,
29 options: [{ label: "Incremental", description: "One module at a time" }],
30 multiSelect: false,
31 })),
32};
33​
34function Questions({ initialIndex = 0 }: { initialIndex?: number }) {
35 const [questionIndex, setQuestionIndex] = useState(initialIndex);
36 const [answers, setAnswers] = useState<Record<string, PendingUserInputDraftAnswer>>({});
37 const advance = () => setQuestionIndex(questionIndex + 1);
38 return (
39 <>
40 <output>{questionIndex}</output>
41 <button onClick={advance}>Next</button>
42 <button onClick={() => setQuestionIndex(questionIndex - 1)}>Previous</button>
43 <ComposerPendingUserInputPanel
44 pendingUserInputs={[prompt]}
45 cwd="/repo"
46 threadRef={threadRef}
47 disabled={false}
48 respondingRequestIds={[]}
49 answers={answers}
50 questionIndex={questionIndex}
51 onAdvance={advance}
52 onToggleOption={(questionId, value) => {
53 const question = prompt.questions.find((entry) => entry.id === questionId)!;
54 setAnswers((existing) => ({
55 ...existing,
56 [questionId]: togglePendingUserInputOptionSelection(
57 question,
58 existing[questionId],
59 value,
60 ),
61 }));
62 }}
63 onDismiss={() => {}}
64 />
65 </>
66 );
68​
69let root: Root;
70let container: HTMLDivElement;
71beforeEach(() => {
72 vi.useFakeTimers();
73 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
74 container = document.createElement("div");
75 document.body.append(container);
76 root = createRoot(container);
77});
78afterEach(async () => {
79 await act(async () => root.unmount());
80 container.remove();
81 vi.unstubAllGlobals();
82 vi.useRealTimers();
83});
84​
85function buttonWithText(text: string) {
86 const button = [...document.querySelectorAll("button")].find((element) =>
87 element.textContent?.includes(text),
88 );
89 expect(button, `button ${text}`).toBeDefined();
90 return button!;
92​
93async function chooseOption() {
94 await act(async () => buttonWithText("Incremental").click());
96​
97async function navigate(label: string) {
98 await act(async () => buttonWithText(label).click());
100​
101function activeIndex() {
102 return container.querySelector("output")!.textContent;
104​
105it("does not skip the next unanswered question after manual advance", async () => {
106 await act(async () => root.render(<Questions />));
107 await chooseOption();
108 await navigate("Next");
109 expect(activeIndex()).toBe("1");
110 await act(async () => vi.advanceTimersByTime(200));
111 expect(activeIndex()).toBe("1");
112​
113 await chooseOption();
114 await act(async () => vi.advanceTimersByTime(200));
115 expect(activeIndex()).toBe("2");
116});
117​
118it("does not undo previous-question navigation with a stale timer", async () => {
119 await act(async () => root.render(<Questions initialIndex={1} />));
120 await chooseOption();
121 await navigate("Previous");
122 await act(async () => vi.advanceTimersByTime(200));
123 expect(activeIndex()).toBe("0");
124});
125​
126it("still auto-advances once after the answer draft rerenders the same question", async () => {
127 await act(async () => root.render(<Questions />));
128 await chooseOption();
129 await act(async () => vi.advanceTimersByTime(200));
130 expect(activeIndex()).toBe("1");
131 await act(async () => vi.advanceTimersByTime(200));
132 expect(activeIndex()).toBe("1");
133});