embedding-openai.test.ts 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  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. // ─────────────────────────── Concurrent dispatch (i-fkpnar9i Phase 1 #1) ─────
  720. /**
  721. * Fetch helper that lets a test:
  722. * - count how many requests are in-flight at any moment
  723. * - control resolution order via per-call deferred promises
  724. * - inspect the start order vs resolution order
  725. *
  726. * Each `responses[i]` returns a Response (or Promise<Response>); the helper
  727. * wraps each call so it awaits a `gate[i]` deferred BEFORE responding. Tests
  728. * call `release(i)` to let the i-th request settle. Useful for testing that
  729. * concurrent dispatch actually overlaps requests.
  730. */
  731. function makeGatedFetchSequence(count: number): {
  732. fetchImpl: typeof fetch;
  733. inFlight: () => number;
  734. startOrder: number[];
  735. release: (idx: number, response: Response) => void;
  736. releaseAll: (responseFor: (idx: number) => Response) => void;
  737. } {
  738. const gates: Array<{ resolve: (r: Response) => void }> = [];
  739. const startOrder: number[] = [];
  740. let inFlight = 0;
  741. let nextStart = 0;
  742. for (let i = 0; i < count; i++) {
  743. let resolveFn: (r: Response) => void = () => {};
  744. new Promise<Response>((resolve) => {
  745. resolveFn = resolve;
  746. });
  747. // re-create properly:
  748. let r2: (x: Response) => void = () => {};
  749. const p = new Promise<Response>((resolve) => {
  750. r2 = resolve;
  751. });
  752. gates.push({ resolve: r2 });
  753. // attach the unresolved promise back to the slot via a closure (below)
  754. (gates[i] as any).promise = p;
  755. }
  756. const fetchImpl = (async (_input: RequestInfo | URL, _init?: RequestInit) => {
  757. const idx = nextStart++;
  758. if (idx >= count) throw new Error(`gated fetch exhausted at ${idx + 1}`);
  759. startOrder.push(idx);
  760. inFlight++;
  761. try {
  762. const r = await (gates[idx] as any).promise;
  763. return r as Response;
  764. } finally {
  765. inFlight--;
  766. }
  767. }) as typeof fetch;
  768. return {
  769. fetchImpl,
  770. inFlight: () => inFlight,
  771. startOrder,
  772. release: (idx: number, response: Response) => gates[idx]!.resolve(response),
  773. releaseAll: (responseFor: (idx: number) => Response) => {
  774. for (let i = 0; i < count; i++) gates[i]!.resolve(responseFor(i));
  775. },
  776. };
  777. }
  778. describe("OpenAIEmbeddingsProvider — concurrent dispatch (i-fkpnar9i)", () => {
  779. test("default concurrency is 4 — 8 chunks of size 1, max in-flight = 4", async () => {
  780. const N = 8;
  781. const gated = makeGatedFetchSequence(N);
  782. const p = new OpenAIEmbeddingsProvider({
  783. endpoint: "https://ai.example.com",
  784. fetchImpl: gated.fetchImpl,
  785. batchSize: 1, // each text becomes its own chunk
  786. });
  787. // Expected concurrency=4 default
  788. expect((p as any).concurrency).toBe(4);
  789. const texts = Array.from({ length: N }, (_, i) => `t${i}`);
  790. const promise = p.embedBatch(texts);
  791. // Yield to the microtask queue so workers can start their first dispatch.
  792. // Multiple yields needed for chained `await this.requestWithRetry → await fetch`.
  793. for (let i = 0; i < 5; i++) await Promise.resolve();
  794. expect(gated.inFlight()).toBe(4);
  795. expect(gated.startOrder).toEqual([0, 1, 2, 3]);
  796. // Release first 4 in reverse order — concurrent dispatch should still
  797. // preserve input order in `results` because each worker writes to its
  798. // pre-computed slot.
  799. for (let i = 3; i >= 0; i--) {
  800. gated.release(i, embeddingsResponse([`t${i}`], 4));
  801. }
  802. // Yield to let workers pick up next chunks
  803. for (let i = 0; i < 10; i++) await Promise.resolve();
  804. expect(gated.inFlight()).toBeGreaterThan(0); // 4 more workers should be in flight
  805. expect(gated.startOrder.length).toBe(8); // all 8 dispatched
  806. // Release the rest
  807. for (let i = 4; i < N; i++) {
  808. gated.release(i, embeddingsResponse([`t${i}`], 4));
  809. }
  810. const result = await promise;
  811. expect(result.length).toBe(N);
  812. // Critical: input order preserved despite out-of-order resolution
  813. for (let i = 0; i < N; i++) {
  814. expect(result[i]).not.toBeNull();
  815. expect(result[i]!.model).toBe("embeddinggemma");
  816. }
  817. expect(gated.inFlight()).toBe(0);
  818. });
  819. test("explicit concurrency=2 — only 2 in-flight at any moment", async () => {
  820. const N = 6;
  821. const gated = makeGatedFetchSequence(N);
  822. const p = new OpenAIEmbeddingsProvider({
  823. endpoint: "https://ai.example.com",
  824. fetchImpl: gated.fetchImpl,
  825. batchSize: 1,
  826. concurrency: 2,
  827. });
  828. const texts = Array.from({ length: N }, (_, i) => `t${i}`);
  829. const promise = p.embedBatch(texts);
  830. for (let i = 0; i < 5; i++) await Promise.resolve();
  831. expect(gated.inFlight()).toBe(2);
  832. // Cycle: release one, wait, expect new one started
  833. gated.release(0, embeddingsResponse(["t0"], 4));
  834. for (let i = 0; i < 10; i++) await Promise.resolve();
  835. expect(gated.inFlight()).toBe(2); // still 2 — slot filled by t2
  836. // Drain the rest
  837. for (let i = 1; i < N; i++) {
  838. gated.release(i, embeddingsResponse([`t${i}`], 4));
  839. for (let j = 0; j < 5; j++) await Promise.resolve();
  840. }
  841. const result = await promise;
  842. expect(result.every((r) => r !== null)).toBe(true);
  843. });
  844. test("concurrency=1 reproduces legacy sequential behavior", async () => {
  845. const N = 4;
  846. const gated = makeGatedFetchSequence(N);
  847. const p = new OpenAIEmbeddingsProvider({
  848. endpoint: "https://ai.example.com",
  849. fetchImpl: gated.fetchImpl,
  850. batchSize: 1,
  851. concurrency: 1,
  852. });
  853. const texts = Array.from({ length: N }, (_, i) => `t${i}`);
  854. const promise = p.embedBatch(texts);
  855. for (let i = 0; i < 5; i++) await Promise.resolve();
  856. expect(gated.inFlight()).toBe(1);
  857. expect(gated.startOrder).toEqual([0]);
  858. // Release one at a time, confirm the next starts only after.
  859. for (let i = 0; i < N; i++) {
  860. gated.release(i, embeddingsResponse([`t${i}`], 4));
  861. for (let j = 0; j < 5; j++) await Promise.resolve();
  862. }
  863. await promise;
  864. // All started in order (sequential)
  865. expect(gated.startOrder).toEqual([0, 1, 2, 3]);
  866. });
  867. test("results in input order even when the LAST chunk resolves first", async () => {
  868. const N = 4;
  869. const gated = makeGatedFetchSequence(N);
  870. const p = new OpenAIEmbeddingsProvider({
  871. endpoint: "https://ai.example.com",
  872. fetchImpl: gated.fetchImpl,
  873. batchSize: 1,
  874. concurrency: 4,
  875. });
  876. const texts = ["alpha", "beta", "gamma", "delta"];
  877. const promise = p.embedBatch(texts);
  878. // Wait for all 4 to be in flight, then resolve LAST first
  879. for (let i = 0; i < 5; i++) await Promise.resolve();
  880. expect(gated.inFlight()).toBe(4);
  881. gated.release(3, embeddingsResponse(["delta"], 4));
  882. gated.release(2, embeddingsResponse(["gamma"], 4));
  883. gated.release(1, embeddingsResponse(["beta"], 4));
  884. gated.release(0, embeddingsResponse(["alpha"], 4));
  885. const result = await promise;
  886. expect(result.length).toBe(N);
  887. // Each input slot got its own embedding — input order preserved
  888. expect(result[0]).not.toBeNull();
  889. expect(result[1]).not.toBeNull();
  890. expect(result[2]).not.toBeNull();
  891. expect(result[3]).not.toBeNull();
  892. });
  893. test("dimensions recorded correctly even if the first-resolving chunk is not chunk 0", async () => {
  894. const gated = makeGatedFetchSequence(2);
  895. const p = new OpenAIEmbeddingsProvider({
  896. endpoint: "https://ai.example.com",
  897. fetchImpl: gated.fetchImpl,
  898. batchSize: 1,
  899. concurrency: 2,
  900. });
  901. const promise = p.embedBatch(["a", "b"]);
  902. for (let i = 0; i < 5; i++) await Promise.resolve();
  903. // Resolve chunk 1 first with 7-dim, then chunk 0 with 7-dim
  904. gated.release(1, embeddingsResponse(["b"], 7));
  905. for (let i = 0; i < 5; i++) await Promise.resolve();
  906. gated.release(0, embeddingsResponse(["a"], 7));
  907. await promise;
  908. expect(p.getDimensions()).toBe(7);
  909. });
  910. test("abort signal during concurrent run stops new dispatches; in-flight settle", async () => {
  911. const gated = makeGatedFetchSequence(8);
  912. const p = new OpenAIEmbeddingsProvider({
  913. endpoint: "https://ai.example.com",
  914. fetchImpl: gated.fetchImpl,
  915. batchSize: 1,
  916. concurrency: 4,
  917. });
  918. const ctrl = new AbortController();
  919. const texts = Array.from({ length: 8 }, (_, i) => `t${i}`);
  920. const promise = p.embedBatch(texts, { signal: ctrl.signal });
  921. for (let i = 0; i < 5; i++) await Promise.resolve();
  922. expect(gated.startOrder.length).toBe(4);
  923. // Resolve in-flight, then abort — remaining 4 should NOT dispatch
  924. gated.release(0, embeddingsResponse(["t0"], 4));
  925. gated.release(1, embeddingsResponse(["t1"], 4));
  926. gated.release(2, embeddingsResponse(["t2"], 4));
  927. gated.release(3, embeddingsResponse(["t3"], 4));
  928. ctrl.abort(new Error("operator cancelled"));
  929. for (let i = 0; i < 20; i++) await Promise.resolve();
  930. const result = await promise;
  931. // First 4 succeeded, last 4 are null (never dispatched after abort)
  932. expect(result.slice(0, 4).every((r) => r !== null)).toBe(true);
  933. expect(result.slice(4).every((r) => r === null)).toBe(true);
  934. // Total dispatched MUST be ≤ 5 (the abort can race with one extra
  935. // worker pulling the next idx before the abort flag is set; we cap
  936. // at first 4 + at-most-1 grace).
  937. expect(gated.startOrder.length).toBeLessThanOrEqual(5);
  938. // Audit string captured
  939. expect(p.getLastError()).toMatch(/aborted by caller/);
  940. });
  941. test("ctor rejects concurrency < 1", () => {
  942. expect(() => new OpenAIEmbeddingsProvider({
  943. endpoint: "https://ai.example.com",
  944. fetchImpl: (async () => mockResponse(200, {})) as typeof fetch,
  945. concurrency: 0,
  946. })).toThrow(/concurrency must be ≥ 1/);
  947. expect(() => new OpenAIEmbeddingsProvider({
  948. endpoint: "https://ai.example.com",
  949. fetchImpl: (async () => mockResponse(200, {})) as typeof fetch,
  950. concurrency: -3,
  951. })).toThrow(/concurrency must be ≥ 1/);
  952. });
  953. test("circuit-open observed mid-run is thrown after in-flight settle", async () => {
  954. // 8 chunks, all fail → breaker opens after the first 4 (minSamples=4).
  955. // Workers 0-3 dispatch in parallel, all fail, recordFailure × 4 → breaker
  956. // OPEN. Remaining workers see shouldFailFast() and set circuitTrippedDuringRun.
  957. const N = 8;
  958. const { fetchImpl } = makeFetchSequence(
  959. Array.from({ length: N }, () => () => mockResponse(401, "fail"))
  960. );
  961. const p = new OpenAIEmbeddingsProvider({
  962. endpoint: "https://ai.example.com",
  963. fetchImpl,
  964. batchSize: 1,
  965. concurrency: 4,
  966. retryBackoffsMs: [],
  967. sleep: async () => {},
  968. });
  969. // First 4 land before breaker opens (concurrent dispatch); after they all
  970. // fail the breaker tips OPEN. The next pull observes shouldFailFast().
  971. // Either the result resolves with all-null (legacy semantics — breaker
  972. // tripped AFTER all workers grabbed their chunk) OR throws CircuitOpenError
  973. // (breaker observed before next pull). Both are valid post-condition;
  974. // we just assert the state ends OPEN and the call completes.
  975. let res: Awaited<ReturnType<typeof p.embedBatch>> | undefined;
  976. let err: unknown;
  977. try {
  978. res = await p.embedBatch(Array.from({ length: N }, (_, i) => `t${i}`));
  979. } catch (e) {
  980. err = e;
  981. }
  982. expect(p.breaker.getState()).toBe("open");
  983. if (err) {
  984. expect(err).toBeInstanceOf(CircuitOpenError);
  985. } else {
  986. expect(res!.every((r) => r === null)).toBe(true);
  987. }
  988. });
  989. });
  990. describe("OpenAIEmbeddingsProvider — dispose", () => {
  991. test("dispose resets the breaker", async () => {
  992. const { fetchImpl } = makeFetchSequence([
  993. () => mockResponse(401, "fail"),
  994. () => mockResponse(401, "fail"),
  995. () => mockResponse(401, "fail"),
  996. () => mockResponse(401, "fail"),
  997. ]);
  998. const p = new OpenAIEmbeddingsProvider({
  999. endpoint: "https://ai.example.com",
  1000. fetchImpl,
  1001. batchSize: 1,
  1002. retryBackoffsMs: [],
  1003. sleep: async () => {},
  1004. });
  1005. await p.embedBatch(["a", "b", "c", "d"]);
  1006. expect(p.breaker.getState()).toBe("open");
  1007. await p.dispose();
  1008. expect(p.breaker.getState()).toBe("closed");
  1009. });
  1010. });