2019-09-09 10:48:09 -07:00
|
|
|
require('./stub/logger');
|
|
|
|
require('./stub/data');
|
|
|
|
const data = require('../lib/util/data.js');
|
2019-12-09 10:27:39 -07:00
|
|
|
const utils = require('../lib/util/utils.js');
|
2019-03-09 09:15:20 -07:00
|
|
|
const settings = require('../lib/util/settings.js');
|
2019-09-09 10:48:09 -07:00
|
|
|
const fs = require('fs');
|
2019-03-09 09:15:20 -07:00
|
|
|
const configurationFile = data.joinPath('configuration.yaml');
|
|
|
|
const devicesFile = data.joinPath('devices.yaml');
|
|
|
|
const groupsFile = data.joinPath('groups.yaml');
|
2019-10-25 09:43:14 -07:00
|
|
|
const secretFile = data.joinPath('secret.yaml');
|
2019-09-09 10:48:09 -07:00
|
|
|
const yaml = require('js-yaml');
|
2019-02-24 07:49:41 -07:00
|
|
|
|
|
|
|
describe('Settings', () => {
|
2019-10-25 09:43:14 -07:00
|
|
|
const write = (file, json, reread=true) => {
|
2019-09-09 10:48:09 -07:00
|
|
|
fs.writeFileSync(file, yaml.safeDump(json))
|
2019-10-25 09:43:14 -07:00
|
|
|
if (reread) {
|
|
|
|
settings._reRead();
|
|
|
|
}
|
2019-09-09 10:48:09 -07:00
|
|
|
};
|
|
|
|
const read = (file) => yaml.safeLoad(fs.readFileSync(file, 'utf8'));
|
|
|
|
const remove = (file) => {
|
|
|
|
if (fs.existsSync(file)) fs.unlinkSync(file);
|
|
|
|
}
|
2019-02-24 07:49:41 -07:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2019-09-09 10:48:09 -07:00
|
|
|
remove(configurationFile);
|
|
|
|
remove(devicesFile);
|
|
|
|
remove(groupsFile);
|
2019-02-24 07:49:41 -07:00
|
|
|
});
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
it('Should return default settings', () => {
|
|
|
|
write(configurationFile, {});
|
|
|
|
const s = settings.get();
|
2019-09-16 11:03:51 -07:00
|
|
|
const expected = settings._getDefaults();
|
|
|
|
expected.devices = {};
|
|
|
|
expected.groups = {};
|
|
|
|
expect(s).toStrictEqual(expected);
|
2019-02-24 07:49:41 -07:00
|
|
|
});
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
it('Should return settings', () => {
|
|
|
|
write(configurationFile, {permit_join: true});
|
|
|
|
const s = settings.get();
|
|
|
|
const expected = settings._getDefaults();
|
2019-09-16 11:03:51 -07:00
|
|
|
expected.devices = {};
|
|
|
|
expected.groups = {};
|
2019-09-09 10:48:09 -07:00
|
|
|
expected.permit_join = true;
|
|
|
|
expect(s).toStrictEqual(expected);
|
|
|
|
});
|
2019-02-24 07:49:41 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
it('Should add devices', () => {
|
|
|
|
write(configurationFile, {});
|
|
|
|
settings.addDevice('0x12345678');
|
2019-02-24 07:49:41 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
const actual = read(configurationFile);
|
|
|
|
const expected = {
|
|
|
|
devices: {
|
|
|
|
'0x12345678': {
|
|
|
|
friendly_name: '0x12345678',
|
2019-02-24 07:49:41 -07:00
|
|
|
},
|
2019-09-09 10:48:09 -07:00
|
|
|
},
|
|
|
|
};
|
2019-02-24 07:49:41 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
expect(actual).toStrictEqual(expected);
|
|
|
|
});
|
2019-02-24 07:49:41 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
it('Should read devices', () => {
|
|
|
|
const content = {
|
|
|
|
devices: {
|
|
|
|
'0x12345678': {
|
|
|
|
friendly_name: '0x12345678',
|
|
|
|
retain: false,
|
2019-02-24 07:49:41 -07:00
|
|
|
},
|
2019-09-09 10:48:09 -07:00
|
|
|
},
|
|
|
|
};
|
2019-02-24 07:49:41 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
write(configurationFile, content);
|
2019-02-24 07:49:41 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
const device = settings.getDevice('0x12345678');
|
|
|
|
const expected = {
|
|
|
|
ID: "0x12345678",
|
|
|
|
friendlyName: "0x12345678",
|
|
|
|
friendly_name: '0x12345678',
|
|
|
|
retain: false,
|
|
|
|
};
|
2019-02-24 07:49:41 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
expect(device).toStrictEqual(expected);
|
|
|
|
});
|
2019-02-24 07:49:41 -07:00
|
|
|
|
2019-09-16 10:49:49 -07:00
|
|
|
it('Should not throw error when devices is null', () => {
|
|
|
|
const content = {devices: null};
|
|
|
|
write(configurationFile, content);
|
|
|
|
settings.getDevice('0x12345678');
|
|
|
|
});
|
|
|
|
|
2019-11-11 09:45:11 -07:00
|
|
|
it('Should read MQTT username and password form a separate file', () => {
|
2019-10-25 09:43:14 -07:00
|
|
|
const contentConfiguration = {
|
|
|
|
mqtt: {
|
|
|
|
server: 'my.mqtt.server',
|
|
|
|
user: '!secret username',
|
|
|
|
password: '!secret password',
|
|
|
|
},
|
2019-10-28 10:05:50 -07:00
|
|
|
advanced: {
|
|
|
|
network_key: '!secret network_key'
|
|
|
|
}
|
2019-10-25 09:43:14 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
const contentSecret = {
|
|
|
|
username: 'mysecretusername',
|
|
|
|
password: 'mysecretpassword',
|
2019-10-28 10:05:50 -07:00
|
|
|
network_key: [1,2,3],
|
2019-10-25 09:43:14 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
write(secretFile, contentSecret, false);
|
|
|
|
write(configurationFile, contentConfiguration);
|
|
|
|
|
|
|
|
const expected = {
|
|
|
|
include_device_information: false,
|
|
|
|
password: "mysecretpassword",
|
|
|
|
server: "my.mqtt.server",
|
|
|
|
user: "mysecretusername",
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(settings.get().mqtt).toStrictEqual(expected);
|
2019-10-28 10:05:50 -07:00
|
|
|
expect(settings.get().advanced.network_key).toStrictEqual([1,2,3]);
|
2019-10-25 10:17:47 -07:00
|
|
|
|
|
|
|
settings._write();
|
|
|
|
expect(read(configurationFile)).toStrictEqual(contentConfiguration);
|
2019-10-25 09:43:14 -07:00
|
|
|
});
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
it('Should read devices form a separate file', () => {
|
|
|
|
const contentConfiguration = {
|
|
|
|
devices: 'devices.yaml',
|
|
|
|
};
|
2019-02-24 07:49:41 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
const contentDevices = {
|
|
|
|
'0x12345678': {
|
|
|
|
friendly_name: '0x12345678',
|
|
|
|
retain: false,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
write(configurationFile, contentConfiguration);
|
|
|
|
write(devicesFile, contentDevices);
|
|
|
|
const device = settings.getDevice('0x12345678');
|
|
|
|
const expected = {
|
|
|
|
ID: "0x12345678",
|
|
|
|
friendlyName: "0x12345678",
|
|
|
|
friendly_name: '0x12345678',
|
|
|
|
retain: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(device).toStrictEqual(expected);
|
|
|
|
});
|
2019-02-24 07:49:41 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
it('Should add devices to a separate file', () => {
|
|
|
|
const contentConfiguration = {
|
|
|
|
devices: 'devices.yaml',
|
|
|
|
};
|
2019-02-24 07:49:41 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
const contentDevices = {
|
|
|
|
'0x12345678': {
|
2019-02-24 07:49:41 -07:00
|
|
|
friendly_name: '0x12345678',
|
|
|
|
retain: false,
|
2019-09-09 10:48:09 -07:00
|
|
|
},
|
|
|
|
};
|
2019-02-24 07:49:41 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
write(configurationFile, contentConfiguration);
|
|
|
|
write(devicesFile, contentDevices);
|
2019-02-24 07:49:41 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
settings.addDevice('0x1234');
|
2019-02-24 07:49:41 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
expect(read(configurationFile)).toStrictEqual({devices: 'devices.yaml'});
|
2019-02-24 07:49:41 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
const expected = {
|
|
|
|
'0x12345678': {
|
|
|
|
friendly_name: '0x12345678',
|
|
|
|
retain: false,
|
|
|
|
},
|
|
|
|
'0x1234': {
|
|
|
|
friendly_name: '0x1234',
|
|
|
|
},
|
|
|
|
};
|
2019-02-24 07:49:41 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
expect(read(devicesFile)).toStrictEqual(expected);
|
|
|
|
});
|
2019-02-24 07:49:41 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
it('Should add devices to a separate file if devices.yaml doesnt exist', () => {
|
|
|
|
const contentConfiguration = {
|
|
|
|
devices: 'devices.yaml',
|
|
|
|
};
|
2019-02-24 07:49:41 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
write(configurationFile, contentConfiguration);
|
2019-02-24 07:49:41 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
settings.addDevice('0x1234');
|
2019-02-24 07:49:41 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
expect(read(configurationFile)).toStrictEqual({devices: 'devices.yaml'});
|
2019-02-24 07:49:41 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
const expected = {
|
|
|
|
'0x1234': {
|
|
|
|
friendly_name: '0x1234',
|
|
|
|
},
|
|
|
|
};
|
2019-02-24 07:49:41 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
expect(read(devicesFile)).toStrictEqual(expected);
|
|
|
|
}
|
|
|
|
);
|
2019-02-24 07:49:41 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
it('Should add and remove devices to a separate file if devices.yaml doesnt exist', () => {
|
|
|
|
const contentConfiguration = {
|
|
|
|
devices: 'devices.yaml',
|
|
|
|
};
|
2019-02-24 07:49:41 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
write(configurationFile, contentConfiguration);
|
2019-02-24 07:49:41 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
settings.addDevice('0x1234');
|
|
|
|
expect(read(configurationFile)).toStrictEqual({devices: 'devices.yaml'});
|
2019-02-24 07:49:41 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
settings.removeDevice('0x1234');
|
|
|
|
expect(read(configurationFile)).toStrictEqual({devices: 'devices.yaml'});
|
2019-02-24 07:49:41 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
expect(read(devicesFile)).toStrictEqual({});
|
|
|
|
}
|
|
|
|
);
|
2019-02-24 07:49:41 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
it('Should read groups', () => {
|
|
|
|
const content = {
|
|
|
|
groups: {
|
|
|
|
'1': {
|
|
|
|
friendly_name: '123',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
2019-02-24 07:49:41 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
write(configurationFile, content);
|
2019-02-24 07:49:41 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
const group = settings.getGroup('1');
|
|
|
|
const expected = {
|
|
|
|
ID: 1,
|
|
|
|
friendlyName: '123',
|
|
|
|
friendly_name: '123',
|
|
|
|
devices: [],
|
2019-09-29 05:35:05 -07:00
|
|
|
optimistic: true,
|
2019-09-09 10:48:09 -07:00
|
|
|
};
|
2019-02-24 07:49:41 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
expect(group).toStrictEqual(expected);
|
|
|
|
});
|
2019-02-24 07:49:41 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
it('Should read groups from a separate file', () => {
|
|
|
|
const contentConfiguration = {
|
|
|
|
groups: 'groups.yaml',
|
|
|
|
};
|
2019-02-24 07:49:41 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
const contentGroups = {
|
|
|
|
'1': {
|
2019-02-24 07:49:41 -07:00
|
|
|
friendly_name: '123',
|
2019-09-09 10:48:09 -07:00
|
|
|
},
|
|
|
|
};
|
2019-02-24 07:49:41 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
write(configurationFile, contentConfiguration);
|
|
|
|
write(groupsFile, contentGroups);
|
2019-02-24 07:49:41 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
const group = settings.getGroup('1');
|
|
|
|
const expected = {
|
|
|
|
ID: 1,
|
|
|
|
friendlyName: '123',
|
|
|
|
friendly_name: '123',
|
|
|
|
devices: [],
|
2019-09-29 05:35:05 -07:00
|
|
|
optimistic: true,
|
2019-09-09 10:48:09 -07:00
|
|
|
};
|
2019-02-24 07:49:41 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
expect(group).toStrictEqual(expected);
|
|
|
|
});
|
2019-02-24 07:49:41 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
it('Combine everything! groups and devices from separate file :)', () => {
|
|
|
|
const contentConfiguration = {
|
|
|
|
devices: 'devices.yaml',
|
|
|
|
groups: 'groups.yaml',
|
|
|
|
};
|
2019-02-24 07:49:41 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
const contentGroups = {
|
|
|
|
'1': {
|
2019-02-24 07:49:41 -07:00
|
|
|
friendly_name: '123',
|
2019-04-29 11:38:40 -07:00
|
|
|
devices: [],
|
2019-09-09 10:48:09 -07:00
|
|
|
},
|
|
|
|
};
|
|
|
|
write(configurationFile, contentConfiguration);
|
|
|
|
write(groupsFile, contentGroups);
|
2019-02-24 07:49:41 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
const expectedConfiguration = {
|
|
|
|
devices: 'devices.yaml',
|
|
|
|
groups: 'groups.yaml',
|
|
|
|
};
|
2019-02-24 07:49:41 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
expect(read(configurationFile)).toStrictEqual(expectedConfiguration);
|
2019-02-24 07:49:41 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
settings.addDevice('0x1234');
|
2019-02-24 07:49:41 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
expect(read(configurationFile)).toStrictEqual(expectedConfiguration);
|
2019-02-24 07:49:41 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
const expectedDevice = {
|
|
|
|
'0x1234': {
|
|
|
|
friendly_name: '0x1234',
|
|
|
|
},
|
|
|
|
};
|
2019-02-24 07:49:41 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
expect(read(devicesFile)).toStrictEqual(expectedDevice);
|
2019-02-24 07:49:41 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
const group = settings.getGroup('1');
|
|
|
|
const expectedGroup = {
|
2019-09-29 05:35:05 -07:00
|
|
|
optimistic: true,
|
2019-09-09 10:48:09 -07:00
|
|
|
ID: 1,
|
|
|
|
friendlyName: '123',
|
|
|
|
friendly_name: '123',
|
|
|
|
devices: [],
|
|
|
|
};
|
2019-02-24 07:49:41 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
expect(group).toStrictEqual(expectedGroup);
|
2019-02-24 07:49:41 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
expect(read(configurationFile)).toStrictEqual(expectedConfiguration);
|
2019-02-24 07:49:41 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
const expectedDevice2 = {
|
|
|
|
ID: '0x1234',
|
|
|
|
friendlyName: '0x1234',
|
|
|
|
friendly_name: '0x1234',
|
|
|
|
};
|
2019-02-24 07:49:41 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
expect(settings.getDevice('0x1234')).toStrictEqual(expectedDevice2);
|
|
|
|
});
|
2019-02-24 07:49:41 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
it('Should add groups', () => {
|
|
|
|
write(configurationFile, {});
|
2019-02-24 07:49:41 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
const added = settings.addGroup('test123');
|
|
|
|
const expected = {
|
|
|
|
'1': {
|
|
|
|
friendly_name: 'test123',
|
2019-11-04 09:59:00 -07:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(settings.get().groups).toStrictEqual(expected);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Should add groups with specific ID', () => {
|
|
|
|
write(configurationFile, {});
|
|
|
|
|
|
|
|
const added = settings.addGroup('test123', 123);
|
|
|
|
const expected = {
|
|
|
|
'123': {
|
|
|
|
friendly_name: 'test123',
|
2019-09-09 10:48:09 -07:00
|
|
|
},
|
|
|
|
};
|
2019-02-24 07:49:41 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
expect(settings.get().groups).toStrictEqual(expected);
|
|
|
|
});
|
2019-02-24 07:49:41 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
it('Should not add duplicate groups', () => {
|
|
|
|
write(configurationFile, {});
|
2019-06-19 09:35:57 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
settings.addGroup('test123');
|
|
|
|
expect(() => {
|
|
|
|
settings.addGroup('test123');
|
2019-09-27 13:54:07 -07:00
|
|
|
}).toThrow(new Error("friendly_name 'test123' is already in use"));
|
2019-09-09 10:48:09 -07:00
|
|
|
const expected = {
|
|
|
|
'1': {
|
|
|
|
friendly_name: 'test123',
|
2019-11-04 09:59:00 -07:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(settings.get().groups).toStrictEqual(expected);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Should not add duplicate groups with specific ID', () => {
|
|
|
|
write(configurationFile, {});
|
|
|
|
|
|
|
|
settings.addGroup('test123', 123);
|
|
|
|
expect(() => {
|
|
|
|
settings.addGroup('test_id_123', 123);
|
|
|
|
}).toThrow(new Error("group id '123' is already in use"));
|
|
|
|
const expected = {
|
|
|
|
'123': {
|
|
|
|
friendly_name: 'test123',
|
2019-09-09 10:48:09 -07:00
|
|
|
},
|
|
|
|
};
|
2019-06-19 09:35:57 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
expect(settings.get().groups).toStrictEqual(expected);
|
|
|
|
});
|
2019-06-19 09:35:57 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
it('Should add devices to groups', () => {
|
|
|
|
write(configurationFile, {
|
|
|
|
devices: {
|
|
|
|
'0x123': {
|
|
|
|
friendly_name: 'bulb',
|
|
|
|
retain: true,
|
|
|
|
}
|
|
|
|
}
|
2019-06-19 09:35:57 -07:00
|
|
|
});
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
settings.addGroup('test123');
|
|
|
|
settings.addDeviceToGroup('test123', ['0x123']);
|
|
|
|
const expected = {
|
|
|
|
'1': {
|
|
|
|
friendly_name: 'test123',
|
|
|
|
devices: ['0x123'],
|
|
|
|
},
|
|
|
|
};
|
2019-06-19 09:35:57 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
expect(settings.get().groups).toStrictEqual(expected);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Should remove devices from groups', () => {
|
|
|
|
write(configurationFile, {
|
|
|
|
devices: {
|
|
|
|
'0x123': {
|
|
|
|
friendly_name: 'bulb',
|
|
|
|
retain: true,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
groups: {
|
2019-06-19 09:35:57 -07:00
|
|
|
'1': {
|
|
|
|
friendly_name: 'test123',
|
2019-09-09 10:48:09 -07:00
|
|
|
devices: ['0x123'],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
settings.removeDeviceFromGroup('test123', ['0x123']);
|
|
|
|
const expected = {
|
|
|
|
'1': {
|
|
|
|
friendly_name: 'test123',
|
|
|
|
devices: [],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(settings.get().groups).toStrictEqual(expected);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Should throw when adding device to non-existing group', () => {
|
|
|
|
write(configurationFile, {
|
|
|
|
devices: {
|
|
|
|
'0x123': {
|
|
|
|
friendly_name: 'bulb',
|
|
|
|
retain: true,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(() => {
|
|
|
|
settings.removeDeviceFromGroup('test123', 'bulb')
|
|
|
|
}).toThrow(new Error("Group 'test123' does not exist"));
|
|
|
|
});
|
2019-06-19 09:35:57 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
it('Should throw when adding device which already exists', () => {
|
|
|
|
write(configurationFile, {
|
|
|
|
devices: {
|
|
|
|
'0x123': {
|
|
|
|
friendly_name: 'bulb',
|
|
|
|
retain: true,
|
|
|
|
}
|
|
|
|
},
|
2019-06-19 09:35:57 -07:00
|
|
|
});
|
2019-09-09 10:48:09 -07:00
|
|
|
|
|
|
|
expect(() => {
|
|
|
|
settings.addDevice('0x123')
|
|
|
|
}).toThrow(new Error("Device '0x123' already exists"));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Should ban devices', () => {
|
|
|
|
write(configurationFile, {});
|
|
|
|
settings.banDevice('0x123');
|
|
|
|
expect(settings.get().ban).toStrictEqual(['0x123']);
|
|
|
|
settings.banDevice('0x1234');
|
|
|
|
expect(settings.get().ban).toStrictEqual(['0x123', '0x1234']);
|
2019-02-24 07:49:41 -07:00
|
|
|
});
|
2019-09-11 12:42:44 -07:00
|
|
|
|
|
|
|
it('Should throw error when yaml file is invalid', () => {
|
|
|
|
fs.writeFileSync(configurationFile, `
|
|
|
|
good: 9
|
|
|
|
\t wrong
|
|
|
|
`)
|
|
|
|
|
|
|
|
let error;
|
|
|
|
try {
|
|
|
|
settings._reRead();
|
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(error.message).toContain(`Your YAML file '${configurationFile}' is invalid`);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Should throw error when yaml file does not exist', () => {
|
|
|
|
let error;
|
|
|
|
try {
|
|
|
|
settings._reRead();
|
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(error.message).toContain(`no such file or directory, open`);
|
|
|
|
});
|
2019-09-27 13:54:07 -07:00
|
|
|
|
|
|
|
it('Configuration shouldnt be valid when duplicate friendly_name are used', async () => {
|
|
|
|
write(configurationFile, {
|
|
|
|
devices: {'0x0017880104e45519': {friendly_name: 'myname', retain: false}},
|
|
|
|
groups: {'1': {friendly_name: 'myname', retain: false}},
|
|
|
|
});
|
|
|
|
|
|
|
|
settings._reRead();
|
|
|
|
|
|
|
|
expect(() => {
|
|
|
|
settings.validate();
|
|
|
|
}).toThrowError(`Duplicate friendly_name 'myname' found`);
|
|
|
|
});
|
|
|
|
|
2019-12-09 10:27:39 -07:00
|
|
|
it('Configuration shouldnt be valid when friendly_name is a postfix', async () => {
|
|
|
|
write(configurationFile, {
|
|
|
|
devices: {'0x0017880104e45519': {friendly_name: 'left', retain: false}},
|
|
|
|
});
|
|
|
|
|
|
|
|
settings._reRead();
|
|
|
|
|
|
|
|
expect(() => {
|
|
|
|
settings.validate();
|
|
|
|
}).toThrowError(`Following friendly_name are not allowed: '${utils.getPostfixes()}'`);
|
|
|
|
});
|
|
|
|
|
2019-09-27 13:54:07 -07:00
|
|
|
it('Configuration shouldnt be valid when duplicate friendly_name are used', async () => {
|
|
|
|
write(configurationFile, {
|
|
|
|
devices: {
|
|
|
|
'0x0017880104e45519': {friendly_name: 'myname', retain: false},
|
|
|
|
'0x0017880104e45511': {friendly_name: 'myname1', retain: false}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
settings._reRead();
|
|
|
|
|
|
|
|
expect(() => {
|
|
|
|
settings.changeFriendlyName('myname1', 'myname');
|
|
|
|
}).toThrowError(`friendly_name 'myname' is already in use`);
|
|
|
|
});
|
2019-10-03 11:06:31 -07:00
|
|
|
|
2020-02-23 13:51:30 -07:00
|
|
|
it('Should throw when removing device which doesnt exist', async () => {
|
|
|
|
write(configurationFile, {
|
|
|
|
devices: {
|
|
|
|
'0x0017880104e45519': {friendly_name: 'myname', retain: false},
|
|
|
|
'0x0017880104e45511': {friendly_name: 'myname1', retain: false}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
settings._reRead();
|
|
|
|
|
|
|
|
expect(() => {
|
|
|
|
settings.removeDevice('myname33');
|
|
|
|
}).toThrowError(`Device 'myname33' does not exist`);
|
|
|
|
});
|
|
|
|
|
2019-10-03 11:06:31 -07:00
|
|
|
it('Shouldnt write to configuration.yaml when there are no changes in it', () => {
|
|
|
|
const contentConfiguration = {devices: 'devices.yaml'};
|
|
|
|
const contentDevices = {};
|
|
|
|
write(configurationFile, contentConfiguration);
|
|
|
|
const before = fs.statSync(configurationFile).mtimeMs;
|
|
|
|
write(devicesFile, contentDevices);
|
|
|
|
settings.addDevice('0x1234');
|
|
|
|
const after = fs.statSync(configurationFile).mtimeMs;
|
|
|
|
expect(before).toBe(after);
|
|
|
|
});
|
2019-02-24 07:49:41 -07:00
|
|
|
});
|