test: read an ErrorResponse off the broker; every answer has its own messageId
Step 2 replaced uuid by crypto.randomUUID() at three call sites (AlexaStatusMessage, AlexaErrorResponse, Device.sendSceneResponse) and only the first was asserted, in the ES module test. With the messageId dropped or set to a constant in the other two, the suite still passed 22/22, and no test read an ErrorResponse at all. New: an ErrorResponse compared whole against the documented shape (header, endpoint, payload, no context), and one test that sends two directives each to a light, an unreachable light and a scene and requires six different version 4 UUIDs. The Response, StateReport, scene and change report tests check the messageId too; the pattern lives in the harness. Checked in a scratch copy: a missing or constant messageId at each of the three sites now fails the suite (the missing one in AlexaStatusMessage does not compile). No change to src/ or dist/. npm test: 24 pass in 5.5 s. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
parent
1d88c7811a
commit
f74773132a
4 changed files with 78 additions and 12 deletions
|
|
@ -4,7 +4,7 @@
|
|||
const { test } = require("node:test");
|
||||
const assert = require("node:assert/strict");
|
||||
const { Alex2MQTT, AlexaInterfaceType, DisplayCategory, PowerController } = require("alex2node");
|
||||
const { setup, until } = require("./helpers/harness.js");
|
||||
const { setup, until, UUID_V4 } = require("./helpers/harness.js");
|
||||
|
||||
test("a change report carries the changed properties in payload.change and the rest in context", async () => {
|
||||
const { alexa, bridge } = await setup(Alex2MQTT, "root");
|
||||
|
|
@ -22,6 +22,7 @@ test("a change report carries the changed properties in payload.change and the r
|
|||
await until(() => alexa.on("root/changeReport").length === 1, 3000, "change report");
|
||||
const [report] = alexa.on("root/changeReport");
|
||||
assert.equal(report.event.header.name, "ChangeReport");
|
||||
assert.match(report.event.header.messageId, UUID_V4);
|
||||
assert.equal(report.event.endpoint.endpointId, "5020AA");
|
||||
assert.equal(report.event.payload.change.cause.type, "PHYSICAL_INTERACTION");
|
||||
assert.deepEqual(report.event.payload.change.properties.map((p) => [p.name, p.value]), [["powerState", "OFF"]]);
|
||||
|
|
|
|||
|
|
@ -4,9 +4,9 @@
|
|||
const { test } = require("node:test");
|
||||
const assert = require("node:assert/strict");
|
||||
const {
|
||||
Alex2MQTT, AlexaInterfaceType, DisplayCategory, PowerController, EndpointHealth, TemperatureSensorScale,
|
||||
Alex2MQTT, AlexaErrorType, AlexaInterfaceType, DisplayCategory, PowerController, EndpointHealth, TemperatureSensorScale,
|
||||
} = require("alex2node");
|
||||
const { setup, directive, until } = require("./helpers/harness.js");
|
||||
const { setup, directive, until, UUID_V4 } = require("./helpers/harness.js");
|
||||
|
||||
// A light that answers every directive with its new power state and ReportState with "off at 0 %".
|
||||
function diningRoomLight(bridge) {
|
||||
|
|
@ -29,6 +29,28 @@ function diningRoomLight(bridge) {
|
|||
return lamp;
|
||||
}
|
||||
|
||||
// A light that is not there: every directive is answered with ENDPOINT_UNREACHABLE.
|
||||
function unpluggedLight(bridge) {
|
||||
const lamp = bridge.registerDevice("Garage Light", "5020AB", DisplayCategory.LIGHT);
|
||||
lamp.addCapability(AlexaInterfaceType.POWER_CONTROLLER);
|
||||
lamp.on("Event", (request) => {
|
||||
const error = lamp.getErrorMessage(request.header.correlationToken);
|
||||
error.setErrorMessage(AlexaErrorType.ENDPOINT_UNREACHABLE, "the light is unplugged");
|
||||
error.send();
|
||||
});
|
||||
return lamp;
|
||||
}
|
||||
|
||||
// A scene that answers Activate and Deactivate with the matching event.
|
||||
function eveningScene(bridge) {
|
||||
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");
|
||||
});
|
||||
return scene;
|
||||
}
|
||||
|
||||
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 = [];
|
||||
|
|
@ -40,6 +62,7 @@ test("a directive reaches the \"Event\" listener and is answered with a Response
|
|||
const [response] = alexa.on("root/5020AA/alexaResponce");
|
||||
assert.equal(response.event.header.name, "Response");
|
||||
assert.equal(response.event.header.correlationToken, "ct1");
|
||||
assert.match(response.event.header.messageId, UUID_V4);
|
||||
assert.equal(response.context.properties.find((p) => p.name === "powerState").value, "ON");
|
||||
assert.deepEqual(dispatched, [{ endpointId: "5020AA", namespace: "Alexa.PowerController", name: "TurnOn" }]);
|
||||
});
|
||||
|
|
@ -53,26 +76,65 @@ test("ReportState reaches the \"ReportState\" listener and is answered with a St
|
|||
const [report] = alexa.on("root/5020AA/alexaResponce");
|
||||
assert.equal(report.event.header.name, "StateReport");
|
||||
assert.equal(report.event.header.correlationToken, "ct2");
|
||||
assert.match(report.event.header.messageId, UUID_V4);
|
||||
assert.deepEqual(report.context.properties.map((p) => [p.name, p.value]), [["powerState", "OFF"], ["brightness", 0]]);
|
||||
});
|
||||
|
||||
test("getErrorMessage: the directive is answered with an ErrorResponse, the error type and message as its payload", async () => {
|
||||
const { alexa, bridge } = await setup(Alex2MQTT, "root");
|
||||
unpluggedLight(bridge);
|
||||
|
||||
alexa.send("root", directive("Alexa.PowerController", "TurnOn", "5020AB", "ct4"));
|
||||
await until(() => alexa.on("root/5020AB/alexaResponce").length === 1, 3000, "error response");
|
||||
const [answer] = alexa.on("root/5020AB/alexaResponce");
|
||||
const { messageId, ...header } = answer.event.header;
|
||||
assert.match(messageId, UUID_V4);
|
||||
// The event alone, without a context (Alexa.ErrorResponse, "Synchronous error response")
|
||||
assert.deepEqual({ ...answer, event: { ...answer.event, header } }, {
|
||||
event: {
|
||||
header: { namespace: "Alexa", name: "ErrorResponse", correlationToken: "ct4", payloadVersion: "3" },
|
||||
endpoint: { endpointId: "5020AB" },
|
||||
payload: { type: "ENDPOINT_UNREACHABLE", message: "the light is unplugged" },
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
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");
|
||||
});
|
||||
eveningScene(bridge);
|
||||
|
||||
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.match(started.event.header.messageId, UUID_V4);
|
||||
assert.equal(started.event.payload.cause.type, "VOICE_INTERACTION");
|
||||
assert.ok(started.event.payload.timestamp);
|
||||
});
|
||||
|
||||
test("no two answers share a messageId: Response, ErrorResponse and the scene events each get a new one", async () => {
|
||||
const { alexa, bridge } = await setup(Alex2MQTT, "root");
|
||||
diningRoomLight(bridge);
|
||||
unpluggedLight(bridge);
|
||||
eveningScene(bridge);
|
||||
const answers = () => alexa.got.filter((m) => m.topic.endsWith("/alexaResponce")).map((m) => m.payload.event.header);
|
||||
|
||||
for (const token of ["first", "second"]) {
|
||||
alexa.send("root", directive("Alexa.PowerController", "TurnOn", "5020AA", token));
|
||||
alexa.send("root", directive("Alexa.PowerController", "TurnOn", "5020AB", token));
|
||||
alexa.send("root", directive("Alexa.SceneController", "Activate", "scene-1", token));
|
||||
}
|
||||
await until(() => answers().length === 6, 3000, "six answers");
|
||||
assert.deepEqual(
|
||||
answers().map((header) => header.name).sort(),
|
||||
["ActivationStarted", "ActivationStarted", "ErrorResponse", "ErrorResponse", "Response", "Response"]
|
||||
);
|
||||
const ids = answers().map((header) => header.messageId);
|
||||
for (const id of ids) assert.match(id, UUID_V4);
|
||||
assert.equal(new Set(ids).size, 6, `a messageId is used twice: ${ids.join(" ")}`);
|
||||
});
|
||||
|
||||
test("the temperature helpers convert Fahrenheit to Celsius (as 1.4.0 did)", async () => {
|
||||
const { bridge } = await setup(Alex2MQTT, "root");
|
||||
const lamp = diningRoomLight(bridge);
|
||||
|
|
|
|||
|
|
@ -22,6 +22,10 @@ function cleanup(fn) {
|
|||
|
||||
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
||||
|
||||
// What crypto.randomUUID() returns. Alexa wants a new messageId in every response and event and recommends a
|
||||
// version 4 UUID (Message guide, "Header object").
|
||||
const UUID_V4 = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;
|
||||
|
||||
/** Poll fn every 15 ms until it returns something truthy. Throws "timeout waiting for <what>" after ms. */
|
||||
async function until(fn, ms = 3000, what = "condition") {
|
||||
const start = Date.now();
|
||||
|
|
@ -102,4 +106,4 @@ async function setup(Alex2MQTT, root, options = {}) {
|
|||
return { url, alexa, bridge };
|
||||
}
|
||||
|
||||
module.exports = { broker, watcher, directive, connected, setup, until, sleep, cleanup };
|
||||
module.exports = { broker, watcher, directive, connected, setup, until, sleep, cleanup, UUID_V4 };
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import assert from "node:assert/strict";
|
|||
import { Alex2MQTT, AlexaInterfaceType, DisplayCategory, EndpointHealth, PowerController } from "alex2node";
|
||||
import harness from "./helpers/harness.js";
|
||||
|
||||
const { setup, directive, until } = harness;
|
||||
const { setup, directive, until, UUID_V4 } = harness;
|
||||
|
||||
test("import { Alex2MQTT } from \"alex2node\": discovery, a directive and a change report on the ES module build", async () => {
|
||||
const { alexa, bridge } = await setup(Alex2MQTT, "root");
|
||||
|
|
@ -30,8 +30,7 @@ test("import { Alex2MQTT } from \"alex2node\": discovery, a directive and a chan
|
|||
const [response] = alexa.on("root/lamp-1/alexaResponce");
|
||||
assert.equal(response.event.header.name, "Response");
|
||||
assert.equal(response.event.header.correlationToken, "ct1");
|
||||
// crypto.randomUUID() writes the messageId: a version 4 UUID, as uuid's v4() did
|
||||
assert.match(response.event.header.messageId, /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/);
|
||||
assert.match(response.event.header.messageId, UUID_V4);
|
||||
|
||||
assert.equal(await lamp.getChangeReport().addPowerControllerProp(PowerController.OFF).send(), "root/changeReport");
|
||||
await until(() => alexa.on("root/changeReport").length === 1, 3000, "change report");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue