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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
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
import debug from "debug";
import {
  afterEach,
  beforeEach,
  describe,
  expect,
  it,
  type MockedFunction,
  vi,
} from "vitest";

function createMockResponse(
  body: string,
  {
    status,
    ok = status === undefined ? true : status >= 200 && status < 300,
    headers = {},
    contentType = "application/json",
    unreadBody = null,
  }: {
    ok?: boolean;
    status?: number;
    headers?: Record<string, string>;
    contentType?: string;
    unreadBody?: { cancel: () => Promise<void> } | null;
  } = {},
): Response {
  const resolvedStatus = status ?? (ok ? 200 : 503);
  return {
    ok,
    status: resolvedStatus,
    statusText: ok ? "OK" : "Error",
    headers: new Headers({ "content-type": contentType, ...headers }),
    redirected: false,
    type: "basic" as ResponseType,
    url: "http://test.com",
    clone: function () {
      return this;
    },
    body: unreadBody,
    bodyUsed: false,
    arrayBuffer: () => Promise.resolve(new ArrayBuffer(0)),
    blob: () => Promise.resolve(new Blob()),
    formData: () => Promise.resolve(new FormData()),
    json: () => Promise.resolve(JSON.parse(body)),
    text: () => Promise.resolve(body),
  } as unknown as Response;
}

const SESSION_ID = "session-from-step-one";
const QUERY = "borogoves outgrabe mimsy-42";
// The module's own cap on the exchange; every test that is not about the
// caller's budget passes it as the budget.
const FULL_BUDGET_MS = 15_000;

const INITIALIZE_REPLY = JSON.stringify({
  jsonrpc: "2.0",
  id: 1,
  result: { protocolVersion: "2025-06-18", capabilities: {}, serverInfo: {} },
});

const NOTIFICATION_ACCEPTED = "";

function toolReply(document: unknown): string {
  return JSON.stringify({
    jsonrpc: "2.0",
    id: 2,
    result: {
      content: [{ type: "text", text: JSON.stringify(document) }],
    },
  });
}

function sseReply(message: string): string {
  return `event: message\ndata: ${message}\n\n`;
}

function documentWith(results: unknown[]): string {
  return toolReply({ search_id: "search-1", results });
}

// You.com's reply wraps its web results one level deeper, under
// `results.web`, and each result carries a description and, when the query
// matched the page, query-relevant highlights.
function youcomDocumentWith(web: unknown[]): string {
  return toolReply({ results: { web } });
}

const PARALLEL_URL = "https://search.parallel.ai/mcp";
const YOUCOM_KEYLESS_URL = "https://api.you.com/mcp?profile=free";
const YOUCOM_AUTHENTICATED_URL = "https://api.you.com/mcp";

const DESCRIPTION =
  "A plain description that You.com returns for a page that matched the query, long enough to read as a summary.";

const EXCERPT =
  "The opening sentence of a very long excerpt that has to survive the filter. alpha bravo charlie delta echo foxtrot golf hotel india juliet kilo lima mike november oscar papa quebec";

const LONG_EXCERPT = `${EXCERPT} romeo sierra tango uniform victor whiskey xray yankee zulu alpha bravo charlie delta echo foxtrot golf hotel india juliet kilo lima mike november oscar papa quebec romeo sierra tango uniform victor whiskey xray yankee zulu`;

let originalFetch: typeof fetch;
let fetchMock: MockedFunction<typeof fetch>;

type FallbackModule = typeof import("./fallbackSearchService");
let fetchFallbackTextResults: FallbackModule["fetchFallbackTextResults"];
let isSearchFallbackEnabled: FallbackModule["isSearchFallbackEnabled"];
let assertSearchFallbackConfiguration: FallbackModule["assertSearchFallbackConfiguration"];
let getFallbackProviderStats: FallbackModule["getFallbackProviderStats"];

const FALLBACK_VARIABLES = [
  "SEARCH_FALLBACK_ENABLED",
  "SEARCH_FALLBACK_PROVIDERS",
  "SEARCH_FALLBACK_PARALLEL_API_KEY",
  "SEARCH_FALLBACK_YOUCOM_API_KEY",
  "SEARCH_FALLBACK_PROVIDER",
  "SEARCH_FALLBACK_API_KEY",
] as const;
const originalVariables = Object.fromEntries(
  FALLBACK_VARIABLES.map((name) => [name, process.env[name]]),
);

beforeEach(async () => {
  for (const name of FALLBACK_VARIABLES) delete process.env[name];
  // Pinned to one provider so the tests below that are about one exchange
  // run it alone, without the shuffle; the cascade tests clear it.
  process.env.SEARCH_FALLBACK_PROVIDERS = "parallel";
  originalFetch = global.fetch;
  fetchMock = vi.fn() as unknown as MockedFunction<typeof fetch>;
  global.fetch = fetchMock;
  // A fresh module per test: pauses and counters are process state, and one
  // test's 429 must not leave a provider boxed for the next.
  vi.resetModules();
  ({
    fetchFallbackTextResults,
    isSearchFallbackEnabled,
    assertSearchFallbackConfiguration,
    getFallbackProviderStats,
  } = await import("./fallbackSearchService"));
});

afterEach(() => {
  global.fetch = originalFetch;
  for (const name of FALLBACK_VARIABLES) {
    const original = originalVariables[name];
    if (original === undefined) delete process.env[name];
    else process.env[name] = original;
  }
  vi.useRealTimers();
  vi.restoreAllMocks();
});

/** Answers the requests in order with the given tool-call reply. */
function mockExchange(
  toolBody: string,
  {
    initializeBody = INITIALIZE_REPLY,
    initializeContentType = "application/json",
    toolContentType = "application/json",
    initializeSession = SESSION_ID,
  }: {
    initializeBody?: string;
    initializeContentType?: string;
    toolContentType?: string;
    initializeSession?: string | null;
  } = {},
) {
  fetchMock.mockResolvedValueOnce(
    createMockResponse(initializeBody, {
      contentType: initializeContentType,
      headers: initializeSession ? { "mcp-session-id": initializeSession } : {},
    }),
  );

  // A stateless initialize answers no session, so the initialized
  // notification never goes out and the tool call follows at once.
  if (initializeSession) {
    fetchMock.mockResolvedValueOnce(
      createMockResponse(NOTIFICATION_ACCEPTED, { status: 202 }),
    );
  }

  fetchMock.mockResolvedValueOnce(
    createMockResponse(toolBody, { contentType: toolContentType }),
  );
}

function requestBody(index: number): {
  jsonrpc?: string;
  id?: number;
  method?: string;
  params?: {
    name?: string;
    arguments?: Record<string, unknown>;
    protocolVersion?: string;
    capabilities?: unknown;
    clientInfo?: unknown;
  };
} {
  const [, init] = fetchMock.mock.calls[index];
  return JSON.parse(String(init?.body));
}

function requestHeaders(index: number): Record<string, string> {
  const [, init] = fetchMock.mock.calls[index];
  return init?.headers as Record<string, string>;
}

describe("isSearchFallbackEnabled", () => {
  it("is off when the variable is unset", () => {
    expect(isSearchFallbackEnabled()).toBe(false);
  });

  it("accepts 'true' and ' TRUE ' and '1'", () => {
    process.env.SEARCH_FALLBACK_ENABLED = "true";
    expect(isSearchFallbackEnabled()).toBe(true);

    process.env.SEARCH_FALLBACK_ENABLED = " TRUE ";
    expect(isSearchFallbackEnabled()).toBe(true);

    process.env.SEARCH_FALLBACK_ENABLED = "1";
    expect(isSearchFallbackEnabled()).toBe(true);
  });

  it("rejects a value that is not true or 1", () => {
    process.env.SEARCH_FALLBACK_ENABLED = "yes";
    expect(isSearchFallbackEnabled()).toBe(false);
  });

  it("reads the variable at call time", () => {
    expect(isSearchFallbackEnabled()).toBe(false);
    process.env.SEARCH_FALLBACK_ENABLED = "true";
    expect(isSearchFallbackEnabled()).toBe(true);
  });
});

