@@ -11,10 +11,13 @@ import {
1111import { composerDraftEnvironmentId } from "../lib/composerAttachmentUploadQueue" ;
1212import { isQueuedEditDraftKey , queuedEditDraftKey } from "./queued-edit-draft-key" ;
1313import {
14- queuedRunEditIsDirty ,
14+ beginQueuedRunEdit ,
15+ getQueuedRunEdit ,
16+ recoverQueuedRunEdit ,
1517 resolveQueuedEditPayload ,
1618 type QueuedRunEdit ,
1719} from "./queued-run-edit" ;
20+ import { getComposerDraftSnapshot , setComposerDraftText } from "./use-composer-drafts" ;
1821
1922const image = ( id : string ) : ChatAttachment => ( {
2023 type : "image" ,
@@ -110,13 +113,36 @@ describe("resolveQueuedEditPayload", () => {
110113 } ) ;
111114} ) ;
112115
113- describe ( "queuedRunEditIsDirty" , ( ) => {
114- it ( "treats an untouched edit as clean so a steered message is not restored" , ( ) => {
115- expect ( queuedRunEditIsDirty ( edit ( [ ] ) , { text : "original" , attachments : [ ] } ) ) . toBe ( false ) ;
116+ describe ( "recoverQueuedRunEdit" , ( ) => {
117+ const threadKey = "env:thread-recover" ;
118+ const runId = RunId . make ( "run-1" ) ;
119+ const editKey = queuedEditDraftKey ( threadKey , runId ) ;
120+
121+ it ( "drops an untouched edit without restoring the sent message" , ( ) => {
122+ beginQueuedRunEdit ( threadKey , edit ( [ ] ) ) ;
123+
124+ expect ( recoverQueuedRunEdit ( threadKey , runId ) ) . toBe ( "clean" ) ;
125+ expect ( getQueuedRunEdit ( threadKey ) ) . toBeNull ( ) ;
126+ expect ( getComposerDraftSnapshot ( threadKey ) . text ) . toBe ( "" ) ;
127+ expect ( getComposerDraftSnapshot ( editKey ) . text ) . toBe ( "" ) ;
128+ } ) ;
129+
130+ it ( "discards a changed edit when the thread draft already has content" , ( ) => {
131+ setComposerDraftText ( threadKey , "unsent draft" ) ;
132+ beginQueuedRunEdit ( threadKey , edit ( [ ] ) ) ;
133+ setComposerDraftText ( editKey , "revised" ) ;
134+
135+ expect ( recoverQueuedRunEdit ( threadKey , runId ) ) . toBe ( "discarded" ) ;
136+ expect ( getQueuedRunEdit ( threadKey ) ) . toBeNull ( ) ;
137+ expect ( getComposerDraftSnapshot ( threadKey ) . text ) . toBe ( "unsent draft" ) ;
138+ setComposerDraftText ( threadKey , "" ) ;
116139 } ) ;
117140
118- it ( "treats changed text or a new attachment as an edit worth keeping" , ( ) => {
119- expect ( queuedRunEditIsDirty ( edit ( [ ] ) , { text : "revised" , attachments : [ ] } ) ) . toBe ( true ) ;
120- expect ( queuedRunEditIsDirty ( edit ( [ ] ) , { text : "original" , attachments : [ { } ] } ) ) . toBe ( true ) ;
141+ it ( "ignores a run the composer is no longer editing" , ( ) => {
142+ beginQueuedRunEdit ( threadKey , edit ( [ ] ) ) ;
143+
144+ expect ( recoverQueuedRunEdit ( threadKey , RunId . make ( "run-2" ) ) ) . toBeNull ( ) ;
145+ expect ( getQueuedRunEdit ( threadKey ) ?. runId ) . toBe ( runId ) ;
146+ expect ( recoverQueuedRunEdit ( threadKey , runId ) ) . toBe ( "clean" ) ;
121147 } ) ;
122148} ) ;
0 commit comments