@@ -66,6 +66,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"
6666
6767import {
6868 COMPOSER_DRAFT_STORAGE_KEY ,
69+ beginBackgroundDraftSubmissionByRef ,
6970 clearComposerDraftsEnvironment ,
7071 composerDraftHasUserContent ,
7172 finalizePromotedDraftThreadByRef ,
@@ -1628,6 +1629,28 @@ describe("composerDraftStore project draft thread mapping", () => {
16281629 expect ( draftFor ( threadId , TEST_ENVIRONMENT_ID ) ?. prompt ) . toBe ( "typed during setup" ) ;
16291630 } ) ;
16301631
1632+ it ( "cleans up a completed background draft without replacing the active draft" , ( ) => {
1633+ const store = useComposerDraftStore . getState ( ) ;
1634+ const threadRef = scopeThreadRef ( TEST_ENVIRONMENT_ID , threadId ) ;
1635+ const nextDraftId = DraftId . make ( "next-draft" ) ;
1636+ store . setProjectDraftThreadId ( projectRef , draftId , { threadId } ) ;
1637+ beginBackgroundDraftSubmissionByRef ( threadRef ) ;
1638+ markPromotedDraftThreadByRef ( threadRef ) ;
1639+ store . setProjectDraftThreadId ( projectRef , nextDraftId , {
1640+ threadId : ThreadId . make ( "next-thread" ) ,
1641+ } ) ;
1642+ store . setPrompt ( nextDraftId , "Keep my next task" ) ;
1643+
1644+ finalizePromotedDraftThreadByRef ( threadRef ) ;
1645+
1646+ expect ( store . getDraftSession ( draftId ) ) . toBeNull ( ) ;
1647+ expect ( store . getDraftThreadByProjectRef ( projectRef ) ?. draftId ) . toBe ( nextDraftId ) ;
1648+ expect ( store . getComposerDraft ( nextDraftId ) ?. prompt ) . toBe ( "Keep my next task" ) ;
1649+ expect (
1650+ useComposerDraftStore . getState ( ) . backgroundSubmissionThreadKeys [ scopedThreadKey ( threadRef ) ] ,
1651+ ) . toBeUndefined ( ) ;
1652+ } ) ;
1653+
16311654 it ( "finalizes a matching materialized draft even when promotion was not pre-marked" , ( ) => {
16321655 const store = useComposerDraftStore . getState ( ) ;
16331656 store . setProjectDraftThreadId ( projectRef , draftId , { threadId } ) ;
0 commit comments