2019-02-24 07:49:41 -07:00
|
|
|
const sinon = require('sinon');
|
2019-03-09 09:15:20 -07:00
|
|
|
const objectAssignDeep = require('object-assign-deep');
|
|
|
|
|
2019-02-24 07:49:41 -07:00
|
|
|
const data = require('../lib/util/data');
|
2019-03-09 09:15:20 -07:00
|
|
|
const settings = require('../lib/util/settings.js');
|
|
|
|
const fs = require('../lib/util/fs');
|
|
|
|
|
|
|
|
const configurationFile = data.joinPath('configuration.yaml');
|
|
|
|
const devicesFile = data.joinPath('devices.yaml');
|
|
|
|
const groupsFile = data.joinPath('groups.yaml');
|
2019-02-24 07:49:41 -07:00
|
|
|
|
|
|
|
describe('Settings', () => {
|
2019-03-09 09:15:20 -07:00
|
|
|
const write = (file, json) => fs.writeYaml(file, json);
|
2019-02-24 07:49:41 -07:00
|
|
|
|
2019-03-09 09:15:20 -07:00
|
|
|
const read = (file) => fs.readYaml(file);
|
2019-02-24 07:49:41 -07:00
|
|
|
|
2019-03-09 09:15:20 -07:00
|
|
|
const files = new Map();
|
2019-03-09 14:12:10 -07:00
|
|
|
|
|
|
|
beforeAll(() => {
|
2019-03-09 09:15:20 -07:00
|
|
|
sinon.stub(fs, 'readYaml').callsFake((file) => {
|
|
|
|
if (files.has(file)) return objectAssignDeep.noMutate(files.get(file));
|
|
|
|
throw new Error(`Fake file not found: ${file}`);
|
|
|
|
});
|
|
|
|
sinon.stub(fs, 'readYamlIfExists').callsFake((file) => {
|
|
|
|
if (files.has(file)) return objectAssignDeep.noMutate(files.get(file));
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
sinon.stub(fs, 'writeYaml').callsFake((file, content) => {
|
|
|
|
files.set(file, objectAssignDeep.noMutate(content));
|
|
|
|
});
|
|
|
|
});
|
2019-02-24 07:49:41 -07:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2019-03-09 09:15:20 -07:00
|
|
|
settings._clear();
|
|
|
|
files.clear();
|
2019-02-24 07:49:41 -07:00
|
|
|
});
|
|
|
|
|
2019-03-09 14:12:10 -07:00
|
|
|
afterAll(() => {
|
2019-03-09 09:15:20 -07:00
|
|
|
fs.readYaml.restore();
|
|
|
|
fs.readYamlIfExists.restore();
|
|
|
|
fs.writeYaml.restore();
|
2019-02-24 07:49:41 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('Settings', () => {
|
|
|
|
it('Should return default settings', () => {
|
2019-03-09 09:15:20 -07:00
|
|
|
write(configurationFile, {});
|
2019-02-24 07:49:41 -07:00
|
|
|
const s = settings.get();
|
2019-03-09 14:12:10 -07:00
|
|
|
expect(s).toEqual(settings._getDefaults());
|
2019-02-24 07:49:41 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Should return settings', () => {
|
2019-03-09 09:15:20 -07:00
|
|
|
write(configurationFile, {permit_join: true});
|
2019-02-24 07:49:41 -07:00
|
|
|
const s = settings.get();
|
|
|
|
const expected = settings._getDefaults();
|
|
|
|
expected.permit_join = true;
|
2019-03-09 14:12:10 -07:00
|
|
|
expect(s).toEqual(expected);
|
2019-02-24 07:49:41 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Should add devices', () => {
|
2019-03-09 09:15:20 -07:00
|
|
|
write(configurationFile, {});
|
2019-02-24 07:49:41 -07:00
|
|
|
settings.addDevice('0x12345678');
|
|
|
|
|
|
|
|
const actual = read(configurationFile);
|
|
|
|
const expected = {
|
|
|
|
devices: {
|
|
|
|
'0x12345678': {
|
|
|
|
friendly_name: '0x12345678',
|
|
|
|
retain: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-03-09 14:12:10 -07:00
|
|
|
expect(actual).toEqual(expected);
|
2019-02-24 07:49:41 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Should read devices', () => {
|
|
|
|
const content = {
|
|
|
|
devices: {
|
|
|
|
'0x12345678': {
|
|
|
|
friendly_name: '0x12345678',
|
|
|
|
retain: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-03-09 09:15:20 -07:00
|
|
|
write(configurationFile, content);
|
2019-02-24 07:49:41 -07:00
|
|
|
|
|
|
|
const device = settings.getDevice('0x12345678');
|
|
|
|
const expected = {
|
|
|
|
friendly_name: '0x12345678',
|
|
|
|
retain: false,
|
|
|
|
};
|
|
|
|
|
2019-03-09 14:12:10 -07:00
|
|
|
expect(device).toEqual(expected);
|
2019-02-24 07:49:41 -07:00
|
|
|
});
|
|
|
|
|
2019-03-09 06:36:06 -07:00
|
|
|
it('Should read devices form a separate file', () => {
|
2019-02-24 07:49:41 -07:00
|
|
|
const contentConfiguration = {
|
|
|
|
devices: 'devices.yaml',
|
|
|
|
};
|
|
|
|
|
|
|
|
const contentDevices = {
|
|
|
|
'0x12345678': {
|
|
|
|
friendly_name: '0x12345678',
|
|
|
|
retain: false,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-03-09 09:15:20 -07:00
|
|
|
write(devicesFile, contentDevices);
|
|
|
|
write(configurationFile, contentConfiguration);
|
2019-02-24 07:49:41 -07:00
|
|
|
|
|
|
|
const device = settings.getDevice('0x12345678');
|
|
|
|
const expected = {
|
|
|
|
friendly_name: '0x12345678',
|
|
|
|
retain: false,
|
|
|
|
};
|
|
|
|
|
2019-03-09 14:12:10 -07:00
|
|
|
expect(device).toEqual(expected);
|
2019-02-24 07:49:41 -07:00
|
|
|
});
|
|
|
|
|
2019-03-09 06:36:06 -07:00
|
|
|
it('Should add devices to a separate file', () => {
|
2019-02-24 07:49:41 -07:00
|
|
|
const contentConfiguration = {
|
|
|
|
devices: 'devices.yaml',
|
|
|
|
};
|
|
|
|
|
|
|
|
const contentDevices = {
|
|
|
|
'0x12345678': {
|
|
|
|
friendly_name: '0x12345678',
|
|
|
|
retain: false,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
write(devicesFile, contentDevices);
|
2019-03-09 09:15:20 -07:00
|
|
|
write(configurationFile, contentConfiguration);
|
2019-02-24 07:49:41 -07:00
|
|
|
|
|
|
|
settings.addDevice('0x1234');
|
|
|
|
|
2019-03-09 14:12:10 -07:00
|
|
|
expect(read(configurationFile)).toEqual({devices: 'devices.yaml'});
|
2019-02-24 07:49:41 -07:00
|
|
|
|
|
|
|
const expected = {
|
|
|
|
'0x12345678': {
|
|
|
|
friendly_name: '0x12345678',
|
|
|
|
retain: false,
|
|
|
|
},
|
|
|
|
'0x1234': {
|
|
|
|
friendly_name: '0x1234',
|
|
|
|
retain: false,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-03-09 14:12:10 -07:00
|
|
|
expect(read(devicesFile)).toEqual(expected);
|
2019-02-24 07:49:41 -07:00
|
|
|
});
|
|
|
|
|
2019-03-09 06:36:06 -07:00
|
|
|
it('Should add devices to a separate file if devices.yaml doesnt exist', () => {
|
2019-02-24 07:49:41 -07:00
|
|
|
const contentConfiguration = {
|
|
|
|
devices: 'devices.yaml',
|
|
|
|
};
|
|
|
|
|
2019-03-09 09:15:20 -07:00
|
|
|
write(configurationFile, contentConfiguration);
|
2019-02-24 07:49:41 -07:00
|
|
|
|
|
|
|
settings.addDevice('0x1234');
|
|
|
|
|
2019-03-09 14:12:10 -07:00
|
|
|
expect(read(configurationFile)).toEqual({devices: 'devices.yaml'});
|
2019-02-24 07:49:41 -07:00
|
|
|
|
|
|
|
const expected = {
|
|
|
|
'0x1234': {
|
|
|
|
friendly_name: '0x1234',
|
|
|
|
retain: false,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-03-09 14:12:10 -07:00
|
|
|
expect(read(devicesFile)).toEqual(expected);
|
|
|
|
}
|
|
|
|
);
|
2019-02-24 07:49:41 -07:00
|
|
|
|
2019-03-09 06:36:06 -07:00
|
|
|
it('Should add and remove devices to a separate file if devices.yaml doesnt exist', () => {
|
2019-02-24 07:49:41 -07:00
|
|
|
const contentConfiguration = {
|
|
|
|
devices: 'devices.yaml',
|
|
|
|
};
|
|
|
|
|
2019-03-09 09:15:20 -07:00
|
|
|
write(configurationFile, contentConfiguration);
|
2019-02-24 07:49:41 -07:00
|
|
|
|
|
|
|
settings.addDevice('0x1234');
|
2019-03-09 14:12:10 -07:00
|
|
|
expect(read(configurationFile)).toEqual({devices: 'devices.yaml'});
|
2019-02-24 07:49:41 -07:00
|
|
|
|
|
|
|
settings.removeDevice('0x1234');
|
2019-03-09 14:12:10 -07:00
|
|
|
expect(read(configurationFile)).toEqual({devices: 'devices.yaml'});
|
2019-02-24 07:49:41 -07:00
|
|
|
|
2019-03-09 14:12:10 -07:00
|
|
|
expect(read(devicesFile)).toEqual({});
|
|
|
|
}
|
|
|
|
);
|
2019-02-24 07:49:41 -07:00
|
|
|
|
|
|
|
it('Should read groups', () => {
|
|
|
|
const content = {
|
|
|
|
groups: {
|
|
|
|
'1': {
|
|
|
|
friendly_name: '123',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-03-09 09:15:20 -07:00
|
|
|
write(configurationFile, content);
|
2019-02-24 07:49:41 -07:00
|
|
|
|
|
|
|
const group = settings.getGroup('1');
|
|
|
|
const expected = {
|
|
|
|
friendly_name: '123',
|
|
|
|
};
|
|
|
|
|
2019-03-09 14:12:10 -07:00
|
|
|
expect(group).toEqual(expected);
|
2019-02-24 07:49:41 -07:00
|
|
|
});
|
|
|
|
|
2019-03-09 06:36:06 -07:00
|
|
|
it('Should read groups form a separate file', () => {
|
2019-02-24 07:49:41 -07:00
|
|
|
const contentConfiguration = {
|
|
|
|
groups: 'groups.yaml',
|
|
|
|
};
|
|
|
|
|
|
|
|
const contentGroups = {
|
|
|
|
'1': {
|
|
|
|
friendly_name: '123',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-03-09 09:15:20 -07:00
|
|
|
write(groupsFile, contentGroups);
|
|
|
|
write(configurationFile, contentConfiguration);
|
2019-02-24 07:49:41 -07:00
|
|
|
|
|
|
|
const group = settings.getGroup('1');
|
|
|
|
const expected = {
|
|
|
|
friendly_name: '123',
|
|
|
|
};
|
|
|
|
|
2019-03-09 14:12:10 -07:00
|
|
|
expect(group).toEqual(expected);
|
2019-02-24 07:49:41 -07:00
|
|
|
});
|
|
|
|
|
2019-03-09 06:36:06 -07:00
|
|
|
it('Combine everything! groups and devices from separate file :)', () => {
|
2019-02-24 07:49:41 -07:00
|
|
|
const contentConfiguration = {
|
|
|
|
devices: 'devices.yaml',
|
|
|
|
groups: 'groups.yaml',
|
|
|
|
};
|
|
|
|
|
|
|
|
const contentGroups = {
|
|
|
|
'1': {
|
|
|
|
friendly_name: '123',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-03-09 09:15:20 -07:00
|
|
|
write(groupsFile, contentGroups);
|
|
|
|
write(configurationFile, contentConfiguration);
|
2019-02-24 07:49:41 -07:00
|
|
|
|
|
|
|
const expectedConfiguration = {
|
|
|
|
devices: 'devices.yaml',
|
|
|
|
groups: 'groups.yaml',
|
|
|
|
};
|
|
|
|
|
2019-03-09 14:12:10 -07:00
|
|
|
expect(read(configurationFile)).toEqual(expectedConfiguration);
|
2019-02-24 07:49:41 -07:00
|
|
|
|
|
|
|
settings.addDevice('0x1234');
|
|
|
|
|
2019-03-09 14:12:10 -07:00
|
|
|
expect(read(configurationFile)).toEqual(expectedConfiguration);
|
2019-02-24 07:49:41 -07:00
|
|
|
|
|
|
|
const expectedDevice = {
|
|
|
|
'0x1234': {
|
|
|
|
friendly_name: '0x1234',
|
|
|
|
retain: false,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-03-09 14:12:10 -07:00
|
|
|
expect(read(devicesFile)).toEqual(expectedDevice);
|
2019-02-24 07:49:41 -07:00
|
|
|
|
|
|
|
const group = settings.getGroup('1');
|
|
|
|
const expectedGroup = {
|
|
|
|
friendly_name: '123',
|
|
|
|
};
|
|
|
|
|
2019-03-09 14:12:10 -07:00
|
|
|
expect(group).toEqual(expectedGroup);
|
2019-02-24 07:49:41 -07:00
|
|
|
|
2019-03-09 14:12:10 -07:00
|
|
|
expect(read(configurationFile)).toEqual(expectedConfiguration);
|
2019-02-24 07:49:41 -07:00
|
|
|
|
|
|
|
const expectedDevice2 = {
|
|
|
|
friendly_name: '0x1234',
|
|
|
|
retain: false,
|
|
|
|
};
|
|
|
|
|
2019-03-09 14:12:10 -07:00
|
|
|
expect(settings.getDevice('0x1234')).toEqual(expectedDevice2);
|
2019-02-24 07:49:41 -07:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|