File size: 2,857 Bytes
0c8b3c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { describe, it, expect } from "vitest";
import { parseSSEBlock, parseSSEStream } from "../codex-sse.js";

describe("parseSSEBlock", () => {
  it("parses event + data", () => {
    const block = "event: response.created\ndata: {\"id\":\"resp_1\"}";
    const result = parseSSEBlock(block);
    expect(result).toEqual({
      event: "response.created",
      data: { id: "resp_1" },
    });
  });

  it("returns null for empty block", () => {
    expect(parseSSEBlock("")).toBeNull();
    expect(parseSSEBlock("   ")).toBeNull();
  });

  it("returns null for [DONE]", () => {
    const block = "data: [DONE]";
    expect(parseSSEBlock(block)).toBeNull();
  });

  it("handles data without event", () => {
    const block = 'data: {"type":"text"}';
    const result = parseSSEBlock(block);
    expect(result).toEqual({ event: "", data: { type: "text" } });
  });

  it("handles event without data", () => {
    const block = "event: done";
    const result = parseSSEBlock(block);
    expect(result).toEqual({ event: "done", data: "" });
  });

  it("joins multi-line data", () => {
    const block = "event: test\ndata: line1\ndata: line2";
    const result = parseSSEBlock(block);
    expect(result?.data).toBe("line1\nline2");
  });

  it("handles non-JSON data gracefully", () => {
    const block = "event: error\ndata: plain text error";
    const result = parseSSEBlock(block);
    expect(result?.data).toBe("plain text error");
  });
});

describe("parseSSEStream", () => {
  function makeResponse(text: string): Response {
    const encoder = new TextEncoder();
    const stream = new ReadableStream({
      start(controller) {
        controller.enqueue(encoder.encode(text));
        controller.close();
      },
    });
    return new Response(stream);
  }

  it("yields events from SSE stream", async () => {
    const sse = "event: response.created\ndata: {\"id\":\"r1\"}\n\nevent: response.done\ndata: {\"id\":\"r1\"}\n\n";
    const events = [];
    for await (const evt of parseSSEStream(makeResponse(sse))) {
      events.push(evt);
    }
    expect(events).toHaveLength(2);
    expect(events[0].event).toBe("response.created");
    expect(events[1].event).toBe("response.done");
  });

  it("handles non-SSE response as error event", async () => {
    const json = '{"detail":"unauthorized"}';
    const events = [];
    for await (const evt of parseSSEStream(makeResponse(json))) {
      events.push(evt);
    }
    expect(events).toHaveLength(1);
    expect(events[0].event).toBe("error");
    const data = events[0].data as Record<string, Record<string, string>>;
    expect(data.error.message).toBe("unauthorized");
  });

  it("throws on null body", async () => {
    const response = new Response(null);
    const gen = parseSSEStream(response);
    await expect(gen.next()).rejects.toThrow("Response body is null");
  });
});