@@ -350,6 +350,58 @@ describe('JmapMailProvider', () => {
350350 'Failed to create email for sending' ,
351351 ) ;
352352 } ) ;
353+
354+ test ( 'When sending from an existing draft, then the new email is created with the latest content and the previous draft is removed in the same operation' , async ( ) => {
355+ const sentMailbox = newJmapMailbox ( { role : 'sent' } ) ;
356+ const identity = newJmapIdentity ( ) ;
357+
358+ jmapService . request . mockResolvedValueOnce (
359+ jmapResponse ( { list : [ identity ] } ) ,
360+ ) ;
361+ jmapService . request . mockResolvedValueOnce (
362+ jmapResponse ( { list : [ sentMailbox ] } ) ,
363+ ) ;
364+ jmapService . request . mockResolvedValueOnce (
365+ jmapMultiResponse (
366+ { created : { draft : { id : 'sent-email-id' } } } ,
367+ { created : { submission : { id : 'sub-id' } } } ,
368+ ) ,
369+ ) ;
370+
371+ const dto = newSendEmailDto ( { draftId : 'old-draft-id' } ) ;
372+ const result = await provider . sendEmail ( 'user@test.com' , dto ) ;
373+
374+ const lastCall = jmapService . request . mock . calls . at ( - 1 ) ! ;
375+ const [ emailSetName , emailSetArgs ] = lastCall [ 1 ] [ 0 ] ! ;
376+ expect ( emailSetName ) . toBe ( 'Email/set' ) ;
377+ expect ( emailSetArgs [ 'destroy' ] ) . toEqual ( [ 'old-draft-id' ] ) ;
378+ expect ( emailSetArgs [ 'create' ] ) . toBeDefined ( ) ;
379+ expect ( result ) . toEqual ( { id : 'sent-email-id' } ) ;
380+ } ) ;
381+
382+ test ( 'When sending without a draftId, then the Email/set call does not include any destroy operation' , async ( ) => {
383+ const sentMailbox = newJmapMailbox ( { role : 'sent' } ) ;
384+ const identity = newJmapIdentity ( ) ;
385+
386+ jmapService . request . mockResolvedValueOnce (
387+ jmapResponse ( { list : [ identity ] } ) ,
388+ ) ;
389+ jmapService . request . mockResolvedValueOnce (
390+ jmapResponse ( { list : [ sentMailbox ] } ) ,
391+ ) ;
392+ jmapService . request . mockResolvedValueOnce (
393+ jmapMultiResponse (
394+ { created : { draft : { id : 'sent-email-id' } } } ,
395+ { created : { submission : { id : 'sub-id' } } } ,
396+ ) ,
397+ ) ;
398+
399+ await provider . sendEmail ( 'user@test.com' , newSendEmailDto ( ) ) ;
400+
401+ const lastCall = jmapService . request . mock . calls . at ( - 1 ) ! ;
402+ const [ , emailSetArgs ] = lastCall [ 1 ] [ 0 ] ! ;
403+ expect ( emailSetArgs [ 'destroy' ] ) . toBeUndefined ( ) ;
404+ } ) ;
353405 } ) ;
354406
355407 describe ( 'saveDraft' , ( ) => {
0 commit comments