Skip to content

Commit 359089e

Browse files
committed
fix: preserve snapshot metadata after lock
(cherry picked from commit 5e5fa68)
1 parent 22e211d commit 359089e

2 files changed

Lines changed: 38 additions & 2 deletions

File tree

lib/storage/account-snapshot.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,18 @@ export async function describeAccountSnapshot(
5656
try {
5757
const { normalized, schemaErrors, storedVersion } =
5858
await deps.loadAccountsFromPath(path);
59+
const resolvedStats =
60+
stats.bytes === undefined || stats.mtimeMs === undefined
61+
? await deps.statSnapshot(path)
62+
: stats;
5963
return {
6064
kind,
6165
path,
6266
index: deps.index,
6367
exists: true,
6468
valid: !!normalized,
65-
bytes: stats.bytes,
66-
mtimeMs: stats.mtimeMs,
69+
bytes: resolvedStats.bytes,
70+
mtimeMs: resolvedStats.mtimeMs,
6771
version: typeof storedVersion === "number" ? storedVersion : undefined,
6872
accountCount: normalized?.accounts.length,
6973
schemaErrors: schemaErrors.length > 0 ? schemaErrors : undefined,

test/account-snapshot.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,38 @@ describe("describeAccountSnapshot", () => {
132132
});
133133
});
134134

135+
it("refreshes snapshot metadata after a transient stat lock", async () => {
136+
const statSnapshot = vi
137+
.fn()
138+
.mockResolvedValueOnce({ exists: true })
139+
.mockResolvedValueOnce({ exists: true, bytes: 12, mtimeMs: 34 });
140+
141+
await expect(
142+
describeAccountSnapshot("accounts.json", "accounts-primary", {
143+
index: 0,
144+
statSnapshot,
145+
loadAccountsFromPath: vi.fn(async () => ({
146+
normalized: { accounts: [{ id: 1 }] },
147+
schemaErrors: [],
148+
storedVersion: 3,
149+
})),
150+
logWarn: vi.fn(),
151+
}),
152+
).resolves.toEqual({
153+
kind: "accounts-primary",
154+
path: "accounts.json",
155+
index: 0,
156+
exists: true,
157+
valid: true,
158+
bytes: 12,
159+
mtimeMs: 34,
160+
version: 3,
161+
accountCount: 1,
162+
});
163+
164+
expect(statSnapshot).toHaveBeenCalledTimes(2);
165+
});
166+
135167
it("returns invalid metadata when the loader fails", async () => {
136168
const logWarn = vi.fn();
137169
await expect(

0 commit comments

Comments
 (0)