add integration test with mock server
This commit is contained in:
@@ -0,0 +1,14 @@
|
|||||||
|
import { createSocket, RemoteInfo } from "node:dgram";
|
||||||
|
export const mockServer = (onMessage: (msg: DataView) => Uint8Array[]) => {
|
||||||
|
const sock = createSocket("udp4");
|
||||||
|
const SEND_PORT = 32108;
|
||||||
|
sock.bind(SEND_PORT);
|
||||||
|
sock.on("message", (msg, rinfo: RemoteInfo) => {
|
||||||
|
const dv = new DataView(new Uint8Array(msg).buffer);
|
||||||
|
onMessage(dv).forEach((out) => {
|
||||||
|
sock.send(out, rinfo.port, rinfo.address);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
return sock;
|
||||||
|
};
|
||||||
+2
-7
@@ -2,9 +2,9 @@ import "../shim.ts";
|
|||||||
|
|
||||||
import assert from "assert";
|
import assert from "assert";
|
||||||
|
|
||||||
import { XqBytesDec, XqBytesEnc } from "../func_replacements.js";
|
|
||||||
import { hexdump } from "../hexdump.js";
|
import { hexdump } from "../hexdump.js";
|
||||||
import { SendStartVideo, SendDevStatus, SendUsrChk, SendWifiDetails, parse_PunchPkt } from "../impl.ts";
|
import { XqBytesDec, XqBytesEnc } from "../func_replacements.js";
|
||||||
|
import { parse_PunchPkt, SendDevStatus, SendStartVideo, SendUsrChk, SendWifiDetails } from "../impl.ts";
|
||||||
import { placeholderTypes, sprintf } from "../utils.js";
|
import { placeholderTypes, sprintf } from "../utils.js";
|
||||||
|
|
||||||
describe("debug_tools", () => {
|
describe("debug_tools", () => {
|
||||||
@@ -122,11 +122,6 @@ describe("module", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const hstrToBA = (hs) => new Uint8Array(hs.match(/../g).map((h) => parseInt(h, 16))).buffer;
|
const hstrToBA = (hs) => new Uint8Array(hs.match(/../g).map((h) => parseInt(h, 16))).buffer;
|
||||||
describe("events", () => {
|
|
||||||
it("emits login event upon logging in", () => {
|
|
||||||
// TODO
|
|
||||||
});
|
|
||||||
});
|
|
||||||
describe("parse packet", () => {
|
describe("parse packet", () => {
|
||||||
it("parses PunchPkt", () => {
|
it("parses PunchPkt", () => {
|
||||||
const in_pkt_str = "f14100144241544400000000000262ca574f4e4a4d000000";
|
const in_pkt_str = "f14100144241544400000000000262ca574f4e4a4d000000";
|
||||||
|
|||||||
@@ -0,0 +1,33 @@
|
|||||||
|
import { mockServer } from "../mock_server.js";
|
||||||
|
import { discoverDevices } from "../discovery.js";
|
||||||
|
import { buildLogger } from "../logger.js";
|
||||||
|
import { Commands } from "../datatypes.js";
|
||||||
|
|
||||||
|
import assert from "assert";
|
||||||
|
|
||||||
|
const hstrToU8 = (hs) => new Uint8Array(hs.match(/../g).map((h) => parseInt(h, 16)));
|
||||||
|
describe("integration", () => {
|
||||||
|
it("emits login event upon logging in", () => {
|
||||||
|
// TODO
|
||||||
|
});
|
||||||
|
|
||||||
|
it("discovers a device", () => {
|
||||||
|
buildLogger("trace");
|
||||||
|
const EXPECTED_SERIAL = "BATD156362WONJM";
|
||||||
|
const punchPkt = "f14100144241544400000000000262ca574f4e4a4d000000";
|
||||||
|
const mockSock = mockServer((msg) => {
|
||||||
|
const cmd = msg.readU16();
|
||||||
|
if (cmd == Commands.LanSearch) {
|
||||||
|
const buf = hstrToU8(punchPkt);
|
||||||
|
return [buf];
|
||||||
|
}
|
||||||
|
return [];
|
||||||
|
});
|
||||||
|
const ev = discoverDevices("127.0.0.1");
|
||||||
|
ev.on("discover", (rinfo, dev) => {
|
||||||
|
assert.deepEqual(dev.devId, EXPECTED_SERIAL);
|
||||||
|
ev.emit("close");
|
||||||
|
mockSock.close();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user