test: isolate oauth refresh queue mocks

This commit is contained in:
Peter Steinberger
2026-04-17 18:10:07 +01:00
parent 89d3117ad0
commit dfca5bd0fe

View File

@@ -18,6 +18,12 @@ async function loadOAuthModuleForTest() {
({ resolveApiKeyForProfile, resetOAuthRefreshQueuesForTest } = await import("./oauth.js"));
}
function resolveApiKeyForProfileInTest(
params: Omit<Parameters<typeof resolveApiKeyForProfile>[0], "cfg">,
) {
return resolveApiKeyForProfile({ cfg: {}, ...params });
}
const {
refreshProviderOAuthCredentialWithPluginMock,
formatProviderAuthProfileApiKeyWithPluginMock,
@@ -35,16 +41,36 @@ vi.mock("../cli-credentials.js", () => ({
writeCodexCliCredentials: () => true,
}));
vi.mock("@mariozechner/pi-ai/oauth", () => ({
getOAuthApiKey: vi.fn(async () => null),
getOAuthProviders: () => [{ id: "openai-codex" }],
}));
vi.mock("../../plugins/provider-runtime.runtime.js", () => ({
formatProviderAuthProfileApiKeyWithPlugin: (params: { context?: { access?: string } }) =>
formatProviderAuthProfileApiKeyWithPluginMock() ?? params?.context?.access,
refreshProviderOAuthCredentialWithPlugin: refreshProviderOAuthCredentialWithPluginMock,
}));
vi.mock("../../infra/file-lock.js", () => ({
resetFileLockStateForTest: () => undefined,
withFileLock: async <T>(_filePath: string, _options: unknown, run: () => Promise<T>) => run(),
}));
vi.mock("../../plugin-sdk/file-lock.js", () => ({
resetFileLockStateForTest: () => undefined,
withFileLock: async <T>(_filePath: string, _options: unknown, run: () => Promise<T>) => run(),
}));
vi.mock("./doctor.js", () => ({
formatAuthDoctorHint: async () => undefined,
}));
vi.mock("./external-auth.js", () => ({
overlayExternalAuthProfiles: <T>(store: T) => store,
shouldPersistExternalAuthProfile: () => true,
}));
vi.mock("./external-cli-sync.js", () => ({
syncExternalCliCredentials: () => false,
readManagedExternalCliCredential: () => null,
@@ -70,7 +96,11 @@ function createExpiredOauthStore(params: {
}
describe("OAuth refresh in-process queue", () => {
const envSnapshot = captureEnv(["OPENCLAW_STATE_DIR"]);
const envSnapshot = captureEnv([
"OPENCLAW_STATE_DIR",
"OPENCLAW_AGENT_DIR",
"PI_CODING_AGENT_DIR",
]);
let tempRoot = "";
let agentDir = "";
@@ -84,6 +114,8 @@ describe("OAuth refresh in-process queue", () => {
tempRoot = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-oauth-queue-"));
process.env.OPENCLAW_STATE_DIR = tempRoot;
agentDir = path.join(tempRoot, "agents", "main", "agent");
process.env.OPENCLAW_AGENT_DIR = agentDir;
process.env.PI_CODING_AGENT_DIR = agentDir;
await fs.mkdir(agentDir, { recursive: true });
await loadOAuthModuleForTest();
resetOAuthRefreshQueuesForTest();
@@ -127,17 +159,17 @@ describe("OAuth refresh in-process queue", () => {
// Fire three resolves concurrently against the same agent+profile.
const results = await Promise.all([
resolveApiKeyForProfile({
resolveApiKeyForProfileInTest({
store: ensureAuthProfileStore(agentDir),
profileId,
agentDir,
}).catch((e) => e),
resolveApiKeyForProfile({
resolveApiKeyForProfileInTest({
store: ensureAuthProfileStore(agentDir),
profileId,
agentDir,
}).catch((e) => e),
resolveApiKeyForProfile({
resolveApiKeyForProfileInTest({
store: ensureAuthProfileStore(agentDir),
profileId,
agentDir,
@@ -172,12 +204,12 @@ describe("OAuth refresh in-process queue", () => {
});
const [first, second] = await Promise.all([
resolveApiKeyForProfile({
resolveApiKeyForProfileInTest({
store: ensureAuthProfileStore(agentDir),
profileId,
agentDir,
}).catch((e) => e),
resolveApiKeyForProfile({
resolveApiKeyForProfileInTest({
store: ensureAuthProfileStore(agentDir),
profileId,
agentDir,
@@ -239,7 +271,7 @@ describe("OAuth refresh in-process queue", () => {
const results = await Promise.all(
Array.from({ length: 10 }, () =>
resolveApiKeyForProfile({
resolveApiKeyForProfileInTest({
store: ensureAuthProfileStore(agentDir),
profileId,
agentDir,