1- import { describe , expect , it } from "vite-plus/test" ;
1+ import { afterEach , beforeEach , describe , expect , it , vi } from "vite-plus/test" ;
22import {
33 ChatAttachmentId ,
44 ComposerContextId ,
@@ -12,12 +12,32 @@ import { composerDraftEnvironmentId } from "../lib/composerAttachmentUploadQueue
1212import { isQueuedEditDraftKey , queuedEditDraftKey } from "./queued-edit-draft-key" ;
1313import {
1414 beginQueuedRunEdit ,
15+ endQueuedRunEdit ,
1516 getQueuedRunEdit ,
1617 recoverQueuedRunEdit ,
1718 resolveQueuedEditPayload ,
1819 type QueuedRunEdit ,
1920} from "./queued-run-edit" ;
20- import { getComposerDraftSnapshot , setComposerDraftText } from "./use-composer-drafts" ;
21+ import {
22+ clearComposerDraft ,
23+ flushComposerDrafts ,
24+ getComposerDraftSnapshot ,
25+ setComposerDraftText ,
26+ waitForComposerDraftsLoaded ,
27+ } from "./use-composer-drafts" ;
28+
29+ vi . mock ( "expo-file-system" , ( ) => ( {
30+ Directory : class {
31+ create ( ) { }
32+ } ,
33+ File : class {
34+ exists = false ;
35+ create ( ) { }
36+ write ( ) { }
37+ moveSync ( ) { }
38+ } ,
39+ Paths : { document : { uri : "file:///documents" } } ,
40+ } ) ) ;
2141
2242const image = ( id : string ) : ChatAttachment => ( {
2343 type : "image" ,
@@ -117,31 +137,52 @@ describe("recoverQueuedRunEdit", () => {
117137 const runId = RunId . make ( "run-1" ) ;
118138 const editKey = queuedEditDraftKey ( threadKey , runId ) ;
119139
120- it ( "drops an untouched edit without restoring the sent message" , ( ) => {
140+ beforeEach ( async ( ) => {
141+ await waitForComposerDraftsLoaded ( ) ;
142+ } ) ;
143+
144+ afterEach ( async ( ) => {
145+ endQueuedRunEdit ( threadKey ) ;
146+ clearComposerDraft ( threadKey ) ;
147+ await flushComposerDrafts ( ) ;
148+ } ) ;
149+
150+ it ( "drops an untouched edit without restoring the sent message" , async ( ) => {
121151 beginQueuedRunEdit ( threadKey , edit ( [ ] ) ) ;
122152
123- expect ( recoverQueuedRunEdit ( threadKey , runId ) ) . toBe ( "clean" ) ;
124- expect ( getQueuedRunEdit ( threadKey ) ) . toBeNull ( ) ;
153+ const recovery = recoverQueuedRunEdit ( threadKey , runId ) ;
154+ await flushComposerDrafts ( ) ;
155+
125156 expect ( getComposerDraftSnapshot ( threadKey ) . text ) . toBe ( "" ) ;
126157 expect ( getComposerDraftSnapshot ( editKey ) . text ) . toBe ( "" ) ;
158+ expect ( getQueuedRunEdit ( threadKey ) ) . toBeNull ( ) ;
159+ expect ( recovery ) . toBe ( "clean" ) ;
127160 } ) ;
128161
129- it ( "discards a changed edit when the thread draft already has content" , ( ) => {
130- setComposerDraftText ( threadKey , "unsent draft" ) ;
162+ it ( "recovers a changed edit into an empty thread draft" , async ( ) => {
131163 beginQueuedRunEdit ( threadKey , edit ( [ ] ) ) ;
132164 setComposerDraftText ( editKey , "revised" ) ;
133165
134- expect ( recoverQueuedRunEdit ( threadKey , runId ) ) . toBe ( "discarded" ) ;
166+ const recovery = recoverQueuedRunEdit ( threadKey , runId ) ;
167+ await flushComposerDrafts ( ) ;
168+
169+ expect ( getComposerDraftSnapshot ( threadKey ) . text ) . toBe ( "revised" ) ;
170+ expect ( getComposerDraftSnapshot ( editKey ) . text ) . toBe ( "" ) ;
135171 expect ( getQueuedRunEdit ( threadKey ) ) . toBeNull ( ) ;
136- expect ( getComposerDraftSnapshot ( threadKey ) . text ) . toBe ( "unsent draft" ) ;
137- setComposerDraftText ( threadKey , "" ) ;
172+ expect ( recovery ) . toBe ( "kept" ) ;
138173 } ) ;
139174
140- it ( "ignores a run the composer is no longer editing" , ( ) => {
175+ it ( "preserves an occupied thread draft when discarding a changed edit" , async ( ) => {
176+ setComposerDraftText ( threadKey , "unsent draft" ) ;
141177 beginQueuedRunEdit ( threadKey , edit ( [ ] ) ) ;
178+ setComposerDraftText ( editKey , "revised" ) ;
142179
143- expect ( recoverQueuedRunEdit ( threadKey , RunId . make ( "run-2" ) ) ) . toBeNull ( ) ;
144- expect ( getQueuedRunEdit ( threadKey ) ?. runId ) . toBe ( runId ) ;
145- expect ( recoverQueuedRunEdit ( threadKey , runId ) ) . toBe ( "clean" ) ;
180+ const recovery = recoverQueuedRunEdit ( threadKey , runId ) ;
181+ await flushComposerDrafts ( ) ;
182+
183+ expect ( getComposerDraftSnapshot ( threadKey ) . text ) . toBe ( "unsent draft" ) ;
184+ expect ( getComposerDraftSnapshot ( editKey ) . text ) . toBe ( "" ) ;
185+ expect ( getQueuedRunEdit ( threadKey ) ) . toBeNull ( ) ;
186+ expect ( recovery ) . toBe ( "discarded" ) ;
146187 } ) ;
147188} ) ;
0 commit comments