botsProviderRequest.test.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { describe, expect, it, vi } from "vitest";
  2. import {
  3. fetchBotProvider,
  4. fetchBotProviderJson,
  5. } from "#src/bots/providers/providerRequest.js";
  6. describe("fetchBotProvider", () => {
  7. it("aborts a provider request when its deadline expires", async () => {
  8. vi.useFakeTimers();
  9. const fetchMock = vi.fn(
  10. (_input: string | URL | Request, init?: RequestInit) =>
  11. new Promise<Response>((_resolve, reject) => {
  12. init?.signal?.addEventListener(
  13. "abort",
  14. () => reject(init.signal?.reason ?? new Error("aborted")),
  15. { once: true },
  16. );
  17. }),
  18. );
  19. vi.stubGlobal("fetch", fetchMock);
  20. const request = fetchBotProvider("https://provider.test/callback", {}, 25);
  21. const rejection = expect(request).rejects.toThrow("timed out after 25ms");
  22. await vi.advanceTimersByTimeAsync(25);
  23. await rejection;
  24. vi.useRealTimers();
  25. });
  26. it("forwards an external abort and removes its listener after completion", async () => {
  27. const external = new AbortController();
  28. const fetchMock = vi.fn(
  29. (_input: string | URL | Request, init?: RequestInit) =>
  30. new Promise<Response>((_resolve, reject) => {
  31. init?.signal?.addEventListener("abort", () => reject(new Error("external abort")), {
  32. once: true,
  33. });
  34. }),
  35. );
  36. vi.stubGlobal("fetch", fetchMock);
  37. const request = fetchBotProvider(
  38. "https://provider.test/callback",
  39. { signal: external.signal },
  40. 1_000,
  41. );
  42. external.abort();
  43. await expect(request).rejects.toThrow("external abort");
  44. });
  45. it("keeps the deadline active while consuming a stalled JSON body", async () => {
  46. vi.useFakeTimers();
  47. const fetchMock = vi.fn(async (_input: string | URL | Request, init?: RequestInit) => {
  48. const body = new ReadableStream<Uint8Array>({
  49. start(controller) {
  50. init?.signal?.addEventListener(
  51. "abort",
  52. () => controller.error(init.signal?.reason ?? new Error("aborted")),
  53. { once: true },
  54. );
  55. },
  56. });
  57. return new Response(body, {
  58. status: 200,
  59. headers: { "content-type": "application/json" },
  60. });
  61. });
  62. vi.stubGlobal("fetch", fetchMock);
  63. const request = fetchBotProviderJson("https://provider.test/callback", {}, 25);
  64. const rejection = expect(request).rejects.toThrow("timed out after 25ms");
  65. await vi.advanceTimersByTimeAsync(25);
  66. await rejection;
  67. vi.useRealTimers();
  68. });
  69. it("keeps the deadline active after headers while consuming a stalled body", async () => {
  70. vi.useFakeTimers();
  71. const fetchMock = vi.fn(async (_input: string | URL | Request, init?: RequestInit) => {
  72. const body = new ReadableStream<Uint8Array>({
  73. start(controller) {
  74. init?.signal?.addEventListener(
  75. "abort",
  76. () => controller.error(init.signal?.reason ?? new Error("aborted")),
  77. { once: true },
  78. );
  79. },
  80. });
  81. return new Response(body, { status: 200 });
  82. });
  83. vi.stubGlobal("fetch", fetchMock);
  84. const request = fetchBotProvider("https://provider.test/callback", {}, 25);
  85. const rejection = expect(request).rejects.toThrow("timed out after 25ms");
  86. await vi.advanceTimersByTimeAsync(25);
  87. await rejection;
  88. vi.useRealTimers();
  89. });
  90. });