Alex2Node/test/transport.test.js
David d12767f9a7 bridge: the discovery answer is published through send()
The answer on <root>/discover_r called the publisher directly; a publisher
that threw or rejected was only caught by the handler meant for listeners
of "discover". It now takes the path of every device message: send()
resolves, and a failure goes to the "error" event when somebody listens.
A test covers a publish that fails, throws and rejects, with and without
an "error" listener: receive() resolves each time and "discover" is not
emitted.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-28 21:15:47 +00:00

91 lines
4.5 KiB
JavaScript

"use strict";
// Publishing without a broker: MemoryPublisher under a device, a device on no bridge, and the topics by name.
const { test } = require("node:test");
const assert = require("node:assert/strict");
const { Alex2MQTT, MemoryPublisher, PowerController, topics } = require("alex2node");
const { endpoint } = require("./helpers/endpoint.js");
test("the topics of the contract, \"alexaResponce\" spelt as the backend spells it", () => {
assert.equal(topics.discoverReply("root"), "root/discover_r");
assert.equal(topics.response("root", "lamp-1"), "root/lamp-1/alexaResponce");
assert.equal(topics.deferred("root", "lamp-1"), "root/lamp-1/deferredResponse");
assert.equal(topics.changeReport("root"), "root/changeReport");
});
test("MemoryPublisher: what a device sends, by topic, as a subscriber parses it", async () => {
const lamp = endpoint("lamp-1");
const sent = lamp.publisher = new MemoryPublisher();
assert.equal(await lamp.getStatusMessage("ct", true).addPowerControllerProp(PowerController.ON).send(), "root/lamp-1/alexaResponce");
assert.equal(await lamp.getErrorMessage("ct").send(true), "root/lamp-1/deferredResponse");
assert.equal(await lamp.sendSceneResponse("ct", true), "root/lamp-1/alexaResponce");
assert.equal(await lamp.getChangeReport().addPowerControllerProp(PowerController.OFF).send(), "root/changeReport");
assert.deepEqual(sent.published.map(({ topic, message }) => [topic, message.event.header.name]), [
["root/lamp-1/alexaResponce", "Response"],
["root/lamp-1/deferredResponse", "ErrorResponse"],
["root/lamp-1/alexaResponce", "ActivationStarted"],
["root/changeReport", "ChangeReport"],
]);
assert.deepEqual(await sent.publish("root/x", { a: 1, b: undefined }), { ok: true, topic: "root/x" });
assert.deepEqual(sent.published[4].message, { a: 1 });
});
test("MemoryPublisher.failWith: send() resolves \"\" and the error is reported, nothing is kept", async () => {
const lamp = endpoint("lamp-1");
const sent = lamp.publisher = new MemoryPublisher();
sent.failWith = new Error("broker gone");
const reported = [];
lamp.onPublishError = (err) => reported.push(err.message);
assert.equal(await lamp.sendSceneResponse("ct", false), "");
assert.equal(await lamp.getErrorMessage("ct").send(), "");
assert.deepEqual(reported, ["broker gone", "broker gone"]);
assert.deepEqual(sent.published, []);
});
test("a device on no bridge: send() resolves \"\" and the error says to register it", async () => {
const lamp = endpoint("lamp-1");
const reported = [];
lamp.onPublishError = (err) => reported.push(err.message);
assert.equal(await lamp.getStatusMessage("ct", true).send(), "");
assert.deepEqual(reported, [
"nothing was published to root/lamp-1/alexaResponce: the device is on no bridge, register it with addDevice() or registerDevice()",
]);
});
test("unregisterDevice and clearDevices take the publisher from the device", () => {
const bridge = new Alex2MQTT("u", "p", "root");
const lamp = bridge.registerDevice("Lamp", "lamp-1", null);
const plug = bridge.registerDevice("Plug", "plug-1", null);
assert.ok(lamp.publisher && lamp.publisher === plug.publisher, "one publisher for the devices of a bridge");
bridge.unregisterDevice("lamp-1");
assert.equal(lamp.publisher, null);
bridge.clearDevices();
assert.equal(plug.publisher, null);
});
test("a discovery answer that cannot be published: receive() resolves and the \"error\" event says why", async () => {
const gone = new MemoryPublisher();
gone.failWith = new Error("broker gone");
const publishers = {
"fails": gone,
"throws": { publish() { throw new Error("publisher threw"); } },
"rejects": { publish: () => Promise.reject(new Error("publisher rejected")) },
};
const expected = { "fails": "broker gone", "throws": "publisher threw", "rejects": "publisher rejected" };
for (const [how, publisher] of Object.entries(publishers)) {
const bridge = new Alex2MQTT("user", "password", "root", false, { publisher });
bridge.addDevice({ endpointId: "lamp-1", name: "Lamp", categories: ["LIGHT"] }).add(PowerController);
const errors = [];
const discovered = [];
bridge.on("error", (err) => errors.push(err.message));
bridge.on("discover", (n) => discovered.push(n));
await bridge.receive("root/discover", "");
assert.deepEqual(errors, [expected[how]], how);
assert.deepEqual(discovered, [], how);
// Without a listener the error has nowhere to go, and still nothing is thrown or rejected
bridge.removeAllListeners("error");
await bridge.receive("root/discover", "");
}
});