Alex2Node/test/connection.test.js
David 58869d44c1 test: split the 1.5.2 suite into a harness and per-topic files
test/helpers/harness.js holds the aedes broker, the Alexa-side watcher, until() and the cleanups, plus directive(),
connected() and setup(), which the old file spelled out in each test. test/alex2node.test.js (194 lines, 8 tests)
becomes connection, discovery, directives, change-report and typings.

The 50-line round-trip test is cut at its topic boundaries and the duplicate-endpointId test no longer carries the
thermostat discovery check, so a failure names a case: 8 tests become 15, every 1.5.2 assertion kept. Four checks
are stricter: the "directive" event is compared whole, the StateReport is checked for its token and properties, the
Celsius conversion covers lowerSetpoint as well as temperature, and a duplicate endpointId is listed once.

No change to src/ or dist/. npm test: 15 pass in 0.9 s.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-28 13:45:38 +00:00

83 lines
4.6 KiB
JavaScript

"use strict";
// The broker connection: the default host, an outage reported as events that never crash the host, disconnect() then
// connect(), and a publish that fails without rejecting.
const { test } = require("node:test");
const assert = require("node:assert/strict");
const { Alex2MQTT, AlexaInterfaceType, DisplayCategory, PowerController, DEFAULT_HOST } = require("../dist/index.js");
const { broker, setup, connected, directive, until, sleep, cleanup } = require("./helpers/harness.js");
// Nothing listens on port 1, so every attempt is refused at once.
const DEAD_BROKER = { host: "mqtt://127.0.0.1:1", mqtt: { reconnectPeriod: 50, connectTimeout: 200 } };
test("defaults: the 1.4.0 host stays the default; options.host overrides it", () => {
assert.equal(DEFAULT_HOST, "mqtt://Alex2MQTT.stormysdream.club:1883");
assert.equal(new Alex2MQTT("u", "p", "root").getHost(), DEFAULT_HOST);
assert.equal(new Alex2MQTT("u", "p", "root", false, { host: "mqtt://127.0.0.1:1" }).getHost(), "mqtt://127.0.0.1:1");
});
test("a dead broker: 'offline'/'error' events when listened to, and NO crash when nobody listens (1.4.0 killed the host)", async () => {
const unheard = new Alex2MQTT("u", "p", "root", false, DEAD_BROKER);
unheard.connect();
cleanup(() => unheard.disconnect());
await sleep(300);
assert.equal(unheard.connected, false, "still alive, not connected");
const seen = [];
const heard = new Alex2MQTT("u", "p", "root", false, DEAD_BROKER);
heard.on("error", (e) => seen.push(e.code || e.message));
heard.on("offline", () => seen.push("offline"));
heard.on("reconnect", () => seen.push("reconnect"));
heard.connect();
cleanup(() => heard.disconnect());
await until(() => seen.includes("ECONNREFUSED") && seen.includes("reconnect"), 3000, "error + reconnect events");
});
test("registerDevice before connect throws (unchanged from 1.4.0)", () => {
assert.throws(() => new Alex2MQTT("u", "p", "root").registerDevice("x", "1", null), /connect before/);
});
test("disconnect() then connect(): devices registered before still answer (1.5.1 left them on the closed client)", async () => {
const { alexa, bridge } = await setup(Alex2MQTT, "root");
const lamp = bridge.registerDevice("Lamp", "lamp-1", DisplayCategory.LIGHT);
lamp.addCapability(AlexaInterfaceType.POWER_CONTROLLER);
lamp.on("Event", (d) => {
lamp.getStatusMessage(d.header.correlationToken, true).addPowerControllerProp(PowerController.ON).send();
});
const turnOn = (token) => alexa.send("root", directive("Alexa.PowerController", "TurnOn", "lamp-1", token));
const tokens = () => alexa.on("root/lamp-1/alexaResponce").map((m) => m.event.header.correlationToken);
const answered = (token) => tokens().includes(token);
turnOn("before");
await until(() => answered("before"), 3000, "response before disconnect");
await bridge.disconnect();
assert.equal(bridge.connected, false);
await connected(bridge);
assert.equal(bridge.getDevices().length, 1, "the devices stay registered");
turnOn("after");
await until(() => answered("after"), 3000, "response after disconnect() + connect()");
});
test("1.5.2: a failed publish never rejects send() - it resolves \"\" and reaches the bridge's error listener", async () => {
const { url } = await broker();
const bridge = new Alex2MQTT("u", "p", "root-send", false, { host: url });
const errors = [];
bridge.on("error", (e) => errors.push(e.message));
await connected(bridge);
const lamp = bridge.registerDevice("Lamp", "lamp-send", null);
lamp.addCapability(AlexaInterfaceType.POWER_CONTROLLER);
await bridge.disconnect(); // the device still holds the ended client until the next connect(): every publish now fails
const status = await lamp.getStatusMessage("ct", true).addPowerControllerProp(PowerController.ON).send();
const error = await lamp.getErrorMessage("ct").send();
const scene = await lamp.sendSceneResponse("ct", true);
const change = await lamp.getChangeReport("PHYSICAL_INTERACTION").addPowerControllerProp(PowerController.OFF).send();
assert.deepEqual([status, error, scene, change], ["", "", "", ""], "each resolves \"\" instead of rejecting");
assert.ok(errors.length >= 4, `every failure reached the error listener: ${errors.join(" | ")}`);
// With no listener the same failure is swallowed: an "error" event without a listener would throw.
const quiet = await connected(new Alex2MQTT("u", "p", "root-send-quiet", false, { host: url }));
const q = quiet.registerDevice("Q", "q-1", null);
await quiet.disconnect();
assert.equal(await q.getStatusMessage("ct", true).addPowerControllerProp(PowerController.ON).send(), "");
});