UNPKG

zwave-js

Version:

Z-Wave driver written entirely in JavaScript/TypeScript

349 lines (348 loc) 13.9 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var mocks_exports = {}; __export(mocks_exports, { MockRequestMessageWithExpectation: () => MockRequestMessageWithExpectation, MockRequestMessageWithoutExpectation: () => MockRequestMessageWithoutExpectation, MockResponseMessage: () => MockResponseMessage, createEmptyMockDriver: () => createEmptyMockDriver, createTestEndpoint: () => createTestEndpoint, createTestNode: () => createTestNode, mockDriverDummyCallbackId: () => mockDriverDummyCallbackId }); module.exports = __toCommonJS(mocks_exports); var import_tslib = require("tslib"); var import_cc = require("@zwave-js/cc"); var import_config = require("@zwave-js/config"); var import_core = require("@zwave-js/core"); var import_serial = require("@zwave-js/serial"); var import_serialapi = require("@zwave-js/serial/serialapi"); var import_sinon = __toESM(require("sinon"), 1); var nodeUtils = __toESM(require("../node/utils.js"), 1); const MockRequestMessageWithExpectation_FunctionType = 250; const MockRequestMessageWithoutExpectation_FunctionType = 251; const MockResponseMessage_FunctionType = 255; let MockRequestMessageWithExpectation = (() => { let _classDecorators = [(0, import_serial.messageTypes)(import_serial.MessageType.Request, MockRequestMessageWithExpectation_FunctionType), (0, import_serial.expectedResponse)(MockResponseMessage_FunctionType), (0, import_serial.priority)(import_core.MessagePriority.Normal)]; let _classDescriptor; let _classExtraInitializers = []; let _classThis; let _classSuper = import_serial.Message; var MockRequestMessageWithExpectation2 = class extends _classSuper { static { __name(this, "MockRequestMessageWithExpectation"); } static { _classThis = this; } static { const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0; (0, import_tslib.__esDecorate)(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers); MockRequestMessageWithExpectation2 = _classThis = _classDescriptor.value; if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata }); (0, import_tslib.__runInitializers)(_classThis, _classExtraInitializers); } }; return MockRequestMessageWithExpectation2 = _classThis; })(); let MockRequestMessageWithoutExpectation = (() => { let _classDecorators = [(0, import_serial.messageTypes)(import_serial.MessageType.Request, MockRequestMessageWithoutExpectation_FunctionType), (0, import_serial.priority)(import_core.MessagePriority.Normal)]; let _classDescriptor; let _classExtraInitializers = []; let _classThis; let _classSuper = import_serial.Message; var MockRequestMessageWithoutExpectation2 = class extends _classSuper { static { __name(this, "MockRequestMessageWithoutExpectation"); } static { _classThis = this; } static { const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0; (0, import_tslib.__esDecorate)(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers); MockRequestMessageWithoutExpectation2 = _classThis = _classDescriptor.value; if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata }); (0, import_tslib.__runInitializers)(_classThis, _classExtraInitializers); } }; return MockRequestMessageWithoutExpectation2 = _classThis; })(); let MockResponseMessage = (() => { let _classDecorators = [(0, import_serial.messageTypes)(import_serial.MessageType.Response, MockResponseMessage_FunctionType)]; let _classDescriptor; let _classExtraInitializers = []; let _classThis; let _classSuper = import_serial.Message; var MockResponseMessage2 = class extends _classSuper { static { __name(this, "MockResponseMessage"); } static { _classThis = this; } static { const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0; (0, import_tslib.__esDecorate)(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers); MockResponseMessage2 = _classThis = _classDescriptor.value; if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata }); (0, import_tslib.__runInitializers)(_classThis, _classExtraInitializers); } }; return MockResponseMessage2 = _classThis; })(); const mockDriverDummyCallbackId = 254; function createEmptyMockDriver() { const ret = { sendMessage: import_sinon.default.stub().callsFake(() => Promise.resolve()), sendCommand: import_sinon.default.stub(), getSupportedCCVersion: import_sinon.default.stub(), getSafeCCVersion: import_sinon.default.stub(), isCCSecure: import_sinon.default.stub().callsFake(() => false), getNextCallbackId: import_sinon.default.stub().callsFake(() => mockDriverDummyCallbackId), controller: { nodes: /* @__PURE__ */ new Map(), ownNodeId: 1 }, get nodes() { return ret.controller.nodes; }, get ownNodeId() { return ret.controller.ownNodeId; }, valueDB: /* @__PURE__ */ new Map(), getValueDB: /* @__PURE__ */ __name((nodeId) => { return ret.controller.nodes.get(nodeId).valueDB; }, "getValueDB"), metadataDB: /* @__PURE__ */ new Map(), networkCache: /* @__PURE__ */ new Map(), cacheGet: import_sinon.default.stub().callsFake((cacheKey, options) => { let _ret = ret.networkCache.get(cacheKey); if (_ret !== void 0 && typeof options?.reviver === "function") { try { _ret = options.reviver(_ret); } catch { } } return _ret; }), cacheSet: import_sinon.default.stub().callsFake((cacheKey, value, options) => { if (value !== void 0 && typeof options?.serializer === "function") { value = options.serializer(value); } if (value === void 0) { ret.networkCache.delete(cacheKey); } else { ret.networkCache.set(cacheKey, value); } }), options: { timeouts: { ack: 1e3, byte: 150, response: 1600, report: 1600, nonce: 5e3, sendDataCallback: 65e3 }, attempts: { sendData: 3, controller: 3 } }, driverLog: new Proxy({}, { get() { return () => { }; } }), controllerLog: new Proxy({}, { get() { return () => { }; } }), configManager: new import_config.ConfigManager(), getLogConfig: /* @__PURE__ */ __name(() => { return { enabled: false, level: "info" }; }, "getLogConfig") }; ret.sendCommand.callsFake(async (command, options) => { const msg = new import_serialapi.SendDataRequest({ command }); const resp = await ret.sendMessage(msg, options); return resp?.command; }); ret.getSupportedCCVersion.callsFake((ccId, nodeId, endpointIndex = 0) => { if (ret.controller?.nodes instanceof Map && ret.controller.nodes.has(nodeId)) { const node = ret.controller.nodes.get(nodeId); const ccVersion = node.getEndpoint(endpointIndex).getCCVersion(ccId); return ccVersion; } return 0; }); ret.getSafeCCVersion.callsFake((ccId, nodeId, endpointIndex = 0) => { return ret.getSupportedCCVersion(ccId, nodeId, endpointIndex) || (0, import_cc.getImplementedVersion)(ccId); }); return ret; } __name(createEmptyMockDriver, "createEmptyMockDriver"); function createTestNode(host, options) { const endpointCache = /* @__PURE__ */ new Map(); const securityClasses = /* @__PURE__ */ new Map(); const ret = { id: options.id, ...createTestEndpoint(host, { nodeId: options.id, index: 0, commandClasses: options.commandClasses }), isListening: options.isListening ?? true, isFrequentListening: options.isFrequentListening ?? false, get canSleep() { if (ret.isListening == void 0) return void 0; if (ret.isFrequentListening == void 0) return void 0; return !ret.isListening && !ret.isFrequentListening; }, setEndpoint: /* @__PURE__ */ __name((endpoint) => { endpointCache.set(endpoint.index, createTestEndpoint(host, { nodeId: options.id, index: endpoint.index, commandClasses: endpoint.commandClasses })); }, "setEndpoint"), getEndpoint: /* @__PURE__ */ __name(((index) => { if (index === 0) return ret; if (!endpointCache.has(index)) { if (!options.endpoints?.[index]) { return void 0; } ret.setEndpoint({ nodeId: options.id, index, commandClasses: options.endpoints[index].commandClasses }); } return endpointCache.get(index); }), "getEndpoint"), getEndpointOrThrow(index) { const ep = ret.getEndpoint(index); if (!ep) { throw new import_core.ZWaveError(`Endpoint ${index} does not exist on Node ${ret.id}`, import_core.ZWaveErrorCodes.Controller_EndpointNotFound); } return ep; }, // These are copied from Node.ts getHighestSecurityClass() { if (securityClasses.size === 0) return void 0; let missingSome = false; for (const secClass of import_core.securityClassOrder) { if (securityClasses.get(secClass) === true) return secClass; if (!securityClasses.has(secClass)) { missingSome = true; } } return missingSome ? void 0 : import_core.SecurityClass.None; }, hasSecurityClass(securityClass) { return securityClasses.get(securityClass); }, setSecurityClass(securityClass, granted) { securityClasses.set(securityClass, granted); }, get isSecure() { const securityClass = ret.getHighestSecurityClass(); if (securityClass == void 0) return import_core.NOT_KNOWN; if (securityClass === import_core.SecurityClass.None) return false; return true; } }; endpointCache.set(0, ret); if (options.endpoints) { nodeUtils.setIndividualEndpointCount(host, ret.id, Object.keys(options.endpoints).length); nodeUtils.setEndpointIndizes(host, ret.id, Object.keys(options.endpoints).map((index) => parseInt(index, 10))); nodeUtils.setAggregatedEndpointCount(host, ret.id, 0); nodeUtils.setMultiChannelInterviewComplete(host, ret.id, true); } return ret; } __name(createTestNode, "createTestNode"); function createTestEndpoint(host, options) { const ret = { virtual: false, nodeId: options.nodeId, index: options.index, supportsCC: /* @__PURE__ */ __name((cc) => { const ccInfo = options.commandClasses?.[cc]; if (!ccInfo) return false; return ccInfo.isSupported ?? true; }, "supportsCC"), controlsCC: /* @__PURE__ */ __name((cc) => { const ccInfo = options.commandClasses?.[cc]; if (!ccInfo) return false; return ccInfo.isControlled ?? false; }, "controlsCC"), isCCSecure: /* @__PURE__ */ __name((cc) => { const ccInfo = options.commandClasses?.[cc]; if (!ccInfo) return false; return ccInfo.secure ?? false; }, "isCCSecure"), getCCVersion: /* @__PURE__ */ __name((cc) => { const ccInfo = options.commandClasses?.[cc]; const defaultVersion = ccInfo?.isSupported ? (0, import_cc.getImplementedVersion)(cc) : 0; return ccInfo?.version ?? host.getNode(options.nodeId)?.getCCVersion(cc) ?? defaultVersion; }, "getCCVersion") }; return ret; } __name(createTestEndpoint, "createTestEndpoint"); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { MockRequestMessageWithExpectation, MockRequestMessageWithoutExpectation, MockResponseMessage, createEmptyMockDriver, createTestEndpoint, createTestNode, mockDriverDummyCallbackId }); //# sourceMappingURL=mocks.js.map