serialization.test.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { describe, it, expect } from "vitest";
  2. import { VSBuffer } from "../src/buffer.js";
  3. import { BufferReader, BufferWriter, serialize, deserialize } from "../src/serialization.js";
  4. function roundtrip(data: any): any {
  5. const writer = new BufferWriter();
  6. serialize(writer, data);
  7. const reader = new BufferReader(writer.buffer);
  8. return deserialize(reader);
  9. }
  10. describe("serialization", () => {
  11. it("should roundtrip undefined", () => {
  12. expect(roundtrip(undefined)).toBeUndefined();
  13. });
  14. it("should roundtrip strings", () => {
  15. expect(roundtrip("")).toBe("");
  16. expect(roundtrip("hello")).toBe("hello");
  17. expect(roundtrip("你好🌍")).toBe("你好🌍");
  18. });
  19. it("should roundtrip integers", () => {
  20. expect(roundtrip(0)).toBe(0);
  21. expect(roundtrip(1)).toBe(1);
  22. expect(roundtrip(127)).toBe(127);
  23. expect(roundtrip(128)).toBe(128);
  24. expect(roundtrip(65535)).toBe(65535);
  25. });
  26. it("should roundtrip arrays", () => {
  27. expect(roundtrip([])).toEqual([]);
  28. expect(roundtrip([1, 2, 3])).toEqual([1, 2, 3]);
  29. expect(roundtrip(["a", "b"])).toEqual(["a", "b"]);
  30. });
  31. it("should roundtrip nested arrays", () => {
  32. expect(roundtrip([1, [2, 3], "four"])).toEqual([1, [2, 3], "four"]);
  33. });
  34. it("should roundtrip objects via JSON fallback", () => {
  35. expect(roundtrip({ a: 1, b: "two" })).toEqual({ a: 1, b: "two" });
  36. });
  37. it("should roundtrip VSBuffer", () => {
  38. const original = VSBuffer.fromString("binary data");
  39. const result = roundtrip(original);
  40. expect(result).toBeInstanceOf(VSBuffer);
  41. expect(result.toString()).toBe("binary data");
  42. });
  43. it("should roundtrip Uint8Array", () => {
  44. const original = new Uint8Array([1, 2, 3, 4]);
  45. const result = roundtrip(original);
  46. expect(result).toBeInstanceOf(Uint8Array);
  47. expect([...result]).toEqual([1, 2, 3, 4]);
  48. });
  49. it("should roundtrip floats via JSON fallback", () => {
  50. expect(roundtrip(3.14)).toBeCloseTo(3.14);
  51. });
  52. it("should roundtrip booleans via JSON fallback", () => {
  53. expect(roundtrip(true)).toBe(true);
  54. expect(roundtrip(false)).toBe(false);
  55. });
  56. it("should handle multiple serialize/deserialize in sequence", () => {
  57. const writer = new BufferWriter();
  58. serialize(writer, "hello");
  59. serialize(writer, 42);
  60. serialize(writer, [1, 2]);
  61. const reader = new BufferReader(writer.buffer);
  62. expect(deserialize(reader)).toBe("hello");
  63. expect(deserialize(reader)).toBe(42);
  64. expect(deserialize(reader)).toEqual([1, 2]);
  65. });
  66. });
  67. describe("BufferReader / BufferWriter", () => {
  68. it("should read sequentially", () => {
  69. const buf = VSBuffer.fromString("abcdef");
  70. const reader = new BufferReader(buf);
  71. expect(reader.read(3).toString()).toBe("abc");
  72. expect(reader.read(3).toString()).toBe("def");
  73. });
  74. it("BufferWriter should concat all writes", () => {
  75. const writer = new BufferWriter();
  76. writer.write(VSBuffer.fromString("hello"));
  77. writer.write(VSBuffer.fromString(" world"));
  78. expect(writer.buffer.toString()).toBe("hello world");
  79. });
  80. });