11import { runCommand } from "citty" ;
2- import { existsSync , mkdtempSync , readFileSync , readdirSync , rmSync , writeFileSync } from "node:fs" ;
2+ import {
3+ existsSync ,
4+ linkSync ,
5+ lstatSync ,
6+ mkdirSync ,
7+ mkdtempSync ,
8+ readFileSync ,
9+ readdirSync ,
10+ rmSync ,
11+ symlinkSync ,
12+ writeFileSync ,
13+ } from "node:fs" ;
314import { tmpdir } from "node:os" ;
415import { join } from "node:path" ;
516import { afterEach , beforeEach , describe , expect , it , vi } from "vitest" ;
617
718const capture = vi . hoisted ( ( ) => ( {
819 evaluate : vi . fn ( ) ,
9- screenshot : vi . fn ( async ( { path } : { path : string } ) => {
10- const bytes = Buffer . from ( "captured PNG" ) ;
11- writeFileSync ( path , bytes ) ;
12- return bytes ;
13- } ) ,
20+ screenshot : vi . fn ( async ( ) => Buffer . from ( "captured PNG" ) ) ,
1421 closeBrowser : vi . fn ( async ( ) => undefined ) ,
1522 closeServer : vi . fn ( async ( ) => undefined ) ,
1623 seek : vi . fn ( async ( ) => undefined ) ,
@@ -165,11 +172,7 @@ describe("snapshot contact sheet failures", () => {
165172 historyRoot : historyDeps . historyRoot ,
166173 ownerWaitMs : 0 ,
167174 } ) ;
168- capture . screenshot . mockImplementation ( async ( { path } : { path : string } ) => {
169- const bytes = Buffer . from ( "replaced PNG" ) ;
170- writeFileSync ( path , bytes ) ;
171- return bytes ;
172- } ) ;
175+ capture . screenshot . mockImplementation ( async ( ) => Buffer . from ( "replaced PNG" ) ) ;
173176 primePage ( 2 ) ;
174177 try {
175178 await runCommand ( snapshotCommand , args ) ;
@@ -209,10 +212,7 @@ describe("snapshot --keep", () => {
209212 } ) ;
210213
211214 async function snapshot ( at : string , bytes : string ) {
212- capture . screenshot . mockImplementation ( async ( { path } : { path : string } ) => {
213- writeFileSync ( path , bytes ) ;
214- return Buffer . from ( bytes ) ;
215- } ) ;
215+ capture . screenshot . mockImplementation ( async ( ) => Buffer . from ( bytes ) ) ;
216216 primePage ( at . split ( "," ) . length ) ;
217217 await runCommand ( snapshotCommand , {
218218 rawArgs : [ project , "--keep" , "--at" , at , "--no-end" , "--describe" , "false" , "-o" , stills ] ,
@@ -229,4 +229,34 @@ describe("snapshot --keep", () => {
229229 expect ( readFileSync ( join ( stills , "at-1.5s.png" ) , "utf8" ) ) . toBe ( "first PNG" ) ;
230230 expect ( existsSync ( join ( stills , "contact-sheet.jpg" ) ) ) . toBe ( false ) ;
231231 } ) ;
232+
233+ it ( "refuses two times that would share a still, before touching the folder" , async ( ) => {
234+ await snapshot ( "1.5" , "first PNG" ) ;
235+ capture . screenshot . mockClear ( ) ;
236+
237+ await expect ( snapshot ( "1.5,1.5004" , "second PNG" ) ) . rejects . toThrow (
238+ "--keep would name both 1.5 and 1.5004 at-1.5s.png" ,
239+ ) ;
240+ expect ( capture . screenshot ) . not . toHaveBeenCalled ( ) ;
241+ expect ( readFileSync ( join ( stills , "at-1.5s.png" ) , "utf8" ) ) . toBe ( "first PNG" ) ;
242+ } ) ;
243+
244+ it ( "replaces a linked still without writing through the link" , async ( ) => {
245+ mkdirSync ( stills , { recursive : true } ) ;
246+ const hardVictim = join ( project , "hard-victim.png" ) ;
247+ writeFileSync ( hardVictim , "hard victim" ) ;
248+ linkSync ( hardVictim , join ( stills , "at-0.5s.png" ) ) ;
249+ const softVictim = join ( project , "soft-victim.png" ) ;
250+ writeFileSync ( softVictim , "soft victim" ) ;
251+ if ( process . platform !== "win32" ) symlinkSync ( softVictim , join ( stills , "at-1.5s.png" ) ) ;
252+
253+ await snapshot ( "0.5,1.5" , "new PNG" ) ;
254+
255+ expect ( readFileSync ( hardVictim , "utf8" ) ) . toBe ( "hard victim" ) ;
256+ expect ( readFileSync ( softVictim , "utf8" ) ) . toBe ( "soft victim" ) ;
257+ for ( const still of [ "at-0.5s.png" , "at-1.5s.png" ] ) {
258+ expect ( lstatSync ( join ( stills , still ) ) . isFile ( ) ) . toBe ( true ) ;
259+ expect ( readFileSync ( join ( stills , still ) , "utf8" ) ) . toBe ( "new PNG" ) ;
260+ }
261+ } ) ;
232262} ) ;
0 commit comments