11import { afterEach , describe , expect , it , vi } from "vitest" ;
22import { Hono } from "hono" ;
3- import { mkdirSync , mkdtempSync , rmSync , writeFileSync } from "node:fs" ;
3+ import {
4+ existsSync ,
5+ mkdirSync ,
6+ mkdtempSync ,
7+ readdirSync ,
8+ rmSync ,
9+ truncateSync ,
10+ utimesSync ,
11+ writeFileSync ,
12+ } from "node:fs" ;
413import { tmpdir } from "node:os" ;
514import { join } from "node:path" ;
6- import { registerThumbnailRoutes } from "./thumbnail" ;
15+ import { pruneThumbnailCache , registerThumbnailRoutes } from "./thumbnail" ;
716import type { StudioApiAdapter } from "../types" ;
817
918const tempProjectDirs : string [ ] = [ ] ;
@@ -52,10 +61,37 @@ describe("registerThumbnailRoutes", () => {
5261 seekTime : 1.2 ,
5362 selector : "#title-card" ,
5463 format : "jpeg" ,
64+ outputWidth : 240 ,
65+ outputHeight : 135 ,
66+ signal : expect . any ( AbortSignal ) ,
5567 } ) ,
5668 ) ;
5769 } ) ;
5870
71+ it ( "deduplicates concurrent generation and writes one complete cache entry" , async ( ) => {
72+ const adapter = createAdapter ( ) ;
73+ const project = await adapter . resolveProject ( "demo" ) ;
74+ if ( ! project ) throw new Error ( "missing project" ) ;
75+ let resolve ! : ( buffer : Buffer ) => void ;
76+ const generated = new Promise < Buffer > ( ( done ) => ( resolve = done ) ) ;
77+ adapter . generateThumbnail = vi . fn ( async ( ) => generated ) ;
78+ const app = new Hono ( ) ;
79+ registerThumbnailRoutes ( app , adapter ) ;
80+
81+ const url = "http://localhost/projects/demo/thumbnail/index.html?t=3" ;
82+ const first = app . request ( url ) ;
83+ const second = app . request ( url ) ;
84+ await vi . waitFor ( ( ) => expect ( adapter . generateThumbnail ) . toHaveBeenCalledTimes ( 1 ) ) ;
85+ resolve ( Buffer . from ( "shared" ) ) ;
86+
87+ expect ( await ( await first ) . text ( ) ) . toBe ( "shared" ) ;
88+ expect ( await ( await second ) . text ( ) ) . toBe ( "shared" ) ;
89+ expect ( adapter . generateThumbnail ) . toHaveBeenCalledTimes ( 1 ) ;
90+ const cached = readdirSync ( join ( project . dir , ".thumbnails" ) ) ;
91+ expect ( cached ) . toHaveLength ( 1 ) ;
92+ expect ( cached [ 0 ] ) . not . toContain ( ".tmp" ) ;
93+ } ) ;
94+
5995 it ( "forwards png capture requests and returns a png content type" , async ( ) => {
6096 const adapter = createAdapter ( ) ;
6197 const app = new Hono ( ) ;
@@ -72,10 +108,27 @@ describe("registerThumbnailRoutes", () => {
72108 compPath : "compositions/intro.html" ,
73109 seekTime : 2 ,
74110 format : "png" ,
111+ outputWidth : 1920 ,
112+ outputHeight : 1080 ,
75113 } ) ,
76114 ) ;
77115 } ) ;
78116
117+ it ( "allows png callers to opt into bounded preview output" , async ( ) => {
118+ const adapter = createAdapter ( ) ;
119+ const app = new Hono ( ) ;
120+ registerThumbnailRoutes ( app , adapter ) ;
121+
122+ const response = await app . request (
123+ "http://localhost/projects/demo/thumbnail/index.html?format=png&output=preview" ,
124+ ) ;
125+
126+ expect ( response . status ) . toBe ( 200 ) ;
127+ expect ( adapter . generateThumbnail ) . toHaveBeenCalledWith (
128+ expect . objectContaining ( { outputWidth : 240 , outputHeight : 135 } ) ,
129+ ) ;
130+ } ) ;
131+
79132 it ( "preserves an explicit zero seek time" , async ( ) => {
80133 const adapter = createAdapter ( ) ;
81134 const app = new Hono ( ) ;
@@ -220,4 +273,26 @@ describe("registerThumbnailRoutes", () => {
220273
221274 expect ( adapter . generateThumbnail ) . toHaveBeenCalledTimes ( 2 ) ;
222275 } ) ;
276+
277+ it ( "prunes expired and over-budget files without touching protected work" , ( ) => {
278+ const cacheDir = mkdtempSync ( join ( tmpdir ( ) , "hf-thumbnail-cache-test-" ) ) ;
279+ tempProjectDirs . push ( cacheDir ) ;
280+ const expiredPath = join ( cacheDir , "expired.jpg" ) ;
281+ const protectedPath = join ( cacheDir , "protected.jpg" ) ;
282+ const overflowPath = join ( cacheDir , "overflow.jpg" ) ;
283+ writeFileSync ( expiredPath , "expired" ) ;
284+ writeFileSync ( protectedPath , "protected" ) ;
285+ writeFileSync ( overflowPath , "overflow" ) ;
286+ const now = Date . now ( ) ;
287+ const expiredSeconds = ( now - 15 * 24 * 60 * 60 * 1000 ) / 1000 ;
288+ utimesSync ( expiredPath , expiredSeconds , expiredSeconds ) ;
289+ truncateSync ( protectedPath , 400 * 1024 * 1024 ) ;
290+ truncateSync ( overflowPath , 200 * 1024 * 1024 ) ;
291+
292+ pruneThumbnailCache ( cacheDir , new Set ( [ protectedPath ] ) , now ) ;
293+
294+ expect ( existsSync ( expiredPath ) ) . toBe ( false ) ;
295+ expect ( existsSync ( protectedPath ) ) . toBe ( true ) ;
296+ expect ( existsSync ( overflowPath ) ) . toBe ( false ) ;
297+ } ) ;
223298} ) ;
0 commit comments