Use 'as const' on the type field to ensure the literal type "join" is preserved, matching the ClientMessage union type requirements.
21 lines
657 B
TypeScript
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();
|
|
});
|
|
});
|