describe("the three requests", () => {
  it("runs initialize, the initialized notification and the tool call in order", async () => {
    mockExchange(documentWith([]));

    await fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS);

    expect(fetchMock).toHaveBeenCalledTimes(3);
    expect(requestBody(0)).toEqual({
      jsonrpc: "2.0",
      id: 1,
      method: "initialize",
      params: {
        protocolVersion: "2025-06-18",
        capabilities: {},
        clientInfo: { name: "MiniSearch", version: "1" },
      },
    });
    expect(requestBody(1)).toEqual({
      jsonrpc: "2.0",
      method: "notifications/initialized",
    });
    expect(requestBody(2).method).toBe("tools/call");
    expect(requestBody(2).params?.name).toBe("web_search");
    expect(requestBody(2).params?.arguments).toEqual({
      objective: QUERY,
      search_queries: [QUERY],
      session_id: expect.any(String),
    });
  });

  it("carries the session id from step 1 onto steps 2 and 3, with the protocol version", async () => {
    mockExchange(documentWith([]));

    await fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS);

    expect(requestHeaders(0)["Mcp-Session-Id"]).toBeUndefined();
    expect(requestHeaders(0)["MCP-Protocol-Version"]).toBeUndefined();

    for (const index of [1, 2]) {
      expect(requestHeaders(index)["Mcp-Session-Id"]).toBe(SESSION_ID);
      expect(requestHeaders(index)["MCP-Protocol-Version"]).toBe("2025-06-18");
    }
  });

  it("sends one session id for the process, not one per search", async () => {
    mockExchange(documentWith([]));
    mockExchange(documentWith([]));

    await fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS);
    await fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS);

    const first = requestBody(2).params?.arguments?.session_id;
    const second = requestBody(5).params?.arguments?.session_id;
    expect(typeof first).toBe("string");
    expect(second).toBe(first);
  });

  it("sends the content type and accept header on every request", async () => {
    mockExchange(documentWith([]));

    await fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS);

    for (let index = 0; index < 3; index++) {
      const headers = requestHeaders(index);
      expect(headers["Content-Type"]).toBe("application/json");
      expect(headers.Accept).toBe("application/json, text/event-stream");
    }
  });

  it("never sends a model name", async () => {
    mockExchange(documentWith([]));

    await fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS);

    const toolCall = JSON.stringify(fetchMock.mock.calls[2][1]?.body);
    expect(toolCall).not.toContain("model_name");
    expect(Object.keys(requestBody(2).params?.arguments ?? {})).toEqual([
      "objective",
      "search_queries",
      "session_id",
    ]);
  });

  it("bounds all three requests with one abort signal", async () => {
    mockExchange(documentWith([]));

    await fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS);

    const signals = fetchMock.mock.calls.map(([, init]) => init?.signal);
    expect(signals[0]).toBeInstanceOf(AbortSignal);
    expect(signals[1]).toBe(signals[0]);
    expect(signals[2]).toBe(signals[0]);
  });

  it("hands the exchange the caller's budget, capped at its own", async () => {
    mockExchange(documentWith([]));
    mockExchange(documentWith([]));
    // A frozen clock, so the remaining budget is exactly what was passed in.
    vi.useFakeTimers({ toFake: ["Date"] });
    const timeoutSpy = vi.spyOn(AbortSignal, "timeout");

    await fetchFallbackTextResults(QUERY, 10, 3_000);
    await fetchFallbackTextResults(QUERY, 10, 20_000);

    expect(timeoutSpy).toHaveBeenNthCalledWith(1, 3_000);
    // A budget past the module's own 15 s is not passed on: the exchange still
    // ends at the cap, so the caller cannot lend it more time than it allows.
    // Restored by the afterEach below, along with every other mock.
    expect(timeoutSpy).toHaveBeenNthCalledWith(2, 15_000);
  });

  it("discards the unread bodies of the first two responses", async () => {
    const cancelInitialize = vi.fn().mockResolvedValue(undefined);
    const cancelInitialized = vi.fn().mockResolvedValue(undefined);
    fetchMock
      .mockResolvedValueOnce(
        createMockResponse(INITIALIZE_REPLY, {
          headers: { "mcp-session-id": SESSION_ID },
          unreadBody: { cancel: cancelInitialize },
        }),
      )
      .mockResolvedValueOnce(
        createMockResponse(NOTIFICATION_ACCEPTED, {
          status: 202,
          unreadBody: { cancel: cancelInitialized },
        }),
      )
      .mockResolvedValueOnce(createMockResponse(documentWith([])));

    await fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS);

    // An unread body keeps its socket out of the connection pool until it is
    // collected, so the two replies nobody reads have to be dropped.
    expect(cancelInitialize).toHaveBeenCalledTimes(1);
    expect(cancelInitialized).toHaveBeenCalledTimes(1);
  });

  it("discards an unread body before throwing on a rejected initialize", async () => {
    const cancelInitialize = vi.fn().mockResolvedValue(undefined);
    fetchMock.mockResolvedValueOnce(
      createMockResponse("", {
        status: 500,
        unreadBody: { cancel: cancelInitialize },
      }),
    );

    await expect(
      fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS),
    ).rejects.toThrow("did not answer the initialize request (status 500)");
    expect(cancelInitialize).toHaveBeenCalledTimes(1);
  });

  it("omits the Bearer header when no key is configured", async () => {
    mockExchange(documentWith([]));

    await fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS);

    for (let index = 0; index < 3; index++) {
      expect(requestHeaders(index).Authorization).toBeUndefined();
    }
  });

  it("sends the Bearer header on every request when a key is configured", async () => {
    process.env.SEARCH_FALLBACK_PARALLEL_API_KEY = "sk-fallback-key";
    mockExchange(documentWith([]));

    await fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS);

    for (let index = 0; index < 3; index++) {
      expect(requestHeaders(index).Authorization).toBe(
        "Bearer sk-fallback-key",
      );
    }
  });

  it("treats a whitespace-only key as no key", async () => {
    process.env.SEARCH_FALLBACK_PARALLEL_API_KEY = "   ";
    mockExchange(documentWith([]));

    await fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS);

    expect(requestHeaders(0).Authorization).toBeUndefined();
  });
});

