test: speed up auth profile store tests

This commit is contained in:
Peter Steinberger
2026-04-07 23:17:39 +01:00
parent b2169c4295
commit d471bbc94d
5 changed files with 24 additions and 6 deletions

View File

@@ -10,6 +10,10 @@ import {
import { AUTH_STORE_VERSION, log } from "./auth-profiles/constants.js";
import type { AuthProfileCredential } from "./auth-profiles/types.js";
vi.mock("../plugins/provider-runtime.js", () => ({
resolveExternalAuthProfilesWithPlugins: () => [],
}));
describe("ensureAuthProfileStore", () => {
function withTempAgentDir<T>(prefix: string, run: (agentDir: string) => T): T {
const agentDir = fs.mkdtempSync(path.join(os.tmpdir(), prefix));

View File

@@ -22,6 +22,10 @@ vi.mock("./auth-profiles/external-cli-sync.js", () => ({
syncExternalCliCredentials: mocks.syncExternalCliCredentials,
}));
vi.mock("../plugins/provider-runtime.js", () => ({
resolveExternalAuthProfilesWithPlugins: () => [],
}));
let clearRuntimeAuthProfileStoreSnapshots: typeof import("./auth-profiles.js").clearRuntimeAuthProfileStoreSnapshots;
let loadAuthProfileStoreForRuntime: typeof import("./auth-profiles.js").loadAuthProfileStoreForRuntime;

View File

@@ -15,6 +15,10 @@ vi.mock("./auth-profiles/external-cli-sync.js", () => ({
syncExternalCliCredentials: mocks.syncExternalCliCredentials,
}));
vi.mock("../plugins/provider-runtime.js", () => ({
resolveExternalAuthProfilesWithPlugins: () => [],
}));
let clearRuntimeAuthProfileStoreSnapshots: typeof import("./auth-profiles.js").clearRuntimeAuthProfileStoreSnapshots;
let ensureAuthProfileStore: typeof import("./auth-profiles.js").ensureAuthProfileStore;

View File

@@ -34,6 +34,10 @@ vi.mock("../../plugins/provider-runtime.runtime.js", () => ({
refreshProviderOAuthCredentialWithPlugin: async () => null,
}));
vi.mock("../plugins/provider-runtime.js", () => ({
resolveExternalAuthProfilesWithPlugins: () => [],
}));
let clearRuntimeAuthProfileStoreSnapshots: typeof import("./store.js").clearRuntimeAuthProfileStoreSnapshots;
let ensureAuthProfileStore: typeof import("./store.js").ensureAuthProfileStore;
let resolveApiKeyForProfile: typeof import("./oauth.js").resolveApiKeyForProfile;

View File

@@ -1,7 +1,7 @@
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import { resetFileLockStateForTest } from "../../infra/file-lock.js";
import { captureEnv } from "../../test-utils/env.js";
import {
@@ -66,10 +66,9 @@ vi.mock("../../plugins/provider-runtime.runtime.js", () => ({
buildProviderAuthDoctorHintWithPlugin: buildProviderAuthDoctorHintWithPluginMock,
}));
async function loadFreshOAuthModuleForTest() {
vi.resetModules();
({ resolveApiKeyForProfile } = await import("./oauth.js"));
}
vi.mock("../plugins/provider-runtime.js", () => ({
resolveExternalAuthProfilesWithPlugins: () => [],
}));
async function readPersistedStore(agentDir: string): Promise<AuthProfileStore> {
return JSON.parse(
@@ -105,6 +104,10 @@ describe("resolveApiKeyForProfile openai-codex refresh fallback", () => {
let tempRoot = "";
let agentDir = "";
beforeAll(async () => {
({ resolveApiKeyForProfile } = await import("./oauth.js"));
});
beforeEach(async () => {
resetFileLockStateForTest();
getOAuthApiKeyMock.mockReset();
@@ -128,7 +131,6 @@ describe("resolveApiKeyForProfile openai-codex refresh fallback", () => {
process.env.OPENCLAW_STATE_DIR = tempRoot;
process.env.OPENCLAW_AGENT_DIR = agentDir;
process.env.PI_CODING_AGENT_DIR = agentDir;
await loadFreshOAuthModuleForTest();
});
afterEach(async () => {