|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest" |
| 2 | +import type { Message } from "@/types" |
| 3 | + |
| 4 | +const authMock = vi.hoisted(() => vi.fn()) |
| 5 | +const getUserProviderApiKeyMock = vi.hoisted(() => vi.fn()) |
| 6 | +const streamGPTMock = vi.hoisted(() => vi.fn()) |
| 7 | +const generateGeminiVerdictWithApiKeyMock = vi.hoisted(() => vi.fn()) |
| 8 | +const generateGoogleAiContentWithApiKeyMock = vi.hoisted(() => vi.fn()) |
| 9 | + |
| 10 | +const validVerdict = { |
| 11 | + recommendedAnswer: "Choose Option A.", |
| 12 | + voteSplit: "2/2 unanimous", |
| 13 | + confidence: 88, |
| 14 | + reasons: ["Reason one", "Reason two"], |
| 15 | + minorityView: "Option B could work with different constraints.", |
| 16 | + oppositeCase: "Choose B if latency matters most.", |
| 17 | +} |
| 18 | + |
| 19 | +vi.mock("@/lib/auth", () => ({ auth: authMock })) |
| 20 | +vi.mock("@/lib/user-api-keys", () => ({ getUserProviderApiKey: getUserProviderApiKeyMock })) |
| 21 | +vi.mock("@/lib/providers/gpt", () => ({ streamGPT: streamGPTMock })) |
| 22 | +vi.mock("@/lib/providers/claude", () => ({ streamClaude: vi.fn() })) |
| 23 | +vi.mock("@/lib/providers/perplexity", () => ({ streamPerplexity: vi.fn() })) |
| 24 | +vi.mock("@/lib/providers/gemini", () => ({ |
| 25 | + getConfiguredGeminiApiKey: vi.fn(() => "server-gemini-key"), |
| 26 | + generateGeminiVerdictWithApiKey: generateGeminiVerdictWithApiKeyMock, |
| 27 | + generateGoogleAiContentWithApiKey: generateGoogleAiContentWithApiKeyMock, |
| 28 | + streamGemini: vi.fn(), |
| 29 | +})) |
| 30 | +vi.mock("@/lib/vertex-config", () => ({ |
| 31 | + getVertexConfig: () => ({ projectId: "project", location: "us-central1" }), |
| 32 | +})) |
| 33 | +vi.mock("@google-cloud/vertexai", () => ({ |
| 34 | + VertexAI: vi.fn(() => ({ |
| 35 | + getGenerativeModel: vi.fn(() => ({ |
| 36 | + generateContent: vi.fn(), |
| 37 | + })), |
| 38 | + })), |
| 39 | + HarmCategory: { |
| 40 | + HARM_CATEGORY_HARASSMENT: "harassment", |
| 41 | + HARM_CATEGORY_DANGEROUS_CONTENT: "dangerous", |
| 42 | + }, |
| 43 | + HarmBlockThreshold: { BLOCK_ONLY_HIGH: "high" }, |
| 44 | + SchemaType: { |
| 45 | + OBJECT: "object", |
| 46 | + STRING: "string", |
| 47 | + NUMBER: "number", |
| 48 | + ARRAY: "array", |
| 49 | + }, |
| 50 | +})) |
| 51 | + |
| 52 | +import { POST as chatPOST } from "@/app/api/chat/route" |
| 53 | +import { POST as consensusPOST } from "@/app/api/consensus/route" |
| 54 | +import { POST as ocrPOST } from "@/app/api/ocr/route" |
| 55 | + |
| 56 | +const messages: Message[] = [ |
| 57 | + { |
| 58 | + id: "u1", |
| 59 | + sender: "user", |
| 60 | + displayName: "You", |
| 61 | + content: "Which option?", |
| 62 | + timestamp: new Date(), |
| 63 | + }, |
| 64 | + { |
| 65 | + id: "g1", |
| 66 | + sender: "gemini", |
| 67 | + displayName: "Gemini", |
| 68 | + content: "Option A.", |
| 69 | + timestamp: new Date(), |
| 70 | + }, |
| 71 | + { |
| 72 | + id: "c1", |
| 73 | + sender: "claude", |
| 74 | + displayName: "Claude", |
| 75 | + content: "Option B.", |
| 76 | + timestamp: new Date(), |
| 77 | + }, |
| 78 | +] |
| 79 | + |
| 80 | +function jsonRequest(path: string, body: unknown): Request { |
| 81 | + return new Request(`http://localhost${path}`, { |
| 82 | + method: "POST", |
| 83 | + headers: { "Content-Type": "application/json" }, |
| 84 | + body: JSON.stringify(body), |
| 85 | + }) |
| 86 | +} |
| 87 | + |
| 88 | +describe("BYOK-required route guards", () => { |
| 89 | + let previousRequireUserApiKeys: string | undefined |
| 90 | + |
| 91 | + beforeEach(() => { |
| 92 | + previousRequireUserApiKeys = process.env.REQUIRE_USER_API_KEYS |
| 93 | + process.env.REQUIRE_USER_API_KEYS = "true" |
| 94 | + authMock.mockResolvedValue(null) |
| 95 | + getUserProviderApiKeyMock.mockResolvedValue(undefined) |
| 96 | + streamGPTMock.mockImplementation(async function* () { |
| 97 | + yield "server response" |
| 98 | + }) |
| 99 | + generateGeminiVerdictWithApiKeyMock.mockResolvedValue(JSON.stringify(validVerdict)) |
| 100 | + generateGoogleAiContentWithApiKeyMock.mockResolvedValue("ocr text") |
| 101 | + }) |
| 102 | + |
| 103 | + afterEach(() => { |
| 104 | + if (previousRequireUserApiKeys === undefined) { |
| 105 | + delete process.env.REQUIRE_USER_API_KEYS |
| 106 | + } else { |
| 107 | + process.env.REQUIRE_USER_API_KEYS = previousRequireUserApiKeys |
| 108 | + } |
| 109 | + vi.clearAllMocks() |
| 110 | + }) |
| 111 | + |
| 112 | + it("chat returns no_key before calling the provider when no user key exists", async () => { |
| 113 | + const response = await chatPOST( |
| 114 | + jsonRequest("/api/chat", { |
| 115 | + messages, |
| 116 | + provider: "gpt", |
| 117 | + locale: "en", |
| 118 | + responseLength: "medium", |
| 119 | + }) |
| 120 | + ) |
| 121 | + |
| 122 | + expect(response.status).toBe(402) |
| 123 | + await expect(response.json()).resolves.toEqual({ error: "no_key", provider: "gpt" }) |
| 124 | + expect(streamGPTMock).not.toHaveBeenCalled() |
| 125 | + }) |
| 126 | + |
| 127 | + it("chat returns key_lookup_failed when the saved key lookup fails", async () => { |
| 128 | + authMock.mockResolvedValue({ user: { id: "user-1" } }) |
| 129 | + getUserProviderApiKeyMock.mockRejectedValueOnce(new Error("database unavailable")) |
| 130 | + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => undefined) |
| 131 | + |
| 132 | + try { |
| 133 | + const response = await chatPOST( |
| 134 | + jsonRequest("/api/chat", { |
| 135 | + messages, |
| 136 | + provider: "gpt", |
| 137 | + locale: "en", |
| 138 | + responseLength: "medium", |
| 139 | + }) |
| 140 | + ) |
| 141 | + |
| 142 | + expect(response.status).toBe(500) |
| 143 | + await expect(response.json()).resolves.toEqual({ error: "key_lookup_failed" }) |
| 144 | + expect(streamGPTMock).not.toHaveBeenCalled() |
| 145 | + } finally { |
| 146 | + errorSpy.mockRestore() |
| 147 | + } |
| 148 | + }) |
| 149 | + |
| 150 | + it("consensus returns no_key before using Gemini server credentials", async () => { |
| 151 | + const response = await consensusPOST( |
| 152 | + jsonRequest("/api/consensus", { |
| 153 | + messages, |
| 154 | + locale: "en", |
| 155 | + responseLength: "medium", |
| 156 | + }) as never |
| 157 | + ) |
| 158 | + |
| 159 | + expect(response.status).toBe(402) |
| 160 | + await expect(response.json()).resolves.toEqual({ error: "no_key", provider: "gemini" }) |
| 161 | + expect(generateGeminiVerdictWithApiKeyMock).not.toHaveBeenCalled() |
| 162 | + }) |
| 163 | + |
| 164 | + it("ocr returns no_key before using Gemini server credentials", async () => { |
| 165 | + const response = await ocrPOST(jsonRequest("/api/ocr", { images: ["abc"] }) as never) |
| 166 | + |
| 167 | + expect(response.status).toBe(402) |
| 168 | + await expect(response.json()).resolves.toEqual({ error: "no_key", provider: "gemini" }) |
| 169 | + expect(generateGoogleAiContentWithApiKeyMock).not.toHaveBeenCalled() |
| 170 | + }) |
| 171 | + |
| 172 | + it("chat proceeds with the saved user key when BYOK is required", async () => { |
| 173 | + authMock.mockResolvedValue({ user: { id: "user-1" } }) |
| 174 | + getUserProviderApiKeyMock.mockResolvedValue("user-gpt-key") |
| 175 | + |
| 176 | + const response = await chatPOST( |
| 177 | + jsonRequest("/api/chat", { |
| 178 | + messages, |
| 179 | + provider: "gpt", |
| 180 | + locale: "en", |
| 181 | + responseLength: "medium", |
| 182 | + }) |
| 183 | + ) |
| 184 | + |
| 185 | + expect(response.status).toBe(200) |
| 186 | + await response.text() |
| 187 | + expect(streamGPTMock).toHaveBeenCalled() |
| 188 | + expect(streamGPTMock.mock.calls[0]?.[4]).toBe("user-gpt-key") |
| 189 | + }) |
| 190 | + |
| 191 | + it("chat proceeds without a user key when BYOK is disabled", async () => { |
| 192 | + process.env.REQUIRE_USER_API_KEYS = "false" |
| 193 | + |
| 194 | + const response = await chatPOST( |
| 195 | + jsonRequest("/api/chat", { |
| 196 | + messages, |
| 197 | + provider: "gpt", |
| 198 | + locale: "en", |
| 199 | + responseLength: "medium", |
| 200 | + }) |
| 201 | + ) |
| 202 | + |
| 203 | + expect(response.status).toBe(200) |
| 204 | + await response.text() |
| 205 | + expect(streamGPTMock).toHaveBeenCalled() |
| 206 | + expect(streamGPTMock.mock.calls[0]?.[4]).toBeUndefined() |
| 207 | + }) |
| 208 | + |
| 209 | + it("consensus proceeds with the saved Gemini key when BYOK is required", async () => { |
| 210 | + authMock.mockResolvedValue({ user: { id: "user-1" } }) |
| 211 | + getUserProviderApiKeyMock.mockResolvedValue("user-gemini-key") |
| 212 | + |
| 213 | + const response = await consensusPOST( |
| 214 | + jsonRequest("/api/consensus", { |
| 215 | + messages, |
| 216 | + locale: "en", |
| 217 | + responseLength: "medium", |
| 218 | + }) as never |
| 219 | + ) |
| 220 | + |
| 221 | + expect(response.status).toBe(200) |
| 222 | + await expect(response.json()).resolves.toMatchObject(validVerdict) |
| 223 | + expect(generateGeminiVerdictWithApiKeyMock).toHaveBeenCalledWith( |
| 224 | + expect.objectContaining({ apiKey: "user-gemini-key" }) |
| 225 | + ) |
| 226 | + }) |
| 227 | + |
| 228 | + it("consensus falls back to the configured Gemini key when BYOK is unset", async () => { |
| 229 | + delete process.env.REQUIRE_USER_API_KEYS |
| 230 | + |
| 231 | + const response = await consensusPOST( |
| 232 | + jsonRequest("/api/consensus", { |
| 233 | + messages, |
| 234 | + locale: "en", |
| 235 | + responseLength: "medium", |
| 236 | + }) as never |
| 237 | + ) |
| 238 | + |
| 239 | + expect(response.status).toBe(200) |
| 240 | + await expect(response.json()).resolves.toMatchObject(validVerdict) |
| 241 | + expect(generateGeminiVerdictWithApiKeyMock).toHaveBeenCalledWith( |
| 242 | + expect.objectContaining({ apiKey: "server-gemini-key" }) |
| 243 | + ) |
| 244 | + }) |
| 245 | + |
| 246 | + it("ocr proceeds with the saved Gemini key when BYOK is required", async () => { |
| 247 | + authMock.mockResolvedValue({ user: { id: "user-1" } }) |
| 248 | + getUserProviderApiKeyMock.mockResolvedValue("user-gemini-key") |
| 249 | + |
| 250 | + const response = await ocrPOST(jsonRequest("/api/ocr", { images: ["abc"] }) as never) |
| 251 | + |
| 252 | + expect(response.status).toBe(200) |
| 253 | + await expect(response.json()).resolves.toEqual({ text: "ocr text" }) |
| 254 | + expect(generateGoogleAiContentWithApiKeyMock).toHaveBeenCalledWith( |
| 255 | + expect.objectContaining({ apiKey: "user-gemini-key" }) |
| 256 | + ) |
| 257 | + }) |
| 258 | + |
| 259 | + it("ocr falls back to the configured Gemini key when BYOK is disabled", async () => { |
| 260 | + process.env.REQUIRE_USER_API_KEYS = "false" |
| 261 | + |
| 262 | + const response = await ocrPOST(jsonRequest("/api/ocr", { images: ["abc"] }) as never) |
| 263 | + |
| 264 | + expect(response.status).toBe(200) |
| 265 | + await expect(response.json()).resolves.toEqual({ text: "ocr text" }) |
| 266 | + expect(generateGoogleAiContentWithApiKeyMock).toHaveBeenCalledWith( |
| 267 | + expect.objectContaining({ apiKey: "server-gemini-key" }) |
| 268 | + ) |
| 269 | + }) |
| 270 | +}) |
0 commit comments