describe("choosing the provider", () => {
  it("runs the Parallel exchange by default", async () => {
    mockExchange(documentWith([]));

    await fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS);

    expect(fetchMock).toHaveBeenCalledTimes(3);
    for (const [requestUrl] of fetchMock.mock.calls) {
      expect(String(requestUrl)).toBe(PARALLEL_URL);
    }
    expect(requestBody(2).params?.name).toBe("web_search");
  });

  it("runs the You.com exchange when SEARCH_FALLBACK_PROVIDERS lists youcom", async () => {
    process.env.SEARCH_FALLBACK_PROVIDERS = "youcom";
    mockExchange(youcomDocumentWith([]), { initializeSession: null });

    await fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS);

    expect(fetchMock).toHaveBeenCalledTimes(2);
    for (const [requestUrl] of fetchMock.mock.calls) {
      expect(String(requestUrl)).toBe(YOUCOM_KEYLESS_URL);
    }
    expect(requestBody(1).params?.name).toBe("you-search");
    expect(requestBody(1).params?.arguments).toEqual({
      query: QUERY,
      count: 10,
    });
  });

  it("sends the protocol version but no session id when You.com opens no session", async () => {
    process.env.SEARCH_FALLBACK_PROVIDERS = "youcom";
    mockExchange(youcomDocumentWith([]), { initializeSession: null });

    await fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS);

    expect(fetchMock).toHaveBeenCalledTimes(2);
    expect(requestBody(1).method).toBe("tools/call");
    for (const index of [0, 1]) {
      expect(requestHeaders(index)["Mcp-Session-Id"]).toBeUndefined();
    }
    expect(requestHeaders(0)["MCP-Protocol-Version"]).toBeUndefined();
    expect(requestHeaders(1)["MCP-Protocol-Version"]).toBe("2025-06-18");
  });

  it("acknowledges a session and carries it when You.com opens one", async () => {
    process.env.SEARCH_FALLBACK_PROVIDERS = "youcom";
    mockExchange(youcomDocumentWith([]));

    await fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS);

    expect(fetchMock).toHaveBeenCalledTimes(3);
    expect(requestBody(1).method).toBe("notifications/initialized");
    for (const index of [1, 2]) {
      expect(requestHeaders(index)["Mcp-Session-Id"]).toBe(SESSION_ID);
    }
  });

  it("uses You.com's authenticated endpoint and the Bearer key when a key is set", async () => {
    process.env.SEARCH_FALLBACK_PROVIDERS = "youcom";
    process.env.SEARCH_FALLBACK_YOUCOM_API_KEY = "***";
    mockExchange(youcomDocumentWith([]), { initializeSession: null });

    await fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS);

    for (const [requestUrl] of fetchMock.mock.calls) {
      expect(String(requestUrl)).toBe(YOUCOM_AUTHENTICATED_URL);
    }
    for (const index of [0, 1]) {
      expect(requestHeaders(index).Authorization).toBe("Bearer ***");
    }
  });
});

describe("mapping You.com results", () => {
  const runYoucom = async (web: unknown[], limit = 10) => {
    process.env.SEARCH_FALLBACK_PROVIDERS = "youcom";
    mockExchange(youcomDocumentWith(web), { initializeSession: null });
    return fetchFallbackTextResults(QUERY, limit, FULL_BUDGET_MS);
  };

  it("uses the description as the snippet, collapsed to one line", async () => {
    const results = await runYoucom([
      {
        url: "https://example.com/described",
        title: "Described",
        description: "A description with\n\nnewlines and   extra spaces in it.",
      },
    ]);

    expect(results).toEqual([
      [
        "Described",
        "A description with newlines and extra spaces in it.",
        "https://example.com/described",
      ],
    ]);
  });

  it("truncates a long description at a word boundary", async () => {
    const description = `${EXCERPT} ${LONG_EXCERPT}`;
    expect(description.length).toBeGreaterThan(400);

    const results = await runYoucom([
      {
        url: "https://example.com/long",
        title: "Long",
        description,
      },
    ]);

    const snippet = results[0][1];
    expect(snippet.endsWith("โ€ฆ")).toBe(true);
    expect(snippet.length).toBeLessThanOrEqual(400);
    const kept = snippet.slice(0, -1);
    // The kept text is a prefix of the collapsed source, and the source
    // continues with a space, so the cut landed between two words rather
    // than inside one.
    expect(description.startsWith(kept)).toBe(true);
    expect(description[kept.length]).toBe(" ");
  });

  it("keeps a short description that the excerpt length filter would drop", async () => {
    const results = await runYoucom([
      {
        url: "https://example.com/terse",
        title: "Terse",
        description: "Terse.",
      },
    ]);

    expect(results).toEqual([["Terse", "Terse.", "https://example.com/terse"]]);
  });

  it("falls back to the highlights when the description is empty or null", async () => {
    const results = await runYoucom([
      {
        url: "https://example.com/empty",
        title: "Empty",
        description: "",
        contents: { highlights: ["Skip to main content", EXCERPT] },
      },
      {
        url: "https://example.com/null",
        title: "Null",
        description: null,
        contents: { highlights: [EXCERPT] },
      },
    ]);

    expect(results).toEqual([
      ["Empty", EXCERPT, "https://example.com/empty"],
      ["Null", EXCERPT, "https://example.com/null"],
    ]);
  });

  it("drops a result with no url, a null or empty title, or no snippet", async () => {
    const results = await runYoucom([
      { title: "No url", description: DESCRIPTION },
      {
        url: "https://example.com/null-title",
        title: null,
        description: DESCRIPTION,
      },
      {
        url: "https://example.com/empty-title",
        title: "   ",
        description: DESCRIPTION,
      },
      {
        url: "https://example.com/no-snippet",
        title: "No snippet",
        description: "",
      },
      {
        url: "https://example.com/crumb-only",
        title: "Crumb only",
        description: "",
        contents: { highlights: ["Skip to main content"] },
      },
      {
        url: "https://example.com/kept",
        title: "Kept",
        description: DESCRIPTION,
      },
    ]);

    expect(results).toEqual([
      ["Kept", DESCRIPTION, "https://example.com/kept"],
    ]);
  });

  it("drops a result whose url or title is not a string", async () => {
    const results = await runYoucom([
      { url: 42, title: "Number url", description: DESCRIPTION },
      { url: null, title: "Null url", description: DESCRIPTION },
      {
        url: "https://example.com/number-title",
        title: 7,
        description: DESCRIPTION,
      },
      null,
      {
        url: "https://example.com/kept",
        title: "Kept",
        description: DESCRIPTION,
      },
    ]);

    // One malformed entry must not take the rest of the search down with it.
    expect(results).toEqual([
      ["Kept", DESCRIPTION, "https://example.com/kept"],
    ]);
  });

  it("drops duplicate urls in the endpoint's order", async () => {
    const results = await runYoucom([
      {
        url: "https://example.com/dup",
        title: "First",
        description: DESCRIPTION,
      },
      {
        url: "https://example.com/other",
        title: "Second",
        description: DESCRIPTION,
      },
      {
        url: "https://example.com/dup",
        title: "Third",
        description: DESCRIPTION,
      },
    ]);

    expect(results.map(([title]) => title)).toEqual(["First", "Second"]);
  });

  it("keeps the endpoint's relevance order and applies the limit", async () => {
    const results = await runYoucom(
      [
        {
          url: "https://example.com/1",
          title: "One",
          description: DESCRIPTION,
        },
        {
          url: "https://example.com/2",
          title: "Two",
          description: DESCRIPTION,
        },
        {
          url: "https://example.com/3",
          title: "Three",
          description: DESCRIPTION,
        },
      ],
      2,
    );

    expect(results.map(([title]) => title)).toEqual(["One", "Two"]);
  });

  it("throws when the document has no web results array", async () => {
    process.env.SEARCH_FALLBACK_PROVIDERS = "youcom";
    mockExchange(toolReply({ results: {} }), { initializeSession: null });

    await expect(
      fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS),
    ).rejects.toThrow("without a web results array");
  });

  it("reads the id-matched reply out of an SSE stream that carries a notification first", async () => {
    process.env.SEARCH_FALLBACK_PROVIDERS = "youcom";
    mockExchange(
      `${sseReply(
        JSON.stringify({
          jsonrpc: "2.0",
          method: "notifications/message",
          params: {
            level: "info",
            data: `Search successful for query: ${QUERY} - 20 web results`,
          },
        }),
      )}${sseReply(
        JSON.stringify({
          jsonrpc: "2.0",
          id: 2,
          result: {
            content: [
              {
                type: "text",
                text: JSON.stringify({
                  results: {
                    web: [
                      {
                        url: "https://example.com/sse",
                        title: "SSE",
                        description: DESCRIPTION,
                      },
                    ],
                  },
                }),
              },
            ],
          },
        }),
      )}`,
      { initializeSession: null, toolContentType: "text/event-stream" },
    );

    const results = await fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS);

    expect(results).toEqual([["SSE", DESCRIPTION, "https://example.com/sse"]]);
  });
});

