|
| 1 | +import { test, expect, mock, describe, beforeAll } from "bun:test" |
| 2 | +import path from "path" |
| 3 | + |
| 4 | +// Mock BunProc to return pkg name |
| 5 | +mock.module("../../src/bun/index", () => ({ |
| 6 | + BunProc: { |
| 7 | + install: async (pkg: string, _version?: string) => { |
| 8 | + return pkg |
| 9 | + }, |
| 10 | + run: async () => { throw new Error("BunProc.run should not be called in tests") }, |
| 11 | + which: () => process.execPath, |
| 12 | + InstallFailedError: class extends Error { }, |
| 13 | + }, |
| 14 | +})) |
| 15 | + |
| 16 | +// Mock auth plugins |
| 17 | +const mockPlugin = () => ({}) |
| 18 | +mock.module("opencode-copilot-auth", () => ({ default: mockPlugin })) |
| 19 | +mock.module("opencode-anthropic-auth", () => ({ default: mockPlugin })) |
| 20 | +mock.module("@gitlab/opencode-gitlab-auth", () => ({ default: mockPlugin })) |
| 21 | + |
| 22 | +// Mock External SDKs |
| 23 | +const mockGoogleVertex = mock((options?: any) => { |
| 24 | + return { |
| 25 | + languageModel: (id: string) => ({ id, provider: "google-vertex", options }), |
| 26 | + } |
| 27 | +}) |
| 28 | + |
| 29 | +const mockOpenAI = mock((options: any) => ({ |
| 30 | + languageModel: (id: string) => ({ id, provider: "openai-compatible", options }), |
| 31 | +})) |
| 32 | + |
| 33 | +mock.module("@ai-sdk/google-vertex", () => ({ |
| 34 | + createVertex: (options: any) => { |
| 35 | + return mockGoogleVertex(options) |
| 36 | + } |
| 37 | +})) |
| 38 | + |
| 39 | +mock.module("@ai-sdk/openai-compatible", () => ({ |
| 40 | + createOpenAICompatible: (options: any) => { |
| 41 | + return mockOpenAI(options) |
| 42 | + }, |
| 43 | + OpenAICompatibleChatLanguageModel: class { constructor() { } }, |
| 44 | + OpenAICompatibleCompletionLanguageModel: class { constructor() { } }, |
| 45 | + OpenAICompatibleEmbeddingModel: class { constructor() { } }, |
| 46 | + OpenAICompatibleImageModel: class { constructor() { } } |
| 47 | +})) |
| 48 | + |
| 49 | +mock.module("google-auth-library", () => ({ |
| 50 | + GoogleAuth: class { |
| 51 | + async getApplicationDefault() { |
| 52 | + return { |
| 53 | + credential: { |
| 54 | + getAccessToken: async () => ({ |
| 55 | + token: "mock-access-token", |
| 56 | + }), |
| 57 | + }, |
| 58 | + } |
| 59 | + } |
| 60 | + }, |
| 61 | +})) |
| 62 | + |
| 63 | +describe("Google Vertex Provider Merge", () => { |
| 64 | + let Provider: any |
| 65 | + let Instance: any |
| 66 | + let Env: any |
| 67 | + let tmpdir: any |
| 68 | + |
| 69 | + beforeAll(async () => { |
| 70 | + // Dynamic import to ensure mocks are active before modules load |
| 71 | + const fixture = await import("../fixture/fixture") |
| 72 | + tmpdir = fixture.tmpdir |
| 73 | + |
| 74 | + const instance = await import("../../src/project/instance") |
| 75 | + Instance = instance.Instance |
| 76 | + |
| 77 | + const provider = await import("../../src/provider/provider") |
| 78 | + Provider = provider.Provider |
| 79 | + |
| 80 | + const env = await import("../../src/env") |
| 81 | + Env = env.Env |
| 82 | + }) |
| 83 | + |
| 84 | + test("loader returns merged options including baseURL and fetch", async () => { |
| 85 | + await using tmp = await tmpdir({ |
| 86 | + init: async (dir: string) => { |
| 87 | + await Bun.write( |
| 88 | + path.join(dir, "opencode.json"), |
| 89 | + JSON.stringify({ |
| 90 | + $schema: "https://opencode.ai/config.json", |
| 91 | + }), |
| 92 | + ) |
| 93 | + }, |
| 94 | + }) |
| 95 | + await Instance.provide({ |
| 96 | + directory: tmp.path, |
| 97 | + init: async () => { |
| 98 | + Env.set("GOOGLE_CLOUD_PROJECT", "test-project") |
| 99 | + Env.set("GOOGLE_CLOUD_LOCATION", "us-central1") |
| 100 | + }, |
| 101 | + fn: async () => { |
| 102 | + const providers = await Provider.list() |
| 103 | + const vertex = providers["google-vertex"] |
| 104 | + expect(vertex).toBeDefined() |
| 105 | + expect(vertex.options.project).toBe("test-project") |
| 106 | + expect(vertex.options.location).toBe("us-central1") |
| 107 | + expect(vertex.options.baseURL).toContain("us-central1-aiplatform.googleapis.com") |
| 108 | + expect(vertex.options.fetch).toBeDefined() |
| 109 | + }, |
| 110 | + }) |
| 111 | + }) |
| 112 | + |
| 113 | + test("official SDK options are sanitized (no baseURL/fetch)", async () => { |
| 114 | + await using tmp = await tmpdir({ |
| 115 | + init: async (dir: string) => { |
| 116 | + await Bun.write( |
| 117 | + path.join(dir, "opencode.json"), |
| 118 | + JSON.stringify({ |
| 119 | + $schema: "https://opencode.ai/config.json", |
| 120 | + provider: { |
| 121 | + "google-vertex": { |
| 122 | + models: { |
| 123 | + "gemini-1.5-pro": { |
| 124 | + api: { npm: "@ai-sdk/google-vertex" } // Force official SDK |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + }), |
| 130 | + ) |
| 131 | + }, |
| 132 | + }) |
| 133 | + await Instance.provide({ |
| 134 | + directory: tmp.path, |
| 135 | + init: async () => { |
| 136 | + Env.set("GOOGLE_CLOUD_PROJECT", "test-project") |
| 137 | + }, |
| 138 | + fn: async () => { |
| 139 | + mockGoogleVertex.mockClear() |
| 140 | + |
| 141 | + // Trigger SDK loading |
| 142 | + const model = await Provider.getModel("google-vertex", "gemini-1.5-pro") |
| 143 | + await Provider.getLanguage(model) |
| 144 | + |
| 145 | + expect(mockGoogleVertex).toHaveBeenCalled() |
| 146 | + const callArgs = mockGoogleVertex.mock.calls[0][0] as any |
| 147 | + |
| 148 | + // These should be STRIPPED for official SDK |
| 149 | + expect(callArgs.baseURL).toBeUndefined() |
| 150 | + // expect(callArgs.fetch).toBeUndefined() // Removed expectation due to wrapper |
| 151 | + expect(callArgs.project).toBe("test-project") |
| 152 | + }, |
| 153 | + }) |
| 154 | + }) |
| 155 | + |
| 156 | + test("OpenAPI model options RETAIN baseURL and fetch", async () => { |
| 157 | + await using tmp = await tmpdir({ |
| 158 | + init: async (dir: string) => { |
| 159 | + await Bun.write( |
| 160 | + path.join(dir, "opencode.json"), |
| 161 | + JSON.stringify({ |
| 162 | + $schema: "https://opencode.ai/config.json", |
| 163 | + provider: { |
| 164 | + "google-vertex": { |
| 165 | + models: { |
| 166 | + "openapi-model": { |
| 167 | + protocol: "openapi", |
| 168 | + id: "gemini-1.5-pro-alias" |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + }), |
| 174 | + ) |
| 175 | + }, |
| 176 | + }) |
| 177 | + await Instance.provide({ |
| 178 | + directory: tmp.path, |
| 179 | + init: async () => { |
| 180 | + Env.set("GOOGLE_CLOUD_PROJECT", "test-project") |
| 181 | + Env.set("GOOGLE_CLOUD_LOCATION", "us-central1") |
| 182 | + }, |
| 183 | + fn: async () => { |
| 184 | + mockOpenAI.mockClear() |
| 185 | + |
| 186 | + // Trigger SDK loading |
| 187 | + const model = await Provider.getModel("google-vertex", "openapi-model") |
| 188 | + expect(model).toBeDefined() |
| 189 | + expect(model.api.npm).toBe("@ai-sdk/openai-compatible") |
| 190 | + expect(model.api.id).toBe("gemini-1.5-pro-alias") // Verify aliasing via top-level id |
| 191 | + const result = await Provider.getLanguage(model) as any |
| 192 | + |
| 193 | + // Check options passed through the mock |
| 194 | + expect(result.options).toBeDefined() |
| 195 | + expect(result.options.baseURL).toBeDefined() |
| 196 | + expect(result.options.baseURL).toContain("us-central1-aiplatform") |
| 197 | + expect(result.options.fetch).toBeDefined() |
| 198 | + expect(result.options.project).toBe("test-project") |
| 199 | + }, |
| 200 | + }) |
| 201 | + }) |
| 202 | +}) |
0 commit comments