99import { makeThreadRequestsResolvedAnnouncer } from "@t3tools/client-runtime/state/thread-requests" ;
1010import { useLayoutEffect , useRef , type ComponentProps } from "react" ;
1111import { expect , it , vi } from "vite-plus/test" ;
12+ import { userEvent } from "vite-plus/test/browser" ;
1213
1314vi . mock ( import ( "../ChatMarkdown" ) , async ( importOriginal ) => ( {
1415 ...( await importOriginal ( ) ) ,
@@ -73,20 +74,16 @@ function questionProjection(threadId: ThreadId, requestIds = ["first", "second"]
7374 } satisfies OrchestrationV2ThreadProjection ;
7475}
7576
76- let requests : ReturnType < typeof usePendingThreadRequests > ;
77-
78- function PendingRequestsProbe ( {
79- environmentId,
80- projection,
81- loading = false ,
82- } : {
77+ interface ProbeProps {
8378 environmentId : EnvironmentId ;
8479 projection : OrchestrationV2ThreadProjection ;
8580 loading ?: boolean ;
86- } ) {
81+ }
82+
83+ function useProbeRequests ( { environmentId, projection, loading = false } : ProbeProps ) {
8784 const promptRef = useRef ( "" ) ;
8885 const composerRef = useRef ( null ) ;
89- const state = usePendingThreadRequests ( {
86+ return usePendingThreadRequests ( {
9087 environmentId,
9188 activeThreadId : projection . thread . id ,
9289 serverProjection : loading ? null : projection ,
@@ -97,6 +94,12 @@ function PendingRequestsProbe({
9794 composerRef,
9895 setThreadError : ( ) => { } ,
9996 } ) ;
97+ }
98+
99+ let requests : ReturnType < typeof usePendingThreadRequests > ;
100+
101+ function PendingRequestsProbe ( props : ProbeProps ) {
102+ const state = useProbeRequests ( props ) ;
100103 useLayoutEffect ( ( ) => {
101104 requests = state ;
102105 } ) ;
@@ -138,6 +141,40 @@ it("retains answers, selections and question position after leaving chat, includ
138141 expect ( requests ) . toMatchObject ( expected ) ;
139142} ) ;
140143
144+ function QuestionPositionProbe ( props : ProbeProps ) {
145+ const state = useProbeRequests ( props ) ;
146+ return (
147+ < >
148+ < output > Question { state . activePendingQuestionIndex + 1 } </ output >
149+ < button
150+ type = "button"
151+ onClick = { ( ) => state . onSelectActivePendingUserInputOption ( "question-0" , "Incremental" ) }
152+ >
153+ Choose Incremental
154+ </ button >
155+ < button type = "button" onClick = { ( ) => state . onAdvanceActivePendingUserInput ( ) } >
156+ Submit
157+ </ button >
158+ </ >
159+ ) ;
160+ }
161+
162+ it ( "stays on an unanswered question when the composer submits" , async ( ) => {
163+ await using screen = await render (
164+ < QuestionPositionProbe
165+ environmentId = { EnvironmentId . make ( "unanswered-environment" ) }
166+ projection = { questionProjection ( threadRef . threadId ) }
167+ /> ,
168+ ) ;
169+ const submit = screen . getByRole ( "button" , { name : "Submit" } ) ;
170+ await userEvent . click ( submit ) ;
171+ await expect . element ( screen . getByRole ( "status" ) ) . toHaveTextContent ( "Question 1" ) ;
172+
173+ await userEvent . click ( screen . getByRole ( "button" , { name : "Choose Incremental" } ) ) ;
174+ await userEvent . click ( submit ) ;
175+ await expect . element ( screen . getByRole ( "status" ) ) . toHaveTextContent ( "Question 2" ) ;
176+ } ) ;
177+
141178it ( "isolates drafts by environment, thread and request and discards only resolved requests" , async ( ) => {
142179 using _timers = fakeTimers ( ) ;
143180 const environmentId = EnvironmentId . make ( "scope-environment" ) ;
0 commit comments