forked from ZyntariHQ/Invoisio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsse-parser.test.ts
More file actions
71 lines (59 loc) · 2.56 KB
/
Copy pathsse-parser.test.ts
File metadata and controls
71 lines (59 loc) · 2.56 KB
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
import { nextReconnectDelayMs, parseSseBuffer } from "./sse-parser";
describe("parseSseBuffer", () => {
it("parses a single complete frame", () => {
const { events, rest } = parseSseBuffer('data: {"a":1}\n\n');
expect(events).toEqual([{ event: null, data: '{"a":1}', id: null }]);
expect(rest).toBe("");
});
it("parses multiple frames delivered in one chunk", () => {
const { events, rest } = parseSseBuffer(
'data: {"a":1}\n\ndata: {"a":2}\n\n',
);
expect(events.map((e) => e.data)).toEqual(['{"a":1}', '{"a":2}']);
expect(rest).toBe("");
});
it("retains an incomplete trailing frame for the next chunk", () => {
const first = parseSseBuffer('data: {"a":1}\n\ndata: {"a":2');
expect(first.events.map((e) => e.data)).toEqual(['{"a":1}']);
expect(first.rest).toBe('data: {"a":2');
const second = parseSseBuffer(first.rest + "}\n\n");
expect(second.events.map((e) => e.data)).toEqual(['{"a":2}']);
expect(second.rest).toBe("");
});
it("captures event and id fields alongside data", () => {
const { events } = parseSseBuffer(
'event: status\nid: 42\ndata: {"a":1}\n\n',
);
expect(events).toEqual([{ event: "status", data: '{"a":1}', id: "42" }]);
});
it("joins multi-line data fields with newlines", () => {
const { events } = parseSseBuffer("data: line1\ndata: line2\n\n");
expect(events[0]?.data).toBe("line1\nline2");
});
it("ignores comment lines used as SSE keep-alives", () => {
const { events, rest } = parseSseBuffer(": ping\n\ndata: {" + '"a":1}\n\n');
expect(events).toEqual([{ event: null, data: '{"a":1}', id: null }]);
expect(rest).toBe("");
});
it("normalizes CRLF line endings", () => {
const { events } = parseSseBuffer('data: {"a":1}\r\n\r\n');
expect(events).toEqual([{ event: null, data: '{"a":1}', id: null }]);
});
it("returns no events for an empty or whitespace-only buffer", () => {
expect(parseSseBuffer("").events).toEqual([]);
expect(parseSseBuffer("\n\n").events).toEqual([]);
});
});
describe("nextReconnectDelayMs", () => {
it("doubles the delay for each successive attempt", () => {
expect(nextReconnectDelayMs(0, 1000, 30000)).toBe(1000);
expect(nextReconnectDelayMs(1, 1000, 30000)).toBe(2000);
expect(nextReconnectDelayMs(2, 1000, 30000)).toBe(4000);
});
it("caps the delay at the configured maximum", () => {
expect(nextReconnectDelayMs(10, 1000, 30000)).toBe(30000);
});
it("treats negative attempts as the first attempt", () => {
expect(nextReconnectDelayMs(-1, 1000, 30000)).toBe(1000);
});
});