rpc-jsonl.test.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { Readable } from "node:stream";
  2. import { describe, expect, test } from "vitest";
  3. import { attachJsonlLineReader, serializeJsonLine } from "../src/modes/rpc/jsonl.ts";
  4. describe("RPC JSONL framing", () => {
  5. test("serializes strict JSONL records without escaping Unicode separators", () => {
  6. const line = serializeJsonLine({ text: "a\u2028b\u2029c" });
  7. expect(line).toContain("a\u2028b\u2029c");
  8. expect(line.endsWith("\n")).toBe(true);
  9. expect(JSON.parse(line.trim())).toEqual({ text: "a\u2028b\u2029c" });
  10. });
  11. test("splits on LF only and preserves U+2028/U+2029 inside payloads", async () => {
  12. const lines: string[] = [];
  13. const stream = Readable.from([serializeJsonLine({ text: "a\u2028b\u2029c" })]);
  14. const done = new Promise<void>((resolve) => {
  15. stream.on("end", resolve);
  16. });
  17. attachJsonlLineReader(stream, (line) => {
  18. lines.push(line);
  19. });
  20. await done;
  21. expect(lines).toHaveLength(1);
  22. expect(JSON.parse(lines[0])).toEqual({ text: "a\u2028b\u2029c" });
  23. });
  24. test("handles CRLF-delimited input", async () => {
  25. const lines: string[] = [];
  26. const stream = Readable.from([Buffer.from('{"a":1}\r\n{"b":2}\r\n')]);
  27. const done = new Promise<void>((resolve) => {
  28. stream.on("end", resolve);
  29. });
  30. attachJsonlLineReader(stream, (line) => {
  31. lines.push(line);
  32. });
  33. await done;
  34. expect(lines).toEqual(['{"a":1}', '{"b":2}']);
  35. });
  36. test("emits a final line without trailing LF", async () => {
  37. const lines: string[] = [];
  38. const stream = Readable.from([Buffer.from('{"a":1}')]);
  39. const done = new Promise<void>((resolve) => {
  40. stream.on("end", resolve);
  41. });
  42. attachJsonlLineReader(stream, (line) => {
  43. lines.push(line);
  44. });
  45. await done;
  46. expect(lines).toEqual(['{"a":1}']);
  47. });
  48. });