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