Skip to content

Commit dc2347e

Browse files
committed
fix expired cli cache refresh hydration
1 parent 093dcde commit dc2347e

2 files changed

Lines changed: 42 additions & 5 deletions

File tree

lib/accounts.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,7 @@ export class AccountManager {
188188
changed = true;
189189
}
190190

191-
if (
192-
cachedAccessUsable &&
193-
cached.refreshToken &&
194-
!account.refreshToken
195-
) {
191+
if (cached.refreshToken && !account.refreshToken) {
196192
account.refreshToken = cached.refreshToken;
197193
changed = true;
198194
}

test/accounts-edge.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,47 @@ describe("accounts edge branches", () => {
231231
expect(mockSaveAccounts).not.toHaveBeenCalled();
232232
});
233233

234+
it("hydrates a missing local refresh token from an expired CLI cache entry", async () => {
235+
const now = Date.now();
236+
const stored = buildStored([
237+
buildStoredAccount({
238+
refreshToken: "local-refresh-placeholder",
239+
email: "expired@example.com",
240+
accessToken: "local-access",
241+
expiresAt: now + 120_000,
242+
}),
243+
]);
244+
245+
const { AccountManager } = await importAccountsModule();
246+
const manager = new AccountManager(undefined, stored as never);
247+
const account = manager.getAccountByIndex(0)!;
248+
account.refreshToken = "";
249+
250+
mockLoadCodexCliState.mockResolvedValue({
251+
sourceUpdatedAtMs: now - 60_000,
252+
accounts: [
253+
{
254+
email: "expired@example.com",
255+
accessToken: "cached-access-old",
256+
expiresAt: now - 1,
257+
refreshToken: "cached-refresh-restored",
258+
accountId: "expired-account-id",
259+
},
260+
],
261+
});
262+
263+
const hydrate = getPrivate<() => Promise<void>>(
264+
manager as object,
265+
"hydrateFromCodexCli",
266+
);
267+
await hydrate.call(manager);
268+
269+
const snapshot = manager.getAccountsSnapshot();
270+
expect(snapshot[0]?.refreshToken).toBe("cached-refresh-restored");
271+
expect(snapshot[0]?.access).toBe("local-access");
272+
expect(snapshot[0]?.accountId).toBe("expired-account-id");
273+
});
274+
234275
it("returns early when Codex CLI state has no usable cache entries", async () => {
235276
const stored = buildStored([
236277
buildStoredAccount({

0 commit comments

Comments
 (0)