Skip to content

Commit 811a882

Browse files
committed
refactor: extract flagged storage entry wrappers
1 parent c1b0ddf commit 811a882

3 files changed

Lines changed: 75 additions & 0 deletions

File tree

lib/storage.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ import {
3131
} from "./storage/backup-paths.js";
3232
import { restoreAccountsFromBackupPath } from "./storage/backup-restore.js";
3333
import { looksLikeSyntheticFixtureStorage } from "./storage/fixture-guards.js";
34+
import {
35+
clearFlaggedAccountsEntry,
36+
saveFlaggedAccountsEntry,
37+
} from "./storage/flagged-entry.js";
3438
import { normalizeFlaggedStorage } from "./storage/flagged-storage.js";
3539
import {
3640
clearFlaggedAccountsOnDisk,

lib/storage/flagged-entry.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import type { FlaggedAccountStorageV1 } from "../storage.js";
2+
3+
export async function saveFlaggedAccountsEntry(params: {
4+
storage: FlaggedAccountStorageV1;
5+
withStorageLock: <T>(fn: () => Promise<T>) => Promise<T>;
6+
saveUnlocked: (storage: FlaggedAccountStorageV1) => Promise<void>;
7+
}): Promise<void> {
8+
return params.withStorageLock(async () => {
9+
await params.saveUnlocked(params.storage);
10+
});
11+
}
12+
13+
export async function clearFlaggedAccountsEntry(params: {
14+
path: string;
15+
withStorageLock: <T>(fn: () => Promise<T>) => Promise<T>;
16+
markerPath: string;
17+
getBackupPaths: () => Promise<string[]>;
18+
clearFlaggedAccountsOnDisk: (args: {
19+
path: string;
20+
markerPath: string;
21+
backupPaths: string[];
22+
logError: (message: string, details: Record<string, unknown>) => void;
23+
}) => Promise<void>;
24+
logError: (message: string, details: Record<string, unknown>) => void;
25+
}): Promise<void> {
26+
return params.withStorageLock(async () => {
27+
await params.clearFlaggedAccountsOnDisk({
28+
path: params.path,
29+
markerPath: params.markerPath,
30+
backupPaths: await params.getBackupPaths(),
31+
logError: params.logError,
32+
});
33+
});
34+
}

test/flagged-entry.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { describe, expect, it, vi } from "vitest";
2+
import {
3+
clearFlaggedAccountsEntry,
4+
saveFlaggedAccountsEntry,
5+
} from "../lib/storage/flagged-entry.js";
6+
7+
describe("flagged entry helpers", () => {
8+
it("delegates save through the storage lock", async () => {
9+
const saveUnlocked = vi.fn(async () => undefined);
10+
await saveFlaggedAccountsEntry({
11+
storage: { version: 1, accounts: [] },
12+
withStorageLock: async (fn) => fn(),
13+
saveUnlocked,
14+
});
15+
16+
expect(saveUnlocked).toHaveBeenCalledWith({ version: 1, accounts: [] });
17+
});
18+
19+
it("delegates clear through the storage lock and backup resolver", async () => {
20+
const clearFlaggedAccountsOnDisk = vi.fn(async () => undefined);
21+
await clearFlaggedAccountsEntry({
22+
path: "/tmp/flagged.json",
23+
withStorageLock: async (fn) => fn(),
24+
markerPath: "/tmp/flagged.marker",
25+
getBackupPaths: async () => ["/tmp/flagged.json.bak"],
26+
clearFlaggedAccountsOnDisk,
27+
logError: vi.fn(),
28+
});
29+
30+
expect(clearFlaggedAccountsOnDisk).toHaveBeenCalledWith({
31+
path: "/tmp/flagged.json",
32+
markerPath: "/tmp/flagged.marker",
33+
backupPaths: ["/tmp/flagged.json.bak"],
34+
logError: expect.any(Function),
35+
});
36+
});
37+
});

0 commit comments

Comments
 (0)