-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathjsonl-stream.test.ts
More file actions
96 lines (70 loc) · 2.81 KB
/
jsonl-stream.test.ts
File metadata and controls
96 lines (70 loc) · 2.81 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import * as assert from "node:assert";
import { JsonlStream } from "../utils/jsonl-stream.js";
const setup = () => {
const jsonlStream = new JsonlStream();
const callbackCalls: unknown[] = [];
jsonlStream.onJson((data: unknown) => {
callbackCalls.push(data);
});
return {
jsonlStream,
callbackCalls,
};
};
suite("JSONL Streams", () => {
test("should parse and emit complete JSONL messages", () => {
const { jsonlStream, callbackCalls } = setup();
// Test with multiple JSON objects in a single write
const testData = Buffer.from('{"key1":"value1"}\n{"key2":"value2"}\n');
jsonlStream.write(testData);
assert.strictEqual(callbackCalls.length, 2);
assert.deepStrictEqual(callbackCalls[0], { key1: "value1" });
assert.deepStrictEqual(callbackCalls[1], { key2: "value2" });
});
test("should handle incomplete JSONL messages across multiple writes", () => {
const { jsonlStream, callbackCalls } = setup();
// First write with partial message
const firstChunk = Buffer.from('{"key":"value');
jsonlStream.write(firstChunk);
// Shouldn't emit anything yet
assert.strictEqual(callbackCalls.length, 0);
// Complete the message in second write
const secondChunk = Buffer.from('1"}\n');
jsonlStream.write(secondChunk);
// Now it should emit the complete message
assert.strictEqual(callbackCalls.length, 1);
assert.deepStrictEqual(callbackCalls[0], { key: "value1" });
});
test("should handle multiple messages in chunks", () => {
const { jsonlStream, callbackCalls } = setup();
// Write first message and part of second
const firstChunk = Buffer.from('{"first":1}\n{"second":');
jsonlStream.write(firstChunk);
// First message should be emitted
assert.strictEqual(callbackCalls.length, 1);
assert.deepStrictEqual(callbackCalls[0], { first: 1 });
// Complete second message and add third
const secondChunk = Buffer.from('2}\n{"third":3}\n');
jsonlStream.write(secondChunk);
// Should have all three messages now
assert.strictEqual(callbackCalls.length, 3);
assert.deepStrictEqual(callbackCalls[1], { second: 2 });
assert.deepStrictEqual(callbackCalls[2], { third: 3 });
});
test("should ignore invalid JSON lines", () => {
const { jsonlStream, callbackCalls } = setup();
const testData = Buffer.from('not json\n{"valid":true}\n{invalid}\n');
jsonlStream.write(testData);
// Should only emit the valid JSON object
assert.strictEqual(callbackCalls.length, 1);
assert.deepStrictEqual(callbackCalls[0], { valid: true });
});
test("should handle empty lines", () => {
const { jsonlStream, callbackCalls } = setup();
const testData = Buffer.from('\n\n{"key":"value"}\n\n');
jsonlStream.write(testData);
// Should only emit the valid JSON object
assert.strictEqual(callbackCalls.length, 1);
assert.deepStrictEqual(callbackCalls[0], { key: "value" });
});
});