Skip to content

Commit ce5f910

Browse files
committed
refactor: extract live sync wrapper
1 parent 8c4646c commit ce5f910

3 files changed

Lines changed: 97 additions & 4 deletions

File tree

index.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ import {
209209
} from "./lib/runtime/account-status.js";
210210
import { runBrowserOAuthFlow } from "./lib/runtime/browser-oauth-flow.js";
211211
import { handleRuntimeEvent } from "./lib/runtime/event-handler.js";
212+
import { ensureLiveAccountSyncEntry } from "./lib/runtime/live-sync-entry.js";
212213
import { buildManualOAuthFlow } from "./lib/runtime/manual-oauth-flow.js";
213214
import {
214215
applyPreemptiveQuotaSettingsFromConfig,
@@ -560,12 +561,13 @@ export const OpenAIOAuthPlugin: Plugin = async ({ client }: PluginInput) => {
560561
pluginConfig: ReturnType<typeof loadPluginConfig>,
561562
authFallback?: OAuthAuthDetails,
562563
): Promise<void> => {
563-
const next = await ensureLiveAccountSyncState({
564-
enabled: getLiveAccountSync(pluginConfig),
565-
targetPath: getStoragePath(),
564+
const next = await ensureLiveAccountSyncEntry({
565+
pluginConfig,
566+
authFallback,
566567
currentSync: liveAccountSync,
567568
currentPath: liveAccountSyncPath,
568-
authFallback,
569+
getLiveAccountSync,
570+
getStoragePath,
569571
createSync: (oauthFallback) =>
570572
new LiveAccountSync(
571573
async () => {
@@ -579,6 +581,7 @@ export const OpenAIOAuthPlugin: Plugin = async ({ client }: PluginInput) => {
579581
registerCleanup,
580582
logWarn,
581583
pluginName: PLUGIN_NAME,
584+
ensureLiveAccountSyncState,
582585
});
583586
liveAccountSync = next.liveAccountSync;
584587
liveAccountSyncPath = next.liveAccountSyncPath;

lib/runtime/live-sync-entry.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import type { OAuthAuthDetails } from "../types.js";
2+
3+
type LiveAccountSyncLike = {
4+
stop: () => void;
5+
syncToPath: (path: string) => Promise<void>;
6+
};
7+
8+
export async function ensureLiveAccountSyncEntry<
9+
TSync extends LiveAccountSyncLike,
10+
>(params: {
11+
pluginConfig: ReturnType<typeof import("../config.js").loadPluginConfig>;
12+
authFallback?: OAuthAuthDetails;
13+
currentSync: TSync | null;
14+
currentPath: string | null;
15+
getLiveAccountSync: (
16+
config: ReturnType<typeof import("../config.js").loadPluginConfig>,
17+
) => boolean;
18+
getStoragePath: () => string;
19+
createSync: (authFallback?: OAuthAuthDetails) => TSync;
20+
registerCleanup: (cleanup: () => void) => void;
21+
logWarn: (message: string) => void;
22+
pluginName: string;
23+
ensureLiveAccountSyncState: (args: {
24+
enabled: boolean;
25+
targetPath: string;
26+
currentSync: TSync | null;
27+
currentPath: string | null;
28+
authFallback?: OAuthAuthDetails;
29+
createSync: (authFallback?: OAuthAuthDetails) => TSync;
30+
registerCleanup: (cleanup: () => void) => void;
31+
logWarn: (message: string) => void;
32+
pluginName: string;
33+
}) => Promise<{
34+
liveAccountSync: TSync | null;
35+
liveAccountSyncPath: string | null;
36+
}>;
37+
}): Promise<{
38+
liveAccountSync: TSync | null;
39+
liveAccountSyncPath: string | null;
40+
}> {
41+
return params.ensureLiveAccountSyncState({
42+
enabled: params.getLiveAccountSync(params.pluginConfig),
43+
targetPath: params.getStoragePath(),
44+
currentSync: params.currentSync,
45+
currentPath: params.currentPath,
46+
authFallback: params.authFallback,
47+
createSync: params.createSync,
48+
registerCleanup: params.registerCleanup,
49+
logWarn: params.logWarn,
50+
pluginName: params.pluginName,
51+
});
52+
}

test/live-sync-entry.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { describe, expect, it, vi } from "vitest";
2+
import { ensureLiveAccountSyncEntry } from "../lib/runtime/live-sync-entry.js";
3+
4+
describe("live sync entry", () => {
5+
it("delegates plugin-config-derived arguments into the live sync state helper", async () => {
6+
const ensureLiveAccountSyncState = vi.fn(async () => ({
7+
liveAccountSync: { stop: vi.fn(), syncToPath: vi.fn() },
8+
liveAccountSyncPath: "/tmp/accounts.json",
9+
}));
10+
11+
const result = await ensureLiveAccountSyncEntry({
12+
pluginConfig: {} as never,
13+
authFallback: {
14+
type: "oauth",
15+
accessToken: "a",
16+
refreshToken: "r",
17+
} as never,
18+
currentSync: null,
19+
currentPath: null,
20+
getLiveAccountSync: () => true,
21+
getStoragePath: () => "/tmp/accounts.json",
22+
createSync: vi.fn(() => ({ stop: vi.fn(), syncToPath: vi.fn() })),
23+
registerCleanup: vi.fn(),
24+
logWarn: vi.fn(),
25+
pluginName: "plugin",
26+
ensureLiveAccountSyncState,
27+
});
28+
29+
expect(ensureLiveAccountSyncState).toHaveBeenCalledWith(
30+
expect.objectContaining({
31+
enabled: true,
32+
targetPath: "/tmp/accounts.json",
33+
pluginName: "plugin",
34+
}),
35+
);
36+
expect(result.liveAccountSyncPath).toBe("/tmp/accounts.json");
37+
});
38+
});

0 commit comments

Comments
 (0)