Skip to content

Commit 4735177

Browse files
committed
Avoid extra footer storage reads during loader init
1 parent 15867c9 commit 4735177

2 files changed

Lines changed: 29 additions & 4 deletions

File tree

index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,7 +1158,7 @@ export const OpenAIOAuthPlugin: Plugin = async ({ client }: PluginInput) => {
11581158
const updatePersistedAccountCountHint = (
11591159
count: number | null | undefined,
11601160
): void => {
1161-
if (!Number.isFinite(count) || count === undefined || count === null) {
1161+
if (typeof count !== "number" || !Number.isFinite(count)) {
11621162
return;
11631163
}
11641164
persistedAccountCountHint = Math.max(0, Math.trunc(count));
@@ -1993,9 +1993,7 @@ export const OpenAIOAuthPlugin: Plugin = async ({ client }: PluginInput) => {
19931993
}
19941994
let accountManager = await accountManagerPromise;
19951995
cachedAccountManager = accountManager;
1996-
updatePersistedAccountCountHint(
1997-
(await loadAccounts())?.accounts.length ?? accountManager.getAccountCount(),
1998-
);
1996+
updatePersistedAccountCountHint(accountManager.getAccountCount());
19991997
const refreshToken = authFallback?.refresh ?? "";
20001998
const needsPersist =
20011999
refreshToken &&

test/index.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2201,6 +2201,33 @@ describe("OpenAIOAuthPlugin fetch handler", () => {
22012201
expect(storageModule.loadAccounts).not.toHaveBeenCalled();
22022202
});
22032203

2204+
it("does not add storage reads during loader init when footer counts are enabled", async () => {
2205+
const storageModule = await import("../lib/storage.js");
2206+
const getAuth = async () => ({
2207+
type: "oauth" as const,
2208+
access: "access-token",
2209+
refresh: "refresh-token",
2210+
expires: Date.now() + 60_000,
2211+
multiAccount: true,
2212+
});
2213+
const runLoaderAndCountStorageReads = async (): Promise<number> => {
2214+
const mockClient = createMockClient();
2215+
const { OpenAIOAuthPlugin } = await import("../index.js");
2216+
const plugin = await OpenAIOAuthPlugin({ client: mockClient } as never) as unknown as PluginType;
2217+
vi.mocked(storageModule.loadAccounts).mockClear();
2218+
await plugin.auth.loader(getAuth, { options: {}, models: {} });
2219+
return vi.mocked(storageModule.loadAccounts).mock.calls.length;
2220+
};
2221+
2222+
await disablePersistedFooter();
2223+
const baselineReadCount = await runLoaderAndCountStorageReads();
2224+
2225+
await enablePersistedFooter("full-email");
2226+
const footerReadCount = await runLoaderAndCountStorageReads();
2227+
2228+
expect(footerReadCount).toBe(baselineReadCount);
2229+
});
2230+
22042231
it("uses the live account count when the cached footer hint is stale", async () => {
22052232
await enablePersistedFooter("full-email");
22062233
mockStorage.accounts = [

0 commit comments

Comments
 (0)