11import { EnvironmentId , ProjectId , ThreadId } from "@t3tools/contracts" ;
22import { scopeProjectRef , scopeThreadRef } from "@t3tools/client-runtime/environment" ;
3- import { afterEach , beforeEach , describe , expect , it , vi } from "vite-plus/test" ;
3+ import { beforeEach , describe , expect , it , vi } from "vite-plus/test" ;
44
5- import { toastManager } from "../components/ui/toast" ;
6- import { DraftId , useComposerDraftStore } from "../composerDraftStore" ;
7- import { useThreadUndoNotice } from "../hooks/showThreadUndoNotice" ;
8- import { releaseDraftAttachments } from "./attachmentUploadQueue" ;
5+ import {
6+ type ComposerImageAttachment ,
7+ DraftId ,
8+ useComposerDraftStore ,
9+ } from "../composerDraftStore" ;
10+ import { showThreadUndoNotice } from "../hooks/showThreadUndoNotice" ;
911import { discardComposerDraft } from "./discardComposerDraft" ;
1012
1113vi . mock ( "./attachmentUploadQueue" , ( ) => ( { releaseDraftAttachments : vi . fn ( ) } ) ) ;
14+ vi . mock ( "../hooks/showThreadUndoNotice" , ( ) => ( { showThreadUndoNotice : vi . fn ( ) } ) ) ;
1215
1316const environmentId = EnvironmentId . make ( "environment-local" ) ;
1417const projectRef = scopeProjectRef ( environmentId , ProjectId . make ( "project-1" ) ) ;
1518const draftId = DraftId . make ( "draft-1" ) ;
1619const threadRef = scopeThreadRef ( environmentId , ThreadId . make ( "thread-1" ) ) ;
1720
1821function undoNotice ( ) {
19- const notice = useThreadUndoNotice . getState ( ) . notice ;
20- if ( ! notice ) throw new Error ( "Undo notice is missing" ) ;
21- return notice ;
22+ const options = vi . mocked ( showThreadUndoNotice ) . mock . calls . at ( - 1 ) ?. [ 0 ] ;
23+ if ( ! options ) throw new Error ( "Undo notice is missing" ) ;
24+ return options ;
25+ }
26+
27+ function imagePreview ( name = "image.png" ) {
28+ const file = new File ( [ "image bytes" ] , name , { type : "image/png" } ) ;
29+ const previewUrl = URL . createObjectURL ( file ) ;
30+ return {
31+ image : {
32+ type : "image" ,
33+ id : name ,
34+ name,
35+ mimeType : file . type ,
36+ sizeBytes : file . size ,
37+ file,
38+ previewUrl,
39+ } satisfies ComposerImageAttachment ,
40+ [ Symbol . dispose ] ( ) {
41+ URL . revokeObjectURL ( previewUrl ) ;
42+ } ,
43+ } ;
2244}
2345
2446beforeEach ( ( ) => {
25- vi . useFakeTimers ( ) ;
47+ vi . clearAllMocks ( ) ;
2648 useComposerDraftStore . setState ( {
2749 draftsByThreadKey : { } ,
2850 draftThreadsByThreadKey : { } ,
2951 logicalProjectDraftThreadKeyByLogicalProjectKey : { } ,
3052 } ) ;
3153} ) ;
32- afterEach ( ( ) => {
33- vi . runAllTimers ( ) ;
34- vi . useRealTimers ( ) ;
35- vi . clearAllMocks ( ) ;
36- vi . restoreAllMocks ( ) ;
37- } ) ;
3854
3955describe ( "discardComposerDraft" , ( ) => {
40- it ( "restores a discarded new-thread draft with its project mapping" , async ( ) => {
56+ it ( "restores a discarded new-thread draft with its project mapping and a usable preview" , async ( ) => {
57+ using preview = imagePreview ( ) ;
4158 const store = useComposerDraftStore . getState ( ) ;
4259 store . setProjectDraftThreadId ( projectRef , draftId ) ;
4360 store . setPrompt ( draftId , "half-written prompt" ) ;
61+ store . addImage ( draftId , preview . image ) ;
4462 const before = useComposerDraftStore . getState ( ) ;
4563
4664 discardComposerDraft ( draftId ) ;
4765 expect ( useComposerDraftStore . getState ( ) . getDraftSession ( draftId ) ) . toBeNull ( ) ;
48- expect ( undoNotice ( ) ) . toMatchObject ( { action : "Discarded" , count : 1 } ) ;
66+ await expect ( fetch ( preview . image . previewUrl ) ) . rejects . toThrow ( ) ;
4967
5068 await undoNotice ( ) . undo ( ) ;
5169 const after = useComposerDraftStore . getState ( ) ;
@@ -54,21 +72,50 @@ describe("discardComposerDraft", () => {
5472 expect ( after . logicalProjectDraftThreadKeyByLogicalProjectKey ) . toEqual (
5573 before . logicalProjectDraftThreadKeyByLogicalProjectKey ,
5674 ) ;
57- vi . runAllTimers ( ) ;
58- expect ( releaseDraftAttachments ) . not . toHaveBeenCalled ( ) ;
75+ const restoredUrl = after . getComposerDraft ( draftId ) ! . images [ 0 ] ! . previewUrl ;
76+ try {
77+ expect ( await ( await fetch ( restoredUrl ) ) . text ( ) ) . toBe ( "image bytes" ) ;
78+ } finally {
79+ URL . revokeObjectURL ( restoredUrl ) ;
80+ }
81+ } ) ;
82+
83+ it ( "releases a discarded existing-thread preview when Undo is committed" , async ( ) => {
84+ using preview = imagePreview ( ) ;
85+ useComposerDraftStore . getState ( ) . addImage ( threadRef , preview . image ) ;
86+
87+ discardComposerDraft ( threadRef ) ;
88+ expect ( useComposerDraftStore . getState ( ) . getComposerDraft ( threadRef ) ?. images ?? [ ] ) . toEqual ( [ ] ) ;
89+ expect ( await ( await fetch ( preview . image . previewUrl ) ) . text ( ) ) . toBe ( "image bytes" ) ;
90+
91+ undoNotice ( ) . commit ?.( ) ;
92+ await expect ( fetch ( preview . image . previewUrl ) ) . rejects . toThrow ( ) ;
5993 } ) ;
6094
61- it ( "keeps text typed after the discard and releases the old uploads" , async ( ) => {
62- useComposerDraftStore . getState ( ) . setPrompt ( threadRef , "old reply" ) ;
95+ it ( "keeps newer draft content and releases the discarded preview when Undo is refused" , async ( ) => {
96+ using oldPreview = imagePreview ( "old.png" ) ;
97+ using newPreview = imagePreview ( "new.png" ) ;
98+ useComposerDraftStore . getState ( ) . addImage ( threadRef , oldPreview . image ) ;
6399 discardComposerDraft ( threadRef ) ;
64100 useComposerDraftStore . getState ( ) . setPrompt ( threadRef , "new reply" ) ;
65- const addToast = vi . spyOn ( toastManager , "add" ) . mockReturnValue ( "error-toast" ) ;
101+ useComposerDraftStore . getState ( ) . addImage ( threadRef , newPreview . image ) ;
66102
67- await undoNotice ( ) . undo ( ) ;
103+ const result = await undoNotice ( ) . undo ( ) ;
104+ expect ( result . _tag ) . toBe ( "Failure" ) ;
68105 expect ( useComposerDraftStore . getState ( ) . getComposerDraft ( threadRef ) ?. prompt ) . toBe ( "new reply" ) ;
69- expect ( releaseDraftAttachments ) . toHaveBeenCalledOnce ( ) ;
70- expect ( addToast ) . toHaveBeenCalledWith (
71- expect . objectContaining ( { title : "Failed to restore draft" } ) ,
72- ) ;
106+ await expect ( fetch ( oldPreview . image . previewUrl ) ) . rejects . toThrow ( ) ;
107+ expect ( await ( await fetch ( newPreview . image . previewUrl ) ) . text ( ) ) . toBe ( "image bytes" ) ;
108+ } ) ;
109+
110+ it ( "keeps an existing-thread preview usable after successful Undo" , async ( ) => {
111+ using preview = imagePreview ( ) ;
112+ useComposerDraftStore . getState ( ) . addImage ( threadRef , preview . image ) ;
113+ discardComposerDraft ( threadRef ) ;
114+
115+ const result = await undoNotice ( ) . undo ( ) ;
116+ expect ( result . _tag ) . toBe ( "Success" ) ;
117+ const restored = useComposerDraftStore . getState ( ) . getComposerDraft ( threadRef ) ! . images [ 0 ] ! ;
118+ expect ( restored . previewUrl ) . toBe ( preview . image . previewUrl ) ;
119+ expect ( await ( await fetch ( restored . previewUrl ) ) . text ( ) ) . toBe ( "image bytes" ) ;
73120 } ) ;
74121} ) ;
0 commit comments