describe("reading the reply", () => {
  it("parses an application/json reply", async () => {
    mockExchange(
      toolReply({
        search_id: "search-1",
        results: [
          {
            url: "https://example.com/json",
            title: "JSON title",
            excerpts: [
              `${"json ".repeat(12).trim()} an excerpt long enough to be kept by the length filter.`,
            ],
          },
        ],
      }),
    );

    const results = await fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS);

    expect(results).toHaveLength(1);
    expect(results[0][0]).toBe("JSON title");
    expect(results[0][2]).toBe("https://example.com/json");
  });

  it("parses a text/event-stream reply and skips blocks that are not JSON", async () => {
    mockExchange(
      `: keep-alive\n\n${sseReply(
        JSON.stringify({
          jsonrpc: "2.0",
          id: 2,
          result: {
            content: [{ type: "text", text: JSON.stringify({ results: [] }) }],
          },
        }),
      )}event: ping\ndata: not json\n\n`,
      { toolContentType: "text/event-stream" },
    );

    await expect(
      fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS),
    ).resolves.toEqual([]);
  });

  it("parses an SSE initialize reply", async () => {
    mockExchange(documentWith([]), {
      initializeBody: sseReply(INITIALIZE_REPLY),
      initializeContentType: "text/event-stream",
    });

    await expect(
      fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS),
    ).resolves.toEqual([]);
  });

  it("returns an empty array when nothing usable comes back", async () => {
    mockExchange(documentWith([]));

    await expect(
      fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS),
    ).resolves.toEqual([]);
  });
});

describe("mapping a result", () => {
  const run = async (results: unknown[], limit = 10) => {
    mockExchange(documentWith(results));
    return fetchFallbackTextResults(QUERY, limit, FULL_BUDGET_MS);
  };

  it("drops navigation crumbs and keeps the paragraph", async () => {
    const results = await run([
      {
        url: "https://example.com/guide",
        title: "Guide",
        excerpts: [
          "Skip to main content",
          "Home > Docs > Search > Guide",
          EXCERPT,
        ],
      },
    ]);

    expect(results).toEqual([["Guide", EXCERPT, "https://example.com/guide"]]);
  });

  it("strips leading markdown markers", async () => {
    const results = await run([
      {
        url: "https://example.com/markers",
        title: "Markers",
        excerpts: [`# ${EXCERPT}`],
      },
    ]);

    expect(results[0][1]).toBe(EXCERPT);
  });

  it("reduces a markdown link to its text", async () => {
    const results = await run([
      {
        url: "https://example.com/link",
        title: "Link",
        excerpts: [
          `See the [reference page](https://example.com/reference) and then ${EXCERPT}`,
        ],
      },
    ]);

    expect(results[0][1]).toBe(`See the reference page and then ${EXCERPT}`);
  });

  it("joins excerpts and collapses the whitespace between them", async () => {
    const first = `The first excerpt paragraph is long enough to be kept intact.`;
    const second = `The second excerpt paragraph is long enough to be kept intact.`;
    const results = await run([
      {
        url: "https://example.com/join",
        title: "Join",
        excerpts: [`${first}\n\n${second}`, "Short"],
      },
    ]);

    expect(results[0][1]).toBe(`${first} ${second}`);
  });

  it("cuts at 400 characters, at a word boundary, and marks the cut", async () => {
    expect(LONG_EXCERPT.length).toBeGreaterThan(400);

    const results = await run([
      {
        url: "https://example.com/long",
        title: "Long",
        excerpts: [LONG_EXCERPT],
      },
    ]);

    const snippet = results[0][1];
    expect(snippet.endsWith("โ€ฆ")).toBe(true);
    expect(snippet.length).toBeLessThanOrEqual(400);
    const kept = snippet.slice(0, -1);
    // The kept text is a prefix of the source, and the source continues with a
    // space, so the cut landed between two words rather than inside one.
    expect(LONG_EXCERPT.startsWith(kept)).toBe(true);
    expect(LONG_EXCERPT[kept.length]).toBe(" ");
  });

  it("leaves a snippet that fits untouched", async () => {
    expect(EXCERPT.length).toBeLessThanOrEqual(400);

    const results = await run([
      { url: "https://example.com/short", title: "Short", excerpts: [EXCERPT] },
    ]);

    expect(results[0][1]).toBe(EXCERPT);
    expect(results[0][1]).not.toContain("โ€ฆ");
  });

  it("drops a result with no url, a null or empty title, or an empty snippet", async () => {
    const results = await run([
      { title: "No url", excerpts: [EXCERPT] },
      {
        url: "https://example.com/null-title",
        title: null,
        excerpts: [EXCERPT],
      },
      {
        url: "https://example.com/empty-title",
        title: "   ",
        excerpts: [EXCERPT],
      },
      {
        url: "https://example.com/no-snippet",
        title: "No snippet",
        excerpts: [],
      },
      {
        url: "https://example.com/crumb-only",
        title: "Crumb only",
        excerpts: ["Skip to main content"],
      },
      { url: "https://example.com/kept", title: "Kept", excerpts: [EXCERPT] },
    ]);

    expect(results).toEqual([["Kept", EXCERPT, "https://example.com/kept"]]);
  });

  it("drops a result whose url or title is not a string", async () => {
    const results = await run([
      { url: 42, title: "Number url", excerpts: [EXCERPT] },
      { url: null, title: "Null url", excerpts: [EXCERPT] },
      {
        url: "https://example.com/number-title",
        title: 7,
        excerpts: [EXCERPT],
      },
      null,
      { url: "https://example.com/kept", title: "Kept", excerpts: [EXCERPT] },
    ]);

    // One malformed entry must not take the rest of the search down with it.
    expect(results).toEqual([["Kept", EXCERPT, "https://example.com/kept"]]);
  });

  it("drops duplicate urls in the endpoint's order", async () => {
    const results = await run([
      { url: "https://example.com/dup", title: "First", excerpts: [EXCERPT] },
      {
        url: "https://example.com/other",
        title: "Second",
        excerpts: [EXCERPT],
      },
      { url: "https://example.com/dup", title: "Third", excerpts: [EXCERPT] },
    ]);

    expect(results.map(([title]) => title)).toEqual(["First", "Second"]);
  });

  it("keeps the endpoint's relevance order", async () => {
    const results = await run([
      { url: "https://example.com/1", title: "One", excerpts: [EXCERPT] },
      { url: "https://example.com/2", title: "Two", excerpts: [EXCERPT] },
      { url: "https://example.com/3", title: "Three", excerpts: [EXCERPT] },
    ]);

    expect(results.map(([title]) => title)).toEqual(["One", "Two", "Three"]);
  });

  it("applies the limit", async () => {
    const results = await run(
      [
        { url: "https://example.com/1", title: "One", excerpts: [EXCERPT] },
        { url: "https://example.com/2", title: "Two", excerpts: [EXCERPT] },
        { url: "https://example.com/3", title: "Three", excerpts: [EXCERPT] },
      ],
      2,
    );

    expect(results.map(([title]) => title)).toEqual(["One", "Two"]);
  });
});

