@@ -114,14 +114,16 @@ describe("incoming share imports", () => {
114114 it ( "reserves, attaches and consumes once, releasing files the draft did not keep" , async ( ) => {
115115 const kept = fileAttachment ( "kept" ) ;
116116 const skipped = fileAttachment ( "skipped" ) ;
117- const { deps, calls, drafts, inbox, watch } = setup ( {
117+ const { deps, calls, drafts, inbox, imports , watch } = setup ( {
118118 selectAttachments : ( ) => ( { status : "ready" , attachments : [ kept ] , warnings : [ "too big" ] } ) ,
119119 } ) ;
120120 inbox . set ( "share-1" , share ( [ kept , skipped ] ) ) ;
121- const watched = watch ( importTarget ( ) ) ;
121+ const target = importTarget ( ) ;
122+ const watched = watch ( target ) ;
122123
123124 expect ( watched . phase ( ) ) . toBe ( "reserving" ) ;
124- await vi . waitFor ( ( ) => expect ( watched . phase ( ) ) . toBe ( "done" ) ) ;
125+ await imports . settled ( target ) ;
126+ expect ( watched . phase ( ) ) . toBe ( "done" ) ;
125127
126128 expect ( calls ) . toEqual ( [ "reserve" , "merge" , "consume" ] ) ;
127129 expect ( drafts . get ( "draft-a" ) ?. attachments ) . toEqual ( [ kept ] ) ;
@@ -144,7 +146,8 @@ describe("incoming share imports", () => {
144146 tasks . splice ( 0 ) . forEach ( ( task ) => task ( ) ) ;
145147 expect ( registry . getNodes ( ) . size ) . toBe ( 0 ) ;
146148 const unmount = registry . mount ( atom ) ;
147- await vi . waitFor ( ( ) => expect ( registry . get ( atom ) ) . toBe ( "done" ) ) ;
149+ await imports . settled ( importTarget ( ) ) ;
150+ expect ( registry . get ( atom ) ) . toBe ( "done" ) ;
148151 unmount ( ) ;
149152
150153 expect ( deps . reserve ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -159,7 +162,8 @@ describe("incoming share imports", () => {
159162 } ) ;
160163 const target = importTarget ( ) ;
161164 const watched = watch ( target ) ;
162- await vi . waitFor ( ( ) => expect ( watched . phase ( ) ) . toBe ( "failed" ) ) ;
165+ await imports . settled ( target ) ;
166+ expect ( watched . phase ( ) ) . toBe ( "failed" ) ;
163167 expect ( deps . onImportFailure ) . toHaveBeenCalledWith ( target , new Error ( "disk full" ) ) ;
164168
165169 await imports . cancel ( target ) ;
@@ -185,7 +189,8 @@ describe("incoming share imports", () => {
185189 } ) ;
186190 const target = importTarget ( ) ;
187191 const watched = watch ( target ) ;
188- await vi . waitFor ( ( ) => expect ( watched . phase ( ) ) . toBe ( "failed" ) ) ;
192+ await imports . settled ( target ) ;
193+ expect ( watched . phase ( ) ) . toBe ( "failed" ) ;
189194
190195 await imports . cancel ( target ) ;
191196 expect ( watched . phase ( ) ) . toBe ( "failed" ) ;
@@ -200,10 +205,17 @@ describe("incoming share imports", () => {
200205 } ) ;
201206
202207 it ( "stops before consuming once its route leaves the stack" , async ( ) => {
208+ const mergeStarted = Promise . withResolvers < void > ( ) ;
203209 const merged = Promise . withResolvers < { readonly skippedAttachmentCount : number } > ( ) ;
204- const { deps, imports, watch } = setup ( { mergeIntoDraft : ( ) => merged . promise } ) ;
210+ const { deps, imports, watch } = setup ( {
211+ mergeIntoDraft : ( ) => {
212+ mergeStarted . resolve ( ) ;
213+ return merged . promise ;
214+ } ,
215+ } ) ;
205216 const watched = watch ( importTarget ( ) ) ;
206- await vi . waitFor ( ( ) => expect ( watched . phase ( ) ) . toBe ( "attaching" ) ) ;
217+ await mergeStarted . promise ;
218+ expect ( watched . phase ( ) ) . toBe ( "attaching" ) ;
207219
208220 const pruned = imports . prune ( new Set ( [ "flow-b" ] ) ) ;
209221 merged . resolve ( { skippedAttachmentCount : 0 } ) ;
@@ -225,7 +237,8 @@ describe("incoming share imports", () => {
225237 } ) ;
226238 const target = importTarget ( ) ;
227239 const watched = watch ( target ) ;
228- await vi . waitFor ( ( ) => expect ( watched . phase ( ) ) . toBe ( "failed" ) ) ;
240+ await imports . settled ( target ) ;
241+ expect ( watched . phase ( ) ) . toBe ( "failed" ) ;
229242 watched . unmount ( ) ;
230243 await imports . prune ( new Set ( ) ) ;
231244
@@ -238,15 +251,41 @@ describe("incoming share imports", () => {
238251
239252 it ( "a route switching drafts abandons the in-flight import" , async ( ) => {
240253 const reserved = Promise . withResolvers < void > ( ) ;
241- const { deps, watch } = setup ( {
254+ const { deps, imports , watch } = setup ( {
242255 reserve : vi . fn ( ) . mockReturnValueOnce ( reserved . promise ) . mockResolvedValue ( undefined ) ,
243256 } ) ;
244257 watch ( importTarget ( ) ) . unmount ( ) ;
245- const second = watch ( importTarget ( { draftKey : "draft-b" } ) ) ;
258+ const secondTarget = importTarget ( { draftKey : "draft-b" } ) ;
259+ const second = watch ( secondTarget ) ;
246260 reserved . resolve ( ) ;
247261
248- await vi . waitFor ( ( ) => expect ( second . phase ( ) ) . toBe ( "done" ) ) ;
262+ await imports . settled ( secondTarget ) ;
263+ expect ( second . phase ( ) ) . toBe ( "done" ) ;
249264 expect ( deps . mergeIntoDraft ) . toHaveBeenCalledTimes ( 1 ) ;
250265 expect ( deps . mergeIntoDraft ) . toHaveBeenCalledWith ( "draft-b" , expect . anything ( ) ) ;
251266 } ) ;
267+
268+ it ( "stays in flight for its route after the screen stops rendering its target" , async ( ) => {
269+ const mergeStarted = Promise . withResolvers < void > ( ) ;
270+ const merged = Promise . withResolvers < { readonly skippedAttachmentCount : number } > ( ) ;
271+ const { imports, registry, watch } = setup ( {
272+ mergeIntoDraft : ( ) => {
273+ mergeStarted . resolve ( ) ;
274+ return merged . promise ;
275+ } ,
276+ } ) ;
277+ const inFlight = imports . inFlightAtom ( "flow-a" ) ;
278+ const unmountInFlight = registry . mount ( inFlight ) ;
279+ const target = importTarget ( ) ;
280+ const watched = watch ( target ) ;
281+ await mergeStarted . promise ;
282+
283+ watched . unmount ( ) ;
284+ expect ( registry . get ( inFlight ) ) . toBe ( true ) ;
285+
286+ merged . resolve ( { skippedAttachmentCount : 0 } ) ;
287+ await imports . settled ( target ) ;
288+ expect ( registry . get ( inFlight ) ) . toBe ( false ) ;
289+ unmountInFlight ( ) ;
290+ } ) ;
252291} ) ;
0 commit comments