@@ -24,13 +24,44 @@ import { HistoryIdError } from "./historyId";
2424import { HistoryClosedError , openProjectHistory , type ProjectHistory } from "./projectHistory" ;
2525import { START , type HistoryWho } from "./historyLog" ;
2626
27+ // A test can hold a media copy, and run work after its blob is stored but before history records it.
28+ const mediaCopy = vi . hoisted ( ( ) => ( {
29+ held : null as Promise < void > | null ,
30+ stored : null as ( ( ) => Promise < unknown > ) | null ,
31+ copies : [ ] as string [ ] ,
32+ fails : null as ( ( copy : number ) => boolean ) | null ,
33+ } ) ) ;
34+ vi . mock ( "./blobStore" , async ( importOriginal ) => {
35+ const real = await importOriginal < typeof import ( "./blobStore" ) > ( ) ;
36+ return {
37+ ...real ,
38+ openBlobStore : async ( dir : string ) => {
39+ const store = await real . openBlobStore ( dir ) ;
40+ return {
41+ ...store ,
42+ put : async ( path : string ) => {
43+ if ( path . endsWith ( ".mp4" ) ) {
44+ await mediaCopy . held ;
45+ mediaCopy . copies . push ( basename ( path ) ) ;
46+ if ( mediaCopy . fails ?.( mediaCopy . copies . length ) ) throw new Error ( "The disk is full." ) ;
47+ }
48+ const hash = await store . put ( path ) ;
49+ if ( path . endsWith ( ".mp4" ) ) await mediaCopy . stored ?.( ) ;
50+ return hash ;
51+ } ,
52+ } ;
53+ } ,
54+ } ;
55+ } ) ;
56+
2757const you : HistoryWho = { kind : "person" , name : "You" } ;
2858const pause = ( ms : number ) => new Promise ( ( settle ) => setTimeout ( settle , ms ) ) ;
2959const agent : HistoryWho = { kind : "agent" , name : "Agent" } ;
3060const cleanup : Array < ( ) => unknown > = [ ] ;
3161
3262afterEach ( async ( ) => {
3363 for ( const step of cleanup . splice ( 0 ) . reverse ( ) ) await step ( ) ;
64+ Object . assign ( mediaCopy , { held : null , stored : null , copies : [ ] , fails : null } ) ;
3465} ) ;
3566
3667const inside = ( dir : string , path : string ) => readFileSync ( join ( dir , path ) , "utf-8" ) ;
@@ -265,6 +296,138 @@ describe("openProjectHistory", () => {
265296 expect ( [ read ( "index.html" ) , has ( ".media/manifest.jsonl" ) ] ) . toEqual ( [ "A" , false ] ) ;
266297 } ) ;
267298
299+ it ( "a ledger made while closed is still a change after the project's media was copied in the background" , async ( ) => {
300+ const { history, write, has, projectDir, historyRoot } = await project (
301+ { "index.html" : "A" , "clip.mp4" : Buffer . alloc ( 2 * 1024 ** 2 , 7 ) } ,
302+ { quietMs : 30 } ,
303+ ) ;
304+ await vi . waitFor ( ( ) => expect ( history . peek ( START ) ) . toHaveProperty ( [ "clip.mp4" ] ) , {
305+ timeout : 10_000 ,
306+ } ) ;
307+ await history . close ( ) ;
308+ write ( "b.png" , "png" ) ;
309+ write ( ".media/manifest.jsonl" , '{"path":"b.png"}\n' ) ;
310+ const reopened = await open ( projectDir , historyRoot , { quietMs : 30 } ) ;
311+ await reopened . flush ( ) ;
312+ const [ outside ] = reopened . list ( ) ;
313+ expect ( outside ! . files . map ( ( file ) => file . path ) . sort ( ) ) . toEqual ( [
314+ ".media/manifest.jsonl" ,
315+ "b.png" ,
316+ ] ) ;
317+ expect ( ( await reopened . undo ( outside ! . id , { who : you } ) ) . ok ) . toBe ( true ) ;
318+ expect ( [ has ( "b.png" ) , has ( ".media/manifest.jsonl" ) ] ) . toEqual ( [ false , false ] ) ;
319+ } ) ;
320+
321+ it ( "names media in adopting.json until its copy is recorded, so an exit mid-copy resumes it" , async ( ) => {
322+ let release = ( ) => { } ;
323+ mediaCopy . held = new Promise < void > ( ( resolve ) => ( release = resolve ) ) ;
324+ const { history, historyRoot } = await project ( {
325+ "index.html" : "A" ,
326+ "clip.mp4" : Buffer . alloc ( 2 * 1024 ** 2 , 7 ) ,
327+ } ) ;
328+ const adopting = join ( historyRoot , history . projectId , "adopting.json" ) ;
329+ expect ( JSON . parse ( readFileSync ( adopting , "utf-8" ) ) ) . toEqual ( [ "clip.mp4" ] ) ;
330+ release ( ) ;
331+ await vi . waitFor ( ( ) => expect ( history . peek ( START ) ) . toHaveProperty ( [ "clip.mp4" ] ) , {
332+ timeout : 10_000 ,
333+ } ) ;
334+ expect ( existsSync ( adopting ) ) . toBe ( false ) ;
335+ } ) ;
336+
337+ it ( "keeps media whose recording failed for the next open, rather than log it as added" , async ( ) => {
338+ const { history, write, historyRoot } = await project (
339+ { "index.html" : "A" , "clip.mp4" : Buffer . alloc ( 2 * 1024 ** 2 , 7 ) } ,
340+ { budgetBytes : 1 , quietMs : 30 } ,
341+ ) ;
342+ // The edit's prune takes the first copy; storing it again then fails.
343+ mediaCopy . fails = ( copy ) => copy === 2 ;
344+ mediaCopy . stored = ( ) => {
345+ mediaCopy . stored = null ;
346+ return change ( history , you , "Color" , ( ) => write ( "index.html" , "B" ) ) ;
347+ } ;
348+ await vi . waitFor ( ( ) => expect ( mediaCopy . copies ) . toHaveLength ( 2 ) , { timeout : 10_000 } ) ;
349+ await history . flush ( ) ;
350+ expect ( history . list ( ) . flatMap ( ( entry ) => entry . files . map ( ( file ) => file . path ) ) ) . not . toContain (
351+ "clip.mp4" ,
352+ ) ;
353+ const adopting = join ( historyRoot , history . projectId , "adopting.json" ) ;
354+ expect ( JSON . parse ( readFileSync ( adopting , "utf-8" ) ) ) . toEqual ( [ "clip.mp4" ] ) ;
355+ } ) ;
356+
357+ it ( "never copies the media of a folder that replaced the project" , async ( ) => {
358+ let release = ( ) => { } ;
359+ mediaCopy . held = new Promise < void > ( ( resolve ) => ( release = resolve ) ) ;
360+ const media = {
361+ "a.mp4" : Buffer . alloc ( 2 * 1024 ** 2 , 1 ) ,
362+ "b.mp4" : Buffer . alloc ( 2 * 1024 ** 2 , 2 ) ,
363+ } ;
364+ const { history, write, projectDir } = await project ( { "index.html" : "A" , ...media } ) ;
365+ renameSync ( projectDir , `${ projectDir } -moved` ) ;
366+ cleanup . push ( ( ) => rmSync ( `${ projectDir } -moved` , { recursive : true , force : true } ) ) ;
367+ mkdirSync ( projectDir ) ;
368+ for ( const [ path , bytes ] of Object . entries ( media ) ) write ( path , bytes ) ;
369+ release ( ) ;
370+ await history . close ( ) ;
371+ // Only the copy already running when the folder changed may finish.
372+ expect ( mediaCopy . copies ) . toEqual ( [ "a.mp4" ] ) ;
373+ } ) ;
374+
375+ it ( "undoes a clip replaced while closed, though the history closed before its copy finished" , async ( ) => {
376+ const clip = Buffer . alloc ( 2 * 1024 ** 2 , 7 ) ;
377+ let release = ( ) => { } ;
378+ mediaCopy . held = new Promise < void > ( ( resolve ) => ( release = resolve ) ) ;
379+ const { history, write, projectDir, historyRoot } = await project (
380+ { "index.html" : "A" , "clip.mp4" : clip } ,
381+ { quietMs : 30 } ,
382+ ) ;
383+ const closed = history . close ( ) ;
384+ release ( ) ;
385+ await closed ;
386+ write ( "clip.mp4" , Buffer . alloc ( 2 * 1024 ** 2 , 9 ) ) ;
387+ const reopened = await open ( projectDir , historyRoot , { quietMs : 30 } ) ;
388+ await reopened . flush ( ) ;
389+ expect ( reopened . list ( ) . map ( ( entry ) => entry . files . map ( ( file ) => file . path ) ) ) . toEqual ( [
390+ [ "clip.mp4" ] ,
391+ ] ) ;
392+ expect ( await reopened . step ( "back" , you ) ) . toMatchObject ( { ok : true } ) ;
393+ expect ( readFileSync ( join ( projectDir , "clip.mp4" ) ) . equals ( clip ) ) . toBe ( true ) ;
394+ } ) ;
395+
396+ it ( "adopts media whose copy a process exit cut short, rather than log it as added" , async ( ) => {
397+ const { history, write, projectDir, historyRoot } = await project ( { "index.html" : "A" } ) ;
398+ await history . close ( ) ;
399+ // What an exit mid-copy leaves: the clip on disk, named as still being copied, not in the baseline.
400+ write ( "clip.mp4" , Buffer . alloc ( 2 * 1024 ** 2 , 7 ) ) ;
401+ writeFileSync ( join ( historyRoot , history . projectId , "adopting.json" ) , '["clip.mp4"]' ) ;
402+ const reopened = await open ( projectDir , historyRoot , { quietMs : 30 } ) ;
403+ await vi . waitFor ( ( ) => expect ( reopened . peek ( START ) ) . toHaveProperty ( [ "clip.mp4" ] ) , {
404+ timeout : 10_000 ,
405+ } ) ;
406+ await reopened . flush ( ) ;
407+ expect ( reopened . list ( ) ) . toEqual ( [ ] ) ;
408+ } ) ;
409+
410+ it ( "keeps a media copy that a budget prune ran into before history recorded it" , async ( ) => {
411+ const clip = Buffer . alloc ( 2 * 1024 ** 2 , 7 ) ;
412+ let release = ( ) => { } ;
413+ mediaCopy . held = new Promise < void > ( ( resolve ) => ( release = resolve ) ) ;
414+ const { history, write } = await project (
415+ { "index.html" : "A" , "clip.mp4" : clip } ,
416+ { budgetBytes : 1 , quietMs : 30 } ,
417+ ) ;
418+ // The copy is stored; an edit commits and prunes before history records the copy.
419+ mediaCopy . stored = ( ) => {
420+ mediaCopy . stored = null ;
421+ return change ( history , you , "Color" , ( ) => write ( "index.html" , "B" ) ) ;
422+ } ;
423+ release ( ) ;
424+ await vi . waitFor ( ( ) => expect ( history . peek ( START ) ) . toHaveProperty ( [ "clip.mp4" ] ) , {
425+ timeout : 10_000 ,
426+ } ) ;
427+ const start = history . peek ( START ) [ "clip.mp4" ] ! ;
428+ expect ( ( await history . readBlob ( start ) ) . equals ( clip ) ) . toBe ( true ) ;
429+ } ) ;
430+
268431 it ( "a log 0.8.123 wrote without a ledger takes none in, and one made later while closed is a change" , async ( ) => {
269432 const { history, write, projectDir, historyRoot } = await project (
270433 { "index.html" : "A" } ,
0 commit comments