@@ -8,11 +8,10 @@ import {
88 saveDiagramSource ,
99 loadDiagramSource ,
1010 loadDiagramOptions ,
11- cleanupOldDiagrams ,
1211 getConfigDir ,
1312 validateSavePath ,
1413} from "../src/file-utils.js" ;
15- import { writeFile , unlink , mkdir , utimes , rmdir , readdir , mkdtemp } from "fs/promises" ;
14+ import { writeFile , unlink , mkdir , rmdir , readdir , mkdtemp } from "fs/promises" ;
1615import { tmpdir } from "os" ;
1716import { join } from "path" ;
1817
@@ -259,105 +258,6 @@ describe("File Utilities", () => {
259258 } ) ;
260259 } ) ;
261260
262- describe ( "cleanupOldDiagrams" , ( ) => {
263- let tempDir : string ;
264- let testFiles : string [ ] ;
265-
266- beforeEach ( async ( ) => {
267- // Use a test-specific directory
268- tempDir = join ( tmpdir ( ) , "claude-mermaid-test-cleanup" , Date . now ( ) . toString ( ) ) ;
269- await mkdir ( tempDir , { recursive : true } ) ;
270- testFiles = [ ] ;
271- } ) ;
272-
273- afterEach ( async ( ) => {
274- // Clean up test files
275- for ( const file of testFiles ) {
276- try {
277- await unlink ( file ) ;
278- } catch {
279- // Ignore if file doesn't exist
280- }
281- }
282- } ) ;
283-
284- it ( "should return 0 when no files to clean" , async ( ) => {
285- // Test with very short max age to simulate old files
286- const count = await cleanupOldDiagrams ( 0 ) ;
287- expect ( count ) . toBeGreaterThanOrEqual ( 0 ) ;
288- } ) ;
289-
290- it ( "should clean up files older than max age" , async ( ) => {
291- const oldFile = join ( tempDir , "old-diagram.svg" ) ;
292- const newFile = join ( tempDir , "new-diagram.svg" ) ;
293-
294- // Create old file
295- await writeFile ( oldFile , "<svg>old</svg>" , "utf-8" ) ;
296- testFiles . push ( oldFile ) ;
297-
298- // Set file modification time to 8 days ago
299- const eightDaysAgo = new Date ( Date . now ( ) - 8 * 24 * 60 * 60 * 1000 ) ;
300- await utimes ( oldFile , eightDaysAgo , eightDaysAgo ) ;
301-
302- // Create new file
303- await writeFile ( newFile , "<svg>new</svg>" , "utf-8" ) ;
304- testFiles . push ( newFile ) ;
305-
306- // This test verifies the cleanup logic works conceptually
307- // In practice, it cleans the actual live directory, not our test dir
308- const maxAge = 7 * 24 * 60 * 60 * 1000 ; // 7 days
309- expect ( maxAge ) . toBe ( 604800000 ) ;
310- } ) ;
311-
312- it ( "should clean up directories with all their files" , async ( ) => {
313- const testPreviewId = "test-cleanup-dir" ;
314- const testDir = getPreviewDir ( testPreviewId ) ;
315-
316- await mkdir ( testDir , { recursive : true } ) ;
317- await writeFile ( join ( testDir , "diagram.svg" ) , "<svg>test</svg>" , "utf-8" ) ;
318- await writeFile ( join ( testDir , "diagram.mmd" ) , "graph TD; A-->B" , "utf-8" ) ;
319- await writeFile ( join ( testDir , "options.json" ) , "{}" , "utf-8" ) ;
320-
321- const files = await readdir ( testDir ) ;
322- expect ( files . length ) . toBeGreaterThan ( 0 ) ;
323-
324- // Cleanup test directory
325- for ( const file of files ) {
326- await unlink ( join ( testDir , file ) ) ;
327- }
328- await rmdir ( testDir ) ;
329- } ) ;
330-
331- it ( "should handle different max age values" , ( ) => {
332- const oneDayMs = 24 * 60 * 60 * 1000 ;
333- const sevenDaysMs = 7 * 24 * 60 * 60 * 1000 ;
334- const thirtyDaysMs = 30 * 24 * 60 * 60 * 1000 ;
335-
336- expect ( oneDayMs ) . toBe ( 86400000 ) ;
337- expect ( sevenDaysMs ) . toBe ( 604800000 ) ;
338- expect ( thirtyDaysMs ) . toBe ( 2592000000 ) ;
339- } ) ;
340-
341- it ( "should use 7 days as default max age" , ( ) => {
342- const defaultMaxAge = 7 * 24 * 60 * 60 * 1000 ;
343- expect ( defaultMaxAge ) . toBe ( 604800000 ) ;
344- } ) ;
345-
346- it ( "should not throw errors when directory does not exist" , async ( ) => {
347- // The function creates the directory if it doesn't exist
348- await expect ( cleanupOldDiagrams ( ) ) . resolves . not . toThrow ( ) ;
349- } ) ;
350-
351- it ( "should calculate file age correctly" , ( ) => {
352- const now = Date . now ( ) ;
353- const eightDaysAgo = now - 8 * 24 * 60 * 60 * 1000 ;
354- const age = now - eightDaysAgo ;
355- const sevenDaysMs = 7 * 24 * 60 * 60 * 1000 ;
356-
357- expect ( age ) . toBeGreaterThan ( sevenDaysMs ) ;
358- } ) ;
359- } ) ;
360-
361261 describe ( "validateSavePath" , ( ) => {
362262 it ( "should allow valid relative paths" , ( ) => {
363263 expect ( ( ) => validateSavePath ( "./diagrams/test.svg" ) ) . not . toThrow ( ) ;
0 commit comments