describe("failures", () => {
  it("throws when the You.com initialize request fails", async () => {
    process.env.SEARCH_FALLBACK_PROVIDERS = "youcom";
    const cancel = vi.fn(async () => undefined);
    fetchMock.mockResolvedValueOnce(
      createMockResponse("", { status: 500, unreadBody: { cancel } }),
    );

    await expect(
      fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS),
    ).rejects.toThrow("did not answer the initialize request (status 500)");
    // An unread body keeps undici's socket out of the pool until it is
    // garbage-collected, so the non-2xx path has to discard it; dropping
    // that line fails here.
    expect(cancel).toHaveBeenCalledTimes(1);
    expect(fetchMock).toHaveBeenCalledTimes(1);
  });

  it("throws when the tool call is not 2xx", async () => {
    const cancel = vi.fn(async () => undefined);
    fetchMock.mockResolvedValueOnce(
      createMockResponse(INITIALIZE_REPLY, {
        headers: { "mcp-session-id": SESSION_ID },
      }),
    );
    fetchMock.mockResolvedValueOnce(
      createMockResponse(NOTIFICATION_ACCEPTED, { status: 202 }),
    );
    fetchMock.mockResolvedValueOnce(
      createMockResponse("bad", { status: 502, unreadBody: { cancel } }),
    );

    await expect(
      fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS),
    ).rejects.toThrow("The fallback search tool call failed with status 502");
    // An unread body keeps undici's socket out of the pool until it is
    // garbage-collected, so the non-2xx path has to discard it like the two
    // paths above it; dropping that line fails here.
    expect(cancel).toHaveBeenCalledTimes(1);
  });

  it("skips the notification when Parallel opens no session", async () => {
    mockExchange(documentWith([]), { initializeSession: null });

    await fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS);

    // The session is optional for every provider: with none opened, the tool
    // call follows initialize directly and carries no session headers.
    expect(fetchMock).toHaveBeenCalledTimes(2);
    expect(requestBody(1).method).toBe("tools/call");
    expect(requestHeaders(1)["Mcp-Session-Id"]).toBeUndefined();
  });

  it("throws on a JSON-RPC error and keeps the endpoint's message out of it", async () => {
    fetchMock.mockResolvedValueOnce(
      createMockResponse(INITIALIZE_REPLY, {
        headers: { "mcp-session-id": SESSION_ID },
      }),
    );
    fetchMock.mockResolvedValueOnce(
      createMockResponse(NOTIFICATION_ACCEPTED, { status: 202 }),
    );
    fetchMock.mockResolvedValueOnce(
      createMockResponse(
        JSON.stringify({
          jsonrpc: "2.0",
          id: 2,
          error: { code: -32601, message: "SECRET UPSTREAM TEXT" },
        }),
      ),
    );

    const rejection = await fetchFallbackTextResults(
      QUERY,
      10,
      FULL_BUDGET_MS,
    ).catch((error: unknown) => error);
    // The whole message is fixed text plus the code: an upstream message is
    // free-form text from a third party and must not travel any further.
    expect(rejection).toBeInstanceOf(Error);
    expect((rejection as Error).message).toBe(
      "Every fallback search provider failed (parallel: The fallback search endpoint returned a JSON-RPC error (code -32601))",
    );
    expect(String(rejection)).not.toContain("SECRET UPSTREAM TEXT");
  });

  it("keeps a non-numeric JSON-RPC code out of the message", async () => {
    fetchMock.mockResolvedValueOnce(
      createMockResponse(INITIALIZE_REPLY, {
        headers: { "mcp-session-id": SESSION_ID },
      }),
    );
    fetchMock.mockResolvedValueOnce(
      createMockResponse(NOTIFICATION_ACCEPTED, { status: 202 }),
    );
    fetchMock.mockResolvedValueOnce(
      createMockResponse(
        JSON.stringify({
          jsonrpc: "2.0",
          id: 2,
          error: { code: "SECRET", message: "SECRET UPSTREAM TEXT" },
        }),
      ),
    );

    const rejection = await fetchFallbackTextResults(
      QUERY,
      10,
      FULL_BUDGET_MS,
    ).catch((error: unknown) => error);
    // A code is upstream data like the message is, so only a number may be
    // carried into the thrown error.
    expect(rejection).toBeInstanceOf(Error);
    expect((rejection as Error).message).toBe(
      "Every fallback search provider failed (parallel: The fallback search endpoint returned a JSON-RPC error)",
    );
    expect(String(rejection)).not.toContain("SECRET");
  });

  it("throws when the result is marked as an error", async () => {
    mockExchange(
      JSON.stringify({
        jsonrpc: "2.0",
        id: 2,
        result: {
          isError: true,
          content: [{ type: "text", text: "upstream refused" }],
        },
      }),
    );

    await expect(
      fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS),
    ).rejects.toThrow("reported the search as failed");
  });

  it("throws when the tool output is not JSON", async () => {
    mockExchange(
      JSON.stringify({
        jsonrpc: "2.0",
        id: 2,
        result: { content: [{ type: "text", text: "<html>gateway</html>" }] },
      }),
    );

    await expect(
      fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS),
    ).rejects.toThrow("tool output that is not JSON");
  });

  it("throws a fixed message when the reply itself is not JSON", async () => {
    mockExchange("<html>gateway</html>");

    const rejection = await fetchFallbackTextResults(
      QUERY,
      10,
      FULL_BUDGET_MS,
    ).catch((error: unknown) => error);
    // A raw SyntaxError quotes the offending body, which would put the
    // upstream response into the thrown error and then into the log.
    expect((rejection as Error).message).toBe(
      "Every fallback search provider failed (parallel: The fallback search endpoint sent a reply that is not JSON)",
    );
    expect(String(rejection)).not.toContain("<html>");
  });

  it("throws when the document has no results array", async () => {
    mockExchange(toolReply({ search_id: "search-1" }));

    await expect(
      fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS),
    ).rejects.toThrow("without a results array");
  });

  it("throws when the endpoint is unreachable", async () => {
    fetchMock.mockRejectedValue(new Error("Network failure"));

    await expect(
      fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS),
    ).rejects.toThrow("Network failure");
  });
});

describe("privacy", () => {
  let logLines: string[];
  let originalLog: typeof debug.log;

  beforeEach(() => {
    logLines = [];
    originalLog = debug.log;
    debug.log = (...args: unknown[]) => {
      logLines.push(args.map(String).join(" "));
    };
    for (const method of ["log", "info", "warn", "error", "debug"] as const) {
      vi.spyOn(console, method).mockImplementation((...args: unknown[]) => {
        logLines.push(args.map(String).join(" "));
      });
    }
  });

  afterEach(() => {
    debug.log = originalLog;
  });

  it("never writes the query, the API key or the response text to the log", async () => {
    process.env.SEARCH_FALLBACK_PARALLEL_API_KEY = "sk-fallback-key";
    mockExchange(
      documentWith([
        {
          url: "https://example.com/private",
          title: "PRIVATE RESPONSE TITLE",
          excerpts: [
            `PRIVATE RESPONSE EXCERPT that is long enough to be kept by the length filter and returned to the caller.`,
          ],
        },
      ]),
    );

    await fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS);

    // Failure paths too: they carry a fixed line plus a status or an error
    // code, never the payload.
    fetchMock.mockResolvedValueOnce(createMockResponse("", { status: 500 }));
    await expect(
      fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS),
    ).rejects.toThrow();

    const output = logLines.join("\n");
    for (const form of [
      QUERY,
      encodeURIComponent(QUERY),
      QUERY.replaceAll(" ", "+"),
      "PRIVATE RESPONSE TITLE",
      "PRIVATE RESPONSE EXCERPT",
      "sk-fallback-key",
    ]) {
      expect(output).not.toContain(form);
    }
    expect(logLines.length).toBeGreaterThan(0);
  });

  it("never writes the You.com query, the API key or the response text to the log", async () => {
    process.env.SEARCH_FALLBACK_PROVIDERS = "youcom";
    process.env.SEARCH_FALLBACK_YOUCOM_API_KEY = "youcom-fallback-key";
    mockExchange(
      youcomDocumentWith([
        {
          url: "https://example.com/private",
          title: "PRIVATE RESPONSE TITLE",
          description: "PRIVATE RESPONSE DESCRIPTION",
        },
      ]),
      { initializeSession: null },
    );

    await fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS);

    // The stream the endpoint answers with carries the query inside a
    // notification before the reply, so this also covers that path: none of
    // it may reach the log, only the counts do.
    const output = logLines.join("\n");
    for (const form of [
      QUERY,
      "PRIVATE RESPONSE TITLE",
      "PRIVATE RESPONSE DESCRIPTION",
      "youcom-fallback-key",
    ]) {
      expect(output).not.toContain(form);
    }
    expect(logLines.length).toBeGreaterThan(0);
  });
});

