|
1 | | -import { describe, expect, it } from "vitest"; |
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
2 | 2 | import { |
3 | 3 | RunSubscription, |
4 | 4 | SSEStreamPart, |
| 5 | + SSEStreamSubscription, |
5 | 6 | StreamSubscription, |
6 | 7 | StreamSubscriptionFactory, |
7 | 8 | } from "../src/v3/apiClient/runStream.js"; |
@@ -470,6 +471,47 @@ describe("RunSubscription", () => { |
470 | 471 | }); |
471 | 472 | }); |
472 | 473 |
|
| 474 | +describe("SSEStreamSubscription", () => { |
| 475 | + let originalFetch: typeof global.fetch; |
| 476 | + |
| 477 | + beforeEach(() => { |
| 478 | + originalFetch = global.fetch; |
| 479 | + }); |
| 480 | + |
| 481 | + afterEach(() => { |
| 482 | + global.fetch = originalFetch; |
| 483 | + vi.restoreAllMocks(); |
| 484 | + }); |
| 485 | + |
| 486 | + it("does not retry the initial fetch on 401", async () => { |
| 487 | + const fetchMock = vi.fn().mockResolvedValue(new Response(null, { status: 401 })); |
| 488 | + global.fetch = fetchMock; |
| 489 | + |
| 490 | + const sub = new SSEStreamSubscription("https://api.test/realtime/v1/streams/run_x/chat", { |
| 491 | + headers: { Authorization: "Bearer expired" }, |
| 492 | + }); |
| 493 | + |
| 494 | + const stream = await sub.subscribe(); |
| 495 | + const reader = stream.getReader(); |
| 496 | + await expect(reader.read()).rejects.toMatchObject({ status: 401 }); |
| 497 | + expect(fetchMock).toHaveBeenCalledTimes(1); |
| 498 | + }); |
| 499 | + |
| 500 | + it("does not retry the initial fetch on 403", async () => { |
| 501 | + const fetchMock = vi.fn().mockResolvedValue(new Response(null, { status: 403 })); |
| 502 | + global.fetch = fetchMock; |
| 503 | + |
| 504 | + const sub = new SSEStreamSubscription("https://api.test/realtime/v1/streams/run_x/chat", { |
| 505 | + headers: { Authorization: "Bearer denied" }, |
| 506 | + }); |
| 507 | + |
| 508 | + const stream = await sub.subscribe(); |
| 509 | + const reader = stream.getReader(); |
| 510 | + await expect(reader.read()).rejects.toMatchObject({ status: 403 }); |
| 511 | + expect(fetchMock).toHaveBeenCalledTimes(1); |
| 512 | + }); |
| 513 | +}); |
| 514 | + |
473 | 515 | export async function convertAsyncIterableToArray<T>(iterable: AsyncIterable<T>): Promise<T[]> { |
474 | 516 | const result: T[] = []; |
475 | 517 | for await (const item of iterable) { |
|
0 commit comments