-
-
Notifications
You must be signed in to change notification settings - Fork 431
/
Copy pathmonitor-settings-utils.test.ts
193 lines (155 loc) · 5.98 KB
/
monitor-settings-utils.test.ts
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import { expect } from 'chai';
import {
longestPrefixMatch,
reconcileSettings,
} from '../../node/monitor-settings/monitor-settings-utils';
import { PluggableMonitorSettings } from '../../node/monitor-settings/monitor-settings-provider';
type DeepWriteable<T> = { -readonly [P in keyof T]: DeepWriteable<T[P]> };
describe('longestPrefixMatch', () => {
const settings = {
'arduino:avr:uno-port1-protocol1': {
name: 'Arduino Uno',
},
'arduino:avr:due-port1-protocol2': {
name: 'Arduino Due',
},
};
it('should return the exact prefix when found', async () => {
const prefix = 'arduino:avr:uno-port1-protocol1';
const { matchingPrefix } = longestPrefixMatch(
prefix,
settings as unknown as Record<string, PluggableMonitorSettings>
);
expect(matchingPrefix).to.equal(prefix);
});
it('should return the exact object when the prefix match', async () => {
const prefix = 'arduino:avr:uno-port1-protocol1';
const { matchingSettings } = longestPrefixMatch(
prefix,
settings as unknown as Record<string, PluggableMonitorSettings>
);
expect(matchingSettings).to.have.property('name').to.equal('Arduino Uno');
});
it('should return a partial matching prefix when a similar object is found', async () => {
const prefix = 'arduino:avr:due-port2-protocol2';
const { matchingPrefix } = longestPrefixMatch(
prefix,
settings as unknown as Record<string, PluggableMonitorSettings>
);
expect(matchingPrefix).to.equal('arduino:avr:due');
});
it('should return the closest object when the prefix partially match', async () => {
const prefix = 'arduino:avr:uno-port1-protocol2';
const { matchingSettings } = longestPrefixMatch(
prefix,
settings as unknown as Record<string, PluggableMonitorSettings>
);
expect(matchingSettings).to.have.property('name').to.equal('Arduino Uno');
});
it('should return an empty matching prefix when no similar object is found', async () => {
const prefix = 'arduino:avr:tre-port2-protocol2';
const { matchingPrefix } = longestPrefixMatch(
prefix,
settings as unknown as Record<string, PluggableMonitorSettings>
);
expect(matchingPrefix).to.equal('');
});
it('should return an empty object when no similar object is found', async () => {
const prefix = 'arduino:avr:tre-port1-protocol2';
const { matchingSettings } = longestPrefixMatch(
prefix,
settings as unknown as Record<string, PluggableMonitorSettings>
);
expect(matchingSettings).to.be.empty;
});
});
describe('reconcileSettings', () => {
const defaultSettings = {
setting1: {
id: 'setting1',
label: 'Label setting1',
type: 'enum',
values: ['a', 'b', 'c'],
selectedValue: 'b',
},
setting2: {
id: 'setting2',
label: 'Label setting2',
type: 'enum',
values: ['a', 'b', 'c'],
selectedValue: 'b',
},
setting3: {
id: 'setting3',
label: 'Label setting3',
type: 'enum',
values: ['a', 'b', 'c'],
selectedValue: 'b',
},
};
it('should return default settings if new settings are missing', async () => {
const newSettings: PluggableMonitorSettings = {};
const reconciledSettings = reconcileSettings(newSettings, defaultSettings);
expect(reconciledSettings).to.deep.equal(defaultSettings);
});
it('should add missing attributes copying it from the default settings', async () => {
const newSettings: PluggableMonitorSettings = JSON.parse(
JSON.stringify(defaultSettings)
);
delete newSettings.setting2;
const reconciledSettings = reconcileSettings(newSettings, defaultSettings);
expect(reconciledSettings).to.have.property('setting2');
});
it('should remove wrong settings attributes using the default settings as a reference', async () => {
const newSettings: PluggableMonitorSettings = JSON.parse(
JSON.stringify(defaultSettings)
);
newSettings['setting4'] = defaultSettings.setting3;
const reconciledSettings = reconcileSettings(newSettings, defaultSettings);
expect(reconciledSettings).not.to.have.property('setting4');
});
it('should reset non-value fields to those defined in the default settings', async () => {
const newSettings: DeepWriteable<PluggableMonitorSettings> = JSON.parse(
JSON.stringify(defaultSettings)
);
newSettings['setting2'].id = 'fake id';
const reconciledSettings = reconcileSettings(newSettings, defaultSettings);
expect(reconciledSettings.setting2)
.to.have.property('id')
.equal('setting2');
});
it('should accept a selectedValue if it is a valid one', async () => {
const newSettings: PluggableMonitorSettings = JSON.parse(
JSON.stringify(defaultSettings)
);
newSettings.setting2.selectedValue = 'c';
const reconciledSettings = reconcileSettings(newSettings, defaultSettings);
expect(reconciledSettings.setting2)
.to.have.property('selectedValue')
.to.equal('c');
});
it('should fall a back to the first valid setting when the selectedValue is not valid', async () => {
const newSettings: PluggableMonitorSettings = JSON.parse(
JSON.stringify(defaultSettings)
);
newSettings.setting2.selectedValue = 'z';
const reconciledSettings = reconcileSettings(newSettings, defaultSettings);
expect(reconciledSettings.setting2)
.to.have.property('selectedValue')
.to.equal('a');
});
it('should accept any value if default values are not set', async () => {
const wrongDefaults: DeepWriteable<PluggableMonitorSettings> = JSON.parse(
JSON.stringify(defaultSettings)
);
wrongDefaults.setting2.values = [];
const newSettings: PluggableMonitorSettings = JSON.parse(
JSON.stringify(wrongDefaults)
);
newSettings.setting2.selectedValue = 'z';
const reconciledSettings = reconcileSettings(newSettings, wrongDefaults);
expect(reconciledSettings.setting2)
.to.have.property('selectedValue')
.to.equal('z');
});
});