// Indexed by name rather than dotted, so each variable is spelled once.
const PROVIDERS_VARIABLE = "SEARCH_FALLBACK_PROVIDERS";
const PARALLEL_KEY_VARIABLE = "SEARCH_FALLBACK_PARALLEL_API_KEY";
const YOUCOM_KEY_VARIABLE = "SEARCH_FALLBACK_YOUCOM_API_KEY";
const LEGACY_PROVIDER_VARIABLE = "SEARCH_FALLBACK_PROVIDER";
const LEGACY_KEY_VARIABLE = "SEARCH_FALLBACK_API_KEY";

function listProviders(value: string | undefined) {
  if (value === undefined) delete process.env[PROVIDERS_VARIABLE];
  else process.env[PROVIDERS_VARIABLE] = value;
}

// Fisher-Yates over the two built-in providers draws once: 0 swaps them,
// anything near 1 keeps the table's order.
function orderYoucomFirst() {
  return vi.spyOn(Math, "random").mockReturnValue(0);
}

function orderParallelFirst() {
  return vi.spyOn(Math, "random").mockReturnValue(0.99);
}

function mockYoucom(web: unknown[]) {
  mockExchange(youcomDocumentWith(web), { initializeSession: null });
}

function requestUrls(): string[] {
  return fetchMock.mock.calls.map(([requestUrl]) => String(requestUrl));
}

function rateLimited(headers: Record<string, string> = {}) {
  return createMockResponse("", { status: 429, headers });
}

const SERVED_RESULT = {
  url: "https://example.com/served",
  title: "Served",
  description: DESCRIPTION,
};

describe("configuration", () => {
  it("tries every built-in provider when the list is unset", async () => {
    listProviders(undefined);
    orderParallelFirst();
    mockExchange(documentWith([]));
    mockYoucom([]);

    await fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS);

    expect(new Set(requestUrls())).toEqual(
      new Set([PARALLEL_URL, YOUCOM_KEYLESS_URL]),
    );
  });

  it("tries only the listed providers, trimmed, lowercased and without empty items", async () => {
    listProviders(" YouCom ,, ");
    mockYoucom([]);

    await fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS);

    expect(requestUrls()).toEqual([YOUCOM_KEYLESS_URL, YOUCOM_KEYLESS_URL]);
    expect(() => assertSearchFallbackConfiguration()).not.toThrow();
  });

  it("throws on an unknown provider name, naming it and the valid ones", async () => {
    listProviders("parallel, Bing");

    expect(() => assertSearchFallbackConfiguration()).toThrow(
      "SEARCH_FALLBACK_PROVIDERS has unknown provider names: bing; valid names: parallel, youcom.",
    );
    await expect(
      fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS),
    ).rejects.toThrow("unknown provider names: bing");
    expect(fetchMock).not.toHaveBeenCalled();
  });

  it("throws on a list with no names in it", () => {
    listProviders(",");

    expect(() => assertSearchFallbackConfiguration()).toThrow(
      "SEARCH_FALLBACK_PROVIDERS lists no provider",
    );
  });

  it("throws when the removed SEARCH_FALLBACK_PROVIDER is set, naming its replacement", () => {
    process.env[LEGACY_PROVIDER_VARIABLE] = "youcom";

    expect(() => assertSearchFallbackConfiguration()).toThrow(
      "SEARCH_FALLBACK_PROVIDER is no longer read; set SEARCH_FALLBACK_PROVIDERS instead.",
    );
  });

  it("throws when the removed SEARCH_FALLBACK_API_KEY is set, naming both replacements and not the key", async () => {
    process.env[LEGACY_KEY_VARIABLE] = "legacy-test-value";

    const error = (() => {
      try {
        assertSearchFallbackConfiguration();
      } catch (thrown) {
        return thrown as Error;
      }
    })();

    expect(error?.message).toBe(
      "SEARCH_FALLBACK_API_KEY is no longer read; set SEARCH_FALLBACK_PARALLEL_API_KEY or SEARCH_FALLBACK_YOUCOM_API_KEY instead.",
    );
    await expect(
      fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS),
    ).rejects.toThrow("SEARCH_FALLBACK_API_KEY is no longer read");
    expect(fetchMock).not.toHaveBeenCalled();
  });

  it("throws on a key with a control character inside, naming the variable and not the key", async () => {
    process.env[YOUCOM_KEY_VARIABLE] = "youcom-test\nvalue";

    expect(() => assertSearchFallbackConfiguration()).toThrow(
      "SEARCH_FALLBACK_YOUCOM_API_KEY contains a control character; set it to the key alone.",
    );
    const error = (await fetchFallbackTextResults(
      QUERY,
      10,
      FULL_BUDGET_MS,
    ).catch((thrown: unknown) => thrown)) as Error;
    expect(error.message).not.toContain("youcom-test");
    expect(fetchMock).not.toHaveBeenCalled();
  });

  it("ignores a removed variable that is set to blank", () => {
    process.env[LEGACY_PROVIDER_VARIABLE] = "  ";
    process.env[LEGACY_KEY_VARIABLE] = "";

    expect(() => assertSearchFallbackConfiguration()).not.toThrow();
  });

  it("gives each provider its own key, endpoint and Bearer header", async () => {
    listProviders(undefined);
    process.env[PARALLEL_KEY_VARIABLE] = "parallel-test-value";
    orderParallelFirst();
    mockExchange(documentWith([]));
    mockYoucom([]);

    await fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS);

    // Parallel's three requests carry its key; You.com, with no key of its
    // own, stays on the keyless profile and is never sent Parallel's.
    expect(requestUrls()).toEqual([
      PARALLEL_URL,
      PARALLEL_URL,
      PARALLEL_URL,
      YOUCOM_KEYLESS_URL,
      YOUCOM_KEYLESS_URL,
    ]);
    for (const index of [0, 1, 2]) {
      expect(requestHeaders(index).Authorization).toBe(
        "Bearer parallel-test-value",
      );
    }
    for (const index of [3, 4]) {
      expect(requestHeaders(index).Authorization).toBeUndefined();
    }
  });

  it("switches You.com to its authenticated endpoint with its own key only", async () => {
    listProviders(undefined);
    process.env[YOUCOM_KEY_VARIABLE] = "youcom-test-value";
    orderYoucomFirst();
    mockYoucom([]);
    mockExchange(documentWith([]));

    await fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS);

    expect(requestUrls().slice(0, 2)).toEqual([
      YOUCOM_AUTHENTICATED_URL,
      YOUCOM_AUTHENTICATED_URL,
    ]);
    expect(requestHeaders(0).Authorization).toBe("Bearer youcom-test-value");
    expect(requestHeaders(2).Authorization).toBeUndefined();
  });
});

