|
| 1 | +import { redisTest } from "@internal/testcontainers"; |
| 2 | +import { describe, expect, vi } from "vitest"; |
| 3 | +import { RealtimeStreams } from "../app/services/realtimeStreams.server.js"; |
| 4 | +import { convertArrayToReadableStream, convertResponseSSEStreamToArray } from "./utils/streams.js"; |
| 5 | + |
| 6 | +vi.setConfig({ testTimeout: 10_000 }); // 5 seconds |
| 7 | + |
| 8 | +// Mock the logger |
| 9 | +vi.mock("./logger.server", () => ({ |
| 10 | + logger: { |
| 11 | + debug: vi.fn(), |
| 12 | + error: vi.fn(), |
| 13 | + }, |
| 14 | +})); |
| 15 | + |
| 16 | +describe("RealtimeStreams", () => { |
| 17 | + redisTest("should stream data from producer to consumer", async ({ redis }) => { |
| 18 | + const streams = new RealtimeStreams({ redis: redis.options }); |
| 19 | + const runId = "test-run"; |
| 20 | + const streamId = "test-stream"; |
| 21 | + |
| 22 | + // Create a stream of test data |
| 23 | + const stream = convertArrayToReadableStream(["chunk1", "chunk2", "chunk3"]).pipeThrough( |
| 24 | + new TextEncoderStream() |
| 25 | + ); |
| 26 | + |
| 27 | + // Start consuming the stream |
| 28 | + const abortController = new AbortController(); |
| 29 | + const responsePromise = streams.streamResponse(runId, streamId, abortController.signal); |
| 30 | + |
| 31 | + // Start ingesting data |
| 32 | + await streams.ingestData(stream, runId, streamId); |
| 33 | + |
| 34 | + // Get the response and read the stream |
| 35 | + const response = await responsePromise; |
| 36 | + const received = await convertResponseSSEStreamToArray(response); |
| 37 | + |
| 38 | + expect(received).toEqual(["chunk1", "chunk2", "chunk3"]); |
| 39 | + }); |
| 40 | + |
| 41 | + redisTest("should handle multiple concurrent streams", async ({ redis }) => { |
| 42 | + const streams = new RealtimeStreams({ redis: redis.options }); |
| 43 | + const runId = "test-run"; |
| 44 | + |
| 45 | + // Set up two different streams |
| 46 | + const stream1 = convertArrayToReadableStream(["1a", "1b", "1c"]).pipeThrough( |
| 47 | + new TextEncoderStream() |
| 48 | + ); |
| 49 | + const stream2 = convertArrayToReadableStream(["2a", "2b", "2c"]).pipeThrough( |
| 50 | + new TextEncoderStream() |
| 51 | + ); |
| 52 | + |
| 53 | + // Start consuming both streams |
| 54 | + const abortController = new AbortController(); |
| 55 | + const response1Promise = streams.streamResponse(runId, "stream1", abortController.signal); |
| 56 | + const response2Promise = streams.streamResponse(runId, "stream2", abortController.signal); |
| 57 | + |
| 58 | + // Ingest data to both streams |
| 59 | + await Promise.all([ |
| 60 | + streams.ingestData(stream1, runId, "stream1"), |
| 61 | + streams.ingestData(stream2, runId, "stream2"), |
| 62 | + ]); |
| 63 | + |
| 64 | + // Get and verify both responses |
| 65 | + const [response1, response2] = await Promise.all([response1Promise, response2Promise]); |
| 66 | + const [received1, received2] = await Promise.all([ |
| 67 | + convertResponseSSEStreamToArray(response1), |
| 68 | + convertResponseSSEStreamToArray(response2), |
| 69 | + ]); |
| 70 | + |
| 71 | + expect(received1).toEqual(["1a", "1b", "1c"]); |
| 72 | + expect(received2).toEqual(["2a", "2b", "2c"]); |
| 73 | + }); |
| 74 | + |
| 75 | + redisTest("should handle early consumer abort", async ({ redis }) => { |
| 76 | + const streams = new RealtimeStreams({ redis: redis.options }); |
| 77 | + const runId = "test-run"; |
| 78 | + const streamId = "test-stream"; |
| 79 | + |
| 80 | + const stream = convertArrayToReadableStream(["chunk1", "chunk2", "chunk3"]).pipeThrough( |
| 81 | + new TextEncoderStream() |
| 82 | + ); |
| 83 | + |
| 84 | + // Start consuming but abort early |
| 85 | + const abortController = new AbortController(); |
| 86 | + const responsePromise = streams.streamResponse(runId, streamId, abortController.signal); |
| 87 | + |
| 88 | + // Get the response before aborting to ensure stream is properly set up |
| 89 | + const response = await responsePromise; |
| 90 | + |
| 91 | + // Start reading the stream |
| 92 | + const readPromise = convertResponseSSEStreamToArray(response); |
| 93 | + |
| 94 | + // Abort after a small delay to ensure everything is set up |
| 95 | + await new Promise((resolve) => setTimeout(resolve, 100)); |
| 96 | + abortController.abort(); |
| 97 | + |
| 98 | + // Start ingesting data after abort |
| 99 | + await streams.ingestData(stream, runId, streamId); |
| 100 | + |
| 101 | + // Verify the stream was terminated |
| 102 | + const received = await readPromise; |
| 103 | + |
| 104 | + expect(received).toEqual(["chunk1"]); |
| 105 | + }); |
| 106 | +}); |
0 commit comments