@@ -2,6 +2,15 @@ import { describe, expect, it, vi } from "vitest";
22import { describeAccountSnapshot , statSnapshot } from "../lib/storage/account-snapshot.js" ;
33
44describe ( "statSnapshot" , ( ) => {
5+ it ( "returns size and mtime for accessible snapshots" , async ( ) => {
6+ await expect (
7+ statSnapshot ( "accounts.json" , {
8+ stat : vi . fn ( async ( ) => ( { size : 1234 , mtimeMs : 5678 } ) ) ,
9+ logWarn : vi . fn ( ) ,
10+ } ) ,
11+ ) . resolves . toEqual ( { exists : true , bytes : 1234 , mtimeMs : 5678 } ) ;
12+ } ) ;
13+
514 it ( "returns missing metadata for ENOENT" , async ( ) => {
615 await expect (
716 statSnapshot ( "missing.json" , {
@@ -16,7 +25,7 @@ describe("statSnapshot", () => {
1625 it ( "logs and returns missing metadata for non-ENOENT stat failures" , async ( ) => {
1726 const logWarn = vi . fn ( ) ;
1827 await expect (
19- statSnapshot ( "locked .json" , {
28+ statSnapshot ( "denied .json" , {
2029 stat : vi . fn ( async ( ) => {
2130 throw Object . assign ( new Error ( "denied" ) , { code : "EACCES" } ) ;
2231 } ) ,
@@ -25,7 +34,7 @@ describe("statSnapshot", () => {
2534 ) . resolves . toEqual ( { exists : false } ) ;
2635 expect ( logWarn ) . toHaveBeenCalledWith (
2736 "Failed to stat backup candidate" ,
28- expect . objectContaining ( { path : "locked .json" } ) ,
37+ expect . objectContaining ( { path : "denied .json" } ) ,
2938 ) ;
3039 } ) ;
3140 it ( "treats locked snapshots as existing when stat returns EBUSY" , async ( ) => {
@@ -164,6 +173,39 @@ describe("describeAccountSnapshot", () => {
164173 expect ( statSnapshot ) . toHaveBeenCalledTimes ( 2 ) ;
165174 } ) ;
166175
176+ it ( "falls back to zeroed metadata after repeated stat locks" , async ( ) => {
177+ const statSnapshot = vi
178+ . fn ( )
179+ . mockResolvedValueOnce ( { exists : true } )
180+ . mockResolvedValueOnce ( { exists : true } )
181+ . mockResolvedValueOnce ( { exists : true } ) ;
182+
183+ await expect (
184+ describeAccountSnapshot ( "accounts.json" , "accounts-primary" , {
185+ index : 0 ,
186+ statSnapshot,
187+ loadAccountsFromPath : vi . fn ( async ( ) => ( {
188+ normalized : { accounts : [ { id : 1 } ] } ,
189+ schemaErrors : [ ] ,
190+ storedVersion : 3 ,
191+ } ) ) ,
192+ logWarn : vi . fn ( ) ,
193+ } ) ,
194+ ) . resolves . toEqual ( {
195+ kind : "accounts-primary" ,
196+ path : "accounts.json" ,
197+ index : 0 ,
198+ exists : true ,
199+ valid : true ,
200+ bytes : 0 ,
201+ mtimeMs : 0 ,
202+ version : 3 ,
203+ accountCount : 1 ,
204+ } ) ;
205+
206+ expect ( statSnapshot ) . toHaveBeenCalledTimes ( 3 ) ;
207+ } ) ;
208+
167209 it ( "returns invalid metadata when the loader fails" , async ( ) => {
168210 const logWarn = vi . fn ( ) ;
169211 await expect (
0 commit comments