1- import { makeThreadRequestsResolvedAnnouncer } from "@t3tools/client-runtime/state/thread-requests" ;
1+ import {
2+ makeThreadRequestsResolvedAnnouncer ,
3+ userInputDraftKey ,
4+ } from "@t3tools/client-runtime/state/thread-requests" ;
25import { ApprovalRequestId , EnvironmentId , RuntimeRequestId , ThreadId } from "@t3tools/contracts" ;
6+ import { threadContextRecord } from "@t3tools/shared/composerContextReferences" ;
37import { beforeEach , expect , it , vi } from "vite-plus/test" ;
48import { useComposerDraftStore } from "./composerDraftStore" ;
9+ import { formatInlineContextReference } from "./lib/composerContextReferences" ;
10+ import { threadContextReference } from "./lib/composerContextRecords" ;
511import {
612 questionAttachmentDraftId ,
713 countQuestionAttachments ,
814 questionAttachmentDraftPrefix ,
915 changeQuestionAttachmentPreparation ,
1016 clearQuestionAttachmentDraft ,
17+ usePendingUserInputAnswers ,
1118 useQuestionAttachmentPreparation ,
1219} from "./questionAttachments" ;
1320
@@ -19,6 +26,7 @@ const requestId = ApprovalRequestId.make("request-1");
1926beforeEach ( ( ) => {
2027 useComposerDraftStore . setState ( { draftsByThreadKey : { } , draftThreadsByThreadKey : { } } ) ;
2128 useQuestionAttachmentPreparation . setState ( { counts : { } } ) ;
29+ usePendingUserInputAnswers . setState ( { } , true ) ;
2230 release . mockClear ( ) ;
2331} ) ;
2432it ( "keeps question files separate from the normal draft and other questions" , ( ) => {
@@ -126,13 +134,58 @@ it("clears attachment drafts for requests the thread resolved, wherever they wer
126134 const elsewhere = questionAttachmentDraftId ( environmentId , otherThread , resolved , "first" ) ;
127135 for ( const key of [ keep , drop , elsewhere ] ) store . setPrompt ( key , "Answer context" ) ;
128136 changeQuestionAttachmentPreparation ( drop , 1 ) ;
137+ usePendingUserInputAnswers . setState ( {
138+ [ userInputDraftKey ( environmentId , threadId , resolved ) ] : { first : { customAnswer : "Sent" } } ,
139+ } ) ;
129140
130141 // The thread's projection synchronizes with only `pending` left, e.g. after
131142 // `resolved` was answered on another device while no composer showed it.
132- makeThreadRequestsResolvedAnnouncer ( environmentId , threadId ) ( [ pending ] ) ;
143+ makeThreadRequestsResolvedAnnouncer (
144+ environmentId ,
145+ threadId ,
146+ ) ( [
147+ { id : pending , status : "pending" } ,
148+ { id : resolved , status : "resolved" } ,
149+ ] ) ;
133150
134151 expect ( store . getComposerDraft ( keep ) ?. prompt ) . toBe ( "Answer context" ) ;
135152 expect ( store . getComposerDraft ( drop ) ) . toBeNull ( ) ;
136153 expect ( store . getComposerDraft ( elsewhere ) ?. prompt ) . toBe ( "Answer context" ) ;
137154 expect ( useQuestionAttachmentPreparation . getState ( ) . counts [ drop ] ) . toBeUndefined ( ) ;
155+ // An answered question's text was sent, so it does not come back as a draft.
156+ expect ( store . getComposerDraft ( { environmentId, threadId } ) ) . toBeNull ( ) ;
157+ expect ( usePendingUserInputAnswers . getState ( ) ) . toEqual ( { } ) ;
138158} ) ;
159+
160+ // Stop cancels an open question; a provider timeout expires it. Neither sends the answer.
161+ it . each ( [ "cancelled" , "expired" ] as const ) (
162+ "returns typed answers and their thread chips to the thread draft when a question is %s" ,
163+ ( status ) => {
164+ const store = useComposerDraftStore . getState ( ) ;
165+ const request = RuntimeRequestId . make ( "stopped" ) ;
166+ const reference = threadContextRecord (
167+ { environmentId, threadId : ThreadId . make ( "reference-thread" ) } ,
168+ "Reference work" ,
169+ ) ;
170+ const answer = `Purple, like ${ formatInlineContextReference ( threadContextReference ( reference ) ) } ` ;
171+ store . setPrompt ( { environmentId, threadId } , "Earlier draft" ) ;
172+ store . setThreadContexts ( questionAttachmentDraftId ( environmentId , threadId , request , "first" ) , [
173+ reference ,
174+ ] ) ;
175+ usePendingUserInputAnswers . setState ( {
176+ [ userInputDraftKey ( environmentId , threadId , request ) ] : {
177+ first : { customAnswer : answer } ,
178+ second : { customAnswer : " " } ,
179+ third : { customAnswer : "Also this" } ,
180+ } ,
181+ } ) ;
182+
183+ makeThreadRequestsResolvedAnnouncer ( environmentId , threadId ) ( [ { id : request , status } ] ) ;
184+
185+ expect ( store . getComposerDraft ( { environmentId, threadId } ) ) . toMatchObject ( {
186+ prompt : `Earlier draft\n\n${ answer } \n\nAlso this` ,
187+ threadContexts : [ reference ] ,
188+ } ) ;
189+ expect ( usePendingUserInputAnswers . getState ( ) ) . toEqual ( { } ) ;
190+ } ,
191+ ) ;
0 commit comments