Skip to content

Commit 401f21c

Browse files
committed
refactor: extract restore backup facade
1 parent f95fb61 commit 401f21c

3 files changed

Lines changed: 82 additions & 3 deletions

File tree

lib/storage.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ import {
7575
mergeStorageForMigration,
7676
} from "./storage/project-migration.js";
7777
import { buildRestoreAssessment } from "./storage/restore-assessment.js";
78+
import { restoreAccountsFromBackupEntry } from "./storage/restore-backup-entry.js";
7879
import {
7980
loadAccountsFromPath,
8081
parseAndNormalizeStorage,
@@ -810,9 +811,12 @@ export async function restoreAccountsFromBackup(
810811
path: string,
811812
options?: { persist?: boolean },
812813
): Promise<AccountStorageV3> {
813-
return restoreAccountsFromBackupPath(path, {
814-
persist: options?.persist,
815-
backupRoot: getNamedBackupRoot(getStoragePath()),
814+
return restoreAccountsFromBackupEntry({
815+
path,
816+
options,
817+
restoreAccountsFromBackupPath,
818+
getNamedBackupRoot,
819+
getStoragePath,
816820
realpath: fs.realpath,
817821
loadAccountsFromPath: (path) =>
818822
loadAccountsFromPath(path, {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import type { AccountStorageV3 } from "../storage.js";
2+
3+
export async function restoreAccountsFromBackupEntry(params: {
4+
path: string;
5+
options?: { persist?: boolean };
6+
restoreAccountsFromBackupPath: (
7+
path: string,
8+
options: {
9+
persist?: boolean;
10+
backupRoot: string;
11+
realpath: (path: string) => Promise<string>;
12+
loadAccountsFromPath: (path: string) => Promise<{
13+
normalized: AccountStorageV3 | null;
14+
}>;
15+
saveAccounts: (storage: AccountStorageV3) => Promise<void>;
16+
},
17+
) => Promise<AccountStorageV3>;
18+
getNamedBackupRoot: (storagePath: string) => string;
19+
getStoragePath: () => string;
20+
realpath: (path: string) => Promise<string>;
21+
loadAccountsFromPath: (
22+
path: string,
23+
) => Promise<{ normalized: AccountStorageV3 | null }>;
24+
saveAccounts: (storage: AccountStorageV3) => Promise<void>;
25+
}): Promise<AccountStorageV3> {
26+
return params.restoreAccountsFromBackupPath(params.path, {
27+
persist: params.options?.persist,
28+
backupRoot: params.getNamedBackupRoot(params.getStoragePath()),
29+
realpath: params.realpath,
30+
loadAccountsFromPath: params.loadAccountsFromPath,
31+
saveAccounts: params.saveAccounts,
32+
});
33+
}

test/restore-backup-entry.test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { describe, expect, it, vi } from "vitest";
2+
import { restoreAccountsFromBackupEntry } from "../lib/storage/restore-backup-entry.js";
3+
4+
describe("restore backup entry", () => {
5+
it("passes path, options, and injected deps through to the restore helper", async () => {
6+
const restoreAccountsFromBackupPath = vi.fn(async () => ({
7+
version: 3,
8+
accounts: [],
9+
activeIndex: 0,
10+
activeIndexByFamily: {},
11+
}));
12+
const loadAccountsFromPath = vi.fn(async () => ({ normalized: null }));
13+
const saveAccounts = vi.fn(async () => undefined);
14+
15+
const result = await restoreAccountsFromBackupEntry({
16+
path: "/tmp/backup.json",
17+
options: { persist: false },
18+
restoreAccountsFromBackupPath,
19+
getNamedBackupRoot: () => "/tmp/backups",
20+
getStoragePath: () => "/tmp/accounts.json",
21+
realpath: vi.fn(async (path) => path),
22+
loadAccountsFromPath,
23+
saveAccounts,
24+
});
25+
26+
expect(restoreAccountsFromBackupPath).toHaveBeenCalledWith(
27+
"/tmp/backup.json",
28+
expect.objectContaining({
29+
persist: false,
30+
backupRoot: "/tmp/backups",
31+
loadAccountsFromPath,
32+
saveAccounts,
33+
}),
34+
);
35+
expect(result).toEqual({
36+
version: 3,
37+
accounts: [],
38+
activeIndex: 0,
39+
activeIndexByFamily: {},
40+
});
41+
});
42+
});

0 commit comments

Comments
 (0)