Some checks failed
ClawSweeper Dispatch / dispatch (push) Has been cancelled
CodeQL / Security High (actions) (push) Has been cancelled
CodeQL / Security High (channel-runtime-boundary) (push) Has been cancelled
CodeQL / Security High (core-auth-secrets) (push) Has been cancelled
CodeQL / Security High (mcp-process-tool-boundary) (push) Has been cancelled
CodeQL / Security High (network-ssrf-boundary) (push) Has been cancelled
CodeQL / Security High (plugin-trust-boundary) (push) Has been cancelled
CodeQL / Security High (process-exec-boundary) (push) Has been cancelled
Docs Sync Publish Repo / sync-publish-repo (push) Has been cancelled
Docs / docs (push) Has been cancelled
OpenClaw Stable Main Closeout / Resolve stable release closeout inputs (push) Has been cancelled
OpenClaw Stable Main Closeout / Verify stable main closeout (push) Has been cancelled
Workflow Sanity / no-tabs (push) Has been cancelled
Workflow Sanity / actionlint (push) Has been cancelled
Workflow Sanity / generated-doc-baselines (push) Has been cancelled
CI / runner-admission (push) Has been cancelled
CI / preflight (push) Has been cancelled
CI / security-fast (push) Has been cancelled
CI / pnpm-store-warmup (push) Has been cancelled
CI / build-artifacts (push) Has been cancelled
CI / native-i18n (push) Has been cancelled
CI / ${{ matrix.check_name }} (push) Has been cancelled
CI / ${{ matrix.checkName }} (push) Has been cancelled
CI / checks-node-compat-node22 (push) Has been cancelled
CI / check-bundled-channel-config-metadata (push) Has been cancelled
CI / check-dependencies (push) Has been cancelled
CI / check-guards (push) Has been cancelled
CI / check-lint (push) Has been cancelled
CI / check-prod-types (push) Has been cancelled
CI / check-shrinkwrap (push) Has been cancelled
CI / check-test-types (push) Has been cancelled
CI / check-additional-boundaries-a (push) Has been cancelled
CI / check-additional-boundaries-bcd (push) Has been cancelled
CI / check-additional-extension-bundled (push) Has been cancelled
CI / check-additional-extension-channels (push) Has been cancelled
CI / check-additional-extension-package-boundary (push) Has been cancelled
CI / check-additional-runtime-topology-architecture (push) Has been cancelled
CI / check-session-accessor-boundary (push) Has been cancelled
CI / check-session-transcript-reader-boundary (push) Has been cancelled
CI / check-docs (push) Has been cancelled
CI / skills-python (push) Has been cancelled
CI / macos-swift (push) Has been cancelled
CI / ios-build (push) Has been cancelled
CI / ci-timings-summary (push) Has been cancelled
Native App Locale Refresh / Refresh native fa (push) Has been cancelled
Native App Locale Refresh / Refresh native fr (push) Has been cancelled
Native App Locale Refresh / Refresh native hi (push) Has been cancelled
Native App Locale Refresh / Refresh native id (push) Has been cancelled
Native App Locale Refresh / Refresh native it (push) Has been cancelled
Native App Locale Refresh / Refresh native ja-JP (push) Has been cancelled
Control UI Locale Refresh / plan (push) Has been cancelled
Control UI Locale Refresh / Refresh ${{ matrix.locale }} (push) Has been cancelled
Control UI Locale Refresh / Commit control UI locale refresh (push) Has been cancelled
Live Media Runner Image / Build live media runner image (push) Has been cancelled
Native App Locale Refresh / Refresh native ar (push) Has been cancelled
Native App Locale Refresh / Refresh native de (push) Has been cancelled
Native App Locale Refresh / Refresh native es (push) Has been cancelled
Native App Locale Refresh / Refresh native ko (push) Has been cancelled
Native App Locale Refresh / Refresh native nl (push) Has been cancelled
Native App Locale Refresh / Refresh native pl (push) Has been cancelled
Native App Locale Refresh / Refresh native pt-BR (push) Has been cancelled
Native App Locale Refresh / Refresh native ru (push) Has been cancelled
Native App Locale Refresh / Refresh native sv (push) Has been cancelled
Native App Locale Refresh / Refresh native th (push) Has been cancelled
Native App Locale Refresh / Refresh native tr (push) Has been cancelled
Native App Locale Refresh / Refresh native uk (push) Has been cancelled
Native App Locale Refresh / Refresh native vi (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-CN (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-TW (push) Has been cancelled
Native App Locale Refresh / Commit native locale refresh (push) Has been cancelled
Plugin Init Scaffold Validation / Validate provider scaffold (push) Has been cancelled
Plugin NPM Release / preview_plugins_npm (push) Has been cancelled
Plugin NPM Release / Validate release publish approval (push) Has been cancelled
Plugin NPM Release / preview_plugin_pack (push) Has been cancelled
Plugin NPM Release / publish_plugins_npm (push) Has been cancelled
Sandbox Common Smoke / sandbox-common-smoke (push) Has been cancelled
Website Installer Sync / static (push) Has been cancelled
Website Installer Sync / linux-docker (push) Has been cancelled
Website Installer Sync / macos-installer (push) Has been cancelled
Website Installer Sync / windows-installer (push) Has been cancelled
Website Installer Sync / sync-website (push) Has been cancelled
Adolf is a fork/vendored clone of github.com/openclaw/openclaw (v2026.6.11), free to diverge. Tree copied sans upstream .git; upstream remote added for future syncs. Node pinned to 24 (.nvmrc); engines already require >=22.19. Preserves docs/ARCHITECTURE.md. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LeqyaxJF2nbRXJtae2kNB2
302 lines
11 KiB
TypeScript
302 lines
11 KiB
TypeScript
// Google tests cover api plugin behavior.
|
|
import { describe, expect, it } from "vitest";
|
|
import {
|
|
isGoogleGenerativeAiApi,
|
|
isGoogleVertexBaseUrl,
|
|
isGoogleVertexHostname,
|
|
normalizeGoogleApiBaseUrl,
|
|
normalizeGoogleGenerativeAiBaseUrl,
|
|
normalizeGoogleProviderConfig,
|
|
parseGeminiAuth,
|
|
resolveGoogleGenerativeAiHttpRequestConfig,
|
|
resolveGoogleGenerativeAiApiOrigin,
|
|
resolveGoogleGenerativeAiTransport,
|
|
shouldNormalizeGoogleGenerativeAiProviderConfig,
|
|
} from "./api.js";
|
|
|
|
describe("google generative ai helpers", () => {
|
|
it("detects the Google Generative AI transport id", () => {
|
|
expect(isGoogleGenerativeAiApi("google-generative-ai")).toBe(true);
|
|
expect(isGoogleGenerativeAiApi("google-gemini-cli")).toBe(false);
|
|
expect(isGoogleGenerativeAiApi(undefined)).toBe(false);
|
|
});
|
|
|
|
it("normalizes only explicit Google Generative AI baseUrls", () => {
|
|
expect(normalizeGoogleGenerativeAiBaseUrl("https://generativelanguage.googleapis.com")).toBe(
|
|
"https://generativelanguage.googleapis.com/v1beta",
|
|
);
|
|
expect(normalizeGoogleGenerativeAiBaseUrl("https://proxy.example.com/google/v1beta")).toBe(
|
|
"https://proxy.example.com/google/v1beta",
|
|
);
|
|
expect(normalizeGoogleGenerativeAiBaseUrl("https://aiplatform.googleapis.com")).toBe(
|
|
"https://aiplatform.googleapis.com",
|
|
);
|
|
expect(normalizeGoogleGenerativeAiBaseUrl("proxy/generativelanguage.googleapis.com")).toBe(
|
|
"proxy/generativelanguage.googleapis.com",
|
|
);
|
|
expect(normalizeGoogleGenerativeAiBaseUrl("generativelanguage.googleapis.com")).toBe(
|
|
"generativelanguage.googleapis.com",
|
|
);
|
|
expect(normalizeGoogleGenerativeAiBaseUrl("https://xgenerativelanguage.googleapis.com")).toBe(
|
|
"https://xgenerativelanguage.googleapis.com",
|
|
);
|
|
expect(normalizeGoogleGenerativeAiBaseUrl()).toBeUndefined();
|
|
});
|
|
|
|
it("keeps /openai on generic Google base URL normalization and strips it only for native Gemini callers", () => {
|
|
expect(
|
|
normalizeGoogleApiBaseUrl("https://generativelanguage.googleapis.com/v1beta/openai"),
|
|
).toBe("https://generativelanguage.googleapis.com/v1beta/openai");
|
|
expect(
|
|
normalizeGoogleGenerativeAiBaseUrl("https://generativelanguage.googleapis.com/v1beta/openai"),
|
|
).toBe("https://generativelanguage.googleapis.com/v1beta");
|
|
expect(
|
|
normalizeGoogleGenerativeAiBaseUrl(
|
|
"https://generativelanguage.googleapis.com/v1alpha/openai/",
|
|
),
|
|
).toBe("https://generativelanguage.googleapis.com/v1alpha");
|
|
});
|
|
|
|
it("normalizes Google provider configs by provider key, provider api, or model api", () => {
|
|
expect(
|
|
shouldNormalizeGoogleGenerativeAiProviderConfig("google", {
|
|
models: [{ api: "openai-completions" }],
|
|
}),
|
|
).toBe(true);
|
|
expect(
|
|
shouldNormalizeGoogleGenerativeAiProviderConfig("custom", {
|
|
api: "google-generative-ai",
|
|
models: [{ api: "openai-completions" }],
|
|
}),
|
|
).toBe(true);
|
|
expect(
|
|
shouldNormalizeGoogleGenerativeAiProviderConfig("custom", {
|
|
models: [{ api: "google-generative-ai" }],
|
|
}),
|
|
).toBe(true);
|
|
expect(
|
|
shouldNormalizeGoogleGenerativeAiProviderConfig("custom", {
|
|
api: "openai-completions",
|
|
models: [{ api: "openai-completions" }],
|
|
}),
|
|
).toBe(false);
|
|
expect(
|
|
shouldNormalizeGoogleGenerativeAiProviderConfig("google", {
|
|
api: "openai-completions",
|
|
models: [{ api: "openai-completions" }],
|
|
}),
|
|
).toBe(false);
|
|
expect(
|
|
shouldNormalizeGoogleGenerativeAiProviderConfig("google-vertex", {
|
|
baseUrl: "https://aiplatform.googleapis.com",
|
|
}),
|
|
).toBe(false);
|
|
});
|
|
|
|
it("detects native Google Vertex hosts by hostname only", () => {
|
|
expect(isGoogleVertexHostname("aiplatform.googleapis.com")).toBe(true);
|
|
expect(isGoogleVertexHostname("us-central1-aiplatform.googleapis.com")).toBe(true);
|
|
expect(isGoogleVertexHostname("generativelanguage.googleapis.com")).toBe(false);
|
|
expect(isGoogleVertexHostname("evil-aiplatform.googleapis.com.attacker.com")).toBe(false);
|
|
expect(
|
|
isGoogleVertexBaseUrl(
|
|
"https://generativelanguage.googleapis.com/v1beta/proxy/aiplatform.googleapis.com",
|
|
),
|
|
).toBe(false);
|
|
});
|
|
|
|
it("normalizes transport baseUrls only for Google Generative AI", () => {
|
|
expect(
|
|
resolveGoogleGenerativeAiTransport({
|
|
provider: "google",
|
|
api: undefined,
|
|
baseUrl: "https://generativelanguage.googleapis.com",
|
|
}),
|
|
).toEqual({
|
|
api: "google-generative-ai",
|
|
baseUrl: "https://generativelanguage.googleapis.com/v1beta",
|
|
});
|
|
expect(
|
|
resolveGoogleGenerativeAiTransport({
|
|
api: "google-generative-ai",
|
|
baseUrl: "https://generativelanguage.googleapis.com",
|
|
}),
|
|
).toEqual({
|
|
api: "google-generative-ai",
|
|
baseUrl: "https://generativelanguage.googleapis.com/v1beta",
|
|
});
|
|
expect(
|
|
resolveGoogleGenerativeAiTransport({
|
|
api: "openai-completions",
|
|
baseUrl: "https://generativelanguage.googleapis.com",
|
|
}),
|
|
).toEqual({
|
|
api: "openai-completions",
|
|
baseUrl: "https://generativelanguage.googleapis.com",
|
|
});
|
|
expect(
|
|
resolveGoogleGenerativeAiTransport({
|
|
provider: "google-vertex",
|
|
api: undefined,
|
|
baseUrl: "https://us-central1-aiplatform.googleapis.com",
|
|
}),
|
|
).toEqual({
|
|
api: "google-vertex",
|
|
baseUrl: "https://us-central1-aiplatform.googleapis.com",
|
|
});
|
|
expect(
|
|
resolveGoogleGenerativeAiTransport({
|
|
provider: "google-vertex",
|
|
api: "openai-completions",
|
|
baseUrl:
|
|
"https://aiplatform.googleapis.com/v1/projects/test/locations/us-central1/endpoints/openapi",
|
|
}),
|
|
).toEqual({
|
|
api: "openai-completions",
|
|
baseUrl:
|
|
"https://aiplatform.googleapis.com/v1/projects/test/locations/us-central1/endpoints/openapi",
|
|
});
|
|
});
|
|
|
|
it("normalizes google-vertex model ids without rewriting the OpenAI-compatible baseUrl", () => {
|
|
expect(
|
|
normalizeGoogleProviderConfig("google-vertex", {
|
|
api: "openai-completions",
|
|
baseUrl:
|
|
"https://aiplatform.googleapis.com/v1/projects/test/locations/us-central1/endpoints/openapi",
|
|
models: [
|
|
{
|
|
id: "gemini-3.1-flash-lite",
|
|
name: "Gemini Flash Lite",
|
|
input: ["text"],
|
|
reasoning: false,
|
|
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
|
contextWindow: 1,
|
|
maxTokens: 1,
|
|
},
|
|
],
|
|
}),
|
|
).toEqual({
|
|
api: "openai-completions",
|
|
baseUrl:
|
|
"https://aiplatform.googleapis.com/v1/projects/test/locations/us-central1/endpoints/openapi",
|
|
models: [
|
|
{
|
|
contextWindow: 1,
|
|
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
|
id: "gemini-3.1-flash-lite",
|
|
input: ["text"],
|
|
maxTokens: 1,
|
|
name: "Gemini Flash Lite",
|
|
reasoning: false,
|
|
},
|
|
],
|
|
});
|
|
});
|
|
|
|
it("derives the Gemini API origin without duplicating /v1beta", () => {
|
|
expect(resolveGoogleGenerativeAiApiOrigin()).toBe("https://generativelanguage.googleapis.com");
|
|
expect(resolveGoogleGenerativeAiApiOrigin("https://generativelanguage.googleapis.com")).toBe(
|
|
"https://generativelanguage.googleapis.com",
|
|
);
|
|
expect(
|
|
resolveGoogleGenerativeAiApiOrigin("https://generativelanguage.googleapis.com/v1beta"),
|
|
).toBe("https://generativelanguage.googleapis.com");
|
|
});
|
|
|
|
it("parses project-aware oauth auth payloads into bearer headers", () => {
|
|
expect(
|
|
parseGeminiAuth(JSON.stringify({ token: "oauth-token", projectId: "project-1" })),
|
|
).toEqual({
|
|
headers: {
|
|
Authorization: "Bearer oauth-token",
|
|
"Content-Type": "application/json",
|
|
},
|
|
});
|
|
});
|
|
|
|
it("falls back to API key headers for raw tokens", () => {
|
|
expect(parseGeminiAuth("api-key-123")).toEqual({
|
|
headers: {
|
|
"x-goog-api-key": "api-key-123",
|
|
"Content-Type": "application/json",
|
|
},
|
|
});
|
|
});
|
|
|
|
it("builds shared Google Generative AI HTTP request config", () => {
|
|
const oauthConfig = resolveGoogleGenerativeAiHttpRequestConfig({
|
|
apiKey: JSON.stringify({ token: "oauth-token" }),
|
|
baseUrl: "https://generativelanguage.googleapis.com",
|
|
capability: "audio",
|
|
transport: "media-understanding",
|
|
});
|
|
expect(oauthConfig.baseUrl).toBe("https://generativelanguage.googleapis.com/v1beta");
|
|
expect(oauthConfig.allowPrivateNetwork).toBe(false);
|
|
expect(Object.fromEntries(new Headers(oauthConfig.headers).entries())).toEqual({
|
|
authorization: "Bearer oauth-token",
|
|
"content-type": "application/json",
|
|
});
|
|
|
|
const apiKeyConfig = resolveGoogleGenerativeAiHttpRequestConfig({
|
|
apiKey: "api-key-123",
|
|
capability: "image",
|
|
transport: "http",
|
|
});
|
|
expect(apiKeyConfig.baseUrl).toBe("https://generativelanguage.googleapis.com/v1beta");
|
|
expect(apiKeyConfig.allowPrivateNetwork).toBe(false);
|
|
expect(Object.fromEntries(new Headers(apiKeyConfig.headers).entries())).toEqual({
|
|
"content-type": "application/json",
|
|
"x-goog-api-key": "api-key-123",
|
|
});
|
|
});
|
|
|
|
it("preserves explicit OpenAI-compatible Google endpoints during provider normalization", () => {
|
|
expect(
|
|
resolveGoogleGenerativeAiTransport({
|
|
api: "openai-completions",
|
|
baseUrl: "https://generativelanguage.googleapis.com/v1beta/openai",
|
|
}),
|
|
).toEqual({
|
|
api: "openai-completions",
|
|
baseUrl: "https://generativelanguage.googleapis.com/v1beta/openai",
|
|
});
|
|
});
|
|
|
|
it("strips URL credentials during Google base URL normalization", () => {
|
|
const normalized = normalizeGoogleApiBaseUrl(
|
|
"https://user:secret@generativelanguage.googleapis.com/v1beta/openai?x=1#frag",
|
|
);
|
|
expect(normalized).toBe("https://generativelanguage.googleapis.com/v1beta/openai");
|
|
});
|
|
|
|
it("rejects non-Google Gemini base URLs and honors explicit private-network opt-in", () => {
|
|
expect(() =>
|
|
resolveGoogleGenerativeAiHttpRequestConfig({
|
|
apiKey: "api-key-123",
|
|
baseUrl: "https://proxy.example.com/v1beta",
|
|
capability: "image",
|
|
transport: "http",
|
|
}),
|
|
).toThrow("Google Generative AI baseUrl must use https://generativelanguage.googleapis.com");
|
|
|
|
expect(() =>
|
|
resolveGoogleGenerativeAiHttpRequestConfig({
|
|
apiKey: "api-key-123",
|
|
baseUrl: "http://generativelanguage.googleapis.com/v1beta",
|
|
capability: "image",
|
|
transport: "http",
|
|
}),
|
|
).toThrow("Google Generative AI baseUrl must use https://generativelanguage.googleapis.com");
|
|
|
|
const config = resolveGoogleGenerativeAiHttpRequestConfig({
|
|
apiKey: "api-key-123",
|
|
baseUrl: "https://generativelanguage.googleapis.com/v1beta",
|
|
capability: "image",
|
|
transport: "http",
|
|
request: { allowPrivateNetwork: true },
|
|
});
|
|
expect(config.allowPrivateNetwork).toBe(true);
|
|
});
|
|
});
|