@@ -13,7 +13,11 @@ import { describe, expect, it, onTestFinished, vi } from "vitest";
1313import { ID_PATH , readId } from "./historyId.js" ;
1414import { takeHistoryOwnership } from "./ownerLock.js" ;
1515import { openProjectHistory } from "./projectHistory.js" ;
16- import { KEEP_GONE_PROJECT_HISTORY_MS , pruneGoneProjectHistories } from "./pruneHistories.js" ;
16+ import {
17+ KEEP_GONE_PROJECT_HISTORY_MS ,
18+ listProjectHistories ,
19+ pruneGoneProjectHistories ,
20+ } from "./pruneHistories.js" ;
1721
1822vi . mock ( "./ownerLock.js" , async ( importOriginal ) => {
1923 const actual = await importOriginal < typeof import ( "./ownerLock.js" ) > ( ) ;
@@ -280,3 +284,27 @@ describe("pruneGoneProjectHistories", () => {
280284 expect ( existsSync ( join ( historyRoot , unreadable . id ) ) ) . toBe ( true ) ;
281285 } ) ;
282286} ) ;
287+
288+ describe ( "listProjectHistories" , ( ) => {
289+ it ( "lists only histories whose project is still in its folder, and skips other folders" , async ( ) => {
290+ const historyRoot = tempDir ( "hf-prune-root-" ) ;
291+ const live = await projectWithHistory ( historyRoot ) ;
292+ const gone = await projectWithHistory ( historyRoot ) ;
293+ const replaced = await projectWithHistory ( historyRoot ) ;
294+ await Promise . all ( [ live , gone , replaced ] . map ( ( { history } ) => history . close ( ) ) ) ;
295+ rmSync ( gone . projectDir , { recursive : true , force : true } ) ;
296+ writeFileSync ( join ( replaced . projectDir , ID_PATH ) , "00000000-0000-4000-8000-000000000000\n" ) ;
297+ mkdirSync ( join ( historyRoot , "backup" ) ) ;
298+
299+ const listed = listProjectHistories ( historyRoot ) ;
300+ expect ( listed . map ( ( { id, projectDir } ) => ( { id, projectDir } ) ) ) . toEqual ( [
301+ { id : live . id , projectDir : live . projectDir } ,
302+ ] ) ;
303+ expect ( listed [ 0 ] ! . lastUsedMs ) . toBeGreaterThan ( 0 ) ;
304+ expect ( existsSync ( join ( historyRoot , gone . id ) ) ) . toBe ( true ) ;
305+ } ) ;
306+
307+ it ( "lists nothing for a history root that does not exist" , ( ) => {
308+ expect ( listProjectHistories ( join ( tmpdir ( ) , "hf-no-such-history-root" ) ) ) . toEqual ( [ ] ) ;
309+ } ) ;
310+ } ) ;
0 commit comments