describe("the cascade", () => {
  beforeEach(() => {
    listProviders(undefined);
  });

  it("tries the providers in the order the shuffle draws", async () => {
    const random = orderYoucomFirst();
    mockYoucom([SERVED_RESULT]);

    await fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS);

    expect(random).toHaveBeenCalled();
    expect(requestUrls()).toEqual([YOUCOM_KEYLESS_URL, YOUCOM_KEYLESS_URL]);

    fetchMock.mockReset();
    random.mockReturnValue(0.99);
    mockExchange(
      documentWith([
        { url: "https://example.com/p", title: "P", excerpts: [EXCERPT] },
      ]),
    );

    await fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS);

    expect(requestUrls()[0]).toBe(PARALLEL_URL);
  });

  it("moves on to the next provider when the first throws", async () => {
    orderParallelFirst();
    fetchMock.mockResolvedValueOnce(createMockResponse("", { status: 500 }));
    mockYoucom([SERVED_RESULT]);

    const results = await fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS);

    expect(results).toEqual([
      ["Served", DESCRIPTION, "https://example.com/served"],
    ]);
    expect(getFallbackProviderStats()).toMatchObject({
      parallel: { served: 0, empty: 0, failed: 1 },
      youcom: { served: 1, empty: 0, failed: 0 },
    });
  });

  it("moves on to the next provider when the first answers empty", async () => {
    orderParallelFirst();
    mockExchange(documentWith([]));
    mockYoucom([SERVED_RESULT]);

    const results = await fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS);

    expect(results).toHaveLength(1);
    expect(getFallbackProviderStats()).toMatchObject({
      parallel: { served: 0, empty: 1, failed: 0 },
      youcom: { served: 1, empty: 0, failed: 0 },
    });
  });

  it("stops at the first provider that serves", async () => {
    orderYoucomFirst();
    mockYoucom([SERVED_RESULT]);

    await fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS);

    expect(requestUrls()).not.toContain(PARALLEL_URL);
  });

  it("throws one error naming every provider when all of them fail, without the query", async () => {
    orderParallelFirst();
    fetchMock.mockResolvedValueOnce(createMockResponse("", { status: 500 }));
    fetchMock.mockResolvedValueOnce(createMockResponse("", { status: 503 }));

    const error = (await fetchFallbackTextResults(
      QUERY,
      10,
      FULL_BUDGET_MS,
    ).catch((thrown: unknown) => thrown)) as Error;

    expect(error.message).toBe(
      "Every fallback search provider failed (parallel: The fallback search endpoint did not answer the initialize request (status 500); youcom: The fallback search endpoint did not answer the initialize request (status 503))",
    );
    expect(error.message).not.toContain(QUERY);
  });

  it("returns an empty array when one answered empty and the other failed", async () => {
    orderParallelFirst();
    mockExchange(documentWith([]));
    fetchMock.mockResolvedValueOnce(createMockResponse("", { status: 500 }));

    await expect(
      fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS),
    ).resolves.toEqual([]);
  });

  it("returns an empty array when one failed and the other answered empty", async () => {
    orderParallelFirst();
    fetchMock.mockResolvedValueOnce(createMockResponse("", { status: 500 }));
    mockYoucom([]);

    await expect(
      fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS),
    ).resolves.toEqual([]);
  });

  it("returns an empty array when every provider answers empty", async () => {
    orderParallelFirst();
    mockExchange(documentWith([]));
    mockYoucom([]);

    await expect(
      fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS),
    ).resolves.toEqual([]);
    expect(getFallbackProviderStats()).toMatchObject({
      parallel: { empty: 1 },
      youcom: { empty: 1 },
    });
  });

  it("gives each attempt an equal share of what is left of the budget", async () => {
    vi.useFakeTimers({ toFake: ["Date"] });
    vi.setSystemTime(1_000_000);
    orderParallelFirst();
    const timeoutSpy = vi.spyOn(AbortSignal, "timeout");
    // The first provider spends a second before it fails, so the second one
    // gets what is left, not a share computed at the start.
    fetchMock.mockImplementationOnce(async () => {
      vi.setSystemTime(1_001_000);
      throw new Error("Network failure");
    });
    mockYoucom([SERVED_RESULT]);

    await fetchFallbackTextResults(QUERY, 10, 3_000);

    expect(timeoutSpy.mock.calls).toEqual([[1_500], [2_000]]);
  });

  it("splits the module's own cap when the caller offers more", async () => {
    vi.useFakeTimers({ toFake: ["Date"] });
    orderParallelFirst();
    const timeoutSpy = vi.spyOn(AbortSignal, "timeout");
    mockExchange(
      documentWith([
        { url: "https://example.com/p", title: "P", excerpts: [EXCERPT] },
      ]),
    );

    await fetchFallbackTextResults(QUERY, 10, 60_000);

    expect(timeoutSpy.mock.calls).toEqual([[7_500]]);
  });

  it("does not start a provider once the budget is spent", async () => {
    vi.useFakeTimers({ toFake: ["Date"] });
    vi.setSystemTime(1_000_000);
    orderParallelFirst();
    fetchMock.mockImplementationOnce(async () => {
      vi.setSystemTime(1_005_000);
      throw new Error("Network failure");
    });

    await expect(fetchFallbackTextResults(QUERY, 10, 3_000)).rejects.toThrow(
      "No fallback search provider served the search (parallel: Network failure; not tried, the time budget ran out: youcom)",
    );
    expect(fetchMock).toHaveBeenCalledTimes(1);
  });
});

