Get basic test up and passing

This replaces the 'ws' library with built-in websockets so that we can mock a server and successfully connect to it. A simple test verifies that Unit is handling a message request and response as we expect it to.
This commit is contained in:
2025-01-13 13:06:58 -06:00
parent 6c79e35e04
commit f8e2aa0f3e
10 changed files with 176 additions and 111 deletions

View File

@ -1,3 +1,45 @@
describe("empty test", () => {
it("passes", () => {});
import { Unit } from "../src/index";
import * as messages from "../src/messages/messages";
import WS from "jest-websocket-mock";
function makeid(length: number) {
const characters =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
const charactersLength = characters.length;
let result = "";
let counter = 0;
while (counter < length) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
counter += 1;
}
return result;
}
describe("basic message tests", () => {
let unit: Unit;
let mockServer: WS;
beforeEach(async () => {
mockServer = new WS("ws://localhost:6680");
unit = new Unit("localhost", 6680);
await unit.connect();
await mockServer.connected;
});
afterEach(async () => {
unit.close();
WS.clean();
});
it("can send a message and return its response", async () => {
const msg = messages.GetBodyStatus();
const sender = unit.send(msg);
await expect(mockServer).toReceiveMessage(JSON.stringify(msg));
const response = { messageID: msg.messageID, command: makeid(8) };
mockServer.send(JSON.stringify(response));
const clientResp = await sender;
expect(clientResp).toEqual(response);
});
});