device: the client of new Device() and setMqttClient() warn once
2.0 ignored the first argument of the Device constructor in silence and had removed setMqttClient(), so a 1.x call threw a TypeError. Both are deprecated shims now, as the design lists them: setMqttClient() is back and does nothing, a client given to the constructor is ignored, and each says once in a process what to do. The line goes to the log hook of the bridge; a device on no bridge has none and emits a DeprecationWarning (ALEX2NODE_DEVICE_CLIENT, ALEX2NODE_SET_MQTT_CLIENT). null and undefined as the client warn nothing: the bridge passes null. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
parent
29ee1a315e
commit
2f6730eb52
9 changed files with 188 additions and 5 deletions
46
test/compat/deprecations.test.js
Normal file
46
test/compat/deprecations.test.js
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
"use strict";
|
||||
// What 1.x took and 2.0 ignores: said once, through the log hook of the bridge or as a warning of the process.
|
||||
const { test } = require("node:test");
|
||||
const assert = require("node:assert/strict");
|
||||
const { Alex2MQTT, Device, MemoryPublisher, PowerController } = require("alex2node");
|
||||
|
||||
test("new Device(client, ...): the client is ignored, and the process is warned once", async () => {
|
||||
const warnings = [];
|
||||
const listen = (warning) => warnings.push(warning);
|
||||
process.on("warning", listen);
|
||||
try {
|
||||
new Device(null, "root", "Lamp", "lamp-1", ["LIGHT"]);
|
||||
new Device(undefined, "root", "Lamp", "lamp-2", ["LIGHT"]);
|
||||
const client = { publish() { throw new Error("the client of 1.x is not published through"); } };
|
||||
const lamp = new Device(client, "root", "Lamp", "lamp-3", ["LIGHT"]);
|
||||
new Device(client, "root", "Lamp", "lamp-4", ["LIGHT"]);
|
||||
// A warning is emitted on the next tick
|
||||
await new Promise((resolve) => setImmediate(resolve));
|
||||
|
||||
assert.deepEqual(warnings.map(({ name, code }) => ({ name, code })), [{ name: "DeprecationWarning", code: "ALEX2NODE_DEVICE_CLIENT" }]);
|
||||
assert.match(warnings[0].message, /^new Device\(client, \.\.\.\) ignores the client since 2\.0: .* or set device\.publisher$/);
|
||||
assert.equal(lamp.publisher, null);
|
||||
assert.equal(await lamp.getStatusMessage("ct", true).addPowerControllerProp(PowerController.ON).send(), "");
|
||||
} finally {
|
||||
process.off("warning", listen);
|
||||
}
|
||||
});
|
||||
|
||||
test("setMqttClient(): does nothing, and the log hook of the bridge hears it once", async () => {
|
||||
const sent = new MemoryPublisher();
|
||||
const logged = [];
|
||||
const bridge = new Alex2MQTT("user", "password", "root", false, { publisher: sent, log: (message) => logged.push(message) });
|
||||
const lamp = bridge.registerDevice("Lamp", "lamp-1", null);
|
||||
const plug = bridge.registerDevice("Plug", "plug-1", null);
|
||||
|
||||
lamp.setMqttClient({ publish() { throw new Error("the client of 1.x is not published through"); } });
|
||||
plug.setMqttClient(null);
|
||||
lamp.setMqttClient(null);
|
||||
|
||||
assert.deepEqual(logged.filter((line) => line.includes("setMqttClient")), [
|
||||
"warning: device.setMqttClient() does nothing since 2.0: a device publishes through its bridge, over disconnect() and "
|
||||
+ "connect() too. Remove the call, or set device.publisher to publish somewhere else",
|
||||
]);
|
||||
assert.equal(await lamp.getStatusMessage("ct", true).addPowerControllerProp(PowerController.ON).send(), "root/lamp-1/alexaResponce");
|
||||
assert.equal(sent.published.length, 1);
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue