|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { configureAiTransportHost } from "../host.js"; |
| 3 | +import type { Context, Model } from "../types.js"; |
| 4 | + |
| 5 | +const googleMockState = vi.hoisted(() => ({ configs: [] as unknown[] })); |
| 6 | + |
| 7 | +vi.mock("@google/genai", () => ({ |
| 8 | + GoogleGenAI: class MockGoogleGenAI { |
| 9 | + models = { |
| 10 | + generateContentStream: vi.fn(() => { |
| 11 | + throw new Error("stop after constructor"); |
| 12 | + }), |
| 13 | + }; |
| 14 | + |
| 15 | + constructor(config: unknown) { |
| 16 | + googleMockState.configs.push(config); |
| 17 | + } |
| 18 | + }, |
| 19 | + ResourceScope: { COLLECTION: "COLLECTION" }, |
| 20 | + ThinkingLevel: { |
| 21 | + THINKING_LEVEL_UNSPECIFIED: "THINKING_LEVEL_UNSPECIFIED", |
| 22 | + MINIMAL: "MINIMAL", |
| 23 | + LOW: "LOW", |
| 24 | + MEDIUM: "MEDIUM", |
| 25 | + HIGH: "HIGH", |
| 26 | + }, |
| 27 | +})); |
| 28 | + |
| 29 | +import { streamGoogleVertex } from "./google-vertex.js"; |
| 30 | +import { streamGoogle } from "./google.js"; |
| 31 | + |
| 32 | +const context = { |
| 33 | + messages: [{ role: "user", content: "hello", timestamp: 0 }], |
| 34 | +} satisfies Context; |
| 35 | +const sentinel = "oc-sent-v1-0123456789abcdef01234567"; |
| 36 | + |
| 37 | +function googleModel(): Model<"google-generative-ai"> { |
| 38 | + return { |
| 39 | + id: "gemini-3-flash-preview", |
| 40 | + name: "Gemini 3 Flash Preview", |
| 41 | + api: "google-generative-ai", |
| 42 | + provider: "google", |
| 43 | + baseUrl: "https://generativelanguage.googleapis.com/v1beta", |
| 44 | + reasoning: true, |
| 45 | + input: ["text"], |
| 46 | + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, |
| 47 | + contextWindow: 1_000_000, |
| 48 | + maxTokens: 8192, |
| 49 | + }; |
| 50 | +} |
| 51 | + |
| 52 | +function vertexModel(): Model<"google-vertex"> { |
| 53 | + return { |
| 54 | + ...googleModel(), |
| 55 | + api: "google-vertex", |
| 56 | + provider: "google-vertex", |
| 57 | + baseUrl: "https://us-central1-aiplatform.googleapis.com/v1", |
| 58 | + }; |
| 59 | +} |
| 60 | + |
| 61 | +describe("Google SDK construction auth", () => { |
| 62 | + beforeEach(() => { |
| 63 | + googleMockState.configs = []; |
| 64 | + }); |
| 65 | + |
| 66 | + afterEach(() => { |
| 67 | + configureAiTransportHost({}); |
| 68 | + }); |
| 69 | + |
| 70 | + it("unwraps Google API-key sentinels immediately before client construction", async () => { |
| 71 | + const buildModelFetch = vi.fn(); |
| 72 | + configureAiTransportHost({ |
| 73 | + buildModelFetch, |
| 74 | + resolveSecretSentinel: (value) => value.replaceAll(sentinel, "google-construction-secret"), |
| 75 | + }); |
| 76 | + |
| 77 | + const result = await streamGoogle( |
| 78 | + { |
| 79 | + ...googleModel(), |
| 80 | + headers: { Authorization: `Bearer ${sentinel}` }, |
| 81 | + }, |
| 82 | + context, |
| 83 | + { apiKey: sentinel }, |
| 84 | + ).result(); |
| 85 | + |
| 86 | + expect(result.stopReason).toBe("error"); |
| 87 | + expect(googleMockState.configs[0]).toMatchObject({ |
| 88 | + apiKey: "google-construction-secret", |
| 89 | + httpOptions: { headers: { Authorization: "Bearer google-construction-secret" } }, |
| 90 | + }); |
| 91 | + expect(JSON.stringify(googleMockState.configs[0])).not.toContain(sentinel); |
| 92 | + expect(buildModelFetch).not.toHaveBeenCalled(); |
| 93 | + }); |
| 94 | + |
| 95 | + it("unwraps Vertex API-key sentinels immediately before client construction", async () => { |
| 96 | + const buildModelFetch = vi.fn(); |
| 97 | + configureAiTransportHost({ |
| 98 | + buildModelFetch, |
| 99 | + resolveSecretSentinel: (value) => value.replaceAll(sentinel, "vertex-construction-secret"), |
| 100 | + }); |
| 101 | + |
| 102 | + const result = await streamGoogleVertex( |
| 103 | + { |
| 104 | + ...vertexModel(), |
| 105 | + headers: { "X-Provider-Token": sentinel }, |
| 106 | + }, |
| 107 | + context, |
| 108 | + { |
| 109 | + apiKey: sentinel, |
| 110 | + project: "demo-project", |
| 111 | + location: "us-central1", |
| 112 | + }, |
| 113 | + ).result(); |
| 114 | + |
| 115 | + expect(result.stopReason).toBe("error"); |
| 116 | + expect(googleMockState.configs[0]).toMatchObject({ |
| 117 | + apiKey: "vertex-construction-secret", |
| 118 | + vertexai: true, |
| 119 | + httpOptions: { headers: { "X-Provider-Token": "vertex-construction-secret" } }, |
| 120 | + }); |
| 121 | + expect(JSON.stringify(googleMockState.configs[0])).not.toContain(sentinel); |
| 122 | + expect(buildModelFetch).not.toHaveBeenCalled(); |
| 123 | + }); |
| 124 | +}); |
0 commit comments