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

ComposerPendingUserInputPanel.navigation.browser.test.tsx

183 lines6.3 KB

1import {
2 EnvironmentId,
3 NodeId,
4 RuntimeRequestId,
5 ThreadId,
6 TurnItemId,
7 type OrchestrationV2ThreadProjection,
8} from "@t3tools/contracts";
9import { useLayoutEffect, useRef, type ComponentProps } from "react";
10import { expect, it, vi } from "vite-plus/test";
11​
12vi.mock(import("../ChatMarkdown"), async (importOriginal) => ({
13 ...(await importOriginal()),
14 default: (({ text }: { text: string }) => <p>{text}</p>) as never,
15}));
16​
17import { act, fakeTimers, render } from "../../../test/render";
18import { makeThreadProjectionFixture } from "../../test-fixtures";
19import { usePendingThreadRequests } from "./usePendingThreadRequests";
20​
21const threadRef = { environmentId: EnvironmentId.make("env"), threadId: ThreadId.make("thread") };
22​
23const prompt = {
24 requestId: RuntimeRequestId.make("question-navigation"),
25 createdAt: "2026-10-09T00:00:00.000Z",
26 dismissible: false,
27 responseCapability: "live" as const,
28 questions: [0, 1, 2].map((index) => ({
29 id: `question-${index}`,
30 header: `Question ${index + 1}`,
31 question: `Choose for question ${index + 1}`,
32 options: [{ label: "Incremental", description: "One module at a time" }],
33 multiSelect: false,
34 })),
35};
36​
37function questionProjection(threadId: ThreadId, requestIds = ["first", "second"]) {
38 const projection = makeThreadProjectionFixture();
39 return {
40 ...projection,
41 thread: { ...projection.thread, id: threadId },
42 runtimeRequests: requestIds.map((id) => ({
43 id: RuntimeRequestId.make(id),
44 nodeId: NodeId.make(id),
45 providerTurnId: null,
46 nativeRequestRef: null,
47 kind: "user_input" as const,
48 status: "pending" as const,
49 responseCapability: { type: "message" as const },
50 createdAt: projection.updatedAt,
51 resolvedAt: null,
52 })),
53 turnItems: requestIds.map((id, ordinal) => ({
54 id: TurnItemId.make(id),
55 threadId,
56 runId: null,
57 nodeId: NodeId.make(id),
58 providerThreadId: null,
59 providerTurnId: null,
60 nativeItemRef: null,
61 parentItemId: null,
62 ordinal,
63 status: "completed" as const,
64 title: null,
65 startedAt: projection.updatedAt,
66 completedAt: projection.updatedAt,
67 updatedAt: projection.updatedAt,
68 type: "user_input_request" as const,
69 requestId: RuntimeRequestId.make(id),
70 questions: prompt.questions,
71 })),
72 } satisfies OrchestrationV2ThreadProjection;
74​
75let requests: ReturnType<typeof usePendingThreadRequests>;
76​
77function PendingRequestsProbe({
78 environmentId,
79 projection,
80 loading = false,
81}: {
82 environmentId: EnvironmentId;
83 projection: OrchestrationV2ThreadProjection;
84 loading?: boolean;
85}) {
86 const promptRef = useRef("");
87 const composerRef = useRef(null);
88 const state = usePendingThreadRequests({
89 environmentId,
90 activeThreadId: projection.thread.id,
91 activeThread: projection.thread,
92 serverProjection: loading ? null : projection,
93 supportsQuestionAttachments: false,
94 composerDraftTarget: { environmentId, threadId: projection.thread.id },
95 setComposerDraftPrompt: () => {},
96 promptRef,
97 composerRef,
98 setThreadError: () => {},
99 });
100 useLayoutEffect(() => {
101 requests = state;
102 });
103 return null;
105​
106it("retains answers, selections and question position after leaving chat, including while the projection reloads", async () => {
107 using _timers = fakeTimers();
108 const environmentId = EnvironmentId.make("remount-environment");
109 const projection = questionProjection(threadRef.threadId);
110 const probe = (loading = false) => (
111 <PendingRequestsProbe environmentId={environmentId} projection={projection} loading={loading} />
112 );
113 await using screen = await render(probe());
114 await act(() =>
115 requests.onChangeActivePendingUserInputCustomAnswer("question-0", "Keep my text"),
116 );
117 await act(() => requests.onAdvanceActivePendingUserInput());
118 await act(() => requests.onSelectActivePendingUserInputOption("question-1", "Incremental"));
119 await act(() => requests.onAdvanceActivePendingUserInput());
120 const expected = {
121 activePendingQuestionIndex: 2,
122 activePendingDraftAnswers: {
123 "question-0": { customAnswer: "Keep my text" },
124 "question-1": { selectedOptionValues: ["Incremental"] },
125 },
126 };
127 expect(requests).toMatchObject(expected);
128​
129 // New thread and Settings both unmount the chat that owns this hook.
130 await screen.rerender(null);
131 await screen.rerender(probe());
132 expect(requests).toMatchObject(expected);
133​
134 await screen.rerender(null);
135 await screen.rerender(probe(true));
136 expect(requests.activePendingUserInput).toBeNull();
137 await screen.rerender(probe());
138 expect(requests).toMatchObject(expected);
139});
140​
141it("isolates drafts by environment, thread and request and discards only resolved requests", async () => {
142 using _timers = fakeTimers();
143 const environmentId = EnvironmentId.make("scope-environment");
144 const projection = questionProjection(threadRef.threadId);
145 const scopes = [
146 { environmentId, projection },
147 { environmentId: EnvironmentId.make("other-environment"), projection },
148 { environmentId, projection: questionProjection(ThreadId.make("other-thread")) },
149 { environmentId, projection: questionProjection(threadRef.threadId, ["second", "first"]) },
150 ];
151 await using screen = await render(null);
152 const show = (scope: ComponentProps<typeof PendingRequestsProbe>) =>
153 screen.rerender(<PendingRequestsProbe {...scope} />);
154 for (const [index, scope] of scopes.entries()) {
155 await show(scope);
156 expect(requests.activePendingQuestionIndex).toBe(0);
157 expect(requests.activePendingDraftAnswers["question-0"]?.customAnswer).toBeUndefined();
158 await act(() =>
159 requests.onChangeActivePendingUserInputCustomAnswer("question-0", `Answer ${index}`),
160 );
161 await act(() => requests.onAdvanceActivePendingUserInput());
162 }
163​
164 await show({
165 environmentId,
166 projection: {
167 ...projection,
168 runtimeRequests: projection.runtimeRequests.map((request) =>
169 request.id === "first"
170 ? { ...request, status: "resolved", resolvedAt: projection.updatedAt }
171 : request,
172 ),
173 },
174 });
175 expect(requests.activePendingUserInput?.requestId).toBe("second");
176 for (const [index, scope] of scopes.entries()) {
177 await show(scope);
178 expect(requests.activePendingQuestionIndex).toBe(index === 0 ? 0 : 1);
179 expect(requests.activePendingDraftAnswers["question-0"]?.customAnswer).toBe(
180 index === 0 ? undefined : `Answer ${index}`,
181 );
182 }
183});