Elffuss BraidHub

kikocis/continue public

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