@@ -3,7 +3,7 @@ import { describe, expect, it, vi } from "vite-plus/test";
33
44import type { DraftComposerAttachment } from "../../lib/composerImages" ;
55import type { IncomingShareDraft } from "../sharing/incoming-share-model" ;
6- import type { ComposerDraft } from "../../state/use-composer-drafts" ;
6+ import { undoComposerDraftMergeState , type ComposerDraft } from "../../state/use-composer-drafts" ;
77import {
88 makeIncomingShareImports ,
99 type ShareImportDeps ,
@@ -13,7 +13,34 @@ import {
1313} from "./incomingShareImport" ;
1414
1515const destination = { environmentId : "env-1" , projectId : "project-1" } ;
16- const edited : ComposerDraft = { text : "typed after the restore" , attachments : [ ] } ;
16+ const edited : ComposerDraft = { text : "typed after the undo" , attachments : [ ] } ;
17+
18+ /** The draft store's own undo, applied to the test's draft map. */
19+ function undoInto (
20+ drafts : Map < string , ComposerDraft > ,
21+ draftKey : string ,
22+ snapshot : ComposerDraft ,
23+ merged : ComposerDraft ,
24+ ) : void {
25+ const next = undoComposerDraftMergeState ( Object . fromEntries ( drafts ) , draftKey , snapshot , merged ) ;
26+ drafts . clear ( ) ;
27+ for ( const [ key , draft ] of Object . entries ( next ) ) drafts . set ( key , draft ) ;
28+ }
29+
30+ function mergeInto (
31+ drafts : Map < string , ComposerDraft > ,
32+ draftKey : string ,
33+ content : {
34+ readonly text : string ;
35+ readonly attachments : ReadonlyArray < DraftComposerAttachment > ;
36+ } ,
37+ ) : void {
38+ const existing = drafts . get ( draftKey ) ?? { text : "" , attachments : [ ] } ;
39+ drafts . set ( draftKey , {
40+ text : `${ existing . text } \n${ content . text } ` ,
41+ attachments : [ ...existing . attachments , ...content . attachments ] ,
42+ } ) ;
43+ }
1744
1845function fileAttachment ( id : string ) : DraftComposerAttachment {
1946 return {
@@ -66,17 +93,13 @@ function setup(overrides: Partial<ShareImportDeps> = {}) {
6693 calls . push ( "consume" ) ;
6794 } ) ,
6895 readDraft : ( draftKey ) => drafts . get ( draftKey ) ?? { text : "" , attachments : [ ] } ,
69- restoreDraft : vi . fn ( async ( draftKey , snapshot ) => {
70- calls . push ( "restore " ) ;
71- drafts . set ( draftKey , snapshot ) ;
96+ undoMerge : vi . fn ( async ( draftKey , snapshot , merged ) => {
97+ calls . push ( "undo " ) ;
98+ undoInto ( drafts , draftKey , snapshot , merged ) ;
7299 } ) ,
73100 mergeIntoDraft : vi . fn ( async ( draftKey , content ) => {
74101 calls . push ( "merge" ) ;
75- const existing = drafts . get ( draftKey ) ?? { text : "" , attachments : [ ] } ;
76- drafts . set ( draftKey , {
77- text : `${ existing . text } \n${ content . text } ` ,
78- attachments : [ ...existing . attachments , ...content . attachments ] ,
79- } ) ;
102+ mergeInto ( drafts , draftKey , content ) ;
80103 return { skippedAttachmentCount : 0 } ;
81104 } ) ,
82105 selectAttachments : ( incoming ) => ( {
@@ -170,7 +193,7 @@ describe("incoming share imports", () => {
170193 await imports . cancel ( target ) ;
171194
172195 expect ( watched . phase ( ) ) . toBe ( "cancelled" ) ;
173- expect ( calls ) . toEqual ( [ "reserve" , "merge" , "restore " , "release" ] ) ;
196+ expect ( calls ) . toEqual ( [ "reserve" , "merge" , "undo " , "release" ] ) ;
174197 expect ( drafts . get ( "draft-a" ) ) . toEqual ( { text : "mine" , attachments : [ ] } ) ;
175198 // Unconsumed: the inbox still owns the files.
176199 expect ( deps . releaseUnusedAttachments ) . not . toHaveBeenCalled ( ) ;
@@ -200,7 +223,7 @@ describe("incoming share imports", () => {
200223
201224 await imports . cancel ( target ) ;
202225 expect ( watched . phase ( ) ) . toBe ( "cancelled" ) ;
203- expect ( deps . restoreDraft ) . toHaveBeenCalledTimes ( 1 ) ;
226+ expect ( deps . undoMerge ) . toHaveBeenCalledTimes ( 1 ) ;
204227 expect ( drafts . get ( "draft-a" ) ) . toEqual ( { text : "mine" , attachments : [ ] } ) ;
205228 expect ( deps . leave ) . toHaveBeenCalledTimes ( 1 ) ;
206229 } ) ;
@@ -227,23 +250,27 @@ describe("incoming share imports", () => {
227250 watched . unmount ( ) ;
228251 } ) ;
229252
230- it . each ( [
231- [ "edited" , ( drafts : Map < string , ComposerDraft > ) => drafts . set ( "draft-a" , edited ) ] ,
232- [ "deleted" , ( drafts : Map < string , ComposerDraft > ) => drafts . delete ( "draft-a" ) ] ,
233- ] as const ) (
234- "a retried cancel keeps a draft the user %s after the restore was shown" ,
235- async ( _change , change ) => {
236- let restoreAttempts = 0 ;
253+ const userChanges = [
254+ [ "edited" , ( drafts : Map < string , ComposerDraft > ) => drafts . set ( "draft-a" , edited ) , edited ] ,
255+ [ "deleted" , ( drafts : Map < string , ComposerDraft > ) => drafts . delete ( "draft-a" ) , undefined ] ,
256+ ] as const ;
257+
258+ it . each ( userChanges ) (
259+ "a retried cancel keeps a draft the user %s after the undo was shown" ,
260+ async ( _change , change , expected ) => {
261+ let undoAttempts = 0 ;
237262 const { deps, drafts, watch, imports } = setup ( {
238263 consume : vi . fn ( async ( ) => {
239264 throw new Error ( "disk full" ) ;
240265 } ) ,
241- // Like restoreComposerDraftSnapshot: publishes, then fails to persist.
242- restoreDraft : vi . fn ( async ( draftKey : string , snapshot : ComposerDraft ) => {
243- restoreAttempts += 1 ;
244- drafts . set ( draftKey , snapshot ) ;
245- if ( restoreAttempts === 1 ) throw new Error ( "write failed" ) ;
246- } ) ,
266+ // Like undoComposerDraftMerge: publishes, then fails to persist.
267+ undoMerge : vi . fn (
268+ async ( draftKey : string , snapshot : ComposerDraft , merged : ComposerDraft ) => {
269+ undoAttempts += 1 ;
270+ undoInto ( drafts , draftKey , snapshot , merged ) ;
271+ if ( undoAttempts === 1 ) throw new Error ( "write failed" ) ;
272+ } ,
273+ ) ,
247274 } ) ;
248275 const target = importTarget ( ) ;
249276 const watched = watch ( target ) ;
@@ -252,15 +279,75 @@ describe("incoming share imports", () => {
252279 expect ( deps . onCancelFailure ) . toHaveBeenCalledWith ( target , new Error ( "write failed" ) ) ;
253280
254281 change ( drafts ) ;
255- const expected = drafts . get ( "draft-a" ) ;
256282 await imports . cancel ( target ) ;
257283
258284 expect ( watched . phase ( ) ) . toBe ( "cancelled" ) ;
259- expect ( drafts . get ( "draft-a" ) ) . toBe ( expected ) ;
285+ expect ( drafts . get ( "draft-a" ) ) . toEqual ( expected ) ;
260286 expect ( deps . release ) . toHaveBeenCalledTimes ( 1 ) ;
261287 } ,
262288 ) ;
263289
290+ it ( "cancel undoes the import after an upload finished on its attachment" , async ( ) => {
291+ const file = fileAttachment ( "file" ) ;
292+ const { drafts, inbox, watch, imports } = setup ( {
293+ consume : vi . fn ( async ( ) => {
294+ throw new Error ( "disk full" ) ;
295+ } ) ,
296+ } ) ;
297+ inbox . set ( "share-1" , share ( [ file ] ) ) ;
298+ const target = importTarget ( ) ;
299+ watch ( target ) ;
300+ await imports . settled ( target ) ;
301+
302+ const merged = drafts . get ( "draft-a" ) ;
303+ drafts . set ( "draft-a" , {
304+ text : merged ?. text ?? "" ,
305+ attachments : [ { ...file , uploadedAttachmentId : "uploaded-1" } ] ,
306+ } ) ;
307+ await imports . cancel ( target ) ;
308+
309+ expect ( drafts . get ( "draft-a" ) ) . toEqual ( { text : "mine" , attachments : [ ] } ) ;
310+ } ) ;
311+
312+ it . each ( userChanges ) (
313+ "cancel keeps a draft the user %s after a merge that published but failed to persist" ,
314+ async ( _change , change , expected ) => {
315+ const { drafts, watch, imports } = setup ( {
316+ // Like mergeComposerDraftContent: publishes, then fails to persist.
317+ mergeIntoDraft : vi . fn ( async ( draftKey , content ) => {
318+ mergeInto ( drafts , draftKey , content ) ;
319+ throw new Error ( "write failed" ) ;
320+ } ) ,
321+ } ) ;
322+ const target = importTarget ( ) ;
323+ const watched = watch ( target ) ;
324+ await imports . settled ( target ) ;
325+ expect ( watched . phase ( ) ) . toBe ( "failed" ) ;
326+
327+ change ( drafts ) ;
328+ await imports . cancel ( target ) ;
329+
330+ expect ( watched . phase ( ) ) . toBe ( "cancelled" ) ;
331+ expect ( drafts . get ( "draft-a" ) ) . toEqual ( expected ) ;
332+ } ,
333+ ) ;
334+
335+ it ( "cancel after a merge that published but failed to persist removes the merged share" , async ( ) => {
336+ const { drafts, watch, imports } = setup ( {
337+ mergeIntoDraft : vi . fn ( async ( draftKey , content ) => {
338+ mergeInto ( drafts , draftKey , content ) ;
339+ throw new Error ( "write failed" ) ;
340+ } ) ,
341+ } ) ;
342+ const target = importTarget ( ) ;
343+ watch ( target ) ;
344+ await imports . settled ( target ) ;
345+
346+ await imports . cancel ( target ) ;
347+
348+ expect ( drafts . get ( "draft-a" ) ) . toEqual ( { text : "mine" , attachments : [ ] } ) ;
349+ } ) ;
350+
264351 it ( "a failed import keeps its cancel after its route left, and reports a failed undo" , async ( ) => {
265352 const { calls, deps, imports, watch } = setup ( {
266353 consume : vi . fn ( async ( ) => {
@@ -279,7 +366,7 @@ describe("incoming share imports", () => {
279366
280367 await imports . cancel ( target ) ;
281368
282- expect ( calls ) . toEqual ( [ "reserve" , "merge" , "restore " ] ) ;
369+ expect ( calls ) . toEqual ( [ "reserve" , "merge" , "undo " ] ) ;
283370 expect ( deps . onCancelFailure ) . toHaveBeenCalledWith ( target , new Error ( "locked" ) ) ;
284371 expect ( deps . leave ) . not . toHaveBeenCalled ( ) ;
285372 } ) ;
0 commit comments