1+ import { scopeProjectRef , scopeThreadRef } from "@t3tools/client-runtime/environment" ;
12import {
23 EnvironmentId ,
34 MessageId ,
45 PROVIDER_SEND_TURN_MAX_ATTACHMENTS ,
6+ ProjectId ,
57 ThreadId ,
68} from "@t3tools/contracts" ;
79import { expect , it } from "vite-plus/test" ;
810
9- import { useComposerDraftStore } from "../composerDraftStore" ;
11+ import {
12+ DraftId ,
13+ finalizePromotedDraftThreadByRef ,
14+ markPromotedDraftThreadByRef ,
15+ useComposerDraftStore ,
16+ } from "../composerDraftStore" ;
1017import { snapshotComposerSend } from "../components/ChatView.logic" ;
11- import { restoreComposerDraft } from "../components/chat/chatViewSend" ;
18+ import { reopenUnsentDraftThread , restoreComposerDraft } from "../components/chat/chatViewSend" ;
1219import type { ChatMessage } from "../types" ;
1320import { unsentMessages } from "./unsentMessages" ;
1421
@@ -29,17 +36,14 @@ function image(id: string) {
2936 } ;
3037}
3138
32- function keepUnsentBesideFullDraft ( ) {
33- useComposerDraftStore . setState ( { draftsByThreadKey : { } , draftThreadsByThreadKey : { } } ) ;
34- const store = useComposerDraftStore . getState ( ) ;
35- store . setPrompt ( target , "Newer draft" ) ;
36- store . addImages (
37- target ,
38- Array . from ( { length : PROVIDER_SEND_TURN_MAX_ATTACHMENTS } , ( _ , index ) =>
39- image ( `newer-${ index } ` ) ,
40- ) ,
39+ function newerImages ( ) {
40+ return Array . from ( { length : PROVIDER_SEND_TURN_MAX_ATTACHMENTS } , ( _ , index ) =>
41+ image ( `newer-${ index } ` ) ,
4142 ) ;
42- const snapshot = snapshotComposerSend ( {
43+ }
44+
45+ function unsentSnapshot ( ) {
46+ return snapshotComposerSend ( {
4347 prompt : "Unsent message" ,
4448 images : [ image ( "unsent" ) ] ,
4549 files : [ ] ,
@@ -49,15 +53,24 @@ function keepUnsentBesideFullDraft() {
4953 reviewComments : [ ] ,
5054 threadContexts : [ ] ,
5155 } ) ;
52- const display : ChatMessage = {
53- id : messageId ,
54- role : "user" ,
55- text : "Unsent message" ,
56- runId : null ,
57- createdAt : "2026-10-25T00:00:00.000Z" ,
58- updatedAt : "2026-10-25T00:00:00.000Z" ,
59- streaming : false ,
60- } ;
56+ }
57+
58+ const display : ChatMessage = {
59+ id : messageId ,
60+ role : "user" ,
61+ text : "Unsent message" ,
62+ runId : null ,
63+ createdAt : "2026-10-25T00:00:00.000Z" ,
64+ updatedAt : "2026-10-25T00:00:00.000Z" ,
65+ streaming : false ,
66+ } ;
67+
68+ function keepUnsentBesideFullDraft ( ) {
69+ useComposerDraftStore . setState ( { draftsByThreadKey : { } , draftThreadsByThreadKey : { } } ) ;
70+ const store = useComposerDraftStore . getState ( ) ;
71+ store . setPrompt ( target , "Newer draft" ) ;
72+ store . addImages ( target , newerImages ( ) ) ;
73+ const snapshot = unsentSnapshot ( ) ;
6174 const restore = ( ) => restoreComposerDraft ( target , snapshot ) ;
6275 expect ( restore ( ) ) . toBe ( false ) ;
6376 unsentMessages . keep ( { environmentId, threadId, display, restore } ) ;
@@ -88,3 +101,34 @@ it("drops a kept message only when it is discarded", () => {
88101 expect ( unsentMessages . getSnapshot ( ) ) . toEqual ( [ ] ) ;
89102 expect ( store . getComposerDraft ( target ) ?. prompt ) . toBe ( "Newer draft" ) ;
90103} ) ;
104+
105+ it ( "restores a cancelled launch into its thread after the newer draft there is sent" , ( ) => {
106+ useComposerDraftStore . setState ( { draftsByThreadKey : { } , draftThreadsByThreadKey : { } } ) ;
107+ const store = useComposerDraftStore . getState ( ) ;
108+ const draftId = DraftId . make ( "launch-draft" ) ;
109+ store . setProjectDraftThreadId ( scopeProjectRef ( environmentId , ProjectId . make ( "p" ) ) , draftId , {
110+ threadId : ThreadId . make ( "launched" ) ,
111+ } ) ;
112+ const launched = store . getDraftSession ( draftId ) ! ;
113+ markPromotedDraftThreadByRef ( scopeThreadRef ( environmentId , launched . threadId ) ) ;
114+ store . setPrompt ( draftId , "Newer draft" ) ;
115+ store . addImages ( draftId , newerImages ( ) ) ;
116+
117+ const reopenedRef = scopeThreadRef ( environmentId , ThreadId . make ( "reopened" ) ) ;
118+ const restoreTarget = reopenUnsentDraftThread ( draftId , launched , reopenedRef ) ;
119+ const snapshot = unsentSnapshot ( ) ;
120+ const restore = ( ) => restoreComposerDraft ( restoreTarget , snapshot ) ;
121+ expect ( restore ( ) ) . toBe ( false ) ;
122+ unsentMessages . keep ( { environmentId, threadId : reopenedRef . threadId , display, restore } ) ;
123+
124+ markPromotedDraftThreadByRef ( reopenedRef ) ;
125+ store . clearComposerContent ( draftId ) ;
126+ finalizePromotedDraftThreadByRef ( reopenedRef ) ;
127+ expect ( store . getDraftSession ( draftId ) ) . toBeNull ( ) ;
128+
129+ expect ( unsentMessages . restore ( environmentId , messageId ) ) . toBe ( true ) ;
130+ expect ( unsentMessages . getSnapshot ( ) ) . toEqual ( [ ] ) ;
131+ const draft = store . getComposerDraft ( reopenedRef ) ;
132+ expect ( draft ?. prompt ) . toBe ( "Unsent message" ) ;
133+ expect ( draft ?. images . map ( ( attachment ) => attachment . id ) ) . toEqual ( [ "unsent" ] ) ;
134+ } ) ;
0 commit comments