@@ -41,6 +41,7 @@ describe("freeze-frame route", () => {
4141 const calls : string [ ] [ ] = [ ] ;
4242 const { dir, post } = setup ( async ( args ) => {
4343 calls . push ( args ) ;
44+ writeFileSync ( args . at ( - 1 ) ?? "" , "png" ) ;
4445 return { ok : true } ;
4546 } ) ;
4647 const res = await post ( {
@@ -49,11 +50,12 @@ describe("freeze-frame route", () => {
4950 target : { id : "talk" } ,
5051 playhead : 2.5 ,
5152 } ) ;
52- const body : { before ?: string ; after ?: string ; imageSrc ?: string } = await res . json ( ) ;
53+ const body : { before ?: string ; after ?: string ; imageSrc ?: string ; stillPath ?: string } =
54+ await res . json ( ) ;
5355 expect ( res . status ) . toBe ( 200 ) ;
5456 const output = calls [ 0 ] ?. at ( - 1 ) ?? "" ;
5557 expect ( calls [ 0 ] ) . toEqual ( [
56- "-n " ,
58+ "-y " ,
5759 "-ss" ,
5860 "2.5" ,
5961 "-i" ,
@@ -65,6 +67,7 @@ describe("freeze-frame route", () => {
6567 expect ( dirname ( output ) ) . toBe ( join ( dir , "assets/freeze" ) ) ;
6668 expect ( basename ( output ) ) . toMatch ( / ^ t a l k - [ 0 - 9 a - f ] { 10 } - 2 5 0 0 - [ 0 - 9 a - f ] { 8 } \. p n g $ / ) ;
6769 expect ( body . imageSrc ) . toBe ( `assets/freeze/${ basename ( output ) } ` ) ;
70+ expect ( body . stillPath ) . toBe ( `assets/freeze/${ basename ( output ) } ` ) ;
6871 expect ( body . before ) . toBe ( html ) ;
6972 expect ( readFileSync ( join ( dir , "index.html" ) , "utf-8" ) ) . toBe ( body . after ) ;
7073 expect ( body . after ) . toContain ( 'id="talk-freeze"' ) ;
@@ -86,8 +89,13 @@ describe("freeze-frame route", () => {
8689 expect ( calls ) . toEqual ( [ ] ) ;
8790 } ) ;
8891
89- it ( "leaves the file untouched when extraction fails" , async ( ) => {
90- const { dir, post } = setup ( async ( ) => ( { ok : false , error : "boom" } ) ) ;
92+ it ( "leaves the file untouched, and no partial still, when extraction fails" , async ( ) => {
93+ let still = "" ;
94+ const { dir, post } = setup ( async ( args ) => {
95+ still = args . at ( - 1 ) ?? "" ;
96+ writeFileSync ( still , "partial" ) ;
97+ return { ok : false , error : "boom" } ;
98+ } ) ;
9199 const res = await post ( {
92100 path : "index.html" ,
93101 expectedVersion : fileContentVersion ( html ) ,
@@ -96,6 +104,45 @@ describe("freeze-frame route", () => {
96104 } ) ;
97105 expect ( res . status ) . toBe ( 500 ) ;
98106 expect ( readFileSync ( join ( dir , "index.html" ) , "utf-8" ) ) . toBe ( html ) ;
107+ expect ( still ) . not . toBe ( "" ) ;
108+ expect ( existsSync ( still ) ) . toBe ( false ) ;
109+ } ) ;
110+
111+ it ( "refuses, leaving nothing behind, when ffmpeg succeeds without writing a frame" , async ( ) => {
112+ let still = "" ;
113+ const { dir, post } = setup ( async ( args ) => {
114+ still = args . at ( - 1 ) ?? "" ;
115+ return { ok : true } ;
116+ } ) ;
117+ const res = await post ( {
118+ path : "index.html" ,
119+ expectedVersion : fileContentVersion ( html ) ,
120+ target : { id : "talk" } ,
121+ playhead : 2.5 ,
122+ } ) ;
123+ expect ( res . status ) . toBe ( 500 ) ;
124+ expect ( readFileSync ( join ( dir , "index.html" ) , "utf-8" ) ) . toBe ( html ) ;
125+ expect ( still ) . not . toBe ( "" ) ;
126+ expect ( existsSync ( still ) ) . toBe ( false ) ;
127+ } ) ;
128+
129+ it ( "removes the still it extracted when the page changed before the write" , async ( ) => {
130+ let still = "" ;
131+ const { dir, post } = setup ( async ( args ) => {
132+ still = args . at ( - 1 ) ?? "" ;
133+ writeFileSync ( still , "png" ) ;
134+ writeFileSync ( join ( dir , "index.html" ) , `${ html } \n<!-- edited meanwhile -->` ) ;
135+ return { ok : true } ;
136+ } ) ;
137+ const res = await post ( {
138+ path : "index.html" ,
139+ expectedVersion : fileContentVersion ( html ) ,
140+ target : { id : "talk" } ,
141+ playhead : 2.5 ,
142+ } ) ;
143+ expect ( res . status ) . toBe ( 409 ) ;
144+ expect ( still ) . not . toBe ( "" ) ;
145+ expect ( existsSync ( still ) ) . toBe ( false ) ;
99146 } ) ;
100147
101148 it ( "keeps a traversal clip id inside assets/freeze, for the ffmpeg output and the still's src" , async ( ) => {
@@ -104,6 +151,7 @@ describe("freeze-frame route", () => {
104151 const { dir, post } = setup (
105152 async ( args ) => {
106153 calls . push ( args ) ;
154+ writeFileSync ( args . at ( - 1 ) ?? "" , "png" ) ;
107155 return { ok : true } ;
108156 } ,
109157 { path : "scenes/a.html" , html : evil . replace ( 'src="media/' , 'src="../media/' ) } ,
@@ -114,9 +162,11 @@ describe("freeze-frame route", () => {
114162 target : { id : "../../../../outside/frame" } ,
115163 playhead : 2.5 ,
116164 } ) ;
117- const body : { imageSrc ?: string ; after ?: string } = await res . json ( ) ;
165+ const body : { imageSrc ?: string ; after ?: string ; stillPath ?: string } = await res . json ( ) ;
118166 expect ( res . status ) . toBe ( 200 ) ;
119167 const output = calls [ 0 ] ?. at ( - 1 ) ?? "" ;
168+ // The history names project files from the project root, not from the page that shows the still.
169+ expect ( body . stillPath ) . toBe ( `assets/freeze/${ basename ( output ) } ` ) ;
120170 expect ( basename ( output ) ) . toMatch ( / ^ _ _ _ _ _ _ _ _ _ _ _ _ o u t s i d e _ f r a m e - [ 0 - 9 a - f ] { 10 } - 2 5 0 0 - / ) ;
121171 expect ( dirname ( output ) ) . toBe ( join ( dir , "assets/freeze" ) ) ;
122172 expect ( body . imageSrc ) . toBe ( `../assets/freeze/${ basename ( output ) } ` ) ;
@@ -190,16 +240,26 @@ describe("freeze-frame route", () => {
190240 expect ( readFileSync ( outputs [ 0 ] ?? "" , "utf-8" ) ) . toBe ( first ) ;
191241 } ) ;
192242
193- it ( "gives concurrent requests distinct stills" , async ( ) => {
243+ it ( "gives concurrent requests distinct stills and keeps only the one the page uses " , async ( ) => {
194244 const outputs : string [ ] = [ ] ;
195245 const { dir, post } = setup ( writingExtractor ( outputs ) ) ;
196246 const results = await Promise . all ( [ freeze ( post , dir , "talk" ) , freeze ( post , dir , "talk" ) ] ) ;
197247 expect ( results . map ( ( res ) => res . status ) . sort ( ) ) . toEqual ( [ 200 , 409 ] ) ;
198248 expect ( new Set ( outputs ) . size ) . toBe ( 2 ) ;
199- expect ( outputs . map ( ( output ) => readFileSync ( output , "utf-8" ) ) ) . toEqual ( [
200- expect . stringContaining ( "#" ) ,
201- expect . stringContaining ( "#" ) ,
202- ] ) ;
249+ const kept = outputs . filter ( ( output ) => existsSync ( output ) ) ;
250+ expect ( kept ) . toHaveLength ( 1 ) ;
251+ expect ( readFileSync ( kept [ 0 ] ?? "" , "utf-8" ) ) . toContain ( "#" ) ;
252+ expect ( readFileSync ( join ( dir , "index.html" ) , "utf-8" ) ) . toContain ( basename ( kept [ 0 ] ?? "" ) ) ;
253+ } ) ;
254+
255+ it ( "keeps the winner's still when two requests race for the same name" , async ( ) => {
256+ const outputs : string [ ] = [ ] ;
257+ const { dir, post } = setup ( writingExtractor ( outputs ) , undefined , ( ) => "fixed" ) ;
258+ const results = await Promise . all ( [ freeze ( post , dir , "talk" ) , freeze ( post , dir , "talk" ) ] ) ;
259+ expect ( results . map ( ( res ) => res . status ) . sort ( ) ) . toEqual ( [ 200 , 409 ] ) ;
260+ expect ( outputs ) . toHaveLength ( 1 ) ;
261+ expect ( existsSync ( outputs [ 0 ] ?? "" ) ) . toBe ( true ) ;
262+ expect ( readFileSync ( join ( dir , "index.html" ) , "utf-8" ) ) . toContain ( basename ( outputs [ 0 ] ?? "" ) ) ;
203263 } ) ;
204264
205265 it ( "refuses, without extracting, when the still's name is already taken" , async ( ) => {
0 commit comments