-
Notifications
You must be signed in to change notification settings - Fork 8
/
dyson-utils.test.js
148 lines (123 loc) · 4.26 KB
/
dyson-utils.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
'use strict';
const fs = require('fs');
const { fail } = require('assert');
const { expect } = require('chai');
const sinon = require('sinon');
const dysonUtils = require('./dyson-utils');
describe('dyson-utils', () => {
describe('zeroFill', () => {
it('should left-pad zeroes for positive input numbers', () => {
const data = [
[5, 4, '0005'],
[1, 1, '1'],
[45, 2, '45'],
[734, 8, '00000734']
];
data.forEach(d => {
expect(dysonUtils.zeroFill(d[0], d[1])).to.equal(d[2]);
});
});
it('should left-pad zeroes for negative input numbers (width excludes negative sign)', () => {
const data = [
[-5, 4, '-0005'],
[-1, 1, '-1'],
[-45, 2, '-45'],
[-734, 8, '-00000734']
];
data.forEach(d => {
expect(dysonUtils.zeroFill(d[0], d[1])).to.equal(d[2]);
});
});
it('should return empty string for invalid (non-number or empty) input', () => {
expect(dysonUtils.zeroFill('', 50)).to.equal('');
expect(dysonUtils.zeroFill('äöüß', 50)).to.equal('');
});
});
describe('checkAdapterConfig', () => {
after(() => sinon.restore());
const fakeAdapter = {
log: { debug: sinon.fake(), error: sinon.fake() },
config: null
};
context('given an invalid adapter configuration', () => {
it('should not throw - but wait for configuration', () => {
fakeAdapter.config = {
temperatureUnit: '',
pollInterval: '',
country: '',
email: '',
Password: ''
};
expect(() => dysonUtils.checkAdapterConfig(fakeAdapter)).to.not.throw();
});
});
context('given an valid adapter configuration', () => {
it('should not throw', () => {
fakeAdapter.config = {
temperatureUnit: 'C',
pollInterval: 60,
country: 'DE',
email: '[email protected]',
Password: 'SecretPassword'
};
expect(() => dysonUtils.checkAdapterConfig(fakeAdapter)).to.not.throw();
});
});
});
describe('decrypt', () => {
it.skip('should verify decrypt mechanism', () => {});
});
describe('decryptMqttPasswd', () => {
it.skip('should verify decrypt MQTT password mechanism', () => {});
});
describe('maskConfig', () => {
it('should mask Password while not modifying the rest', () => {
const expectedTemperaturUnit = 'C';
const expectedPollInterval = 60;
const expectedCountry = 'DE';
const expectedEmail = '[email protected]';
const config = {
temperatureUnit: expectedTemperaturUnit,
pollInterval: expectedPollInterval,
country: expectedCountry,
email: expectedEmail,
Password: 'SecretPassword'
};
const maskedConfig = dysonUtils.maskConfig(config);
expect(maskedConfig.Password).to.equal(
'(***)',
"Password wasn't masked to expected value"
);
expect(maskedConfig.temperatureUnit).to.equal(expectedTemperaturUnit);
expect(maskedConfig.pollInterval).to.equal(expectedPollInterval);
expect(maskedConfig.country).to.equal(expectedCountry);
expect(maskedConfig.email).to.equal(expectedEmail);
});
});
describe('parseDysonMsgPayload', () => {
// TODO See adapter.processMsg() for now, considering migration to separate message parser later
it('should ignore empty or null message payload', () => {
try {
dysonUtils.parseDysonMessage('');
dysonUtils.parseDysonMessage(null);
} catch (error) {
fail(`Error ${error} thrown during message processing.`);
}
});
it.skip('should parse a DP01 CURRENT-STATE payload', () => {
const msg = fs.readFileSync('./test/sample-data/sample-msg-DP01-1.json');
const data = JSON.parse(msg);
console.log(data);
});
it.skip('should parse a DP01 ENVIRONMENTAL-CURRENT-SENSOR-DATA payload', () => {
const msg = fs.readFileSync('./test/sample-data/sample-msg-DP01-2.json');
const data = JSON.parse(msg);
console.log(data);
});
it.skip('should parse a DP01 STATE-CHANGE payload', () => {
const msg = fs.readFileSync('./test/sample-data/sample-msg-DP01-3.json');
const data = JSON.parse(msg);
console.log(data);
});
});
});