import assert from "node:assert/strict"; import { once } from "node:events"; import test from "node:test"; import WebSocket from "ws"; import { MAX_MESSAGE_BYTES, WEBSOCKET_SUBPROTOCOL } from "../src/protocol"; import { json } from "../test/fixtures"; import { control, startImage } from "./server"; test( "Rust server accepts fragmented text at the exact message limit and handles ping", { timeout: 10000 }, async (t) => { const image = await startImage(); t.after(() => image.stop()); const socket = new WebSocket(`ws://${image.address}/`, WEBSOCKET_SUBPROTOCOL); t.after(() => socket.terminate()); await once(socket, "open"); assert.equal(socket.protocol, WEBSOCKET_SUBPROTOCOL); const request = json({ version: 4, request_id: "fragmented", command: { operation: "list_actors" }, }); const text = request + " ".repeat(MAX_MESSAGE_BYTES - Buffer.byteLength(request)); const reply = once(socket, "message"); socket.send(text.slice(0, 100), { fin: false }); socket.send(text.slice(100), { fin: true }); const [response] = await reply; assert.deepEqual(JSON.parse(String(response)), { version: 4, request_id: "fragmented", result: { Ok: { kind: "actors", data: [] } }, }); const pong = once(socket, "pong"); socket.ping("runa-debug"); const [payload] = await pong; assert.equal(String(payload), "runa-debug"); }, ); test( "Rust server rejects an oversized fragmented aggregate and keeps the image available", { timeout: 10000 }, async (t) => { const image = await startImage(); t.after(() => image.stop()); const socket = new WebSocket(`ws://${image.address}/`, WEBSOCKET_SUBPROTOCOL); t.after(() => socket.terminate()); await once(socket, "open"); const closed = once(socket, "close"); socket.send(" ".repeat(MAX_MESSAGE_BYTES), { fin: false }); socket.send(" ", { fin: true }); await closed; assert.deepEqual(await control(image.address, { operation: "list_actors" }), { version: 4, request_id: "integration-control", result: { Ok: { kind: "actors", data: [] } }, }); }, );