mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-23 07:01:40 +00:00
test: harden no-isolate oauth contract coverage
This commit is contained in:
@@ -17,7 +17,27 @@ import { suggestOAuthProfileIdForLegacyDefault } from "./repair.js";
|
||||
import { ensureAuthProfileStore, saveAuthProfileStore } from "./store.js";
|
||||
import type { AuthProfileStore, OAuthCredential } from "./types.js";
|
||||
|
||||
const OAUTH_PROVIDER_IDS = new Set<string>(getOAuthProviders().map((provider) => provider.id));
|
||||
function listOAuthProviderIds(): string[] {
|
||||
if (typeof getOAuthProviders !== "function") {
|
||||
return [];
|
||||
}
|
||||
const providers = getOAuthProviders();
|
||||
if (!Array.isArray(providers)) {
|
||||
return [];
|
||||
}
|
||||
return providers
|
||||
.map((provider) =>
|
||||
provider &&
|
||||
typeof provider === "object" &&
|
||||
"id" in provider &&
|
||||
typeof provider.id === "string"
|
||||
? provider.id
|
||||
: undefined,
|
||||
)
|
||||
.filter((providerId): providerId is string => typeof providerId === "string");
|
||||
}
|
||||
|
||||
const OAUTH_PROVIDER_IDS = new Set<string>(listOAuthProviderIds());
|
||||
|
||||
let providerRuntimePromise:
|
||||
| Promise<typeof import("../../plugins/provider-runtime.runtime.js")>
|
||||
@@ -191,6 +211,9 @@ async function refreshOAuthTokenWithLock(params: {
|
||||
if (!oauthProvider) {
|
||||
return null;
|
||||
}
|
||||
if (typeof getOAuthApiKey !== "function") {
|
||||
return null;
|
||||
}
|
||||
return await getOAuthApiKey(oauthProvider, oauthCreds);
|
||||
})();
|
||||
if (!result) {
|
||||
|
||||
@@ -3,6 +3,7 @@ import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import type { DatabaseSync } from "node:sqlite";
|
||||
import { setTimeout as scheduleNativeTimeout } from "node:timers";
|
||||
import type { Mock } from "vitest";
|
||||
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
@@ -39,7 +40,7 @@ function createMockChild(params?: { autoClose?: boolean; closeDelayMs?: number }
|
||||
child.emit("close", 0);
|
||||
});
|
||||
} else {
|
||||
setTimeout(() => {
|
||||
scheduleNativeTimeout(() => {
|
||||
child.emit("close", 0);
|
||||
}, delayMs);
|
||||
}
|
||||
|
||||
@@ -13,13 +13,28 @@ import { requireProviderContractProvider as requireBundledProviderContractProvid
|
||||
const CONTRACT_SETUP_TIMEOUT_MS = 300_000;
|
||||
|
||||
const getOAuthApiKeyMock = vi.hoisted(() => vi.fn());
|
||||
const getOAuthProvidersMock = vi.hoisted(() =>
|
||||
vi.fn(() => [
|
||||
{ id: "anthropic", envApiKey: "ANTHROPIC_API_KEY", oauthTokenEnv: "ANTHROPIC_OAUTH_TOKEN" }, // pragma: allowlist secret
|
||||
{ id: "google", envApiKey: "GOOGLE_API_KEY", oauthTokenEnv: "GOOGLE_OAUTH_TOKEN" }, // pragma: allowlist secret
|
||||
{ id: "openai-codex", envApiKey: "OPENAI_API_KEY", oauthTokenEnv: "OPENAI_OAUTH_TOKEN" }, // pragma: allowlist secret
|
||||
{
|
||||
id: "qwen-portal",
|
||||
envApiKey: "QWEN_PORTAL_API_KEY",
|
||||
oauthTokenEnv: "QWEN_PORTAL_OAUTH_TOKEN",
|
||||
}, // pragma: allowlist secret
|
||||
]),
|
||||
);
|
||||
const refreshQwenPortalCredentialsMock = vi.hoisted(() => vi.fn());
|
||||
|
||||
vi.mock("@mariozechner/pi-ai/oauth", async () => {
|
||||
const actual = await vi.importActual<object>("@mariozechner/pi-ai/oauth");
|
||||
const actual = await vi.importActual<typeof import("@mariozechner/pi-ai/oauth")>(
|
||||
"@mariozechner/pi-ai/oauth",
|
||||
);
|
||||
return {
|
||||
...actual,
|
||||
getOAuthApiKey: getOAuthApiKeyMock,
|
||||
getOAuthProviders: getOAuthProvidersMock,
|
||||
};
|
||||
});
|
||||
|
||||
@@ -75,6 +90,7 @@ function requireProviderContractProvider(providerId: string): ProviderPlugin {
|
||||
describe("provider runtime contract", () => {
|
||||
beforeEach(() => {
|
||||
getOAuthApiKeyMock.mockReset();
|
||||
getOAuthProvidersMock.mockClear();
|
||||
refreshQwenPortalCredentialsMock.mockReset();
|
||||
}, CONTRACT_SETUP_TIMEOUT_MS);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user