import { describe, expect, it } from "vitest"; import { z } from "zod"; import { WebSocketServer } from "ws"; import { ApplicationRouter } from "../../../source/services/router.js"; import { WsTransportRouter, } from "../../../source/services/transport/ws-transport.js"; import { Logger } from "../../../source/utils/logger.js"; import { toExtendedJson } from "@xo-cash/utils"; describe("WebSocket request decoding", () => { it("decodes the minimal route-agnostic envelope and Extended JSON body", async () => { await expect( WsTransportRouter.decodeWebSocketRequest( toExtendedJson({ id: "request-1", path: "/data/write", body: { value: new Uint8Array([1, 2, 3]) }, }), ), ).resolves.toEqual({ requestId: "request-1", path: "/data/write", body: { value: new Uint8Array([1, 2, 3]) }, }); }); it.each([ "{}", '{"path":42}', '{"path":"/data/get","id":1}', '{"path":"/data/get","method":"POST"}', ])("rejects an invalid envelope: %s", async (payload) => { await expect(WsTransportRouter.decodeWebSocketRequest(payload)).rejects.toBeInstanceOf( z.ZodError, ); }); it("rejects malformed JSON", async () => { await expect(WsTransportRouter.decodeWebSocketRequest("{")).rejects.toMatchObject({ statusCode: 400, message: "Invalid JSON in WebSocket message", }); }); }); describe("WsTransportRouter payload limits", () => { it("configures Hono's ws server with the requested maxPayload", async () => { const debug = new Logger("ws-transport-test"); const router = await ApplicationRouter.create([]); const transport = new WsTransportRouter(router, debug, 1024); const wsServer = transport.websocketServer as unknown as WebSocketServer; expect(wsServer.options.maxPayload).toBe(1024); await transport.stop(); }); });