@@ -9,6 +9,25 @@ function sha256(value: string): string {
99}
1010
1111describe ( "inspectStorageHealth" , ( ) => {
12+ async function withStorageModule < T > (
13+ testName : string ,
14+ runner : ( args : { workDir : string ; storagePath : string ; resetMarkerPath : string ; walPath : string ; storageModule : typeof import ( "../lib/storage.js" ) } ) => Promise < T > ,
15+ ) : Promise < T > {
16+ const workDir = join ( tmpdir ( ) , `${ testName } -${ Date . now ( ) } ` ) ;
17+ await fs . mkdir ( workDir , { recursive : true } ) ;
18+ const storagePath = join ( workDir , "accounts.json" ) ;
19+ const resetMarkerPath = `${ storagePath } .reset-intent` ;
20+ const walPath = `${ storagePath } .wal` ;
21+ const storageModule = await import ( "../lib/storage.js" ) ;
22+ storageModule . setStoragePathDirect ( storagePath ) ;
23+ try {
24+ return await runner ( { workDir, storagePath, resetMarkerPath, walPath, storageModule } ) ;
25+ } finally {
26+ storageModule . setStoragePathDirect ( null ) ;
27+ await fs . rm ( workDir , { recursive : true , force : true } ) ;
28+ }
29+ }
30+
1231 afterEach ( ( ) => {
1332 vi . resetModules ( ) ;
1433 } ) ;
@@ -67,4 +86,65 @@ describe("inspectStorageHealth", () => {
6786 await fs . rm ( workDir , { recursive : true , force : true } ) ;
6887 }
6988 } ) ;
89+
90+ it ( "reports healthy storage when primary storage is valid" , async ( ) => {
91+ await withStorageModule ( "storage-health-healthy" , async ( { storagePath, storageModule } ) => {
92+ await fs . writeFile (
93+ storagePath ,
94+ JSON . stringify ( {
95+ version : 3 ,
96+ activeIndex : 0 ,
97+ activeIndexByFamily : { codex : 0 } ,
98+ accounts : [ { refreshToken : "r" , addedAt : 1 , lastUsed : 1 } ] ,
99+ } ) ,
100+ "utf-8" ,
101+ ) ;
102+ const summary = await storageModule . inspectStorageHealth ( ) ;
103+ expect ( summary . state ) . toBe ( "healthy" ) ;
104+ } ) ;
105+ } ) ;
106+
107+ it ( "reports empty storage when primary storage is valid but has no accounts" , async ( ) => {
108+ await withStorageModule ( "storage-health-empty" , async ( { storagePath, storageModule } ) => {
109+ await fs . writeFile (
110+ storagePath ,
111+ JSON . stringify ( { version : 3 , activeIndex : 0 , activeIndexByFamily : { codex : 0 } , accounts : [ ] } ) ,
112+ "utf-8" ,
113+ ) ;
114+ const summary = await storageModule . inspectStorageHealth ( ) ;
115+ expect ( summary . state ) . toBe ( "empty" ) ;
116+ } ) ;
117+ } ) ;
118+
119+ it ( "reports intentional-reset when the reset marker exists" , async ( ) => {
120+ await withStorageModule ( "storage-health-reset" , async ( { resetMarkerPath, storageModule } ) => {
121+ await fs . writeFile ( resetMarkerPath , "" , "utf-8" ) ;
122+ const summary = await storageModule . inspectStorageHealth ( ) ;
123+ expect ( summary . state ) . toBe ( "intentional-reset" ) ;
124+ } ) ;
125+ } ) ;
126+
127+ it ( "reports corrupt storage when primary storage is malformed and WAL is unavailable" , async ( ) => {
128+ await withStorageModule ( "storage-health-corrupt-json" , async ( { storagePath, storageModule } ) => {
129+ await fs . writeFile ( storagePath , "{ malformed-json" , "utf-8" ) ;
130+ const summary = await storageModule . inspectStorageHealth ( ) ;
131+ expect ( summary . state ) . toBe ( "corrupt" ) ;
132+ } ) ;
133+ } ) ;
134+
135+ it ( "reports recoverable storage when invalid primary storage has a valid WAL" , async ( ) => {
136+ await withStorageModule ( "storage-health-recoverable-invalid" , async ( { storagePath, walPath, storageModule } ) => {
137+ await fs . writeFile ( storagePath , "{ malformed-json" , "utf-8" ) ;
138+ const content = JSON . stringify ( {
139+ version : 3 ,
140+ activeIndex : 0 ,
141+ activeIndexByFamily : { codex : 0 } ,
142+ accounts : [ { refreshToken : "refresh-token" , addedAt : 1 , lastUsed : 1 } ] ,
143+ } ) ;
144+ await fs . writeFile ( walPath , JSON . stringify ( { version : 1 , content, checksum : sha256 ( content ) } ) , "utf-8" ) ;
145+ const summary = await storageModule . inspectStorageHealth ( ) ;
146+ expect ( summary . state ) . toBe ( "recoverable" ) ;
147+ expect ( summary . recoverySource ) . toBe ( "wal" ) ;
148+ } ) ;
149+ } ) ;
70150} ) ;
0 commit comments