describe("pausing a rate-limited provider", () => {
  beforeEach(() => {
    vi.useFakeTimers({ toFake: ["Date"] });
    // On a whole second, so an HTTP-date Retry-After lands on an exact delta.
    vi.setSystemTime(Date.UTC(2026, 0, 1, 12, 0, 0));
  });

  async function hitRateLimit(headers: Record<string, string> = {}) {
    fetchMock.mockResolvedValueOnce(rateLimited(headers));
    await expect(
      fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS),
    ).rejects.toThrow("rate-limited the request (status 429)");
  }

  it("pauses for the seconds Retry-After asks, skips without a request, and tries again after", async () => {
    await hitRateLimit({ "retry-after": "120" });
    expect(getFallbackProviderStats().parallel).toMatchObject({
      failed: 1,
      rateLimited: 1,
      pausedForMs: 120_000,
    });

    fetchMock.mockClear();
    vi.setSystemTime(Date.now() + 119_000);
    await expect(
      fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS),
    ).rejects.toThrow("No fallback search provider could be tried");
    expect(fetchMock).not.toHaveBeenCalled();
    expect(getFallbackProviderStats().parallel).toMatchObject({
      skippedWhilePaused: 1,
      pausedForMs: 1_000,
    });

    vi.setSystemTime(Date.now() + 1_000);
    mockExchange(
      documentWith([
        { url: "https://example.com/p", title: "P", excerpts: [EXCERPT] },
      ]),
    );
    await expect(
      fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS),
    ).resolves.toHaveLength(1);
    expect(getFallbackProviderStats().parallel.pausedForMs).toBe(0);
  });

  it("reads an HTTP-date Retry-After on the tool call", async () => {
    fetchMock.mockResolvedValueOnce(
      createMockResponse(INITIALIZE_REPLY, {
        headers: { "mcp-session-id": SESSION_ID },
      }),
    );
    fetchMock.mockResolvedValueOnce(
      createMockResponse(NOTIFICATION_ACCEPTED, { status: 202 }),
    );
    const cancel = vi.fn(async () => undefined);
    fetchMock.mockResolvedValueOnce(
      createMockResponse("", {
        status: 429,
        headers: {
          "retry-after": new Date(Date.now() + 300_000).toUTCString(),
        },
        unreadBody: { cancel },
      }),
    );

    await expect(
      fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS),
    ).rejects.toThrow("status 429");

    expect(cancel).toHaveBeenCalledTimes(1);
    expect(getFallbackProviderStats().parallel.pausedForMs).toBe(300_000);
  });

  it("pauses for a minute when Retry-After is missing or unreadable", async () => {
    await hitRateLimit();
    expect(getFallbackProviderStats().parallel.pausedForMs).toBe(60_000);

    vi.setSystemTime(Date.now() + 60_000);
    await hitRateLimit({ "retry-after": "soon" });
    expect(getFallbackProviderStats().parallel.pausedForMs).toBe(60_000);
  });

  it("pauses for a minute when Retry-After is a string Date.parse would read as a past date", async () => {
    for (const value of ["1.5", "-1", "abc 5"]) {
      await hitRateLimit({ "retry-after": value });
      expect(getFallbackProviderStats().parallel.pausedForMs).toBe(60_000);
      vi.setSystemTime(Date.now() + 60_000);
    }
  });

  it("clamps a past HTTP-date and gives an obsolete date format the default", async () => {
    await hitRateLimit({
      "retry-after": new Date(Date.now() - 60_000).toUTCString(),
    });
    expect(getFallbackProviderStats().parallel.pausedForMs).toBe(1_000);

    vi.setSystemTime(Date.now() + 1_000);
    await hitRateLimit({ "retry-after": "Thursday, 01-Jan-26 12:05:00 GMT" });
    expect(getFallbackProviderStats().parallel.pausedForMs).toBe(60_000);
  });

  it("pauses on a 429 to the initialized notification and discards its body", async () => {
    fetchMock.mockResolvedValueOnce(
      createMockResponse(INITIALIZE_REPLY, {
        headers: { "mcp-session-id": SESSION_ID },
      }),
    );
    const cancel = vi.fn(async () => undefined);
    fetchMock.mockResolvedValueOnce(
      createMockResponse("", {
        status: 429,
        headers: { "retry-after": "45" },
        unreadBody: { cancel },
      }),
    );

    await expect(
      fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS),
    ).rejects.toThrow("status 429");

    expect(cancel).toHaveBeenCalledTimes(1);
    expect(getFallbackProviderStats().parallel).toMatchObject({
      rateLimited: 1,
      pausedForMs: 45_000,
    });
  });

  it("skips a provider that a concurrent search paused while this one was waiting", async () => {
    listProviders(undefined);
    orderParallelFirst();
    // Parallel's attempt is still in flight when another search, pinned to
    // You.com, gets a 429 there and pauses it.
    fetchMock.mockImplementationOnce(async () => {
      listProviders("youcom");
      await fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS).catch(
        () => undefined,
      );
      throw new Error("Network failure");
    });
    fetchMock.mockResolvedValueOnce(rateLimited({ "retry-after": "3600" }));

    await expect(
      fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS),
    ).rejects.toThrow(
      "Every fallback search provider failed (parallel: Network failure)",
    );

    expect(fetchMock).toHaveBeenCalledTimes(2);
    expect(getFallbackProviderStats().youcom).toMatchObject({
      rateLimited: 1,
      skippedWhilePaused: 1,
      pausedForMs: 3_600_000,
    });
  });

  it("never shortens a longer pause that a concurrent search set", async () => {
    // The first search's request is in flight when a second one gets a 429
    // asking for an hour; the first then gets its own 429 asking for a second.
    fetchMock.mockImplementationOnce(async () => {
      await fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS).catch(
        () => undefined,
      );
      return rateLimited({ "retry-after": "1" });
    });
    fetchMock.mockResolvedValueOnce(rateLimited({ "retry-after": "3600" }));

    await expect(
      fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS),
    ).rejects.toThrow("status 429");

    expect(getFallbackProviderStats().parallel.pausedForMs).toBe(3_600_000);
  });

  it("splits the budget only among the providers that are not paused", async () => {
    listProviders("youcom");
    await hitRateLimit();

    listProviders(undefined);
    orderParallelFirst();
    const timeoutSpy = vi.spyOn(AbortSignal, "timeout");
    mockExchange(
      documentWith([
        { url: "https://example.com/p", title: "P", excerpts: [EXCERPT] },
      ]),
    );

    await fetchFallbackTextResults(QUERY, 10, 3_000);

    expect(timeoutSpy.mock.calls).toEqual([[3_000]]);
  });

  it("clamps the pause to an hour at most and a second at least", async () => {
    await hitRateLimit({ "retry-after": "86400" });
    expect(getFallbackProviderStats().parallel.pausedForMs).toBe(3_600_000);

    vi.setSystemTime(Date.now() + 3_600_000);
    await hitRateLimit({ "retry-after": "0" });
    expect(getFallbackProviderStats().parallel.pausedForMs).toBe(1_000);
  });

  it("skips only the paused provider and goes straight to the next", async () => {
    listProviders(undefined);
    orderParallelFirst();
    fetchMock.mockResolvedValueOnce(rateLimited({ "retry-after": "30" }));
    mockYoucom([SERVED_RESULT]);
    await fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS);

    fetchMock.mockClear();
    mockYoucom([SERVED_RESULT]);
    await expect(
      fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS),
    ).resolves.toHaveLength(1);

    expect(requestUrls()).toEqual([YOUCOM_KEYLESS_URL, YOUCOM_KEYLESS_URL]);
    expect(getFallbackProviderStats()).toMatchObject({
      parallel: { failed: 1, rateLimited: 1, skippedWhilePaused: 1 },
      youcom: { served: 2 },
    });
  });

  it("does not count a paused provider as skipped once the budget is spent", async () => {
    listProviders(undefined);
    const random = orderYoucomFirst();
    fetchMock.mockResolvedValueOnce(rateLimited({ "retry-after": "600" }));
    mockExchange(
      documentWith([
        { url: "https://example.com/p", title: "P", excerpts: [EXCERPT] },
      ]),
    );
    await fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS);

    random.mockReturnValue(0.99);
    fetchMock.mockImplementationOnce(async () => {
      vi.setSystemTime(Date.now() + FULL_BUDGET_MS);
      throw new Error("Network failure");
    });

    await expect(
      fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS),
    ).rejects.toThrow("not tried, the time budget ran out: youcom");
    expect(getFallbackProviderStats().youcom.skippedWhilePaused).toBe(0);
  });

  it("throws without a request when every provider is paused", async () => {
    listProviders(undefined);
    orderParallelFirst();
    fetchMock.mockResolvedValueOnce(rateLimited());
    fetchMock.mockResolvedValueOnce(rateLimited());
    await expect(
      fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS),
    ).rejects.toThrow("Every fallback search provider failed");

    fetchMock.mockClear();
    await expect(
      fetchFallbackTextResults(QUERY, 10, FULL_BUDGET_MS),
    ).rejects.toThrow("No fallback search provider could be tried");
    expect(fetchMock).not.toHaveBeenCalled();
  });
});

describe("getFallbackProviderStats", () => {
  it("starts every built-in provider at zero", () => {
    const zero = {
      served: 0,
      empty: 0,
      failed: 0,
      rateLimited: 0,
      skippedWhilePaused: 0,
      pausedForMs: 0,
    };
    expect(getFallbackProviderStats()).toEqual({
      parallel: zero,
      youcom: zero,
    });
  });
});