@@ -106,6 +106,140 @@ test("document job runs without a client, waits for review, and resumes from its
106106 ) ;
107107} ) ;
108108
109+ test ( "document job preserves valid fields while asking to repair an invalid checkbox" , async ( ) => {
110+ const recoveryOwner = "document-field-recovery" ;
111+ await server . workspace . ensureSample ( recoveryOwner , server . actions ) ;
112+ const workspace = await server . workspace . snapshot ( recoveryOwner ) ;
113+ const mail = workspace . mail . find ( ( message ) => message . attachments . length ) ;
114+ assert . ok ( mail ) ;
115+ const task = await server . agent . createTask ( recoveryOwner , {
116+ prompt : "Fill the sample form and prepare a reply" ,
117+ kind : "document" ,
118+ input : {
119+ messageId : mail . id ,
120+ fields : {
121+ participant_name : "Test Student" ,
122+ guardian_name : "Test Guardian" ,
123+ permission_granted : "true" ,
124+ } ,
125+ } ,
126+ } ) ;
127+
128+ await server . agent . worker . tick ( ) ;
129+ const waiting = await server . agent . getTask ( recoveryOwner , task . id ) ;
130+ assert . equal ( waiting . status , "waiting_input" , waiting . error ?? waiting . question ) ;
131+ assert . equal ( waiting . plan [ 0 ] ?. status , "succeeded" ) ;
132+ assert . equal ( waiting . plan [ 1 ] ?. status , "pending" ) ;
133+ assert . ok ( waiting . state . source ) ;
134+ assert . deepEqual ( waiting . input . fields , {
135+ participant_name : "Test Student" ,
136+ guardian_name : "Test Guardian" ,
137+ } ) ;
138+ assert . deepEqual ( waiting . state . fieldErrors , [
139+ {
140+ name : "permission_granted" ,
141+ expected : "boolean" ,
142+ received : "string" ,
143+ } ,
144+ ] ) ;
145+ assert . equal (
146+ ( await db . list < Artifact > ( recoveryOwner , "files" ) ) . filter ( ( file ) => file . parentId ) . length ,
147+ 0 ,
148+ ) ;
149+ const source = waiting . state . source as { fileId : string } ;
150+
151+ await server . agent . answer ( recoveryOwner , task . id , "Use a checked checkbox" , {
152+ permission_granted : true ,
153+ } ) ;
154+ await server . agent . worker . tick ( ) ;
155+
156+ const reviewed = await server . agent . detail ( recoveryOwner , task . id ) ;
157+ assert . equal (
158+ reviewed . task . status ,
159+ "waiting_approval" ,
160+ reviewed . task . error ?? reviewed . task . question ,
161+ ) ;
162+ assert . equal ( ( reviewed . task . state . source as { fileId : string } ) . fileId , source . fileId ) ;
163+ assert . deepEqual ( reviewed . task . input . fields , {
164+ participant_name : "Test Student" ,
165+ guardian_name : "Test Guardian" ,
166+ permission_granted : true ,
167+ } ) ;
168+ assert . equal ( reviewed . task . state . fieldErrors , undefined ) ;
169+ assert . equal ( reviewed . task . state . missingFields , undefined ) ;
170+ assert . deepEqual ( reviewed . task . state . answers , [
171+ {
172+ question : waiting . question ,
173+ answer : "Use a checked checkbox" ,
174+ } ,
175+ ] ) ;
176+ assert . equal ( reviewed . files . length , 1 ) ;
177+ } ) ;
178+
179+ test ( "document job preserves valid fields while asking to replace an unknown field" , async ( ) => {
180+ const recoveryOwner = "document-field-name-recovery" ;
181+ await server . workspace . ensureSample ( recoveryOwner , server . actions ) ;
182+ const workspace = await server . workspace . snapshot ( recoveryOwner ) ;
183+ const mail = workspace . mail . find ( ( message ) => message . attachments . length ) ;
184+ assert . ok ( mail ) ;
185+ const task = await server . agent . createTask ( recoveryOwner , {
186+ prompt : "Fill the sample form and prepare a reply" ,
187+ kind : "document" ,
188+ input : {
189+ messageId : mail . id ,
190+ fields : {
191+ participant_name : "Test Student" ,
192+ guardian_name : "Test Guardian" ,
193+ consent : true ,
194+ } ,
195+ } ,
196+ } ) ;
197+
198+ await server . agent . worker . tick ( ) ;
199+ const waiting = await server . agent . getTask ( recoveryOwner , task . id ) ;
200+ assert . equal ( waiting . status , "waiting_input" , waiting . error ?? waiting . question ) ;
201+ assert . equal ( waiting . plan [ 0 ] ?. status , "succeeded" ) ;
202+ assert . equal ( waiting . plan [ 1 ] ?. status , "pending" ) ;
203+ assert . ok ( waiting . state . source ) ;
204+ assert . deepEqual ( waiting . input . fields , {
205+ participant_name : "Test Student" ,
206+ guardian_name : "Test Guardian" ,
207+ } ) ;
208+ assert . deepEqual ( waiting . state . fieldErrors , [
209+ {
210+ name : "consent" ,
211+ expected : "a supported PDF field" ,
212+ received : "boolean" ,
213+ } ,
214+ ] ) ;
215+ assert . equal (
216+ ( await db . list < Artifact > ( recoveryOwner , "files" ) ) . filter ( ( file ) => file . parentId ) . length ,
217+ 0 ,
218+ ) ;
219+ const source = waiting . state . source as { fileId : string } ;
220+
221+ await server . agent . answer ( recoveryOwner , task . id , "Use the PDF's checkbox field" , {
222+ permission_granted : true ,
223+ } ) ;
224+ await server . agent . worker . tick ( ) ;
225+
226+ const reviewed = await server . agent . detail ( recoveryOwner , task . id ) ;
227+ assert . equal (
228+ reviewed . task . status ,
229+ "waiting_approval" ,
230+ reviewed . task . error ?? reviewed . task . question ,
231+ ) ;
232+ assert . equal ( ( reviewed . task . state . source as { fileId : string } ) . fileId , source . fileId ) ;
233+ assert . deepEqual ( reviewed . task . input . fields , {
234+ participant_name : "Test Student" ,
235+ guardian_name : "Test Guardian" ,
236+ permission_granted : true ,
237+ } ) ;
238+ assert . equal ( reviewed . task . state . fieldErrors , undefined ) ;
239+ assert . equal ( reviewed . task . state . missingFields , undefined ) ;
240+ assert . equal ( reviewed . files . length , 1 ) ;
241+ } ) ;
242+
109243test ( "document retry reuses the filled PDF after its task checkpoint is lost" , async ( t ) => {
110244 const replayOwner = "interrupted-document" ;
111245 await server . workspace . ensureSample ( replayOwner , server . actions ) ;
0 commit comments