expect(result).toBeInstanceOf(Int32Array);
expect(Array.from(result)).toEqual([100, -200, 300]);
});
it("should round-trip Float64Array", () => {
const arr = new Float64Array([1.1, 2.2, 3.3]);
const result = roundTrip(arr);
expect(result).toBeInstanceOf(Float64Array);
expect(Array.from(result)).toEqual([1.1, 2.2, 3.3]);
});
it("should round-trip ArrayBuffer", () => {
const buf = new Uint8Array([10, 20, 30]).buffer;
const result = roundTrip(buf);
expect(result).toBeInstanceOf(ArrayBuffer);
expect(Array.from(new Uint8Array(result))).toEqual([10, 20, 30]);
});
it("should round-trip empty Uint8Array", () => {
const arr = new Uint8Array([]);
const result = roundTrip(arr);
expect(result).toBeInstanceOf(Uint8Array);
expect(result.length).toBe(0);
});
});
// ── Objects and Arrays ──────────────────────────────────────────────
describe("syncToBuffer / syncFromBuffer - Objects and Arrays", () => {
it("should round-trip plain object", () => {
const obj = { a: 1, b: "hello", c: true };
const result = roundTrip(obj);
expect(result).toEqual({ a: 1, b: "hello", c: true });
});
it("should round-trip nested objects", () => {
const obj = { outer: { inner: { deep: 42 } } };
const result = roundTrip(obj);
expect(result.outer.inner.deep).toBe(42);
});
it("should round-trip array", () => {
const arr = [1, "two", true, null];
const result = roundTrip(arr);
expect(result).toEqual([1, "two", true, null]);
});
it("should round-trip nested arrays", () => {
const arr = [
[1, 2],
[3, [4, 5]],
];
const result = roundTrip(arr);
expect(result).toEqual([
[1, 2],
[3, [4, 5]],
]);
});
it("should round-trip empty object", () => {
expect(roundTrip({})).toEqual({});
});
it("should round-trip empty array", () => {
expect(roundTrip([])).toEqual([]);
});
it("should round-trip object with mixed value types", () => {
const obj = {
str: "hello",
num: 42,
bool: false,
nil: null,
undef: undefined,
date: new Date("2026-01-01T00:00:00.000Z"),
regex: /test/i,
bigint: BigInt(123),
sym: Symbol.for("test"),
url: new URL("https://example.com"),
set: new Set([1, 2]),
map: new Map([["k", "v"]]),
};
const result = roundTrip(obj);
expect(result.str).toBe("hello");
expect(result.num).toBe(42);
expect(result.bool).toBe(false);
expect(result.nil).toBe(null);
expect(result.undef).toBeUndefined();
expect(result.date).toBeInstanceOf(Date);
expect(result.date.toISOString()).toBe("2026-01-01T00:00:00.000Z");
expect(result.regex).toBeInstanceOf(RegExp);
expect(result.regex.source).toBe("test");
expect(result.bigint).toBe(BigInt(123));
expect(result.sym).toBe(Symbol.for("test"));
expect(result.url).toBeInstanceOf(URL);
expect(result.set).toBeInstanceOf(Set);
expect(result.set.size).toBe(2);
expect(result.map).toBeInstanceOf(Map);
expect(result.map.get("k")).toBe("v");
});
});
// ── Map and Set with complex values ─────────────────────────────────
describe("syncToBuffer / syncFromBuffer - Complex Map/Set", () => {
it("should round-trip Map with Date values", () => {
const map = new Map([
["created", new Date("2026-01-01T00:00:00.000Z")],
["updated", new Date("2026-03-25T12:00:00.000Z")],
]);
const result = roundTrip(map);
expect(result).toBeInstanceOf(Map);
expect(result.get("created")).toBeInstanceOf(Date);
expect(result.get("updated")).toBeInstanceOf(Date);
expect(result.get("created").toISOString()).toBe(
"2026-01-01T00:00:00.000Z"
);
});
it("should round-trip Set with mixed types", () => {
const set = new Set(["a", 1, true, null]);
const result = roundTrip(set);
expect(result).toBeInstanceOf(Set);
expect(result.size).toBe(4);
expect(result.has("a")).toBe(true);
expect(result.has(null)).toBe(true);
});
it("should round-trip nested Map inside object", () => {
const obj = {
data: new Map([["key", { nested: true }]]),
};
const result = roundTrip(obj);
expect(result.data).toBeInstanceOf(Map);
expect(result.data.get("key")).toEqual({ nested: true });
});
});
// ── Async types (remain as Promises) ────────────────────────────────
describe("syncToBuffer / syncFromBuffer - Async Types", () => {
it("should serialize a Promise and deserialize as a Promise", async () => {
const value = { data: Promise.resolve(42) };
const buffer = syncToBuffer(value);
const result = syncFromBuffer(buffer);
// The promise reference becomes a Promise in the output
expect(result.data).toBeDefined();
expect(typeof result.data.then).toBe("function");
// The promise resolves asynchronously (the resolution chunk is NOT
// in the sync buffer), so it will remain pending.
// We just verify it's a thenable.
});
it("should handle a root-level resolved value (not a Promise)", () => {
const buffer = syncToBuffer("simple");
const result = syncFromBuffer(buffer);
expect(result).toBe("simple");
});
});
// ── Server References ───────────────────────────────────────────────
describe("syncToBuffer / syncFromBuffer - Server References", () => {
it("should round-trip a server reference", () => {
function myAction() {}
const ref = registerServerReference(myAction, "module", "myAction");
const buffer = syncToBuffer(ref);
const result = syncFromBuffer(buffer, {
callServer: () => Promise.resolve(),
});
// Server references are deserialized as functions
expect(typeof result).toBe("function");
});
it("should round-trip a server reference with bound args", () => {
function myAction() {}
const ref = registerServerReference(myAction, "module", "myAction");
const boundRef = ref.bind(null, 1, "two", true);
const buffer = syncToBuffer(boundRef);
const result = syncFromBuffer(buffer, {
callServer: () => Promise.resolve(),
});
expect(typeof result).toBe("function");
});
});
// ── Edge cases ──────────────────────────────────────────────────────
describe("syncToBuffer / syncFromBuffer - Edge Cases", () => {
it("should produce a Uint8Array from syncToBuffer", () => {
const buffer = syncToBuffer({ test: true });
expect(buffer).toBeInstanceOf(Uint8Array);
expect(buffer.length).toBeGreaterThan(0);
});
it("should accept ArrayBuffer in syncFromBuffer", () => {