google-vertex-api-key-resolution.test.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
  2. const googleGenAiMock = vi.hoisted(() => ({
  3. constructorCalls: [] as Array<Record<string, unknown>>,
  4. }));
  5. vi.mock("@google/genai", () => {
  6. class GoogleGenAI {
  7. models = {
  8. generateContentStream: async function* () {
  9. yield {
  10. responseId: "vertex-response-id",
  11. candidates: [
  12. {
  13. content: { parts: [{ text: "ok" }] },
  14. finishReason: "STOP",
  15. },
  16. ],
  17. usageMetadata: {
  18. promptTokenCount: 1,
  19. candidatesTokenCount: 1,
  20. totalTokenCount: 2,
  21. },
  22. };
  23. },
  24. };
  25. constructor(config: Record<string, unknown>) {
  26. googleGenAiMock.constructorCalls.push(config);
  27. }
  28. }
  29. return {
  30. GoogleGenAI,
  31. ResourceScope: {
  32. COLLECTION: "COLLECTION",
  33. },
  34. ThinkingLevel: {
  35. THINKING_LEVEL_UNSPECIFIED: "THINKING_LEVEL_UNSPECIFIED",
  36. MINIMAL: "MINIMAL",
  37. LOW: "LOW",
  38. MEDIUM: "MEDIUM",
  39. HIGH: "HIGH",
  40. },
  41. };
  42. });
  43. import { getModel } from "../src/models.ts";
  44. import { streamGoogleVertex } from "../src/providers/google-vertex.ts";
  45. import type { Context, Model } from "../src/types.ts";
  46. const model = getModel("google-vertex", "gemini-3-flash-preview");
  47. const context: Context = {
  48. messages: [{ role: "user", content: "hello", timestamp: Date.now() }],
  49. };
  50. const originalGoogleCloudApiKey = process.env.GOOGLE_CLOUD_API_KEY;
  51. beforeEach(() => {
  52. googleGenAiMock.constructorCalls.length = 0;
  53. delete process.env.GOOGLE_CLOUD_API_KEY;
  54. });
  55. afterEach(() => {
  56. if (originalGoogleCloudApiKey === undefined) {
  57. delete process.env.GOOGLE_CLOUD_API_KEY;
  58. } else {
  59. process.env.GOOGLE_CLOUD_API_KEY = originalGoogleCloudApiKey;
  60. }
  61. });
  62. describe("google-vertex api key resolution", () => {
  63. it("falls back to ADC when options.apiKey is a placeholder marker", async () => {
  64. const stream = streamGoogleVertex(model, context, {
  65. apiKey: "<authenticated>",
  66. project: "test-project",
  67. location: "us-central1",
  68. });
  69. await stream.result();
  70. expect(googleGenAiMock.constructorCalls).toHaveLength(1);
  71. expect(googleGenAiMock.constructorCalls[0]).toMatchObject({
  72. vertexai: true,
  73. project: "test-project",
  74. location: "us-central1",
  75. apiVersion: "v1",
  76. });
  77. expect(googleGenAiMock.constructorCalls[0]).not.toHaveProperty("apiKey");
  78. });
  79. it("falls back to ADC when options.apiKey is the gcp-vertex-credentials marker", async () => {
  80. const stream = streamGoogleVertex(model, context, {
  81. apiKey: "gcp-vertex-credentials",
  82. project: "test-project",
  83. location: "us-central1",
  84. });
  85. await stream.result();
  86. expect(googleGenAiMock.constructorCalls).toHaveLength(1);
  87. expect(googleGenAiMock.constructorCalls[0]).toMatchObject({
  88. vertexai: true,
  89. project: "test-project",
  90. location: "us-central1",
  91. apiVersion: "v1",
  92. });
  93. expect(googleGenAiMock.constructorCalls[0]).not.toHaveProperty("apiKey");
  94. });
  95. it("falls back to ADC when GOOGLE_CLOUD_API_KEY is a placeholder marker", async () => {
  96. process.env.GOOGLE_CLOUD_API_KEY = "<authenticated>";
  97. const stream = streamGoogleVertex(model, context, {
  98. project: "test-project",
  99. location: "us-central1",
  100. });
  101. await stream.result();
  102. expect(googleGenAiMock.constructorCalls).toHaveLength(1);
  103. expect(googleGenAiMock.constructorCalls[0]).toMatchObject({
  104. vertexai: true,
  105. project: "test-project",
  106. location: "us-central1",
  107. apiVersion: "v1",
  108. });
  109. expect(googleGenAiMock.constructorCalls[0]).not.toHaveProperty("apiKey");
  110. });
  111. it("still uses the API key client for real API keys", async () => {
  112. const stream = streamGoogleVertex(model, context, {
  113. apiKey: "AIzaSyExampleRealisticLookingApiKey123456",
  114. });
  115. await stream.result();
  116. expect(googleGenAiMock.constructorCalls).toHaveLength(1);
  117. expect(googleGenAiMock.constructorCalls[0]).toMatchObject({
  118. vertexai: true,
  119. apiKey: "AIzaSyExampleRealisticLookingApiKey123456",
  120. apiVersion: "v1",
  121. });
  122. expect(googleGenAiMock.constructorCalls[0]).not.toHaveProperty("project");
  123. expect(googleGenAiMock.constructorCalls[0]).not.toHaveProperty("location");
  124. });
  125. it("does not forward generated Vertex base URL placeholders", async () => {
  126. const stream = streamGoogleVertex(model, context, {
  127. project: "test-project",
  128. location: "us-central1",
  129. });
  130. await stream.result();
  131. expect(googleGenAiMock.constructorCalls).toHaveLength(1);
  132. expect(googleGenAiMock.constructorCalls[0]?.httpOptions).toBeUndefined();
  133. });
  134. it("forwards custom baseUrl to the ADC client", async () => {
  135. const customModel: Model<"google-vertex"> = { ...model, baseUrl: "https://proxy.example.com" };
  136. const stream = streamGoogleVertex(customModel, context, {
  137. project: "test-project",
  138. location: "us-central1",
  139. });
  140. await stream.result();
  141. expect(googleGenAiMock.constructorCalls).toHaveLength(1);
  142. expect(googleGenAiMock.constructorCalls[0]).toMatchObject({
  143. vertexai: true,
  144. project: "test-project",
  145. location: "us-central1",
  146. apiVersion: "v1",
  147. httpOptions: {
  148. baseUrl: "https://proxy.example.com",
  149. baseUrlResourceScope: "COLLECTION",
  150. },
  151. });
  152. });
  153. it("forwards custom baseUrl to the API key client", async () => {
  154. const customModel: Model<"google-vertex"> = { ...model, baseUrl: "https://proxy.example.com" };
  155. const stream = streamGoogleVertex(customModel, context, {
  156. apiKey: "AIzaSyExampleRealisticLookingApiKey123456",
  157. });
  158. await stream.result();
  159. expect(googleGenAiMock.constructorCalls).toHaveLength(1);
  160. expect(googleGenAiMock.constructorCalls[0]).toMatchObject({
  161. vertexai: true,
  162. apiKey: "AIzaSyExampleRealisticLookingApiKey123456",
  163. apiVersion: "v1",
  164. httpOptions: {
  165. baseUrl: "https://proxy.example.com",
  166. baseUrlResourceScope: "COLLECTION",
  167. },
  168. });
  169. });
  170. it("does not append apiVersion when custom baseUrl already includes one", async () => {
  171. const customModel: Model<"google-vertex"> = {
  172. ...model,
  173. baseUrl: "https://proxy.example.com/v1/projects/test-project/locations/global",
  174. };
  175. const stream = streamGoogleVertex(customModel, context, {
  176. project: "test-project",
  177. location: "us-central1",
  178. });
  179. await stream.result();
  180. expect(googleGenAiMock.constructorCalls).toHaveLength(1);
  181. expect(googleGenAiMock.constructorCalls[0]).toMatchObject({
  182. httpOptions: {
  183. baseUrl: "https://proxy.example.com/v1/projects/test-project/locations/global",
  184. baseUrlResourceScope: "COLLECTION",
  185. apiVersion: "",
  186. },
  187. });
  188. });
  189. });