cc-libs/tools/mcp-bridge/test/protocol.test.ts

20 lines
963 B
TypeScript

import assert from "node:assert/strict";
import test from "node:test";
import { parseJsonFrame, parseLinkMessage } from "../src/protocol.js";
test("protocol validation accepts valid hello", () => {
const frame = parseJsonFrame(JSON.stringify({ type: "hello", computerId: 12, computerLabel: "base-turtle" }));
assert.deepEqual(parseLinkMessage(frame), { type: "hello", computerId: 12, computerLabel: "base-turtle" });
});
test("protocol validation normalizes empty label", () => {
const frame = parseJsonFrame(JSON.stringify({ type: "hello", computerId: 12, computerLabel: "" }));
assert.deepEqual(parseLinkMessage(frame), { type: "hello", computerId: 12, computerLabel: null });
});
test("protocol validation rejects invalid frames", () => {
assert.equal(parseJsonFrame("not json"), null);
assert.equal(parseLinkMessage({ type: "hello", computerId: "12" }), null);
assert.equal(parseLinkMessage({ type: "response", id: 1, ok: true }), null);
});