88} from "@t3tools/contracts" ;
99import { useLayoutEffect , useRef , type ComponentProps } from "react" ;
1010import { expect , it , vi } from "vite-plus/test" ;
11+ import { userEvent } from "vite-plus/test/browser" ;
1112
1213vi . mock ( import ( "../ChatMarkdown" ) , async ( importOriginal ) => ( {
1314 ...( await importOriginal ( ) ) ,
@@ -72,20 +73,16 @@ function questionProjection(threadId: ThreadId, requestIds = ["first", "second"]
7273 } satisfies OrchestrationV2ThreadProjection ;
7374}
7475
75- let requests : ReturnType < typeof usePendingThreadRequests > ;
76-
77- function PendingRequestsProbe ( {
78- environmentId,
79- projection,
80- loading = false ,
81- } : {
76+ interface ProbeProps {
8277 environmentId : EnvironmentId ;
8378 projection : OrchestrationV2ThreadProjection ;
8479 loading ?: boolean ;
85- } ) {
80+ }
81+
82+ function useProbeRequests ( { environmentId, projection, loading = false } : ProbeProps ) {
8683 const promptRef = useRef ( "" ) ;
8784 const composerRef = useRef ( null ) ;
88- const state = usePendingThreadRequests ( {
85+ return usePendingThreadRequests ( {
8986 environmentId,
9087 activeThreadId : projection . thread . id ,
9188 activeThread : projection . thread ,
@@ -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,22 +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+ { /* Enter in the composer reaches the same advance as the disabled Next button. */ }
156+ < button type = "button" onClick = { ( ) => state . onAdvanceActivePendingUserInput ( ) } >
157+ Submit
158+ </ button >
159+ </ >
160+ ) ;
161+ }
162+
141163it ( "stays on an unanswered question when the composer submits" , async ( ) => {
142164 using _timers = fakeTimers ( ) ;
143- const environmentId = EnvironmentId . make ( "unanswered-environment" ) ;
144- await using _screen = await render (
145- < PendingRequestsProbe
146- environmentId = { environmentId }
165+ await using screen = await render (
166+ < QuestionPositionProbe
167+ environmentId = { EnvironmentId . make ( "unanswered-environment" ) }
147168 projection = { questionProjection ( threadRef . threadId ) }
148169 /> ,
149170 ) ;
150- // Enter in the composer reaches the same advance as the disabled Next button.
151- await act ( ( ) => requests . onAdvanceActivePendingUserInput ( ) ) ;
152- expect ( requests . activePendingQuestionIndex ) . toBe ( 0 ) ;
171+ const submit = screen . getByRole ( "button" , { name : "Submit" } ) ;
172+ await userEvent . click ( submit ) ;
173+ await expect . element ( screen . getByRole ( "status" ) ) . toHaveTextContent ( "Question 1" ) ;
153174
154- await act ( ( ) => requests . onSelectActivePendingUserInputOption ( "question-0 ", " Incremental") ) ;
155- await act ( ( ) => requests . onAdvanceActivePendingUserInput ( ) ) ;
156- expect ( requests . activePendingQuestionIndex ) . toBe ( 1 ) ;
175+ await userEvent . click ( screen . getByRole ( "button ", { name : "Choose Incremental" } ) ) ;
176+ await userEvent . click ( submit ) ;
177+ await expect . element ( screen . getByRole ( "status" ) ) . toHaveTextContent ( "Question 2" ) ;
157178} ) ;
158179
159180it ( "isolates drafts by environment, thread and request and discards only resolved requests" , async ( ) => {
0 commit comments