colabbd/src/protocol.test.ts
Jared Miller 00c9a140c6
Fix type assertion in protocol decode test
Use 'as const' on the type field to ensure the literal type "join" is
preserved, matching the ClientMessage union type requirements.
2026-01-27 21:04:17 -05:00

21 lines
657 B
TypeScript

import { describe, expect, test } from "bun:test";
import { decode, encode } from "./protocol";
describe("protocol", () => {
test("encode produces valid json", () => {
const msg = { type: "sync" as const, data: [1, 2, 3] };
const encoded = encode(msg);
expect(JSON.parse(encoded)).toEqual(msg);
});
test("decode parses valid json", () => {
const msg = { type: "join" as const, room: "test" };
const decoded = decode(JSON.stringify(msg));
expect(decoded).toEqual(msg);
});
test("decode returns null for invalid json", () => {
expect(decode("not json")).toBeNull();
expect(decode("{broken")).toBeNull();
});
});