| 1 | import { Readable } from "stream"; |
| 2 | import { describe, expect, it, test } from "vitest"; |
| 3 | import { parseDataLine, streamSse } from "./stream.js"; |
| 4 | |
| 5 | function createMockResponse(sseLines: string[]): Response { |
| 6 | // Create a Readable stream that emits the SSE lines |
| 7 | const stream = new Readable({ |
| 8 | read() { |
| 9 | for (const line of sseLines) { |
| 10 | this.push(line + "\n\n"); |
| 11 | } |
| 12 | this.push(null); // End of stream |
| 13 | }, |
| 14 | }) as any; |
| 15 | |
| 16 | // Minimal Response mock |
| 17 | return { |
| 18 | status: 200, |
| 19 | body: stream, |
| 20 | text: async () => "", |
| 21 | } as unknown as Response; |
| 22 | } |
| 23 | |
| 24 | describe("streamSse", () => { |
| 25 | it("yields parsed SSE data objects that ends with `data:[DONE]`", async () => { |
| 26 | const sseLines = [ |
| 27 | 'data: {"foo": "bar"}', |
| 28 | 'data: {"baz": 42}', |
| 29 | "data:[DONE]", |
| 30 | ]; |
| 31 | const response = createMockResponse(sseLines); |
| 32 | |
| 33 | const results = []; |
| 34 | for await (const data of streamSse(response)) { |
| 35 | results.push(data); |
| 36 | } |
| 37 | |
| 38 | expect(results).toEqual([{ foo: "bar" }, { baz: 42 }]); |
| 39 | }); |
| 40 | |
| 41 | it("yields parsed SSE data objects that ends with `data: [DONE]` (with a space before [DONE]", async () => { |
| 42 | const sseLines = [ |
| 43 | 'data: {"foo": "bar"}', |
| 44 | 'data: {"baz": 42}', |
| 45 | "data: [DONE]", |
| 46 | ]; |
| 47 | const response = createMockResponse(sseLines); |
| 48 | |
| 49 | const results = []; |
| 50 | for await (const data of streamSse(response)) { |
| 51 | results.push(data); |
| 52 | } |
| 53 | |
| 54 | expect(results).toEqual([{ foo: "bar" }, { baz: 42 }]); |
| 55 | }); |
| 56 | |
| 57 | it("throws on malformed JSON", async () => { |
| 58 | const sseLines = ['data: {"foo": "bar"', "data:[DONE]"]; |
| 59 | const response = createMockResponse(sseLines); |
| 60 | |
| 61 | const iterator = streamSse(response)[Symbol.asyncIterator](); |
| 62 | await expect(iterator.next()).rejects.toThrow(/Malformed JSON/); |
| 63 | }); |
| 64 | }); |
| 65 | |
| 66 | describe("parseDataLine", () => { |
| 67 | test("parseDataLine should parse valid JSON data with 'data: ' prefix", () => { |
| 68 | const line = 'data: {"message":"hello","status":"ok"}'; |
| 69 | const result = parseDataLine(line); |
| 70 | expect(result).toEqual({ message: "hello", status: "ok" }); |
| 71 | }); |
| 72 | |
| 73 | test("parseDataLine should parse valid JSON data with 'data:' prefix (no space)", () => { |
| 74 | const line = 'data:{"message":"hello","status":"ok"}'; |
| 75 | const result = parseDataLine(line); |
| 76 | expect(result).toEqual({ message: "hello", status: "ok" }); |
| 77 | }); |
| 78 | |
| 79 | test("parseDataLine should throw error for malformed JSON", () => { |
| 80 | const line = "data: {invalid json}"; |
| 81 | expect(() => parseDataLine(line)).toThrow( |
| 82 | "Malformed JSON sent from server", |
| 83 | ); |
| 84 | }); |
| 85 | |
| 86 | test("parseDataLine should throw error when data contains error field", () => { |
| 87 | const line = 'data: {"error":"something went wrong"}'; |
| 88 | expect(() => parseDataLine(line)).toThrow( |
| 89 | 'Error streaming response: "something went wrong"', |
| 90 | ); |
| 91 | }); |
| 92 | |
| 93 | test("parseDataLine should throw error when data contains error object with message", () => { |
| 94 | const line = 'data: {"error":{"message":"detailed error message"}}'; |
| 95 | expect(() => parseDataLine(line)).toThrow( |
| 96 | "Error streaming response: detailed error message", |
| 97 | ); |
| 98 | }); |
| 99 | |
| 100 | test("parseDataLine should handle empty objects", () => { |
| 101 | const line = "data: {}"; |
| 102 | const result = parseDataLine(line); |
| 103 | expect(result).toEqual({}); |
| 104 | }); |
| 105 | |
| 106 | test("parseDataLine should handle arrays", () => { |
| 107 | const line = "data: [1,2,3]"; |
| 108 | const result = parseDataLine(line); |
| 109 | expect(result).toEqual([1, 2, 3]); |
| 110 | }); |
| 111 | |
| 112 | test("parseDataLine should handle nested objects", () => { |
| 113 | const line = 'data: {"user":{"name":"John","age":30}}'; |
| 114 | const result = parseDataLine(line); |
| 115 | expect(result).toEqual({ user: { name: "John", age: 30 } }); |
| 116 | }); |
| 117 | }); |