@@ -3,6 +3,7 @@ import { act } from "react";
33import { createRoot } from "react-dom/client" ;
44import { afterEach , beforeEach , describe , expect , it , vi } from "vitest" ;
55import { useEditorSave , type EditorSaveHandle } from "./useEditorSave" ;
6+ import { StudioFileConflictError } from "../utils/studioSaveDiagnostics" ;
67
78( globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT : boolean } ) . IS_REACT_ACT_ENVIRONMENT = true ;
89
@@ -96,4 +97,72 @@ describe("useEditorSave pending work", () => {
9697 expect ( writeProjectFile ) . toHaveBeenCalledOnce ( ) ;
9798 await act ( async ( ) => root . unmount ( ) ) ;
9899 } ) ;
100+
101+ it ( "preserves conflict details when flushing a buffered source candidate" , async ( ) => {
102+ const conflict = new StudioFileConflictError ( {
103+ filePath : "index.html" ,
104+ currentVersion : "external-v2" ,
105+ currentContent : "external" ,
106+ attemptedContent : "studio candidate" ,
107+ } ) ;
108+ const captured : { handle : EditorSaveHandle | null } = { handle : null } ;
109+
110+ function Probe ( ) {
111+ captured . handle = useEditorSave ( {
112+ editingPathRef : { current : "index.html" } ,
113+ projectIdRef : { current : "project-a" } ,
114+ readProjectFile : vi . fn ( async ( ) => "before" ) ,
115+ writeProjectFile : vi . fn ( async ( ) => {
116+ throw conflict ;
117+ } ) ,
118+ recordEdit : vi . fn ( async ( ) => undefined ) ,
119+ domEditSaveTimestampRef : { current : 0 } ,
120+ setRefreshKey : vi . fn ( ) ,
121+ showToast : vi . fn ( ) ,
122+ } ) ;
123+ return null ;
124+ }
125+
126+ const root = createRoot ( document . createElement ( "div" ) ) ;
127+ await act ( async ( ) => root . render ( < Probe /> ) ) ;
128+ act ( ( ) => captured . handle ?. handleContentChange ( "studio candidate" ) ) ;
129+
130+ await expect ( captured . handle ?. flushPendingSave ( ) ) . resolves . toEqual ( {
131+ status : "conflict" ,
132+ error : conflict ,
133+ } ) ;
134+
135+ await act ( async ( ) => root . unmount ( ) ) ;
136+ } ) ;
137+
138+ it ( "discards an rAF-buffered candidate without persisting it" , async ( ) => {
139+ const writeProjectFile = vi . fn ( async ( ) => undefined ) ;
140+ const captured : { handle : EditorSaveHandle | null } = { handle : null } ;
141+
142+ function Probe ( ) {
143+ captured . handle = useEditorSave ( {
144+ editingPathRef : { current : "index.html" } ,
145+ projectIdRef : { current : "project-a" } ,
146+ readProjectFile : vi . fn ( async ( ) => "before" ) ,
147+ writeProjectFile,
148+ recordEdit : vi . fn ( async ( ) => undefined ) ,
149+ domEditSaveTimestampRef : { current : 0 } ,
150+ setRefreshKey : vi . fn ( ) ,
151+ showToast : vi . fn ( ) ,
152+ } ) ;
153+ return null ;
154+ }
155+
156+ const root = createRoot ( document . createElement ( "div" ) ) ;
157+ await act ( async ( ) => root . render ( < Probe /> ) ) ;
158+ act ( ( ) => captured . handle ?. handleContentChange ( "discard me" ) ) ;
159+ act ( ( ) => captured . handle ?. discardPendingSave ( ) ) ;
160+
161+ expect ( captured . handle ?. getPendingCandidate ( ) ) . toBeNull ( ) ;
162+ await expect ( captured . handle ?. flushPendingSave ( ) ) . resolves . toEqual ( { status : "clean" } ) ;
163+ expect ( writeProjectFile ) . not . toHaveBeenCalled ( ) ;
164+ expect ( cancelAnimationFrame ) . toHaveBeenCalledWith ( 41 ) ;
165+
166+ await act ( async ( ) => root . unmount ( ) ) ;
167+ } ) ;
99168} ) ;
0 commit comments