embedding-openai.test.ts 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  1. /**
  2. * embedding-openai.test.ts - Tests for OpenAIEmbeddingsProvider (HTTP backend).
  3. *
  4. * Uses a mock fetch — no network required. Covers:
  5. * - 200 happy path
  6. * - 429 → retry → success
  7. * - 503 persistent → exhausted retries → null
  8. * - 4xx (non-429) → no retry, immediate failure
  9. * - batch chunking (>64 items → multiple HTTP calls)
  10. * - timeout / abort
  11. * - malformed JSON / missing data array
  12. * - circuit breaker open + half-open recovery
  13. * - dimension probing
  14. * - healthcheck endpoint
  15. */
  16. import { describe, test, expect, vi } from "vitest";
  17. import {
  18. OpenAIEmbeddingsProvider,
  19. CircuitBreaker,
  20. CircuitOpenError,
  21. HttpError,
  22. isRetryableStatus,
  23. chunkArray,
  24. RETRY_BACKOFFS_MS,
  25. } from "../src/embedding/openai.js";
  26. // ─────────────────────────── Helpers ─────────────────────────────────────────
  27. function mockResponse(status: number, body: unknown, opts?: { delayMs?: number }): Response {
  28. const text = typeof body === "string" ? body : JSON.stringify(body);
  29. const init: ResponseInit = {
  30. status,
  31. headers: { "content-type": "application/json" },
  32. };
  33. if (opts?.delayMs) {
  34. // Synchronous Response — test code awaits it directly, so delayMs would
  35. // need to be implemented in the fetch wrapper, not here.
  36. }
  37. return new Response(text, init);
  38. }
  39. function makeFetchSequence(responses: Array<() => Promise<Response> | Response>): {
  40. fetchImpl: typeof fetch;
  41. calls: { url: string; init?: RequestInit }[];
  42. } {
  43. const calls: { url: string; init?: RequestInit }[] = [];
  44. let i = 0;
  45. const fetchImpl = (async (input: RequestInfo | URL, init?: RequestInit) => {
  46. const url = typeof input === "string" ? input : input.toString();
  47. calls.push({ url, init });
  48. if (i >= responses.length) throw new Error(`Mock fetch exhausted at call ${i + 1}`);
  49. const r = responses[i++]!();
  50. return r instanceof Promise ? r : r;
  51. }) as typeof fetch;
  52. return { fetchImpl, calls };
  53. }
  54. function fakeEmbedding(dim: number, seed = 0): number[] {
  55. return Array.from({ length: dim }, (_, i) => Math.sin(seed + i) * 0.5);
  56. }
  57. function embeddingsResponse(texts: string[], dim = 4): Response {
  58. return mockResponse(200, {
  59. object: "list",
  60. model: "embeddinggemma:300m",
  61. data: texts.map((_, i) => ({
  62. object: "embedding",
  63. index: i,
  64. embedding: fakeEmbedding(dim, i * 7),
  65. })),
  66. });
  67. }
  68. // ─────────────────────────── Pure helpers ────────────────────────────────────
  69. describe("isRetryableStatus", () => {
  70. test("429 retryable", () => expect(isRetryableStatus(429)).toBe(true));
  71. test("503 retryable", () => expect(isRetryableStatus(503)).toBe(true));
  72. test("400 NOT retryable", () => expect(isRetryableStatus(400)).toBe(false));
  73. test("401 NOT retryable", () => expect(isRetryableStatus(401)).toBe(false));
  74. test("404 NOT retryable", () => expect(isRetryableStatus(404)).toBe(false));
  75. test("500 NOT retryable", () => expect(isRetryableStatus(500)).toBe(false));
  76. test("502 NOT retryable", () => expect(isRetryableStatus(502)).toBe(false));
  77. test("200 NOT retryable", () => expect(isRetryableStatus(200)).toBe(false));
  78. });
  79. describe("chunkArray", () => {
  80. test("empty input → empty output", () => {
  81. expect(chunkArray([], 5)).toEqual([]);
  82. });
  83. test("input ≤ size → single chunk", () => {
  84. expect(chunkArray([1, 2, 3], 5)).toEqual([[1, 2, 3]]);
  85. });
  86. test("input = size → single chunk", () => {
  87. expect(chunkArray([1, 2, 3, 4, 5], 5)).toEqual([[1, 2, 3, 4, 5]]);
  88. });
  89. test("input > size → multiple chunks", () => {
  90. expect(chunkArray([1, 2, 3, 4, 5, 6, 7], 3)).toEqual([
  91. [1, 2, 3],
  92. [4, 5, 6],
  93. [7],
  94. ]);
  95. });
  96. test("65 items at size 64 → 64 + 1", () => {
  97. const items = Array.from({ length: 65 }, (_, i) => i);
  98. const chunks = chunkArray(items, 64);
  99. expect(chunks.length).toBe(2);
  100. expect(chunks[0]!.length).toBe(64);
  101. expect(chunks[1]!.length).toBe(1);
  102. });
  103. test("size < 1 throws", () => {
  104. expect(() => chunkArray([1, 2, 3], 0)).toThrow();
  105. expect(() => chunkArray([1, 2, 3], -1)).toThrow();
  106. });
  107. });
  108. // ─────────────────────────── Circuit Breaker ─────────────────────────────────
  109. describe("CircuitBreaker", () => {
  110. test("starts closed", () => {
  111. const cb = new CircuitBreaker();
  112. expect(cb.getState()).toBe("closed");
  113. expect(cb.shouldFailFast()).toBe(false);
  114. });
  115. test("stays closed below minSamples even with all-failures", () => {
  116. const cb = new CircuitBreaker({ minSamples: 4, threshold: 0.5 });
  117. cb.recordFailure();
  118. cb.recordFailure();
  119. cb.recordFailure();
  120. expect(cb.getState()).toBe("closed");
  121. });
  122. test("opens when failure rate exceeds threshold", () => {
  123. const cb = new CircuitBreaker({ minSamples: 4, threshold: 0.5 });
  124. cb.recordFailure();
  125. cb.recordFailure();
  126. cb.recordFailure();
  127. cb.recordFailure();
  128. expect(cb.getState()).toBe("open");
  129. expect(cb.shouldFailFast()).toBe(true);
  130. });
  131. test("transitions OPEN → HALF-OPEN after openDurationMs", () => {
  132. let now = 1_000_000;
  133. const cb = new CircuitBreaker({
  134. minSamples: 4,
  135. threshold: 0.5,
  136. openDurationMs: 5000,
  137. now: () => now,
  138. });
  139. for (let i = 0; i < 4; i++) cb.recordFailure();
  140. expect(cb.getState()).toBe("open");
  141. now += 5001;
  142. expect(cb.getState()).toBe("half-open");
  143. });
  144. test("HALF-OPEN + success → CLOSED", () => {
  145. let now = 1_000_000;
  146. const cb = new CircuitBreaker({
  147. minSamples: 4,
  148. threshold: 0.5,
  149. openDurationMs: 5000,
  150. now: () => now,
  151. });
  152. for (let i = 0; i < 4; i++) cb.recordFailure();
  153. now += 5001;
  154. cb.recordSuccess(); // half-open probe
  155. expect(cb.getState()).toBe("closed");
  156. });
  157. test("HALF-OPEN + failure → re-OPEN", () => {
  158. let now = 1_000_000;
  159. const cb = new CircuitBreaker({
  160. minSamples: 4,
  161. threshold: 0.5,
  162. openDurationMs: 5000,
  163. now: () => now,
  164. });
  165. for (let i = 0; i < 4; i++) cb.recordFailure();
  166. now += 5001;
  167. expect(cb.getState()).toBe("half-open");
  168. cb.recordFailure();
  169. expect(cb.getState()).toBe("open");
  170. });
  171. test("samples outside window are dropped", () => {
  172. let now = 1_000_000;
  173. const cb = new CircuitBreaker({
  174. minSamples: 4,
  175. threshold: 0.5,
  176. windowMs: 1000,
  177. now: () => now,
  178. });
  179. cb.recordFailure();
  180. cb.recordFailure();
  181. now += 1500; // window expired
  182. cb.recordSuccess();
  183. cb.recordSuccess();
  184. cb.recordSuccess();
  185. cb.recordSuccess();
  186. // Old failures should be discarded; rate = 0/4 < threshold
  187. expect(cb.getState()).toBe("closed");
  188. });
  189. test("reset() clears state", () => {
  190. const cb = new CircuitBreaker({ minSamples: 2, threshold: 0.5 });
  191. cb.recordFailure();
  192. cb.recordFailure();
  193. expect(cb.getState()).toBe("open");
  194. cb.reset();
  195. expect(cb.getState()).toBe("closed");
  196. });
  197. });
  198. // ─────────────────────────── HappyPath ───────────────────────────────────────
  199. describe("OpenAIEmbeddingsProvider — happy path", () => {
  200. test("single embed call → 200 success", async () => {
  201. const { fetchImpl, calls } = makeFetchSequence([
  202. () => embeddingsResponse(["hello"], 4),
  203. ]);
  204. const p = new OpenAIEmbeddingsProvider({
  205. endpoint: "https://ai.example.com",
  206. fetchImpl,
  207. });
  208. const r = await p.embed("hello");
  209. expect(r).not.toBeNull();
  210. expect(r!.embedding.length).toBe(4);
  211. expect(r!.model).toBe("embeddinggemma");
  212. expect(p.getDimensions()).toBe(4);
  213. expect(calls.length).toBe(1);
  214. expect(calls[0]!.url).toBe("https://ai.example.com/v1/embeddings");
  215. });
  216. test("strips trailing slashes from endpoint", async () => {
  217. const { fetchImpl, calls } = makeFetchSequence([
  218. () => embeddingsResponse(["x"], 2),
  219. ]);
  220. const p = new OpenAIEmbeddingsProvider({
  221. endpoint: "https://ai.example.com////",
  222. fetchImpl,
  223. });
  224. await p.embed("x");
  225. expect(calls[0]!.url).toBe("https://ai.example.com/v1/embeddings");
  226. });
  227. test("batch of 3 → 1 HTTP call", async () => {
  228. const { fetchImpl, calls } = makeFetchSequence([
  229. () => embeddingsResponse(["a", "b", "c"], 3),
  230. ]);
  231. const p = new OpenAIEmbeddingsProvider({
  232. endpoint: "https://ai.example.com",
  233. fetchImpl,
  234. });
  235. const result = await p.embedBatch(["a", "b", "c"]);
  236. expect(result.length).toBe(3);
  237. expect(result.every((r) => r !== null)).toBe(true);
  238. expect(calls.length).toBe(1);
  239. });
  240. test("respects custom modelId / upstreamModel in request body", async () => {
  241. const { fetchImpl, calls } = makeFetchSequence([
  242. () => embeddingsResponse(["x"], 2),
  243. ]);
  244. const p = new OpenAIEmbeddingsProvider({
  245. endpoint: "https://ai.example.com",
  246. modelId: "embeddinggemma",
  247. upstreamModel: "embeddinggemma:300m",
  248. fetchImpl,
  249. });
  250. const r = await p.embed("x");
  251. expect(r!.model).toBe("embeddinggemma");
  252. const body = JSON.parse(calls[0]!.init!.body as string);
  253. expect(body.model).toBe("embeddinggemma:300m");
  254. });
  255. test("Authorization header set when apiKey provided", async () => {
  256. const { fetchImpl, calls } = makeFetchSequence([
  257. () => embeddingsResponse(["x"], 2),
  258. ]);
  259. const p = new OpenAIEmbeddingsProvider({
  260. endpoint: "https://ai.example.com",
  261. apiKey: "sk-test-123",
  262. fetchImpl,
  263. });
  264. await p.embed("x");
  265. const headers = calls[0]!.init!.headers as Record<string, string>;
  266. expect(headers["Authorization"]).toBe("Bearer sk-test-123");
  267. });
  268. test("Authorization header omitted when apiKey not provided", async () => {
  269. const { fetchImpl, calls } = makeFetchSequence([
  270. () => embeddingsResponse(["x"], 2),
  271. ]);
  272. const p = new OpenAIEmbeddingsProvider({
  273. endpoint: "https://ai.example.com",
  274. fetchImpl,
  275. });
  276. await p.embed("x");
  277. const headers = calls[0]!.init!.headers as Record<string, string>;
  278. expect(headers["Authorization"]).toBeUndefined();
  279. });
  280. });
  281. // ─────────────────────────── Batch chunking ──────────────────────────────────
  282. describe("OpenAIEmbeddingsProvider — batch chunking", () => {
  283. test("100 items at batchSize=64 → 2 HTTP calls (64 + 36)", async () => {
  284. const { fetchImpl, calls } = makeFetchSequence([
  285. () => embeddingsResponse(Array.from({ length: 64 }, () => "x"), 4),
  286. () => embeddingsResponse(Array.from({ length: 36 }, () => "x"), 4),
  287. ]);
  288. const p = new OpenAIEmbeddingsProvider({
  289. endpoint: "https://ai.example.com",
  290. fetchImpl,
  291. batchSize: 64,
  292. });
  293. const texts = Array.from({ length: 100 }, (_, i) => `text-${i}`);
  294. const result = await p.embedBatch(texts);
  295. expect(result.length).toBe(100);
  296. expect(result.every((r) => r !== null)).toBe(true);
  297. expect(calls.length).toBe(2);
  298. const body0 = JSON.parse(calls[0]!.init!.body as string);
  299. const body1 = JSON.parse(calls[1]!.init!.body as string);
  300. expect(body0.input.length).toBe(64);
  301. expect(body1.input.length).toBe(36);
  302. });
  303. test("custom batchSize=10 → multiple smaller calls", async () => {
  304. const { fetchImpl, calls } = makeFetchSequence([
  305. () => embeddingsResponse(Array.from({ length: 10 }, () => "x"), 2),
  306. () => embeddingsResponse(Array.from({ length: 10 }, () => "x"), 2),
  307. () => embeddingsResponse(Array.from({ length: 5 }, () => "x"), 2),
  308. ]);
  309. const p = new OpenAIEmbeddingsProvider({
  310. endpoint: "https://ai.example.com",
  311. fetchImpl,
  312. batchSize: 10,
  313. });
  314. const texts = Array.from({ length: 25 }, (_, i) => `t${i}`);
  315. const result = await p.embedBatch(texts);
  316. expect(result.length).toBe(25);
  317. expect(result.every((r) => r !== null)).toBe(true);
  318. expect(calls.length).toBe(3);
  319. });
  320. test("empty input → no HTTP calls", async () => {
  321. const { fetchImpl, calls } = makeFetchSequence([]);
  322. const p = new OpenAIEmbeddingsProvider({
  323. endpoint: "https://ai.example.com",
  324. fetchImpl,
  325. });
  326. const result = await p.embedBatch([]);
  327. expect(result).toEqual([]);
  328. expect(calls.length).toBe(0);
  329. });
  330. });
  331. // ─────────────────────────── Retry behavior ──────────────────────────────────
  332. describe("OpenAIEmbeddingsProvider — retry on 429/503", () => {
  333. test("429 → retry → success", async () => {
  334. const sleepCalls: number[] = [];
  335. const { fetchImpl, calls } = makeFetchSequence([
  336. () => mockResponse(429, { error: "rate limit" }),
  337. () => embeddingsResponse(["x"], 4),
  338. ]);
  339. const p = new OpenAIEmbeddingsProvider({
  340. endpoint: "https://ai.example.com",
  341. fetchImpl,
  342. retryBackoffsMs: [10, 20, 40],
  343. sleep: async (ms) => {
  344. sleepCalls.push(ms);
  345. },
  346. });
  347. const r = await p.embed("x");
  348. expect(r).not.toBeNull();
  349. expect(calls.length).toBe(2);
  350. expect(sleepCalls).toEqual([10]);
  351. });
  352. test("503 → retry → success", async () => {
  353. const sleepCalls: number[] = [];
  354. const { fetchImpl, calls } = makeFetchSequence([
  355. () => mockResponse(503, { error: "service unavailable" }),
  356. () => embeddingsResponse(["x"], 4),
  357. ]);
  358. const p = new OpenAIEmbeddingsProvider({
  359. endpoint: "https://ai.example.com",
  360. fetchImpl,
  361. retryBackoffsMs: [5, 10, 20],
  362. sleep: async (ms) => {
  363. sleepCalls.push(ms);
  364. },
  365. });
  366. const r = await p.embed("x");
  367. expect(r).not.toBeNull();
  368. expect(calls.length).toBe(2);
  369. expect(sleepCalls).toEqual([5]);
  370. });
  371. test("503 persistent → exhausted retries → null result", async () => {
  372. const sleepCalls: number[] = [];
  373. const { fetchImpl, calls } = makeFetchSequence([
  374. () => mockResponse(503, "down"),
  375. () => mockResponse(503, "down"),
  376. () => mockResponse(503, "down"),
  377. () => mockResponse(503, "down"),
  378. ]);
  379. const p = new OpenAIEmbeddingsProvider({
  380. endpoint: "https://ai.example.com",
  381. fetchImpl,
  382. retryBackoffsMs: [1, 2, 4],
  383. sleep: async (ms) => {
  384. sleepCalls.push(ms);
  385. },
  386. });
  387. const r = await p.embed("x");
  388. expect(r).toBeNull();
  389. expect(calls.length).toBe(4); // initial + 3 retries
  390. expect(sleepCalls).toEqual([1, 2, 4]);
  391. });
  392. test("default backoff schedule is 1s/4s/16s", () => {
  393. expect(RETRY_BACKOFFS_MS).toEqual([1000, 4000, 16000]);
  394. });
  395. test("4xx (non-429) → immediate failure, no retry", async () => {
  396. const sleepCalls: number[] = [];
  397. const { fetchImpl, calls } = makeFetchSequence([
  398. () => mockResponse(401, { error: "unauthorized" }),
  399. ]);
  400. const p = new OpenAIEmbeddingsProvider({
  401. endpoint: "https://ai.example.com",
  402. fetchImpl,
  403. retryBackoffsMs: [10, 20, 40],
  404. sleep: async (ms) => {
  405. sleepCalls.push(ms);
  406. },
  407. });
  408. const r = await p.embed("x");
  409. expect(r).toBeNull();
  410. expect(calls.length).toBe(1); // no retries
  411. expect(sleepCalls).toEqual([]);
  412. });
  413. test("404 → immediate failure, no retry", async () => {
  414. const { fetchImpl, calls } = makeFetchSequence([
  415. () => mockResponse(404, "not found"),
  416. ]);
  417. const p = new OpenAIEmbeddingsProvider({
  418. endpoint: "https://ai.example.com",
  419. fetchImpl,
  420. retryBackoffsMs: [1],
  421. sleep: async () => {},
  422. });
  423. await p.embed("x");
  424. expect(calls.length).toBe(1);
  425. });
  426. });
  427. // ─────────────────────────── Malformed responses ─────────────────────────────
  428. describe("OpenAIEmbeddingsProvider — malformed responses", () => {
  429. test("malformed JSON → null result", async () => {
  430. const { fetchImpl } = makeFetchSequence([
  431. () => new Response("not-json{}", { status: 200 }),
  432. ]);
  433. const p = new OpenAIEmbeddingsProvider({
  434. endpoint: "https://ai.example.com",
  435. fetchImpl,
  436. retryBackoffsMs: [],
  437. sleep: async () => {},
  438. });
  439. const r = await p.embed("x");
  440. expect(r).toBeNull();
  441. });
  442. test("missing data array → null result", async () => {
  443. const { fetchImpl } = makeFetchSequence([
  444. () => mockResponse(200, { object: "list", model: "x" }),
  445. ]);
  446. const p = new OpenAIEmbeddingsProvider({
  447. endpoint: "https://ai.example.com",
  448. fetchImpl,
  449. retryBackoffsMs: [],
  450. sleep: async () => {},
  451. });
  452. const r = await p.embed("x");
  453. expect(r).toBeNull();
  454. });
  455. test("data item index out of range → null result", async () => {
  456. const { fetchImpl } = makeFetchSequence([
  457. () =>
  458. mockResponse(200, {
  459. object: "list",
  460. data: [
  461. { index: 5, embedding: [0.1, 0.2] }, // out of range for 1 input
  462. ],
  463. }),
  464. ]);
  465. const p = new OpenAIEmbeddingsProvider({
  466. endpoint: "https://ai.example.com",
  467. fetchImpl,
  468. retryBackoffsMs: [],
  469. sleep: async () => {},
  470. });
  471. const r = await p.embed("x");
  472. expect(r).toBeNull();
  473. });
  474. test("response handles out-of-order data array (sorts by index)", async () => {
  475. const { fetchImpl } = makeFetchSequence([
  476. () =>
  477. mockResponse(200, {
  478. object: "list",
  479. data: [
  480. { index: 1, embedding: [0.7, 0.8] },
  481. { index: 0, embedding: [0.1, 0.2] },
  482. ],
  483. }),
  484. ]);
  485. const p = new OpenAIEmbeddingsProvider({
  486. endpoint: "https://ai.example.com",
  487. fetchImpl,
  488. });
  489. const result = await p.embedBatch(["zero", "one"]);
  490. expect(result.length).toBe(2);
  491. expect(result[0]!.embedding).toEqual([0.1, 0.2]);
  492. expect(result[1]!.embedding).toEqual([0.7, 0.8]);
  493. });
  494. });
  495. // ─────────────────────────── Timeout / abort ─────────────────────────────────
  496. describe("OpenAIEmbeddingsProvider — timeout and abort", () => {
  497. test("user abort signal → null result + no further calls", async () => {
  498. const { fetchImpl, calls } = makeFetchSequence([
  499. () => embeddingsResponse(["a", "b"], 2),
  500. ]);
  501. const p = new OpenAIEmbeddingsProvider({
  502. endpoint: "https://ai.example.com",
  503. fetchImpl,
  504. batchSize: 1,
  505. });
  506. const ctrl = new AbortController();
  507. ctrl.abort(new Error("user cancelled"));
  508. const result = await p.embedBatch(["a", "b"], { signal: ctrl.signal });
  509. expect(result).toEqual([null, null]);
  510. expect(calls.length).toBe(0); // signal aborted before first call
  511. });
  512. test("per-attempt timeout aborts a slow request", async () => {
  513. let aborted = false;
  514. const fetchImpl = (async (_url: any, init?: RequestInit) => {
  515. return await new Promise<Response>((_resolve, reject) => {
  516. const sig = init?.signal;
  517. sig?.addEventListener("abort", () => {
  518. aborted = true;
  519. reject(new DOMException("aborted", "AbortError"));
  520. });
  521. });
  522. }) as typeof fetch;
  523. const p = new OpenAIEmbeddingsProvider({
  524. endpoint: "https://ai.example.com",
  525. fetchImpl,
  526. timeoutMs: 50,
  527. retryBackoffsMs: [],
  528. sleep: async () => {},
  529. });
  530. const r = await p.embed("hello");
  531. expect(r).toBeNull();
  532. expect(aborted).toBe(true);
  533. });
  534. });
  535. // ─────────────────────────── Circuit breaker integration ─────────────────────
  536. describe("OpenAIEmbeddingsProvider — circuit breaker integration", () => {
  537. test("repeated failures eventually trip breaker → CircuitOpenError", async () => {
  538. // 4 chunks of size 1 = 4 sample slots. All fail with 401 → 4 failures → breaker opens.
  539. const { fetchImpl } = makeFetchSequence([
  540. () => mockResponse(401, "fail"),
  541. () => mockResponse(401, "fail"),
  542. () => mockResponse(401, "fail"),
  543. () => mockResponse(401, "fail"),
  544. () => mockResponse(401, "fail"), // shouldn't be reached
  545. ]);
  546. const p = new OpenAIEmbeddingsProvider({
  547. endpoint: "https://ai.example.com",
  548. fetchImpl,
  549. batchSize: 1,
  550. retryBackoffsMs: [],
  551. sleep: async () => {},
  552. });
  553. // First call: 4 sub-chunks, all fail, breaker opens during 4th
  554. const result1 = await p.embedBatch(["a", "b", "c", "d"]);
  555. expect(result1.every((x) => x === null)).toBe(true);
  556. expect(p.breaker.getState()).toBe("open");
  557. // Second call: breaker fails fast
  558. await expect(p.embedBatch(["e"])).rejects.toBeInstanceOf(CircuitOpenError);
  559. });
  560. test("breaker recovers after openDuration → success closes it", async () => {
  561. let now = 1_000_000;
  562. const { fetchImpl } = makeFetchSequence([
  563. () => mockResponse(401, "fail"),
  564. () => mockResponse(401, "fail"),
  565. () => mockResponse(401, "fail"),
  566. () => mockResponse(401, "fail"),
  567. () => embeddingsResponse(["recovered"], 2),
  568. ]);
  569. const p = new OpenAIEmbeddingsProvider({
  570. endpoint: "https://ai.example.com",
  571. fetchImpl,
  572. batchSize: 1,
  573. retryBackoffsMs: [],
  574. sleep: async () => {},
  575. now: () => now,
  576. });
  577. // Override breaker with a shorter open duration
  578. (p as any).breaker = new CircuitBreaker({
  579. minSamples: 4,
  580. threshold: 0.5,
  581. openDurationMs: 1000,
  582. now: () => now,
  583. });
  584. await p.embedBatch(["a", "b", "c", "d"]);
  585. expect((p as any).breaker.getState()).toBe("open");
  586. now += 1500;
  587. expect((p as any).breaker.getState()).toBe("half-open");
  588. const r = await p.embed("recovered");
  589. expect(r).not.toBeNull();
  590. expect((p as any).breaker.getState()).toBe("closed");
  591. });
  592. });
  593. // ─────────────────────────── Healthcheck ─────────────────────────────────────
  594. describe("OpenAIEmbeddingsProvider — healthcheck", () => {
  595. test("healthcheck pings GET /health when available", async () => {
  596. const { fetchImpl, calls } = makeFetchSequence([
  597. () =>
  598. mockResponse(200, {
  599. status: "ok",
  600. model: "embeddinggemma:300m",
  601. }),
  602. ]);
  603. const p = new OpenAIEmbeddingsProvider({
  604. endpoint: "https://ai.example.com",
  605. fetchImpl,
  606. });
  607. const h = await p.healthcheck();
  608. expect(h.ok).toBe(true);
  609. expect(calls.length).toBe(1);
  610. expect(calls[0]!.url).toBe("https://ai.example.com/health");
  611. expect(calls[0]!.init!.method).toBe("GET");
  612. });
  613. test("healthcheck failure → falls through to embed probe", async () => {
  614. const { fetchImpl } = makeFetchSequence([
  615. () => mockResponse(404, "no /health"),
  616. // Then fall back to /v1/embeddings probe
  617. () => embeddingsResponse(["healthcheck"], 4),
  618. ]);
  619. const p = new OpenAIEmbeddingsProvider({
  620. endpoint: "https://ai.example.com",
  621. fetchImpl,
  622. });
  623. const h = await p.healthcheck();
  624. // 404 isn't an exception, it returns ok:false from the /health branch
  625. // The fallback probe is only triggered on actual exceptions.
  626. expect(h.ok).toBe(false);
  627. expect(h.detail).toContain("404");
  628. });
  629. });
  630. // ─────────────────────────── HttpError ───────────────────────────────────────
  631. describe("HttpError", () => {
  632. test("preserves status and body preview", () => {
  633. const err = new HttpError(429, "rate limit exceeded");
  634. expect(err.status).toBe(429);
  635. expect(err.bodyPreview).toBe("rate limit exceeded");
  636. expect(err.message).toContain("HTTP 429");
  637. });
  638. test("truncates long bodies in message", () => {
  639. const longBody = "x".repeat(500);
  640. const err = new HttpError(500, longBody);
  641. expect(err.message.length).toBeLessThan(longBody.length + 200);
  642. });
  643. });
  644. // ─────────────────────────── lastError tracking (i-vm1lxwry) ────────────────
  645. describe("OpenAIEmbeddingsProvider — getLastError (i-vm1lxwry)", () => {
  646. test("returns undefined before first call", () => {
  647. const { fetchImpl } = makeFetchSequence([]);
  648. const p = new OpenAIEmbeddingsProvider({
  649. endpoint: "https://ai.example.com",
  650. fetchImpl,
  651. });
  652. expect(p.getLastError()).toBeUndefined();
  653. });
  654. test("captures HTTP status + endpoint after non-retryable failure", async () => {
  655. const { fetchImpl } = makeFetchSequence([
  656. () => mockResponse(500, "internal error: GPU OOM"),
  657. ]);
  658. const p = new OpenAIEmbeddingsProvider({
  659. endpoint: "https://ai.example.com",
  660. fetchImpl,
  661. retryBackoffsMs: [],
  662. sleep: async () => {},
  663. });
  664. const r = await p.embed("hello");
  665. expect(r).toBeNull();
  666. const lastErr = p.getLastError();
  667. expect(lastErr).toBeDefined();
  668. expect(lastErr).toContain("https://ai.example.com/v1/embeddings");
  669. expect(lastErr).toContain("status=500");
  670. expect(lastErr).toContain("internal error: GPU OOM");
  671. });
  672. test("captures malformed-JSON error message", async () => {
  673. const { fetchImpl } = makeFetchSequence([
  674. () => new Response("not json at all", { status: 200, headers: { "content-type": "application/json" } }),
  675. ]);
  676. const p = new OpenAIEmbeddingsProvider({
  677. endpoint: "https://ai.example.com",
  678. fetchImpl,
  679. retryBackoffsMs: [],
  680. sleep: async () => {},
  681. });
  682. const r = await p.embed("hello");
  683. expect(r).toBeNull();
  684. const lastErr = p.getLastError();
  685. expect(lastErr).toBeDefined();
  686. expect(lastErr).toContain("https://ai.example.com/v1/embeddings");
  687. expect(lastErr).toMatch(/error="/);
  688. });
  689. test("clears lastError after a fully-successful sweep", async () => {
  690. const { fetchImpl } = makeFetchSequence([
  691. () => mockResponse(500, "fail"),
  692. () => embeddingsResponse(["recovered"], 4),
  693. ]);
  694. const p = new OpenAIEmbeddingsProvider({
  695. endpoint: "https://ai.example.com",
  696. fetchImpl,
  697. retryBackoffsMs: [],
  698. sleep: async () => {},
  699. });
  700. // First call fails — lastError set
  701. const r1 = await p.embed("first");
  702. expect(r1).toBeNull();
  703. expect(p.getLastError()).toBeDefined();
  704. // Second call succeeds — lastError cleared
  705. const r2 = await p.embed("recovered");
  706. expect(r2).not.toBeNull();
  707. expect(p.getLastError()).toBeUndefined();
  708. });
  709. test("getEndpoint() exposes configured endpoint (no trailing slash)", () => {
  710. const { fetchImpl } = makeFetchSequence([]);
  711. const p = new OpenAIEmbeddingsProvider({
  712. endpoint: "https://ai.example.com//",
  713. fetchImpl,
  714. });
  715. expect(p.getEndpoint()).toBe("https://ai.example.com");
  716. });
  717. });
  718. // ─────────────────────────── dispose ─────────────────────────────────────────
  719. describe("OpenAIEmbeddingsProvider — dispose", () => {
  720. test("dispose resets the breaker", async () => {
  721. const { fetchImpl } = makeFetchSequence([
  722. () => mockResponse(401, "fail"),
  723. () => mockResponse(401, "fail"),
  724. () => mockResponse(401, "fail"),
  725. () => mockResponse(401, "fail"),
  726. ]);
  727. const p = new OpenAIEmbeddingsProvider({
  728. endpoint: "https://ai.example.com",
  729. fetchImpl,
  730. batchSize: 1,
  731. retryBackoffsMs: [],
  732. sleep: async () => {},
  733. });
  734. await p.embedBatch(["a", "b", "c", "d"]);
  735. expect(p.breaker.getState()).toBe("open");
  736. await p.dispose();
  737. expect(p.breaker.getState()).toBe("closed");
  738. });
  739. });