|
| 1 | +import { afterEach, beforeAll, describe, expect, it, vi } from "vitest"; |
| 2 | +import type { GeminiEmbeddingClient } from "./embeddings-gemini.js"; |
| 3 | + |
| 4 | +describe("runGeminiEmbeddingBatches", () => { |
| 5 | + let runGeminiEmbeddingBatches: typeof import("./batch-gemini.js").runGeminiEmbeddingBatches; |
| 6 | + |
| 7 | + beforeAll(async () => { |
| 8 | + ({ runGeminiEmbeddingBatches } = await import("./batch-gemini.js")); |
| 9 | + }); |
| 10 | + |
| 11 | + afterEach(() => { |
| 12 | + vi.resetAllMocks(); |
| 13 | + vi.unstubAllGlobals(); |
| 14 | + }); |
| 15 | + |
| 16 | + const mockClient: GeminiEmbeddingClient = { |
| 17 | + baseUrl: "https://generativelanguage.googleapis.com/v1beta", |
| 18 | + headers: {}, |
| 19 | + model: "gemini-embedding-2-preview", |
| 20 | + modelPath: "models/gemini-embedding-2-preview", |
| 21 | + apiKeys: ["test-key"], |
| 22 | + outputDimensionality: 1536, |
| 23 | + }; |
| 24 | + |
| 25 | + it("includes outputDimensionality in batch upload requests", async () => { |
| 26 | + const fetchMock = vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => { |
| 27 | + const url = |
| 28 | + typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url; |
| 29 | + if (url.includes("/upload/v1beta/files?uploadType=multipart")) { |
| 30 | + const body = init?.body; |
| 31 | + if (!(body instanceof Blob)) { |
| 32 | + throw new Error("expected multipart blob body"); |
| 33 | + } |
| 34 | + const text = await body.text(); |
| 35 | + expect(text).toContain('"taskType":"RETRIEVAL_DOCUMENT"'); |
| 36 | + expect(text).toContain('"outputDimensionality":1536'); |
| 37 | + return new Response(JSON.stringify({ name: "files/file-123" }), { |
| 38 | + status: 200, |
| 39 | + headers: { "Content-Type": "application/json" }, |
| 40 | + }); |
| 41 | + } |
| 42 | + if (url.endsWith(":asyncBatchEmbedContent")) { |
| 43 | + return new Response( |
| 44 | + JSON.stringify({ |
| 45 | + name: "batches/batch-1", |
| 46 | + state: "COMPLETED", |
| 47 | + outputConfig: { file: "files/output-1" }, |
| 48 | + }), |
| 49 | + { |
| 50 | + status: 200, |
| 51 | + headers: { "Content-Type": "application/json" }, |
| 52 | + }, |
| 53 | + ); |
| 54 | + } |
| 55 | + if (url.endsWith("/files/output-1:download")) { |
| 56 | + return new Response( |
| 57 | + JSON.stringify({ |
| 58 | + key: "req-1", |
| 59 | + response: { embedding: { values: [0.1, 0.2, 0.3] } }, |
| 60 | + }), |
| 61 | + { |
| 62 | + status: 200, |
| 63 | + headers: { "Content-Type": "application/jsonl" }, |
| 64 | + }, |
| 65 | + ); |
| 66 | + } |
| 67 | + throw new Error(`unexpected fetch ${url}`); |
| 68 | + }); |
| 69 | + |
| 70 | + vi.stubGlobal("fetch", fetchMock); |
| 71 | + |
| 72 | + const results = await runGeminiEmbeddingBatches({ |
| 73 | + gemini: mockClient, |
| 74 | + agentId: "main", |
| 75 | + requests: [ |
| 76 | + { |
| 77 | + custom_id: "req-1", |
| 78 | + request: { |
| 79 | + content: { parts: [{ text: "hello world" }] }, |
| 80 | + taskType: "RETRIEVAL_DOCUMENT", |
| 81 | + outputDimensionality: 1536, |
| 82 | + }, |
| 83 | + }, |
| 84 | + ], |
| 85 | + wait: true, |
| 86 | + pollIntervalMs: 1, |
| 87 | + timeoutMs: 1000, |
| 88 | + concurrency: 1, |
| 89 | + }); |
| 90 | + |
| 91 | + expect(results.get("req-1")).toEqual([0.1, 0.2, 0.3]); |
| 92 | + expect(fetchMock).toHaveBeenCalledTimes(3); |
| 93 | + }); |
| 94 | +}); |
0 commit comments