Skip to content

Commit d381063

Browse files
committed
fix-export-reset-fallback
1 parent a81b9c8 commit d381063

2 files changed

Lines changed: 71 additions & 3 deletions

File tree

lib/storage.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,7 @@ async function migrateLegacyProjectStorageIfNeeded(options?: {
838838
targetStorage = fallbackStorage;
839839
log.warn("Failed to persist migrated account storage", {
840840
from: legacyPath,
841-
to: state.currentStoragePath,
841+
to: currentStoragePath,
842842
error: String(error),
843843
});
844844
continue;
@@ -864,7 +864,7 @@ async function migrateLegacyProjectStorageIfNeeded(options?: {
864864

865865
log.info("Migrated legacy project account storage", {
866866
from: legacyPath,
867-
to: state.currentStoragePath,
867+
to: currentStoragePath,
868868
accounts: mergedStorage.accounts.length,
869869
});
870870
continue;
@@ -877,7 +877,7 @@ async function migrateLegacyProjectStorageIfNeeded(options?: {
877877
if (migrated) {
878878
return targetStorage;
879879
}
880-
if (targetStorage && !existsSync(state.currentStoragePath)) {
880+
if (targetStorage && !existsSync(currentStoragePath)) {
881881
return targetStorage;
882882
}
883883
return null;

test/storage.test.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { dirname, join } from "node:path";
55
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
66
import { getConfigDir, getProjectStorageKey } from "../lib/storage/paths.js";
77
import { setStoragePathState } from "../lib/storage/path-state.js";
8+
import { getIntentionalResetMarkerPath } from "../lib/storage/backup-paths.js";
89
import {
910
buildNamedBackupPath,
1011
clearAccounts,
@@ -1672,6 +1673,73 @@ describe("storage", () => {
16721673
}
16731674
});
16741675

1676+
it("does not export legacy accounts when an intentional reset marker appears during export fallback migration", async () => {
1677+
const currentStoragePath = join(
1678+
testWorkDir,
1679+
"accounts-reset-during-fallback-current.json",
1680+
);
1681+
const legacyStoragePath = join(
1682+
testWorkDir,
1683+
"accounts-reset-during-fallback-legacy.json",
1684+
);
1685+
const resetMarkerPath =
1686+
getIntentionalResetMarkerPath(currentStoragePath);
1687+
await fs.writeFile(
1688+
legacyStoragePath,
1689+
JSON.stringify({
1690+
version: 3,
1691+
activeIndex: 0,
1692+
activeIndexByFamily: {},
1693+
accounts: [
1694+
{
1695+
accountId: "legacy",
1696+
refreshToken: "legacy-token",
1697+
addedAt: 1,
1698+
lastUsed: 1,
1699+
},
1700+
],
1701+
}),
1702+
);
1703+
1704+
const actualStorageParser = await vi.importActual<
1705+
typeof import("../lib/storage/storage-parser.js")
1706+
>("../lib/storage/storage-parser.js");
1707+
vi.resetModules();
1708+
vi.doMock("../lib/storage/storage-parser.js", () => ({
1709+
...actualStorageParser,
1710+
loadAccountsFromPath: vi.fn(async (path, deps) => {
1711+
if (path === legacyStoragePath && !existsSync(resetMarkerPath)) {
1712+
await fs.writeFile(resetMarkerPath, "");
1713+
}
1714+
return actualStorageParser.loadAccountsFromPath(path, deps);
1715+
}),
1716+
}));
1717+
1718+
try {
1719+
const isolatedStorageModule = await import("../lib/storage.js");
1720+
const isolatedPathState = await import("../lib/storage/path-state.js");
1721+
isolatedPathState.setStoragePathState({
1722+
currentStoragePath,
1723+
currentLegacyProjectStoragePath: legacyStoragePath,
1724+
currentLegacyWorktreeStoragePath: null,
1725+
currentProjectRoot: null,
1726+
});
1727+
1728+
await expect(
1729+
isolatedStorageModule.exportAccounts(exportPath),
1730+
).rejects.toThrow(/No accounts to export/);
1731+
1732+
expect(existsSync(currentStoragePath)).toBe(false);
1733+
expect(existsSync(resetMarkerPath)).toBe(true);
1734+
expect(existsSync(legacyStoragePath)).toBe(true);
1735+
expect(existsSync(exportPath)).toBe(false);
1736+
} finally {
1737+
vi.doUnmock("../lib/storage/storage-parser.js");
1738+
vi.resetModules();
1739+
setStoragePathDirect(testStoragePath);
1740+
}
1741+
});
1742+
16751743
it("does not revive legacy accounts when the current storage reappears before export merges legacy storage", async () => {
16761744
const currentStoragePath = join(
16771745
testWorkDir,

0 commit comments

Comments
 (0)