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>
This commit is contained in:
David 2026-09-28 13:45:38 +00:00
parent 275f00f8a7
commit 58869d44c1
7 changed files with 447 additions and 194 deletions

89
test/directives.test.js Normal file
View file

@ -0,0 +1,89 @@
"use strict";
// Directives: a message on <root>/<endpointId>/alexaDirective reaches the device's "Event" or "ReportState" listener,
// and what the device answers is published on <root>/<endpointId>/alexaResponce.
const { test } = require("node:test");
const assert = require("node:assert/strict");
const {
Alex2MQTT, AlexaInterfaceType, DisplayCategory, PowerController, EndpointHealth, TemperatureSensorScale,
} = require("../dist/index.js");
const { setup, directive, until } = require("./helpers/harness.js");
// A light that answers every directive with its new power state and ReportState with "off at 0 %".
function diningRoomLight(bridge) {
const lamp = bridge.registerDevice("Dining Room Light", "5020AA", DisplayCategory.LIGHT);
lamp.addCapability(AlexaInterfaceType.POWER_CONTROLLER, { proactivelyReported: true });
lamp.addCapability(AlexaInterfaceType.BRIGHTNESS_CONTROLLER);
lamp.on("Event", (request) => {
lamp.getStatusMessage(request.header.correlationToken, true)
.addHealthProp(EndpointHealth.OK)
.addPowerControllerProp(request.header.name === "TurnOn" ? PowerController.ON : PowerController.OFF)
.addBrightnessControllerProp(100)
.send(false);
});
lamp.on("ReportState", (request) => {
lamp.getStatusMessage(request.header.correlationToken)
.addPowerControllerProp(PowerController.OFF)
.addBrightnessControllerProp(0)
.send(false);
});
return lamp;
}
test("a directive reaches the \"Event\" listener and is answered with a Response carrying its correlationToken", async () => {
const { alexa, bridge } = await setup(Alex2MQTT, "root");
const dispatched = [];
bridge.on("directive", (info) => dispatched.push(info));
diningRoomLight(bridge);
alexa.send("root", directive("Alexa.PowerController", "TurnOn", "5020AA", "ct1"));
await until(() => alexa.on("root/5020AA/alexaResponce").length === 1, 3000, "directive response");
const [response] = alexa.on("root/5020AA/alexaResponce");
assert.equal(response.event.header.name, "Response");
assert.equal(response.event.header.correlationToken, "ct1");
assert.equal(response.context.properties.find((p) => p.name === "powerState").value, "ON");
assert.deepEqual(dispatched, [{ endpointId: "5020AA", namespace: "Alexa.PowerController", name: "TurnOn" }]);
});
test("ReportState reaches the \"ReportState\" listener and is answered with a StateReport", async () => {
const { alexa, bridge } = await setup(Alex2MQTT, "root");
diningRoomLight(bridge);
alexa.send("root", directive("Alexa", "ReportState", "5020AA", "ct2"));
await until(() => alexa.on("root/5020AA/alexaResponce").length === 1, 3000, "state report");
const [report] = alexa.on("root/5020AA/alexaResponce");
assert.equal(report.event.header.name, "StateReport");
assert.equal(report.event.header.correlationToken, "ct2");
assert.deepEqual(report.context.properties.map((p) => [p.name, p.value]), [["powerState", "OFF"], ["brightness", 0]]);
});
test("SceneController: Activate is answered with ActivationStarted", async () => {
const { alexa, bridge } = await setup(Alex2MQTT, "root");
const scene = bridge.registerDevice("Evening", "scene-1", DisplayCategory.SCENE_TRIGGER);
scene.addCapability(AlexaInterfaceType.SCENE_CONTROLLER);
scene.on("Event", (request) => {
scene.sendSceneResponse(request.header.correlationToken, request.header.name === "Activate");
});
alexa.send("root", directive("Alexa.SceneController", "Activate", "scene-1", "ct3"));
await until(() => alexa.on("root/scene-1/alexaResponce").length === 1, 3000, "scene response");
const [started] = alexa.on("root/scene-1/alexaResponce");
assert.equal(started.event.header.namespace, "Alexa.SceneController");
assert.equal(started.event.header.name, "ActivationStarted");
assert.equal(started.event.payload.cause.type, "VOICE_INTERACTION");
assert.ok(started.event.payload.timestamp);
});
test("the temperature helpers convert Fahrenheit to Celsius (as 1.4.0 did)", async () => {
const { bridge } = await setup(Alex2MQTT, "root");
const lamp = diningRoomLight(bridge);
const message = lamp.getStatusMessage("x")
.addTemperatureSensorProp(TemperatureSensorScale.FAHRENHEIT, 68)
.addThermostatControllerProp("lowerSetpoint", TemperatureSensorScale.FAHRENHEIT, 68)
.toJSON();
const toOneDecimal = (n) => Math.round(n * 10) / 10;
for (const name of ["temperature", "lowerSetpoint"]) {
const { value } = message.context.properties.find((p) => p.name === name);
assert.equal(toOneDecimal(value.value), 20, name);
assert.equal(value.scale, "CELSIUS", name);
}
});