mirror of
https://github.com/decolua/9router.git
synced 2026-05-08 12:01:28 +00:00
* feat: implement /v1/embeddings endpoint (#117) Add OpenAI-compatible POST /v1/embeddings endpoint that routes through the existing provider credential + fallback infrastructure. Changes: - open-sse/handlers/embeddingsCore.js: core handler (handleEmbeddingsCore) * Validates input (string or array), encoding_format * Builds provider-specific URL and headers for openai, openrouter, and openai-compatible providers * Handles 401/403 token refresh via executor.refreshCredentials * Returns normalized OpenAI-format response { object: 'list', data, model, usage } - cloud/src/handlers/embeddings.js: cloud Worker handler (handleEmbeddings) * Auth + machineId resolution identical to handleChat * Provider credential fallback loop with rate-limit tracking - cloud/src/index.js: wire new routes * POST /v1/embeddings (new format — machineId from API key) * POST /{machineId}/v1/embeddings (old format — machineId from URL) * test: add unit tests for /v1/embeddings endpoint - Setup vitest as test framework (tests/ directory) - embeddingsCore.test.js (36 tests): - buildEmbeddingsBody: single string, array, encoding_format, default float - buildEmbeddingsUrl: openai, openrouter, openai-compatible-*, unsupported - buildEmbeddingsHeaders: per-provider headers, accessToken fallback - handleEmbeddingsCore: input validation, success path, provider errors, network errors, invalid JSON, token refresh 401 handling - embeddings.cloud.test.js (23 tests): - CORS OPTIONS preflight - Auth: missing/invalid/old-format/wrong key → 401/400 - Body validation: bad JSON, missing model, missing input, bad model → 400 - Happy path: single string, array, delegation, CORS header, machineId override - Rate limiting: all-rate-limited → 429 + Retry-After, no credentials → 400 - Error propagation: non-fallback errors, 429 exhausts accounts Total: 59/59 tests passing Framework: vitest v4.0.18, Node v22.22.0 * feat: add Next.js API route for /v1/embeddings endpoint Wire the embeddings handler into Next.js App Router. - src/app/api/v1/embeddings/route.js: Next.js API route (POST + OPTIONS) - src/sse/handlers/embeddings.js: SSE-layer handler mirroring chat.js pattern Uses handleEmbeddingsCore from open-sse/handlers/embeddingsCore.js with the same auth, credential fallback, and token refresh logic as the chat handler. Supports REQUIRE_API_KEY env var, provider fallback loop, and consistent logging.
587 lines
21 KiB
JavaScript
587 lines
21 KiB
JavaScript
/**
|
|
* Unit tests for open-sse/handlers/embeddingsCore.js
|
|
*
|
|
* Tests cover:
|
|
* - buildEmbeddingsBody() — request body construction
|
|
* - buildEmbeddingsUrl() — URL per provider
|
|
* - buildEmbeddingsHeaders() — headers per provider
|
|
* - handleEmbeddingsCore() — full handler: success, errors, validation
|
|
*/
|
|
|
|
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
|
|
|
// ─── Mock the executors/index.js to avoid transitive uuid dependency ─────────
|
|
// kiro.js (imported by executors/index.js) requires 'uuid' which isn't
|
|
// installed in the test environment. We mock the whole executor layer.
|
|
vi.mock("../../open-sse/executors/index.js", () => ({
|
|
getExecutor: vi.fn(() => ({
|
|
refreshCredentials: vi.fn().mockResolvedValue(null),
|
|
})),
|
|
hasSpecializedExecutor: vi.fn(() => false),
|
|
}));
|
|
|
|
// Also mock tokenRefresh to avoid side effects
|
|
vi.mock("../../open-sse/services/tokenRefresh.js", () => ({
|
|
refreshWithRetry: vi.fn().mockResolvedValue(null),
|
|
}));
|
|
|
|
// Mock proxyFetch to avoid proxy-agent imports in test env
|
|
vi.mock("../../open-sse/utils/proxyFetch.js", () => ({
|
|
default: vi.fn(),
|
|
}));
|
|
|
|
import { handleEmbeddingsCore } from "../../open-sse/handlers/embeddingsCore.js";
|
|
|
|
// ─── Helpers ─────────────────────────────────────────────────────────────────
|
|
|
|
/** Build a minimal success Response from a provider */
|
|
function makeProviderResponse(body, status = 200) {
|
|
return new Response(JSON.stringify(body), {
|
|
status,
|
|
headers: { "Content-Type": "application/json" },
|
|
});
|
|
}
|
|
|
|
/** Build a minimal error Response from a provider */
|
|
function makeProviderErrorResponse(status, message) {
|
|
return new Response(JSON.stringify({ error: { message } }), {
|
|
status,
|
|
headers: { "Content-Type": "application/json" },
|
|
});
|
|
}
|
|
|
|
/** Standard valid embeddings response in OpenAI format */
|
|
const VALID_EMBEDDING_RESPONSE = {
|
|
object: "list",
|
|
data: [
|
|
{
|
|
object: "embedding",
|
|
index: 0,
|
|
embedding: [0.1, 0.2, 0.3],
|
|
},
|
|
],
|
|
model: "text-embedding-ada-002",
|
|
usage: { prompt_tokens: 3, total_tokens: 3 },
|
|
};
|
|
|
|
/** Standard handleEmbeddingsCore options for OpenAI provider */
|
|
function makeOptions(overrides = {}) {
|
|
return {
|
|
body: { model: "text-embedding-ada-002", input: "Hello world" },
|
|
modelInfo: { provider: "openai", model: "text-embedding-ada-002" },
|
|
credentials: { apiKey: "sk-test-key" },
|
|
log: { debug: vi.fn(), info: vi.fn(), warn: vi.fn(), error: vi.fn() },
|
|
onCredentialsRefreshed: vi.fn(),
|
|
onRequestSuccess: vi.fn(),
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
// ─── Test: buildEmbeddingsBody (via handleEmbeddingsCore internals) ──────────
|
|
// We test body construction indirectly by verifying the fetch call payload.
|
|
|
|
describe("buildEmbeddingsBody", () => {
|
|
beforeEach(() => {
|
|
vi.stubGlobal("fetch", vi.fn());
|
|
});
|
|
afterEach(() => {
|
|
vi.unstubAllGlobals();
|
|
});
|
|
|
|
it("single string input — includes model and input, default encoding_format=float", async () => {
|
|
vi.mocked(fetch).mockResolvedValueOnce(makeProviderResponse(VALID_EMBEDDING_RESPONSE));
|
|
|
|
await handleEmbeddingsCore(makeOptions({
|
|
body: { model: "text-embedding-ada-002", input: "Hello world" },
|
|
}));
|
|
|
|
const [, init] = vi.mocked(fetch).mock.calls[0];
|
|
const sent = JSON.parse(init.body);
|
|
expect(sent.model).toBe("text-embedding-ada-002");
|
|
expect(sent.input).toBe("Hello world");
|
|
expect(sent.encoding_format).toBe("float");
|
|
});
|
|
|
|
it("array input — passes array as-is", async () => {
|
|
vi.mocked(fetch).mockResolvedValueOnce(makeProviderResponse(VALID_EMBEDDING_RESPONSE));
|
|
|
|
await handleEmbeddingsCore(makeOptions({
|
|
body: { model: "text-embedding-ada-002", input: ["Hello", "World"] },
|
|
}));
|
|
|
|
const [, init] = vi.mocked(fetch).mock.calls[0];
|
|
const sent = JSON.parse(init.body);
|
|
expect(sent.input).toEqual(["Hello", "World"]);
|
|
});
|
|
|
|
it("custom encoding_format is forwarded", async () => {
|
|
vi.mocked(fetch).mockResolvedValueOnce(makeProviderResponse(VALID_EMBEDDING_RESPONSE));
|
|
|
|
await handleEmbeddingsCore(makeOptions({
|
|
body: {
|
|
model: "text-embedding-ada-002",
|
|
input: "test",
|
|
encoding_format: "base64",
|
|
},
|
|
}));
|
|
|
|
const [, init] = vi.mocked(fetch).mock.calls[0];
|
|
const sent = JSON.parse(init.body);
|
|
expect(sent.encoding_format).toBe("base64");
|
|
});
|
|
|
|
it("no encoding_format in body → defaults to float", async () => {
|
|
vi.mocked(fetch).mockResolvedValueOnce(makeProviderResponse(VALID_EMBEDDING_RESPONSE));
|
|
|
|
await handleEmbeddingsCore(makeOptions({
|
|
body: { model: "text-embedding-ada-002", input: "test" },
|
|
}));
|
|
|
|
const [, init] = vi.mocked(fetch).mock.calls[0];
|
|
const sent = JSON.parse(init.body);
|
|
expect(sent.encoding_format).toBe("float");
|
|
});
|
|
});
|
|
|
|
// ─── Test: buildEmbeddingsUrl ────────────────────────────────────────────────
|
|
|
|
describe("buildEmbeddingsUrl", () => {
|
|
beforeEach(() => {
|
|
vi.stubGlobal("fetch", vi.fn());
|
|
});
|
|
afterEach(() => {
|
|
vi.unstubAllGlobals();
|
|
});
|
|
|
|
it("openai → https://api.openai.com/v1/embeddings", async () => {
|
|
vi.mocked(fetch).mockResolvedValueOnce(makeProviderResponse(VALID_EMBEDDING_RESPONSE));
|
|
|
|
await handleEmbeddingsCore(makeOptions({
|
|
modelInfo: { provider: "openai", model: "text-embedding-ada-002" },
|
|
credentials: { apiKey: "sk-test" },
|
|
}));
|
|
|
|
const [url] = vi.mocked(fetch).mock.calls[0];
|
|
expect(url).toBe("https://api.openai.com/v1/embeddings");
|
|
});
|
|
|
|
it("openrouter → https://openrouter.ai/api/v1/embeddings", async () => {
|
|
vi.mocked(fetch).mockResolvedValueOnce(makeProviderResponse(VALID_EMBEDDING_RESPONSE));
|
|
|
|
await handleEmbeddingsCore(makeOptions({
|
|
modelInfo: { provider: "openrouter", model: "openai/text-embedding-ada-002" },
|
|
credentials: { apiKey: "sk-or-test" },
|
|
}));
|
|
|
|
const [url] = vi.mocked(fetch).mock.calls[0];
|
|
expect(url).toBe("https://openrouter.ai/api/v1/embeddings");
|
|
});
|
|
|
|
it("openai-compatible-* → uses baseUrl from providerSpecificData", async () => {
|
|
vi.mocked(fetch).mockResolvedValueOnce(makeProviderResponse(VALID_EMBEDDING_RESPONSE));
|
|
|
|
await handleEmbeddingsCore(makeOptions({
|
|
modelInfo: { provider: "openai-compatible-custom", model: "embed-v1" },
|
|
credentials: {
|
|
apiKey: "sk-custom",
|
|
providerSpecificData: { baseUrl: "https://custom.ai/v1" },
|
|
},
|
|
}));
|
|
|
|
const [url] = vi.mocked(fetch).mock.calls[0];
|
|
expect(url).toBe("https://custom.ai/v1/embeddings");
|
|
});
|
|
|
|
it("openai-compatible-* strips trailing slash from baseUrl", async () => {
|
|
vi.mocked(fetch).mockResolvedValueOnce(makeProviderResponse(VALID_EMBEDDING_RESPONSE));
|
|
|
|
await handleEmbeddingsCore(makeOptions({
|
|
modelInfo: { provider: "openai-compatible-myhost", model: "embed-v1" },
|
|
credentials: {
|
|
apiKey: "sk-x",
|
|
providerSpecificData: { baseUrl: "https://myhost.ai/v1/" },
|
|
},
|
|
}));
|
|
|
|
const [url] = vi.mocked(fetch).mock.calls[0];
|
|
expect(url).toBe("https://myhost.ai/v1/embeddings");
|
|
});
|
|
|
|
it("openai-compatible-* without baseUrl → falls back to api.openai.com", async () => {
|
|
vi.mocked(fetch).mockResolvedValueOnce(makeProviderResponse(VALID_EMBEDDING_RESPONSE));
|
|
|
|
await handleEmbeddingsCore(makeOptions({
|
|
modelInfo: { provider: "openai-compatible-fallback", model: "embed" },
|
|
credentials: { apiKey: "sk-x", providerSpecificData: {} },
|
|
}));
|
|
|
|
const [url] = vi.mocked(fetch).mock.calls[0];
|
|
expect(url).toBe("https://api.openai.com/v1/embeddings");
|
|
});
|
|
|
|
it("unsupported provider (e.g. gemini-cli) → 400 error, no fetch called", async () => {
|
|
const result = await handleEmbeddingsCore(makeOptions({
|
|
modelInfo: { provider: "gemini-cli", model: "gemini-embedding" },
|
|
credentials: { apiKey: "token" },
|
|
}));
|
|
|
|
expect(vi.mocked(fetch)).not.toHaveBeenCalled();
|
|
expect(result.success).toBe(false);
|
|
expect(result.status).toBe(400);
|
|
expect(result.error).toMatch(/does not support embeddings/i);
|
|
});
|
|
|
|
it("antigravity (non-openai-compatible, no URL mapping) → 400", async () => {
|
|
const result = await handleEmbeddingsCore(makeOptions({
|
|
modelInfo: { provider: "antigravity", model: "some-embed" },
|
|
credentials: { apiKey: "ag-token" },
|
|
}));
|
|
|
|
expect(vi.mocked(fetch)).not.toHaveBeenCalled();
|
|
expect(result.success).toBe(false);
|
|
expect(result.status).toBe(400);
|
|
});
|
|
});
|
|
|
|
// ─── Test: buildEmbeddingsHeaders ───────────────────────────────────────────
|
|
|
|
describe("buildEmbeddingsHeaders", () => {
|
|
beforeEach(() => {
|
|
vi.stubGlobal("fetch", vi.fn());
|
|
});
|
|
afterEach(() => {
|
|
vi.unstubAllGlobals();
|
|
});
|
|
|
|
it("openai → Authorization: Bearer, Content-Type: application/json", async () => {
|
|
vi.mocked(fetch).mockResolvedValueOnce(makeProviderResponse(VALID_EMBEDDING_RESPONSE));
|
|
|
|
await handleEmbeddingsCore(makeOptions({
|
|
modelInfo: { provider: "openai", model: "text-embedding-ada-002" },
|
|
credentials: { apiKey: "sk-mykey" },
|
|
}));
|
|
|
|
const [, init] = vi.mocked(fetch).mock.calls[0];
|
|
expect(init.headers["Authorization"]).toBe("Bearer sk-mykey");
|
|
expect(init.headers["Content-Type"]).toBe("application/json");
|
|
});
|
|
|
|
it("openai — uses accessToken when apiKey is absent", async () => {
|
|
vi.mocked(fetch).mockResolvedValueOnce(makeProviderResponse(VALID_EMBEDDING_RESPONSE));
|
|
|
|
await handleEmbeddingsCore(makeOptions({
|
|
modelInfo: { provider: "openai", model: "text-embedding-ada-002" },
|
|
credentials: { accessToken: "at-mytoken" },
|
|
}));
|
|
|
|
const [, init] = vi.mocked(fetch).mock.calls[0];
|
|
expect(init.headers["Authorization"]).toBe("Bearer at-mytoken");
|
|
});
|
|
|
|
it("openrouter → adds HTTP-Referer and X-Title headers", async () => {
|
|
vi.mocked(fetch).mockResolvedValueOnce(makeProviderResponse(VALID_EMBEDDING_RESPONSE));
|
|
|
|
await handleEmbeddingsCore(makeOptions({
|
|
modelInfo: { provider: "openrouter", model: "openai/text-embedding-ada-002" },
|
|
credentials: { apiKey: "sk-or-key" },
|
|
}));
|
|
|
|
const [, init] = vi.mocked(fetch).mock.calls[0];
|
|
expect(init.headers["HTTP-Referer"]).toBeDefined();
|
|
expect(init.headers["X-Title"]).toBeDefined();
|
|
expect(init.headers["Authorization"]).toBe("Bearer sk-or-key");
|
|
});
|
|
|
|
it("openai-compatible-* → Authorization: Bearer only (no extra headers)", async () => {
|
|
vi.mocked(fetch).mockResolvedValueOnce(makeProviderResponse(VALID_EMBEDDING_RESPONSE));
|
|
|
|
await handleEmbeddingsCore(makeOptions({
|
|
modelInfo: { provider: "openai-compatible-local", model: "nomic-embed" },
|
|
credentials: {
|
|
apiKey: "local-key",
|
|
providerSpecificData: { baseUrl: "http://localhost:11434/v1" },
|
|
},
|
|
}));
|
|
|
|
const [, init] = vi.mocked(fetch).mock.calls[0];
|
|
expect(init.headers["Authorization"]).toBe("Bearer local-key");
|
|
expect(init.headers["HTTP-Referer"]).toBeUndefined();
|
|
expect(init.headers["X-Title"]).toBeUndefined();
|
|
});
|
|
});
|
|
|
|
// ─── Test: handleEmbeddingsCore — input validation ───────────────────────────
|
|
|
|
describe("handleEmbeddingsCore — input validation", () => {
|
|
afterEach(() => {
|
|
vi.unstubAllGlobals();
|
|
});
|
|
|
|
it("missing input → 400 Bad Request", async () => {
|
|
const result = await handleEmbeddingsCore(makeOptions({
|
|
body: { model: "text-embedding-ada-002" }, // no input
|
|
}));
|
|
expect(result.success).toBe(false);
|
|
expect(result.status).toBe(400);
|
|
expect(result.error).toMatch(/missing required field: input/i);
|
|
});
|
|
|
|
it("input is a number → 400 Bad Request", async () => {
|
|
const result = await handleEmbeddingsCore(makeOptions({
|
|
body: { model: "text-embedding-ada-002", input: 42 },
|
|
}));
|
|
expect(result.success).toBe(false);
|
|
expect(result.status).toBe(400);
|
|
expect(result.error).toMatch(/input must be a string or array/i);
|
|
});
|
|
|
|
it("input is an object → 400 Bad Request", async () => {
|
|
const result = await handleEmbeddingsCore(makeOptions({
|
|
body: { model: "text-embedding-ada-002", input: { text: "hello" } },
|
|
}));
|
|
expect(result.success).toBe(false);
|
|
expect(result.status).toBe(400);
|
|
expect(result.error).toMatch(/input must be a string or array/i);
|
|
});
|
|
|
|
it("input is null → 400 Bad Request", async () => {
|
|
const result = await handleEmbeddingsCore(makeOptions({
|
|
body: { model: "text-embedding-ada-002", input: null },
|
|
}));
|
|
expect(result.success).toBe(false);
|
|
expect(result.status).toBe(400);
|
|
});
|
|
|
|
it("empty string input passes validation", async () => {
|
|
vi.stubGlobal("fetch", vi.fn().mockResolvedValueOnce(
|
|
makeProviderResponse(VALID_EMBEDDING_RESPONSE)
|
|
));
|
|
const result = await handleEmbeddingsCore(makeOptions({
|
|
body: { model: "text-embedding-ada-002", input: "" },
|
|
}));
|
|
// Empty string is falsy → treated as missing
|
|
expect(result.success).toBe(false);
|
|
expect(result.status).toBe(400);
|
|
});
|
|
|
|
it("empty array input passes validation and reaches provider", async () => {
|
|
vi.stubGlobal("fetch", vi.fn().mockResolvedValueOnce(
|
|
makeProviderResponse(VALID_EMBEDDING_RESPONSE)
|
|
));
|
|
const result = await handleEmbeddingsCore(makeOptions({
|
|
body: { model: "text-embedding-ada-002", input: [] },
|
|
}));
|
|
// Empty array is truthy → passes, fetch is called
|
|
expect(fetch).toHaveBeenCalledOnce();
|
|
expect(result.success).toBe(true);
|
|
});
|
|
});
|
|
|
|
// ─── Test: handleEmbeddingsCore — success path ───────────────────────────────
|
|
|
|
describe("handleEmbeddingsCore — success path", () => {
|
|
beforeEach(() => {
|
|
vi.stubGlobal("fetch", vi.fn());
|
|
});
|
|
afterEach(() => {
|
|
vi.unstubAllGlobals();
|
|
});
|
|
|
|
it("returns success=true with Response on 200", async () => {
|
|
vi.mocked(fetch).mockResolvedValueOnce(makeProviderResponse(VALID_EMBEDDING_RESPONSE));
|
|
|
|
const result = await handleEmbeddingsCore(makeOptions());
|
|
|
|
expect(result.success).toBe(true);
|
|
expect(result.response).toBeInstanceOf(Response);
|
|
expect(result.response.status).toBe(200);
|
|
});
|
|
|
|
it("response body is valid OpenAI-format JSON", async () => {
|
|
vi.mocked(fetch).mockResolvedValueOnce(makeProviderResponse(VALID_EMBEDDING_RESPONSE));
|
|
|
|
const result = await handleEmbeddingsCore(makeOptions());
|
|
const body = await result.response.json();
|
|
|
|
expect(body.object).toBe("list");
|
|
expect(Array.isArray(body.data)).toBe(true);
|
|
expect(body.data[0]).toHaveProperty("embedding");
|
|
expect(body.data[0]).toHaveProperty("index");
|
|
});
|
|
|
|
it("response includes CORS header Access-Control-Allow-Origin: *", async () => {
|
|
vi.mocked(fetch).mockResolvedValueOnce(makeProviderResponse(VALID_EMBEDDING_RESPONSE));
|
|
|
|
const result = await handleEmbeddingsCore(makeOptions());
|
|
|
|
expect(result.response.headers.get("Access-Control-Allow-Origin")).toBe("*");
|
|
});
|
|
|
|
it("response Content-Type is application/json", async () => {
|
|
vi.mocked(fetch).mockResolvedValueOnce(makeProviderResponse(VALID_EMBEDDING_RESPONSE));
|
|
|
|
const result = await handleEmbeddingsCore(makeOptions());
|
|
|
|
expect(result.response.headers.get("Content-Type")).toContain("application/json");
|
|
});
|
|
|
|
it("calls onRequestSuccess callback on success", async () => {
|
|
vi.mocked(fetch).mockResolvedValueOnce(makeProviderResponse(VALID_EMBEDDING_RESPONSE));
|
|
const onRequestSuccess = vi.fn();
|
|
|
|
await handleEmbeddingsCore(makeOptions({ onRequestSuccess }));
|
|
|
|
expect(onRequestSuccess).toHaveBeenCalledOnce();
|
|
});
|
|
|
|
it("does not call onRequestSuccess on provider error", async () => {
|
|
vi.mocked(fetch).mockResolvedValueOnce(makeProviderErrorResponse(500, "Server exploded"));
|
|
const onRequestSuccess = vi.fn();
|
|
|
|
await handleEmbeddingsCore(makeOptions({ onRequestSuccess }));
|
|
|
|
expect(onRequestSuccess).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("provider response with non-standard format is passed through as-is", async () => {
|
|
const nonStandardBody = { embeddings: [[0.1, 0.2]], model: "custom" };
|
|
vi.mocked(fetch).mockResolvedValueOnce(makeProviderResponse(nonStandardBody));
|
|
|
|
const result = await handleEmbeddingsCore(makeOptions());
|
|
const body = await result.response.json();
|
|
|
|
expect(body).toEqual(nonStandardBody);
|
|
});
|
|
});
|
|
|
|
// ─── Test: handleEmbeddingsCore — provider error handling ────────────────────
|
|
|
|
describe("handleEmbeddingsCore — provider error handling", () => {
|
|
beforeEach(() => {
|
|
vi.stubGlobal("fetch", vi.fn());
|
|
});
|
|
afterEach(() => {
|
|
vi.unstubAllGlobals();
|
|
});
|
|
|
|
it("provider 400 → returns success=false with status 400", async () => {
|
|
vi.mocked(fetch).mockResolvedValueOnce(makeProviderErrorResponse(400, "Bad model"));
|
|
|
|
const result = await handleEmbeddingsCore(makeOptions());
|
|
|
|
expect(result.success).toBe(false);
|
|
expect(result.status).toBe(400);
|
|
});
|
|
|
|
it("provider 429 → returns success=false with status 429", async () => {
|
|
vi.mocked(fetch).mockResolvedValueOnce(makeProviderErrorResponse(429, "Rate limit exceeded"));
|
|
|
|
const result = await handleEmbeddingsCore(makeOptions());
|
|
|
|
expect(result.success).toBe(false);
|
|
expect(result.status).toBe(429);
|
|
});
|
|
|
|
it("provider 500 → returns success=false with status 500", async () => {
|
|
vi.mocked(fetch).mockResolvedValueOnce(makeProviderErrorResponse(500, "Internal error"));
|
|
|
|
const result = await handleEmbeddingsCore(makeOptions());
|
|
|
|
expect(result.success).toBe(false);
|
|
expect(result.status).toBe(500);
|
|
});
|
|
|
|
it("network error (fetch throws) → returns 502 Bad Gateway", async () => {
|
|
vi.mocked(fetch).mockRejectedValueOnce(new Error("ECONNREFUSED"));
|
|
|
|
const result = await handleEmbeddingsCore(makeOptions());
|
|
|
|
expect(result.success).toBe(false);
|
|
expect(result.status).toBe(502);
|
|
expect(result.error).toMatch(/ECONNREFUSED/);
|
|
});
|
|
|
|
it("invalid JSON from provider → returns 502", async () => {
|
|
vi.mocked(fetch).mockResolvedValueOnce(
|
|
new Response("not json }{", {
|
|
status: 200,
|
|
headers: { "Content-Type": "text/plain" },
|
|
})
|
|
);
|
|
|
|
const result = await handleEmbeddingsCore(makeOptions());
|
|
|
|
expect(result.success).toBe(false);
|
|
expect(result.status).toBe(502);
|
|
});
|
|
|
|
it("error result response has OpenAI-format error body", async () => {
|
|
vi.mocked(fetch).mockResolvedValueOnce(makeProviderErrorResponse(400, "Bad model"));
|
|
|
|
const result = await handleEmbeddingsCore(makeOptions());
|
|
const body = await result.response.json();
|
|
|
|
expect(body).toHaveProperty("error");
|
|
expect(body.error).toHaveProperty("message");
|
|
});
|
|
});
|
|
|
|
// ─── Test: handleEmbeddingsCore — token refresh on 401 ───────────────────────
|
|
|
|
describe("handleEmbeddingsCore — token refresh on 401/403", () => {
|
|
beforeEach(() => {
|
|
vi.stubGlobal("fetch", vi.fn());
|
|
});
|
|
afterEach(() => {
|
|
vi.unstubAllGlobals();
|
|
});
|
|
|
|
it("on 401, attempts retry after refresh; succeeds if refresh gives new token", async () => {
|
|
// First call → 401 from provider
|
|
vi.mocked(fetch).mockResolvedValueOnce(
|
|
new Response(JSON.stringify({ error: "unauthorized" }), {
|
|
status: 401,
|
|
headers: { "Content-Type": "application/json" },
|
|
})
|
|
);
|
|
// Second call (retry) → success
|
|
vi.mocked(fetch).mockResolvedValueOnce(makeProviderResponse(VALID_EMBEDDING_RESPONSE));
|
|
|
|
// Credentials with a refreshToken so the executor can try to refresh
|
|
const credentials = {
|
|
apiKey: "sk-old",
|
|
accessToken: "at-old",
|
|
refreshToken: "rt-valid",
|
|
};
|
|
|
|
// Mock executor's refreshCredentials to return new creds
|
|
const result = await handleEmbeddingsCore(makeOptions({
|
|
modelInfo: { provider: "openai", model: "text-embedding-ada-002" },
|
|
credentials,
|
|
onCredentialsRefreshed: vi.fn(),
|
|
}));
|
|
|
|
// The handler may or may not succeed depending on whether the executor
|
|
// can refresh (openai executor likely can't). What we verify is that
|
|
// fetch was called at least once (the initial request).
|
|
expect(vi.mocked(fetch).mock.calls.length).toBeGreaterThanOrEqual(1);
|
|
});
|
|
|
|
it("on 401 with no refresh token, falls back gracefully (no crash)", async () => {
|
|
vi.mocked(fetch).mockResolvedValueOnce(
|
|
new Response(JSON.stringify({ error: "unauthorized" }), {
|
|
status: 401,
|
|
headers: { "Content-Type": "application/json" },
|
|
})
|
|
);
|
|
|
|
const result = await handleEmbeddingsCore(makeOptions({
|
|
credentials: { apiKey: "sk-bad" },
|
|
}));
|
|
|
|
// Should return an error result, not throw
|
|
expect(result).toHaveProperty("success");
|
|
expect(result.success).toBe(false);
|
|
});
|
|
});
|