Skip to content

Commit 8e36a86

Browse files
committed
refactor: extract clear accounts entry wrapper
1 parent 811a882 commit 8e36a86

3 files changed

Lines changed: 63 additions & 13 deletions

File tree

lib/storage.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@ import {
1111
} from "./named-backup-export.js";
1212
import { MODEL_FAMILIES, type ModelFamily } from "./prompts/codex.js";
1313
import { clearAccountStorageArtifacts } from "./storage/account-clear.js";
14+
import { clearAccountsEntry } from "./storage/account-clear-entry.js";
1415
import { cloneAccountStorageForPersistence } from "./storage/account-persistence.js";
1516
import {
16-
clearFlaggedAccountsEntry,
1717
exportAccountsSnapshot,
1818
importAccountsSnapshot,
19-
saveFlaggedAccountsEntry,
2019
} from "./storage/account-port.js";
2120
import { saveAccountsToDisk } from "./storage/account-save.js";
2221
import { buildBackupMetadata } from "./storage/backup-metadata-builder.js";
@@ -1847,17 +1846,18 @@ export async function saveAccounts(storage: AccountStorageV3): Promise<void> {
18471846
* Silently ignores if file doesn't exist.
18481847
*/
18491848
export async function clearAccounts(): Promise<void> {
1850-
return withStorageLock(async () => {
1851-
const path = getStoragePath();
1852-
await clearAccountStorageArtifacts({
1853-
path,
1854-
resetMarkerPath: getIntentionalResetMarkerPath(path),
1855-
walPath: getAccountsWalPath(path),
1856-
backupPaths: await getAccountsBackupRecoveryCandidatesWithDiscovery(path),
1857-
logError: (message, details) => {
1858-
log.error(message, details);
1859-
},
1860-
});
1849+
const path = getStoragePath();
1850+
return clearAccountsEntry({
1851+
path,
1852+
withStorageLock,
1853+
resetMarkerPath: getIntentionalResetMarkerPath(path),
1854+
walPath: getAccountsWalPath(path),
1855+
getBackupPaths: () =>
1856+
getAccountsBackupRecoveryCandidatesWithDiscovery(path),
1857+
clearAccountStorageArtifacts,
1858+
logError: (message, details) => {
1859+
log.error(message, details);
1860+
},
18611861
});
18621862
}
18631863

lib/storage/account-clear-entry.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
export async function clearAccountsEntry(params: {
2+
path: string;
3+
withStorageLock: <T>(fn: () => Promise<T>) => Promise<T>;
4+
resetMarkerPath: string;
5+
walPath: string;
6+
getBackupPaths: () => Promise<string[]>;
7+
clearAccountStorageArtifacts: (args: {
8+
path: string;
9+
resetMarkerPath: string;
10+
walPath: string;
11+
backupPaths: string[];
12+
logError: (message: string, details: Record<string, unknown>) => void;
13+
}) => Promise<void>;
14+
logError: (message: string, details: Record<string, unknown>) => void;
15+
}): Promise<void> {
16+
return params.withStorageLock(async () => {
17+
await params.clearAccountStorageArtifacts({
18+
path: params.path,
19+
resetMarkerPath: params.resetMarkerPath,
20+
walPath: params.walPath,
21+
backupPaths: await params.getBackupPaths(),
22+
logError: params.logError,
23+
});
24+
});
25+
}

test/account-clear-entry.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { describe, expect, it, vi } from "vitest";
2+
import { clearAccountsEntry } from "../lib/storage/account-clear-entry.js";
3+
4+
describe("account clear entry", () => {
5+
it("delegates clear through the storage lock and backup resolver", async () => {
6+
const clearAccountStorageArtifacts = vi.fn(async () => undefined);
7+
await clearAccountsEntry({
8+
path: "/tmp/accounts.json",
9+
withStorageLock: async (fn) => fn(),
10+
resetMarkerPath: "/tmp/accounts.reset-intent",
11+
walPath: "/tmp/accounts.wal",
12+
getBackupPaths: async () => ["/tmp/accounts.json.bak"],
13+
clearAccountStorageArtifacts,
14+
logError: vi.fn(),
15+
});
16+
17+
expect(clearAccountStorageArtifacts).toHaveBeenCalledWith({
18+
path: "/tmp/accounts.json",
19+
resetMarkerPath: "/tmp/accounts.reset-intent",
20+
walPath: "/tmp/accounts.wal",
21+
backupPaths: ["/tmp/accounts.json.bak"],
22+
logError: expect.any(Function),
23+
});
24+
});
25+
});

0 commit comments

Comments
 (0)