Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions lib/codex-manager/experimental-sync-target-entry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import type { AccountStorageV3 } from "../storage.js";

export async function loadExperimentalSyncTargetEntry(params: {
loadExperimentalSyncTargetState: (args: {
detectTarget: () => ReturnType<
typeof import("../oc-chatgpt-target-detection.js").detectOcChatgptMultiAuthTarget
>;
readJson: (path: string) => Promise<unknown>;
normalizeAccountStorage: (value: unknown) => AccountStorageV3 | null;
}) => Promise<
| {
kind: "blocked-ambiguous";
detection: ReturnType<
typeof import("../oc-chatgpt-target-detection.js").detectOcChatgptMultiAuthTarget
>;
}
| {
kind: "blocked-none";
detection: ReturnType<
typeof import("../oc-chatgpt-target-detection.js").detectOcChatgptMultiAuthTarget
>;
}
| { kind: "error"; message: string }
| {
kind: "target";
detection: ReturnType<
typeof import("../oc-chatgpt-target-detection.js").detectOcChatgptMultiAuthTarget
>;
destination: AccountStorageV3 | null;
}
>;
detectTarget: () => ReturnType<
typeof import("../oc-chatgpt-target-detection.js").detectOcChatgptMultiAuthTarget
>;
readFileWithRetry: (
path: string,
options: {
retryableCodes: Set<string>;
maxAttempts: number;
sleep: (ms: number) => Promise<void>;
},
) => Promise<string>;
normalizeAccountStorage: (value: unknown) => AccountStorageV3 | null;
sleep: (ms: number) => Promise<void>;
}): ReturnType<typeof params.loadExperimentalSyncTargetState> {
return params.loadExperimentalSyncTargetState({
detectTarget: params.detectTarget,
readJson: async (path) =>
JSON.parse(
await params.readFileWithRetry(path, {
retryableCodes: new Set([
"EBUSY",
"EPERM",
"EAGAIN",
"ENOTEMPTY",
"EACCES",
]),
maxAttempts: 4,
sleep: params.sleep,
}),
),
normalizeAccountStorage: params.normalizeAccountStorage,
});
}
20 changes: 5 additions & 15 deletions lib/codex-manager/settings-hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ import {
mapExperimentalStatusHotkey,
} from "./experimental-settings-schema.js";
import { loadExperimentalSyncTargetState } from "./experimental-sync-target.js";
import { loadExperimentalSyncTargetEntry } from "./experimental-sync-target-entry.js";
import { promptSettingsHubEntry } from "./settings-hub-entry.js";
import {
buildSettingsHubItems,
Expand Down Expand Up @@ -648,23 +649,12 @@ async function loadExperimentalSyncTarget(): Promise<
destination: import("../storage.js").AccountStorageV3 | null;
}
> {
return loadExperimentalSyncTargetState({
return loadExperimentalSyncTargetEntry({
loadExperimentalSyncTargetState,
detectTarget: detectOcChatgptMultiAuthTarget,
readJson: async (path) =>
JSON.parse(
await readFileWithRetry(path, {
retryableCodes: new Set([
"EBUSY",
"EPERM",
"EAGAIN",
"ENOTEMPTY",
"EACCES",
]),
maxAttempts: 4,
sleep,
}),
),
readFileWithRetry,
normalizeAccountStorage,
sleep,
});
}

Expand Down
27 changes: 27 additions & 0 deletions test/experimental-sync-target-entry.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { describe, expect, it, vi } from "vitest";
import { loadExperimentalSyncTargetEntry } from "../lib/codex-manager/experimental-sync-target-entry.js";

describe("experimental sync target entry", () => {
it("delegates retrying file read and normalization through the target loader", async () => {
const loadExperimentalSyncTargetState = vi.fn(async () => ({
kind: "target",
detection: { kind: "target" },
destination: null,
}));

const result = await loadExperimentalSyncTargetEntry({
loadExperimentalSyncTargetState,
detectTarget: () => ({ kind: "target" }) as never,
readFileWithRetry: vi.fn(async () => "{}"),
normalizeAccountStorage: vi.fn(() => null),
sleep: vi.fn(async () => undefined),
});

expect(loadExperimentalSyncTargetState).toHaveBeenCalled();
expect(result).toEqual({
kind: "target",
detection: { kind: "target" },
destination: null,
});
});
});