@@ -11,11 +11,9 @@ import { createRoot, type Root } from "react-dom/client";
1111import { afterEach , beforeEach , expect , it , vi } from "vite-plus/test" ;
1212
1313import {
14- setPendingUserInputCustomAnswer ,
1514 togglePendingUserInputOptionSelection ,
1615 type PendingUserInputDraftAnswer ,
1716} from "../../pendingUserInput" ;
18- import type { PendingUserInput } from "../../session-logic" ;
1917
2018vi . mock ( import ( "../ChatMarkdown" ) , async ( importOriginal ) => ( {
2119 ...( await importOriginal ( ) ) ,
@@ -42,48 +40,17 @@ const prompt = {
4240 } ) ) ,
4341} ;
4442
45- const previewPrompt = {
46- ...prompt ,
47- questions : prompt . questions . map ( ( question , index ) => ( {
48- ...question ,
49- options : [ "First" , "Second" ] . map ( ( label ) => ( {
50- label,
51- value : index === 1 ? label : "same-answer" ,
52- description : "" ,
53- preview : `Preview body: ${ label } , question ${ index + 1 } ` ,
54- } ) ) ,
55- } ) ) ,
56- } ;
57-
58- function Questions ( {
59- initialIndex = 0 ,
60- pendingPrompt = prompt ,
61- } : {
62- initialIndex ?: number ;
63- pendingPrompt ?: PendingUserInput ;
64- } ) {
43+ function Questions ( { initialIndex = 0 } : { initialIndex ?: number } ) {
6544 const [ questionIndex , setQuestionIndex ] = useState ( initialIndex ) ;
6645 const [ answers , setAnswers ] = useState < Record < string , PendingUserInputDraftAnswer > > ( { } ) ;
67- const question = pendingPrompt . questions [ questionIndex ] ! ;
6846 const advance = ( ) => setQuestionIndex ( questionIndex + 1 ) ;
6947 return (
7048 < >
7149 < output > { questionIndex } </ output >
7250 < button onClick = { advance } > Next</ button >
7351 < button onClick = { ( ) => setQuestionIndex ( questionIndex - 1 ) } > Previous</ button >
74- < input
75- aria-label = "Custom answer"
76- value = { answers [ question . id ] ?. customAnswer ?? "" }
77- onChange = { ( event ) => {
78- const value = event . currentTarget . value ;
79- setAnswers ( ( existing ) => ( {
80- ...existing ,
81- [ question . id ] : setPendingUserInputCustomAnswer ( existing [ question . id ] , value , question ) ,
82- } ) ) ;
83- } }
84- />
8552 < ComposerPendingUserInputPanel
86- pendingUserInputs = { [ pendingPrompt ] }
53+ pendingUserInputs = { [ prompt ] }
8754 cwd = "/repo"
8855 threadRef = { threadRef }
8956 disabled = { false }
@@ -92,7 +59,7 @@ function Questions({
9259 questionIndex = { questionIndex }
9360 onAdvance = { advance }
9461 onToggleOption = { ( questionId , value ) => {
95- const question = pendingPrompt . questions . find ( ( entry ) => entry . id === questionId ) ! ;
62+ const question = prompt . questions . find ( ( entry ) => entry . id === questionId ) ! ;
9663 setAnswers ( ( existing ) => ( {
9764 ...existing ,
9865 [ questionId ] : togglePendingUserInputOptionSelection (
@@ -325,94 +292,3 @@ it("isolates drafts by environment, thread and request and discards only resolve
325292 ) ;
326293 }
327294} ) ;
328-
329- function expectPreview ( text : string | null ) {
330- const previews = [ ...container . querySelectorAll ( "p" ) ]
331- . map ( ( element ) => element . textContent )
332- . filter ( ( content ) => content ?. startsWith ( "Preview body:" ) ) ;
333- expect ( previews ) . toEqual ( text === null ? [ ] : [ `Preview body: ${ text } ` ] ) ;
334- }
335-
336- async function typeCustomAnswer ( value : string ) {
337- const input = container . querySelector ( "input" ) ! ;
338- await act ( async ( ) => {
339- input . focus ( ) ;
340- Object . getOwnPropertyDescriptor ( HTMLInputElement . prototype , "value" ) ! . set ! . call ( input , value ) ;
341- input . dispatchEvent ( new Event ( "input" , { bubbles : true } ) ) ;
342- } ) ;
343- }
344-
345- it ( "previews options by focus, click, and shortcut index but only restores unambiguous saved selections" , async ( ) => {
346- await act ( async ( ) => root . render ( < Questions pendingPrompt = { previewPrompt } /> ) ) ;
347- expectPreview ( "First, question 1" ) ;
348-
349- await act ( async ( ) => buttonWithText ( "Second" ) . focus ( ) ) ;
350- expectPreview ( "Second, question 1" ) ;
351- await act ( async ( ) => buttonWithText ( "First" ) . focus ( ) ) ;
352- expectPreview ( "First, question 1" ) ;
353-
354- await act ( async ( ) => buttonWithText ( "Second" ) . click ( ) ) ;
355- expectPreview ( "Second, question 1" ) ;
356- await act ( async ( ) => document . dispatchEvent ( new KeyboardEvent ( "keydown" , { key : "1" } ) ) ) ;
357- expectPreview ( "First, question 1" ) ;
358- await act ( async ( ) => document . dispatchEvent ( new KeyboardEvent ( "keydown" , { key : "2" } ) ) ) ;
359- expectPreview ( "Second, question 1" ) ;
360-
361- await act ( async ( ) => vi . advanceTimersByTime ( 200 ) ) ;
362- expect ( activeIndex ( ) ) . toBe ( "1" ) ;
363- expectPreview ( "First, question 2" ) ;
364-
365- await act ( async ( ) => buttonWithText ( "Second" ) . focus ( ) ) ;
366- expectPreview ( "Second, question 2" ) ;
367- await act ( async ( ) =>
368- root . render (
369- < Questions
370- pendingPrompt = { { ...previewPrompt , requestId : RuntimeRequestId . make ( "replacement" ) } }
371- /> ,
372- ) ,
373- ) ;
374- expectPreview ( "First, question 2" ) ;
375-
376- await act ( async ( ) => buttonWithText ( "Second" ) . click ( ) ) ;
377- expectPreview ( "Second, question 2" ) ;
378- await navigate ( "Next" ) ;
379- expectPreview ( "First, question 3" ) ;
380- await navigate ( "Previous" ) ;
381- expectPreview ( "Second, question 2" ) ;
382-
383- await navigate ( "Previous" ) ;
384- expectPreview ( null ) ;
385- await act ( async ( ) =>
386- root . render (
387- < Questions
388- pendingPrompt = { { ...previewPrompt , requestId : RuntimeRequestId . make ( "remounted" ) } }
389- /> ,
390- ) ,
391- ) ;
392- expectPreview ( null ) ;
393- await act ( async ( ) => buttonWithText ( "Second" ) . click ( ) ) ;
394- expectPreview ( "Second, question 1" ) ;
395- } ) ;
396-
397- it ( "hides and clears hovered or focused previews while a custom answer is active" , async ( ) => {
398- await act ( async ( ) => root . render ( < Questions pendingPrompt = { previewPrompt } /> ) ) ;
399- await act ( async ( ) =>
400- buttonWithText ( "Second" ) . dispatchEvent ( new MouseEvent ( "mouseover" , { bubbles : true } ) ) ,
401- ) ;
402- expectPreview ( "Second, question 1" ) ;
403-
404- await typeCustomAnswer ( "Use a different approach" ) ;
405- expectPreview ( null ) ;
406- await typeCustomAnswer ( "" ) ;
407- expectPreview ( "First, question 1" ) ;
408-
409- await typeCustomAnswer ( "Use a different approach" ) ;
410- await act ( async ( ) => buttonWithText ( "Second" ) . focus ( ) ) ;
411- expectPreview ( null ) ;
412- await act ( async ( ) =>
413- buttonWithText ( "Second" ) . dispatchEvent ( new MouseEvent ( "mouseover" , { bubbles : true } ) ) ,
414- ) ;
415- expectPreview ( null ) ;
416- await typeCustomAnswer ( "" ) ;
417- expectPreview ( "First, question 1" ) ;
418- } ) ;
0 commit comments