File size: 23,340 Bytes
88c4c60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
/**
 * 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");
  });

  it("gemini single input forwards dimensions as outputDimensionality", async () => {
    vi.mocked(fetch).mockResolvedValueOnce(makeProviderResponse({
      embedding: { values: [0.1, 0.2, 0.3] },
    }));

    await handleEmbeddingsCore(makeOptions({
      body: {
        model: "gemini/gemini-embedding-2-preview",
        input: "test",
        dimensions: 1536,
      },
      modelInfo: { provider: "gemini", model: "gemini-embedding-2-preview" },
      credentials: { apiKey: "gemini-key" },
    }));

    const [, init] = vi.mocked(fetch).mock.calls[0];
    const sent = JSON.parse(init.body);
    expect(sent.outputDimensionality).toBe(1536);
  });

  it("gemini batch input forwards dimensions on each request", async () => {
    vi.mocked(fetch).mockResolvedValueOnce(makeProviderResponse({
      embeddings: [
        { values: [0.1, 0.2, 0.3] },
        { values: [0.4, 0.5, 0.6] },
      ],
    }));

    await handleEmbeddingsCore(makeOptions({
      body: {
        model: "gemini/gemini-embedding-2-preview",
        input: ["hello", "world"],
        dimensions: 1536,
      },
      modelInfo: { provider: "gemini", model: "gemini-embedding-2-preview" },
      credentials: { apiKey: "gemini-key" },
    }));

    const [, init] = vi.mocked(fetch).mock.calls[0];
    const sent = JSON.parse(init.body);
    expect(sent.requests).toHaveLength(2);
    expect(sent.requests[0].outputDimensionality).toBe(1536);
    expect(sent.requests[1].outputDimensionality).toBe(1536);
  });
});

// ─── 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("vercel-ai-gateway β†’ https://ai-gateway.vercel.sh/v1/embeddings", async () => {
    vi.mocked(fetch).mockResolvedValueOnce(makeProviderResponse(VALID_EMBEDDING_RESPONSE));

    await handleEmbeddingsCore(makeOptions({
      modelInfo: { provider: "vercel-ai-gateway", model: "openai/text-embedding-3-small" },
      credentials: { apiKey: "vag-test-key" },
    }));

    const [url, init] = vi.mocked(fetch).mock.calls[0];
    const sent = JSON.parse(init.body);
    expect(url).toBe("https://ai-gateway.vercel.sh/v1/embeddings");
    expect(init.headers.Authorization).toBe("Bearer vag-test-key");
    expect(sent.model).toBe("openai/text-embedding-3-small");
  });

  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);
  });
});