"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); });