@@ -71,6 +71,19 @@ async function project(files: Record<string, string | Buffer>, options = {}) {
7171}
7272
7373/** Undoes the newest change still in effect, whoever made it; Cmd+Z steps only over the caller's own. */
74+ /** What 0.8.123 leaves: a log whose baseline never names the media ledger and carries no marker. */
75+ function asWrittenBy0_8_123 ( logFile : string ) {
76+ const lines = readFileSync ( logFile , "utf-8" )
77+ . trim ( )
78+ . split ( "\n" )
79+ . map ( ( line ) => JSON . parse ( line ) ) ;
80+ for ( const record of lines . filter ( ( line ) => line . type === "baseline" ) ) {
81+ delete record . files [ ".media/manifest.jsonl" ] ;
82+ delete record . keepsLedger ;
83+ }
84+ writeFileSync ( logFile , lines . map ( ( record ) => JSON . stringify ( record ) ) . join ( "\n" ) + "\n" ) ;
85+ }
86+
7487async function undoNewest ( history : ProjectHistory ) {
7588 const newest = [ ...history . list ( ) ] . reverse ( ) . find ( ( entry ) => ! entry . undoes && ! entry . undone ) ;
7689 return history . undo ( newest ! . id , { who : you } ) ;
@@ -201,6 +214,73 @@ describe("openProjectHistory", () => {
201214 expect ( reopened . next ( "forward" , you ) ) . toBeUndefined ( ) ;
202215 } ) ;
203216
217+ it ( "a log 0.8.123 wrote without the media ledger takes it in, so the first Undo undoes the edit" , async ( ) => {
218+ const { history, write, read, projectDir, historyRoot } = await project ( {
219+ "index.html" : "A" ,
220+ ".media/manifest.jsonl" : "{}\n" ,
221+ } ) ;
222+ await change ( history , you , "Your edit" , ( ) => write ( "index.html" , "B" ) ) ;
223+ await history . close ( ) ;
224+ asWrittenBy0_8_123 ( join ( historyRoot , history . projectId , "log.jsonl" ) ) ;
225+ const reopened = await open ( projectDir , historyRoot ) ;
226+ expect ( await reopened . step ( "back" , you ) ) . toMatchObject ( { ok : true } ) ;
227+ expect ( [ read ( "index.html" ) , read ( ".media/manifest.jsonl" ) ] ) . toEqual ( [ "A" , "{}\n" ] ) ;
228+ } ) ;
229+
230+ // Windows needs a privilege to create symlinks.
231+ it . skipIf ( process . platform === "win32" ) (
232+ "a log 0.8.123 wrote never takes in a ledger behind a link" ,
233+ async ( ) => {
234+ const { history, projectDir, historyRoot } = await project (
235+ { "index.html" : "A" } ,
236+ { quietMs : 30 } ,
237+ ) ;
238+ await history . close ( ) ;
239+ asWrittenBy0_8_123 ( join ( historyRoot , history . projectId , "log.jsonl" ) ) ;
240+ const outside = tempDir ( "hf-history-linked-" ) ;
241+ writeFileSync ( join ( outside , "manifest.jsonl" ) , "{}\n" ) ;
242+ symlinkSync ( outside , join ( projectDir , ".media" ) , "dir" ) ;
243+ const reopened = await open ( projectDir , historyRoot , { quietMs : 30 } ) ;
244+ await reopened . flush ( ) ;
245+ expect ( reopened . list ( ) ) . toEqual ( [ ] ) ;
246+ } ,
247+ ) ;
248+
249+ it ( "a ledger made while the project was closed is a change like any file, once a log keeps the ledger" , async ( ) => {
250+ const { history, write, read, has, projectDir, historyRoot } = await project (
251+ { "index.html" : "A" } ,
252+ { quietMs : 30 } ,
253+ ) ;
254+ await history . close ( ) ;
255+ write ( "index.html" , "B" ) ;
256+ write ( ".media/manifest.jsonl" , "{}\n" ) ;
257+ const reopened = await open ( projectDir , historyRoot , { quietMs : 30 } ) ;
258+ await reopened . flush ( ) ;
259+ const [ outside ] = reopened . list ( ) ;
260+ expect ( outside ! . files . map ( ( file ) => file . path ) . sort ( ) ) . toEqual ( [
261+ ".media/manifest.jsonl" ,
262+ "index.html" ,
263+ ] ) ;
264+ expect ( ( await reopened . undo ( outside ! . id , { who : you } ) ) . ok ) . toBe ( true ) ;
265+ expect ( [ read ( "index.html" ) , has ( ".media/manifest.jsonl" ) ] ) . toEqual ( [ "A" , false ] ) ;
266+ } ) ;
267+
268+ it ( "takes the media ledger back with the film when a change is undone" , async ( ) => {
269+ const { history, write, read } = await project ( {
270+ "index.html" : "<h1>Hello</h1>" ,
271+ ".media/manifest.jsonl" : "{}\n" ,
272+ } ) ;
273+ const cutout = await change ( history , agent , "Cut out the logo" , ( ) => {
274+ write ( "index.html" , "<h1>Hello</h1><img src='logo.png'>" ) ;
275+ write ( ".media/manifest.jsonl" , '{}\n{"id":"logo"}\n' ) ;
276+ write ( ".media/images/logo.png" , "png" ) ;
277+ write ( ".tools/manifest.jsonl" , "{}\n" ) ;
278+ } ) ;
279+ expect ( cutout . files . map ( ( file ) => file . path ) ) . toEqual ( [ ".media/manifest.jsonl" , "index.html" ] ) ;
280+ expect ( ( await history . undo ( cutout . id , { who : you } ) ) . ok ) . toBe ( true ) ;
281+ expect ( read ( ".media/manifest.jsonl" ) ) . toBe ( "{}\n" ) ;
282+ } ) ;
283+
204284 it ( "never files a hidden file name as a change, but keeps Studio's two manifests" , async ( ) => {
205285 const { history, write } = await project ( { "index.html" : "<h1>Hello</h1>" } , { quietMs : 30 } ) ;
206286 const turn = await history . beginWindow ( agent , "Agent turn" ) ;
0 commit comments