@@ -65,21 +65,33 @@ function createAdapter(projectDir: string): StudioApiAdapter {
6565 } ;
6666}
6767
68- function postElementPatchBatch ( app : Hono , file : string , patches : unknown [ ] ) : Promise < Response > {
68+ function postElementPatchBatch (
69+ app : Hono ,
70+ file : string ,
71+ patches : unknown [ ] ,
72+ writeToken ?: string ,
73+ ) : Promise < Response > {
6974 return app . request ( `http://localhost/projects/demo/file-mutations/patch-elements-batch/${ file } ` , {
7075 method : "POST" ,
71- headers : { "Content-Type" : "application/json" } ,
76+ headers : {
77+ "Content-Type" : "application/json" ,
78+ ...( writeToken ? { "X-Hyperframes-Write-Token" : writeToken } : { } ) ,
79+ } ,
7280 body : JSON . stringify ( { patches } ) ,
7381 } ) ;
7482}
7583
7684function postElementPatchBatches (
7785 app : Hono ,
7886 batches : Array < { sourceFile : string ; patches : unknown [ ] } > ,
87+ writeToken ?: string ,
7988) : Promise < Response > {
8089 return app . request ( "http://localhost/projects/demo/file-mutations/patch-element-batches" , {
8190 method : "POST" ,
82- headers : { "Content-Type" : "application/json" } ,
91+ headers : {
92+ "Content-Type" : "application/json" ,
93+ ...( writeToken ? { "X-Hyperframes-Write-Token" : writeToken } : { } ) ,
94+ } ,
8395 body : JSON . stringify ( { batches } ) ,
8496 } ) ;
8597}
@@ -134,7 +146,7 @@ describe("registerFileRoutes", () => {
134146 expect ( result . after ) . toContain ( 'data-duration="7"' ) ;
135147 expect ( result . after ) . toContain ( `id="${ result . hostId } "` ) ;
136148 expect ( result . version ) . toBe ( fileContentVersion ( result . after ) ) ;
137- expect ( consumeFileWriteReceipt ( join ( projectDir , "index.html" ) ) ) . toEqual ( {
149+ expect ( consumeFileWriteReceipt ( join ( projectDir , "index.html" ) , result . version ) ) . toEqual ( {
138150 path : "index.html" ,
139151 version : result . version ,
140152 writeToken : "studio-insert-1" ,
@@ -374,7 +386,7 @@ describe("registerFileRoutes", () => {
374386 expect ( payload . version ) . toBe ( fileContentVersion ( "after" ) ) ;
375387 expect ( payload . writeToken ) . toBe ( "studio-write-1" ) ;
376388 expect ( response . headers . get ( "etag" ) ) . toBe ( payload . version ) ;
377- expect ( consumeFileWriteReceipt ( join ( projectDir , "index.html" ) ) ) . toEqual ( {
389+ expect ( consumeFileWriteReceipt ( join ( projectDir , "index.html" ) , payload . version ! ) ) . toEqual ( {
378390 path : "index.html" ,
379391 version : payload . version ,
380392 writeToken : "studio-write-1" ,
@@ -458,9 +470,10 @@ describe("registerFileRoutes", () => {
458470 ) ;
459471
460472 expect ( response . status ) . toBe ( 200 ) ;
461- expect ( consumeFileWriteReceipt ( join ( projectDir , "index.html" ) ) ) . toEqual ( {
473+ const version = fileContentVersion ( readFileSync ( join ( projectDir , "index.html" ) , "utf-8" ) ) ;
474+ expect ( consumeFileWriteReceipt ( join ( projectDir , "index.html" ) , version ) ) . toEqual ( {
462475 path : "index.html" ,
463- version : fileContentVersion ( readFileSync ( join ( projectDir , "index.html" ) , "utf-8" ) ) ,
476+ version,
464477 writeToken : "studio-patch-1" ,
465478 } ) ;
466479 } ) ;
@@ -473,16 +486,21 @@ describe("registerFileRoutes", () => {
473486 const app = new Hono ( ) ;
474487 registerFileRoutes ( app , createAdapter ( projectDir ) ) ;
475488
476- const response = await postElementPatchBatch ( app , "index.html" , [
477- {
478- target : { id : "back" } ,
479- operations : [ { type : "inline-style" , property : "z-index" , value : "2" } ] ,
480- } ,
481- {
482- target : { id : "front" } ,
483- operations : [ { type : "inline-style" , property : "z-index" , value : "1" } ] ,
484- } ,
485- ] ) ;
489+ const response = await postElementPatchBatch (
490+ app ,
491+ "index.html" ,
492+ [
493+ {
494+ target : { id : "back" } ,
495+ operations : [ { type : "inline-style" , property : "z-index" , value : "2" } ] ,
496+ } ,
497+ {
498+ target : { id : "front" } ,
499+ operations : [ { type : "inline-style" , property : "z-index" , value : "1" } ] ,
500+ } ,
501+ ] ,
502+ "studio-layer-order-1" ,
503+ ) ;
486504 expect ( response . status ) . toBe ( 200 ) ;
487505 const payload = ( await response . json ( ) ) as {
488506 changed ?: boolean ;
@@ -498,6 +516,12 @@ describe("registerFileRoutes", () => {
498516 expect ( payload . content ) . toContain ( 'id="back" style="z-index: 2"' ) ;
499517 expect ( payload . content ) . toContain ( 'id="front" style="z-index: 1"' ) ;
500518 expect ( readFileSync ( join ( projectDir , payload . backupPath ! ) , "utf-8" ) ) . toBe ( original ) ;
519+ const version = fileContentVersion ( payload . content ! ) ;
520+ expect ( consumeFileWriteReceipt ( join ( projectDir , "index.html" ) , version ) ) . toEqual ( {
521+ path : "index.html" ,
522+ version,
523+ writeToken : "studio-layer-order-1" ,
524+ } ) ;
501525 expect ( readdirSync ( join ( projectDir , ".hyperframes" , "backup" ) ) ) . toHaveLength ( 1 ) ;
502526 } ) ;
503527
@@ -559,6 +583,52 @@ describe("registerFileRoutes", () => {
559583 expect ( existsSync ( join ( projectDir , ".hyperframes" , "backup" ) ) ) . toBe ( false ) ;
560584 } ) ;
561585
586+ it ( "leaves one exact write receipt for every file in a durable element patch batch" , async ( ) => {
587+ const projectDir = createProjectDir ( ) ;
588+ writeFileSync ( join ( projectDir , "index.html" ) , '<div id="index">Before</div>' ) ;
589+ writeFileSync ( join ( projectDir , "scene.html" ) , '<div id="scene">Before</div>' ) ;
590+ const app = new Hono ( ) ;
591+ registerFileRoutes ( app , createAdapter ( projectDir ) ) ;
592+
593+ const response = await postElementPatchBatches (
594+ app ,
595+ [
596+ {
597+ sourceFile : "index.html" ,
598+ patches : [
599+ {
600+ target : { id : "index" } ,
601+ operations : [ { type : "text-content" , property : "textContent" , value : "After" } ] ,
602+ } ,
603+ ] ,
604+ } ,
605+ {
606+ sourceFile : "scene.html" ,
607+ patches : [
608+ {
609+ target : { id : "scene" } ,
610+ operations : [ { type : "text-content" , property : "textContent" , value : "After" } ] ,
611+ } ,
612+ ] ,
613+ } ,
614+ ] ,
615+ "studio-group-drag-1" ,
616+ ) ;
617+ const payload = ( await response . json ( ) ) as {
618+ files : Array < { sourceFile : string ; after : string } > ;
619+ } ;
620+
621+ expect ( response . status ) . toBe ( 200 ) ;
622+ for ( const file of payload . files ) {
623+ const version = fileContentVersion ( file . after ) ;
624+ expect ( consumeFileWriteReceipt ( join ( projectDir , file . sourceFile ) , version ) ) . toEqual ( {
625+ path : file . sourceFile ,
626+ version,
627+ writeToken : "studio-group-drag-1" ,
628+ } ) ;
629+ }
630+ } ) ;
631+
562632 it ( "refuses every file when one batch contains an unmatched target" , async ( ) => {
563633 const projectDir = createProjectDir ( ) ;
564634 const indexOriginal = '<div id="present" style="z-index: 1">Present</div>' ;
@@ -772,7 +842,9 @@ describe("registerFileRoutes", () => {
772842 expect ( payload . files [ 0 ] . after ) . toContain ( 'id="a-split"' ) ;
773843 expect ( payload . files [ 0 ] . after ) . toContain ( 'id="b-split"' ) ;
774844 expect ( readFileSync ( join ( projectDir , "index.html" ) , "utf-8" ) ) . toBe ( payload . files [ 0 ] . after ) ;
775- expect ( consumeFileWriteReceipt ( join ( projectDir , "index.html" ) ) ) . toEqual ( {
845+ expect (
846+ consumeFileWriteReceipt ( join ( projectDir , "index.html" ) , payload . files [ 0 ] . version ) ,
847+ ) . toEqual ( {
776848 path : "index.html" ,
777849 version : payload . files [ 0 ] . version ,
778850 writeToken : "cut-test" ,
0 commit comments