|
| 1 | +import { afterAll, beforeAll, describe, expect, it, vi } from "vitest"; |
| 2 | +import { resolveAgentDir } from "../agents/agent-scope.js"; |
| 3 | +import { getFreePort, installGatewayTestHooks } from "./test-helpers.js"; |
| 4 | + |
| 5 | +installGatewayTestHooks({ scope: "suite" }); |
| 6 | + |
| 7 | +let startGatewayServer: typeof import("./server.js").startGatewayServer; |
| 8 | +let createEmbeddingProviderMock: ReturnType<typeof vi.fn>; |
| 9 | +let enabledServer: Awaited<ReturnType<typeof startServer>>; |
| 10 | +let enabledPort: number; |
| 11 | + |
| 12 | +beforeAll(async () => { |
| 13 | + vi.resetModules(); |
| 14 | + createEmbeddingProviderMock = vi.fn(async (options: { provider: string; model: string }) => ({ |
| 15 | + provider: { |
| 16 | + id: options.provider, |
| 17 | + model: options.model, |
| 18 | + embedQuery: async () => [0.1, 0.2], |
| 19 | + embedBatch: async (texts: string[]) => |
| 20 | + texts.map((_text, index) => [index + 0.1, index + 0.2]), |
| 21 | + }, |
| 22 | + })); |
| 23 | + vi.doMock("../memory/embeddings.js", async () => { |
| 24 | + const actual = |
| 25 | + await vi.importActual<typeof import("../memory/embeddings.js")>("../memory/embeddings.js"); |
| 26 | + return { |
| 27 | + ...actual, |
| 28 | + createEmbeddingProvider: createEmbeddingProviderMock, |
| 29 | + }; |
| 30 | + }); |
| 31 | + ({ startGatewayServer } = await import("./server.js")); |
| 32 | + enabledPort = await getFreePort(); |
| 33 | + enabledServer = await startServer(enabledPort, { openAiChatCompletionsEnabled: true }); |
| 34 | +}); |
| 35 | + |
| 36 | +afterAll(async () => { |
| 37 | + await enabledServer.close({ reason: "embeddings http enabled suite done" }); |
| 38 | + vi.resetModules(); |
| 39 | +}); |
| 40 | + |
| 41 | +async function startServer(port: number, opts?: { openAiChatCompletionsEnabled?: boolean }) { |
| 42 | + return await startGatewayServer(port, { |
| 43 | + host: "127.0.0.1", |
| 44 | + auth: { mode: "token", token: "secret" }, |
| 45 | + controlUiEnabled: false, |
| 46 | + openAiChatCompletionsEnabled: opts?.openAiChatCompletionsEnabled ?? false, |
| 47 | + }); |
| 48 | +} |
| 49 | + |
| 50 | +async function postEmbeddings(body: unknown, headers?: Record<string, string>) { |
| 51 | + return await fetch(`http://127.0.0.1:${enabledPort}/v1/embeddings`, { |
| 52 | + method: "POST", |
| 53 | + headers: { |
| 54 | + authorization: "Bearer secret", |
| 55 | + "content-type": "application/json", |
| 56 | + ...headers, |
| 57 | + }, |
| 58 | + body: JSON.stringify(body), |
| 59 | + }); |
| 60 | +} |
| 61 | + |
| 62 | +describe("OpenAI-compatible embeddings HTTP API (e2e)", () => { |
| 63 | + it("embeds string and array inputs", async () => { |
| 64 | + const single = await postEmbeddings({ |
| 65 | + model: "text-embedding-3-small", |
| 66 | + input: "hello", |
| 67 | + }); |
| 68 | + expect(single.status).toBe(200); |
| 69 | + const singleJson = (await single.json()) as { |
| 70 | + object?: string; |
| 71 | + data?: Array<{ object?: string; embedding?: number[]; index?: number }>; |
| 72 | + }; |
| 73 | + expect(singleJson.object).toBe("list"); |
| 74 | + expect(singleJson.data?.[0]?.object).toBe("embedding"); |
| 75 | + expect(singleJson.data?.[0]?.embedding).toEqual([0.1, 0.2]); |
| 76 | + |
| 77 | + const batch = await postEmbeddings({ |
| 78 | + model: "text-embedding-3-small", |
| 79 | + input: ["a", "b"], |
| 80 | + }); |
| 81 | + expect(batch.status).toBe(200); |
| 82 | + const batchJson = (await batch.json()) as { |
| 83 | + data?: Array<{ embedding?: number[]; index?: number }>; |
| 84 | + }; |
| 85 | + expect(batchJson.data).toEqual([ |
| 86 | + { object: "embedding", index: 0, embedding: [0.1, 0.2] }, |
| 87 | + { object: "embedding", index: 1, embedding: [1.1, 1.2] }, |
| 88 | + ]); |
| 89 | + |
| 90 | + const qualified = await postEmbeddings({ |
| 91 | + model: "openai/text-embedding-3-small", |
| 92 | + input: "hello again", |
| 93 | + }); |
| 94 | + expect(qualified.status).toBe(200); |
| 95 | + const qualifiedJson = (await qualified.json()) as { model?: string }; |
| 96 | + expect(qualifiedJson.model).toBe("openai/text-embedding-3-small"); |
| 97 | + const lastCall = createEmbeddingProviderMock.mock.calls.at(-1)?.[0] as |
| 98 | + | { provider?: string; model?: string } |
| 99 | + | undefined; |
| 100 | + expect(lastCall).toMatchObject({ |
| 101 | + provider: "openai", |
| 102 | + model: "text-embedding-3-small", |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + it("supports base64 encoding and agent-scoped auth/config resolution", async () => { |
| 107 | + const res = await postEmbeddings( |
| 108 | + { |
| 109 | + model: "text-embedding-3-small", |
| 110 | + input: "hello", |
| 111 | + encoding_format: "base64", |
| 112 | + }, |
| 113 | + { "x-openclaw-agent-id": "beta" }, |
| 114 | + ); |
| 115 | + expect(res.status).toBe(200); |
| 116 | + const json = (await res.json()) as { data?: Array<{ embedding?: string }> }; |
| 117 | + expect(typeof json.data?.[0]?.embedding).toBe("string"); |
| 118 | + expect(createEmbeddingProviderMock).toHaveBeenCalled(); |
| 119 | + const lastCall = createEmbeddingProviderMock.mock.calls.at(-1)?.[0] as |
| 120 | + | { provider?: string; model?: string; fallback?: string; agentDir?: string } |
| 121 | + | undefined; |
| 122 | + expect(lastCall?.model).toBe("text-embedding-3-small"); |
| 123 | + expect(lastCall?.fallback).toBe("none"); |
| 124 | + expect(lastCall?.agentDir).toBe(resolveAgentDir({}, "beta")); |
| 125 | + }); |
| 126 | + |
| 127 | + it("rejects invalid input shapes", async () => { |
| 128 | + const res = await postEmbeddings({ |
| 129 | + model: "text-embedding-3-small", |
| 130 | + input: [{ nope: true }], |
| 131 | + }); |
| 132 | + expect(res.status).toBe(400); |
| 133 | + const json = (await res.json()) as { error?: { type?: string } }; |
| 134 | + expect(json.error?.type).toBe("invalid_request_error"); |
| 135 | + }); |
| 136 | + |
| 137 | + it("rejects disallowed provider-prefixed model overrides", async () => { |
| 138 | + const res = await postEmbeddings({ |
| 139 | + model: "ollama/nomic-embed-text", |
| 140 | + input: "hello", |
| 141 | + }); |
| 142 | + expect(res.status).toBe(400); |
| 143 | + const json = (await res.json()) as { error?: { type?: string; message?: string } }; |
| 144 | + expect(json.error).toEqual({ |
| 145 | + type: "invalid_request_error", |
| 146 | + message: "This agent does not allow that embedding provider on `/v1/embeddings`.", |
| 147 | + }); |
| 148 | + }); |
| 149 | + |
| 150 | + it("rejects oversized batches", async () => { |
| 151 | + const res = await postEmbeddings({ |
| 152 | + model: "text-embedding-3-small", |
| 153 | + input: Array.from({ length: 129 }, () => "x"), |
| 154 | + }); |
| 155 | + expect(res.status).toBe(400); |
| 156 | + const json = (await res.json()) as { error?: { type?: string; message?: string } }; |
| 157 | + expect(json.error).toEqual({ |
| 158 | + type: "invalid_request_error", |
| 159 | + message: "Too many inputs (max 128).", |
| 160 | + }); |
| 161 | + }); |
| 162 | + |
| 163 | + it("sanitizes provider failures", async () => { |
| 164 | + createEmbeddingProviderMock.mockRejectedValueOnce(new Error("secret upstream failure")); |
| 165 | + const res = await postEmbeddings({ |
| 166 | + model: "text-embedding-3-small", |
| 167 | + input: "hello", |
| 168 | + }); |
| 169 | + expect(res.status).toBe(500); |
| 170 | + const json = (await res.json()) as { error?: { type?: string; message?: string } }; |
| 171 | + expect(json.error).toEqual({ |
| 172 | + type: "api_error", |
| 173 | + message: "internal error", |
| 174 | + }); |
| 175 | + }); |
| 176 | +}); |
0 commit comments