Skip to content

Commit 16e09d1

Browse files
committed
Fix release branch review follow-ups
1 parent 6ba04b7 commit 16e09d1

6 files changed

Lines changed: 83 additions & 19 deletions

File tree

lib/named-backup-export.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,12 +229,12 @@ export async function exportNamedBackupFile(
229229
const destination = resolveNamedBackupPath(name, storagePath);
230230
const backupRoot = getNamedBackupRoot(storagePath);
231231
const exportKey = normalizePathForComparison(destination);
232-
if (!options?.force && inFlightNamedBackupExports.has(exportKey)) {
232+
if (inFlightNamedBackupExports.has(exportKey)) {
233233
throw new Error(`File already exists: ${destination}`);
234234
}
235+
inFlightNamedBackupExports.add(exportKey);
235236
assertWithinDirectory(dirname(backupRoot), backupRoot);
236237
await fs.mkdir(backupRoot, { recursive: true });
237-
inFlightNamedBackupExports.add(exportKey);
238238
try {
239239
await dependencies.exportAccounts(
240240
destination,

lib/runtime/auth-facade.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,28 @@ export async function runRuntimeOAuthFlow(
1919
pluginName: string;
2020
},
2121
): Promise<TokenResult> {
22+
const pluginPrefix = `[${deps.pluginName}]`;
23+
const prefixLogMessage = (
24+
message: string,
25+
options?: { leadingNewline?: boolean },
26+
): string => {
27+
if (
28+
message.startsWith(pluginPrefix) ||
29+
message.startsWith(`\n${pluginPrefix}`)
30+
) {
31+
return message;
32+
}
33+
return options?.leadingNewline
34+
? `\n${pluginPrefix} ${message}`
35+
: `${pluginPrefix} ${message}`;
36+
};
2237
return deps.runBrowserOAuthFlow({
2338
forceNewLogin,
2439
manualModeLabel: deps.manualModeLabel,
2540
logInfo: deps.logInfo,
26-
logDebug: deps.logDebug,
27-
logWarn: deps.logWarn,
41+
logDebug: (message) => deps.logDebug(prefixLogMessage(message)),
42+
logWarn: (message) =>
43+
deps.logWarn(prefixLogMessage(message, { leadingNewline: true })),
2844
});
2945
}
3046

lib/storage/import-export.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,16 @@ export function mergeImportedAccounts(params: {
130130
}
131131

132132
const deduplicatedAccounts = params.deduplicateAccounts(merged);
133+
const deduplicatedExistingAccounts =
134+
params.deduplicateAccounts(existingAccounts);
133135
const newStorage: AccountStorageV3 = {
134136
version: 3,
135137
accounts: deduplicatedAccounts,
136138
activeIndex: existingActiveIndex,
137139
activeIndexByFamily: params.existing?.activeIndexByFamily,
138140
};
139-
const importedCount = deduplicatedAccounts.length - existingAccounts.length;
141+
const importedCount =
142+
deduplicatedAccounts.length - deduplicatedExistingAccounts.length;
140143
const skippedCount = params.imported.accounts.length - importedCount;
141144
return {
142145
newStorage,

test/import-export.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,32 @@ describe("import export helpers", () => {
3232
expect(result.imported).toBe(1);
3333
});
3434

35+
it("counts imports against deduplicated existing storage", () => {
36+
const result = mergeImportedAccounts({
37+
existing: {
38+
version: 3,
39+
accounts: [{ refreshToken: "a" }, { refreshToken: "a" }],
40+
activeIndex: 0,
41+
activeIndexByFamily: {},
42+
},
43+
imported: {
44+
version: 3,
45+
accounts: [{ refreshToken: "b" }],
46+
activeIndex: 0,
47+
activeIndexByFamily: {},
48+
},
49+
maxAccounts: 10,
50+
deduplicateAccounts: (accounts) =>
51+
Array.from(
52+
new Map(accounts.map((account) => [account.refreshToken, account])).values(),
53+
),
54+
});
55+
56+
expect(result.total).toBe(2);
57+
expect(result.imported).toBe(1);
58+
expect(result.skipped).toBe(0);
59+
});
60+
3561
it("throws for invalid import payloads and empty exports", async () => {
3662
await expect(
3763
readImportFile({

test/index-retry.test.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -324,15 +324,25 @@ vi.mock("../lib/accounts.js", async () => {
324324
};
325325
});
326326

327-
vi.mock("../lib/storage.js", () => ({
328-
getStoragePath: () => "",
329-
loadAccounts: async () => null,
330-
saveAccounts: async () => {},
331-
setStoragePath: () => {},
332-
setStorageBackupEnabled: () => {},
333-
exportAccounts: async () => {},
334-
importAccounts: async () => ({ imported: 0, total: 0 }),
335-
}));
327+
vi.mock("../lib/storage.js", async () => {
328+
const actual = await vi.importActual("../lib/storage.js");
329+
return {
330+
...actual,
331+
getStoragePath: () => "",
332+
loadAccounts: async () => null,
333+
saveAccounts: async () => {},
334+
withAccountStorageTransaction: async <T>(
335+
handler: (
336+
loadedStorage: null,
337+
persist: (storage: unknown) => Promise<void>,
338+
) => Promise<T>,
339+
): Promise<T> => handler(null, async (_storage: unknown) => {}),
340+
setStoragePath: () => {},
341+
setStorageBackupEnabled: () => {},
342+
exportAccounts: async () => {},
343+
importAccounts: async () => ({ imported: 0, total: 0 }),
344+
};
345+
});
336346

337347
vi.mock("../lib/recovery.js", () => ({
338348
createSessionRecoveryHook: () => null,

test/runtime-account-selection.test.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ function createTokens(): TokenSuccess {
2424
};
2525
}
2626

27+
function createDeps(logInfo = vi.fn()) {
28+
return {
29+
envAccountId: process.env.CODEX_AUTH_ACCOUNT_ID,
30+
logInfo,
31+
getAccountIdCandidates,
32+
selectBestAccountCandidate,
33+
};
34+
}
35+
2736
describe("resolveAccountSelection", () => {
2837
beforeEach(() => {
2938
vi.clearAllMocks();
@@ -39,7 +48,7 @@ describe("resolveAccountSelection", () => {
3948
const tokens = createTokens();
4049
const logInfo = vi.fn();
4150

42-
const result = resolveAccountSelection(tokens, { logInfo });
51+
const result = resolveAccountSelection(tokens, createDeps(logInfo));
4352

4453
expect(result).toEqual({
4554
...tokens,
@@ -58,7 +67,7 @@ describe("resolveAccountSelection", () => {
5867
vi.mocked(getAccountIdCandidates).mockReturnValueOnce([]);
5968
const tokens = createTokens();
6069

61-
const result = resolveAccountSelection(tokens, { logInfo: vi.fn() });
70+
const result = resolveAccountSelection(tokens, createDeps());
6271

6372
expect(result).toBe(tokens);
6473
expect(selectBestAccountCandidate).not.toHaveBeenCalled();
@@ -75,7 +84,7 @@ describe("resolveAccountSelection", () => {
7584
]);
7685
const tokens = createTokens();
7786

78-
const result = resolveAccountSelection(tokens, { logInfo: vi.fn() });
87+
const result = resolveAccountSelection(tokens, createDeps());
7988

8089
expect(result).toEqual({
8190
...tokens,
@@ -112,7 +121,7 @@ describe("resolveAccountSelection", () => {
112121
vi.mocked(selectBestAccountCandidate).mockReturnValueOnce(candidates[1]);
113122
const tokens = createTokens();
114123

115-
const result = resolveAccountSelection(tokens, { logInfo: vi.fn() });
124+
const result = resolveAccountSelection(tokens, createDeps());
116125

117126
expect(selectBestAccountCandidate).toHaveBeenCalledWith(candidates);
118127
expect(result).toEqual({
@@ -154,7 +163,7 @@ describe("resolveAccountSelection", () => {
154163
vi.mocked(selectBestAccountCandidate).mockReturnValueOnce(null);
155164
const tokens = createTokens();
156165

157-
const result = resolveAccountSelection(tokens, { logInfo: vi.fn() });
166+
const result = resolveAccountSelection(tokens, createDeps());
158167

159168
expect(selectBestAccountCandidate).toHaveBeenCalledWith(candidates);
160169
expect(result).toBe(tokens);

0 commit comments

Comments
 (0)