Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

thing-it-test

Package Overview
Dependencies
Maintainers
0
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

thing-it-test

Test and verification for all [thing-it-node] Plugins.

  • 0.1.6
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
773
increased by189.51%
Maintainers
0
Weekly downloads
 
Created
Source

thing-it-test

NPM NPM

Test and verification utility for [thing-it-node] Plugin development.

Device, Sensor and Actor Test

Setting up a Test

Install the package via

npm install thing-it-test

in the directory of your [thing-it-node] Plugin.

In your test program load the [thing-it] Test Driver

var testDriver = require("thing-it-test").createTestDriver({logLevel: "debug"});

Load your Device, Actor and Sensor Plugins, e.g.

testDriver.registerDevicePlugin(__dirname + "/dummyDevice");
testDriver.registerUnitPlugin(__dirname + "/dummyActor");
testDriver.registerUnitPlugin(__dirname + "/dummySensor");
  • note that you need to load Device Plugins before Unit Plugins - and create your configuration
var testConfiguration = {
    label: "Room 22",
    id: "room22",
    devices: [{
        id: "dummyDevice1",
        label: "Dummy Device 1",
        plugin: "dummy-device/dummyDevice",
        logLevel: "debug",
        configuration: {simulated: true, frequency: 5},
        actors: [{
            id: "dummyActor1",
            label: "Dummy Actor 1",
            type: "dummyActor",
            logLevel: "debug",
            configuration: {
            }
        }],
        sensors: [{
            id: "dummySensor1",
            label: "Dummy Sensor 1",
            type: "dummySensor",
            logLevel: "debug",
            configuration: {
                frequency: 5
            }
        }]
    }]
};

You can also load the configuration via require, e.g.

var testConfiguration = require("./dummyConfiguration")

Then run the test, e.g.

testDriver.start({
    configuration: testConfiguration,
    heartbeat: 10,
    simulated: true
}).then(function () {
    setTimeout(function () {
        testDriver.dummyDevice1.dummyActor1.on();
        testDriver.dummyDevice1.dummyActor1.off();
        testDriver.dummyDevice1.dummyActor1.toggle();

        testDriver.stop().then(function () {
        }).fail(function (error) {
            testDriver.logError("Could not stop Device: " + error);
        });
    }, 20000);
}).fail(function (error) {
    testDriver.logError("Could not start Device: " + error);
});

Find a full example here.

Using mocha

If you have installed mocha via

npm install mocha -g

you can define tests like

describe('[thing-it] Philips Hue Plugin', function () {
    var testDriver;

    before(function () {
        testDriver = require("thing-it-test").createTestDriver();

        testDriver.registerDevicePlugin(__dirname + "/../hueBridge");
        testDriver.registerUnitPlugin(__dirname + "/../default-units/lightBulb");
        testDriver.registerUnitPlugin(__dirname + "/../default-units/livingColorLamp");
    });
    describe('Start Configuration', function () {
        this.timeout(5000);

        it('should complete without error', function () {
            return testDriver.start({
                configuration: require("../examples/configuration.js"),
                heartbeat: 10,
                logLevel: "error"
            });
        });
    });
    describe('Service calls', function () {
        it('should complete without error', function (done) {
            testDriver.philipsHueBridge.lightBulbBedroom.setBrightnessPercent({brightnessPercent: 100});
            testDriver.philipsHueBridge.livingColorLampBar.setBrightnessPercent({brightnessPercent: 100});
            testDriver.philipsHueBridge.lightBulbBedroom.setBrightnessPercent({brightnessPercent: 0});
            testDriver.philipsHueBridge.livingColorLampBar.setRgbHex({rgbHex: "#FF0000"});

            done();
        });
    });
});

and run with mocha.

Using Listeners

Many test cases for [thing-it-node] Plugins require listening to the Device's and Actor's reactions to Service Calls or just listening to Sensor Events.

To cover this in test suites you can register event listeners with the test driver e.g. as in the following mocha code

describe('Brightness = 100', function () {
        this.timeout(5000);

        before(function () {
            testDriver.removeAllListeners();
        });
        it('should produce Actor State Change message', function (done) {
            testDriver.addListener({
                publishActorStateChange: function (device, actor, state) {
                    if (actor.id === "lightBulbBedroom" && device.id === "philipsHueBridge" && state.brightnessPercent === 100)
                    {
                        done();
                    }
                    else
                    {
                        done("Unexpected Actor State Change message.");
                    }
                }
            });

            testDriver.philipsHueBridge.lightBulbBedroom.setBrightnessPercent({brightnessPercent: 100});
        });
    });

Listeners are available for

  • publishMessage(message)
  • publishEvent(event)
  • publishDeviceStateChange(device, state)
  • publishDeviceOperationalStateChange(device, state)
  • publishDeviceStateChangeHistory(device, history)
  • publishActorStateChange(device, actor, state)
  • publishActorOperationalStateChange(device, actor, state)
  • publishActorStateChangeHistory(device, actor, history)
  • publishSensorStateChange(device, sensor, state)
  • publishSensorOperationalStateChange(device, sensor, state)
  • publishSensorStateChangeHistory(device, sensor, history)

Test Suite for Device UIs

descriptopn of ussage

Keywords

FAQs

Package last updated on 18 Sep 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc