homebridge-tsvesync 1.4.6 → 1.4.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/dist/__tests__/utils/test-helpers.js +2 -2
- package/dist/__tests__/utils/test-helpers.js.map +1 -1
- package/dist/platform.d.ts +10 -0
- package/dist/platform.d.ts.map +1 -1
- package/dist/platform.js +62 -16
- package/dist/platform.js.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/accessories/air-purifier-filter-maintenance.test.ts +58 -21
- package/src/__tests__/accessories/air-purifier-integration.test.ts +47 -401
- package/src/__tests__/base.accessory.test.ts +11 -56
- package/src/__tests__/devices/fan.test.ts +19 -5
- package/src/__tests__/devices/outlet.test.ts +7 -6
- package/src/__tests__/devices/switch.test.ts +6 -6
- package/src/__tests__/platform.test.ts +154 -414
- package/src/__tests__/utils/test-helpers.ts +2 -2
- package/src/platform.ts +70 -17
|
@@ -1,37 +1,16 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Integration tests for AirPurifierAccessory
|
|
2
|
+
* Integration-ish tests for AirPurifierAccessory service wiring.
|
|
3
|
+
*
|
|
4
|
+
* NOTE: Air quality is now exposed via a separate accessory (AirQualitySensorAccessory).
|
|
5
|
+
* These tests focus on the AirPurifierAccessory's primary service setup and migration
|
|
6
|
+
* away from legacy embedded services.
|
|
3
7
|
*/
|
|
4
8
|
|
|
5
|
-
import { API } from 'homebridge';
|
|
6
|
-
import { Logger } from 'homebridge';
|
|
9
|
+
import { API, Logger } from 'homebridge';
|
|
7
10
|
import { TSVESyncPlatform } from '../../platform';
|
|
8
|
-
import { VeSync } from 'tsvesync';
|
|
9
11
|
import { AirPurifierAccessory } from '../../accessories/air-purifier.accessory';
|
|
10
12
|
import { createMockLogger, createMockVeSync, createMockService } from '../utils/test-helpers';
|
|
11
|
-
|
|
12
|
-
// Complete mock for air purifier with all features
|
|
13
|
-
interface CompleteMockAirPurifier {
|
|
14
|
-
deviceName: string;
|
|
15
|
-
deviceType: string;
|
|
16
|
-
cid: string;
|
|
17
|
-
uuid: string;
|
|
18
|
-
deviceStatus: string;
|
|
19
|
-
airQualityValue: number;
|
|
20
|
-
pm1: number;
|
|
21
|
-
pm10: number;
|
|
22
|
-
filterLife: number;
|
|
23
|
-
hasFeature: jest.Mock;
|
|
24
|
-
details: {
|
|
25
|
-
air_quality_value?: number;
|
|
26
|
-
pm25?: number;
|
|
27
|
-
pm1?: number;
|
|
28
|
-
pm10?: number;
|
|
29
|
-
filter_life?: number | { percent: number };
|
|
30
|
-
};
|
|
31
|
-
getDetails: jest.Mock;
|
|
32
|
-
turnOn: jest.Mock;
|
|
33
|
-
turnOff: jest.Mock;
|
|
34
|
-
}
|
|
13
|
+
import { VeSync } from 'tsvesync';
|
|
35
14
|
|
|
36
15
|
describe('AirPurifierAccessory Integration Tests', () => {
|
|
37
16
|
let mockAPI: jest.Mocked<API>;
|
|
@@ -46,18 +25,15 @@ describe('AirPurifierAccessory Integration Tests', () => {
|
|
|
46
25
|
mockAPI = {
|
|
47
26
|
hap: {
|
|
48
27
|
Characteristic: {
|
|
49
|
-
// Air
|
|
50
|
-
AirQuality: jest.fn(),
|
|
51
|
-
PM2_5Density: jest.fn(),
|
|
52
|
-
PM10Density: jest.fn(),
|
|
53
|
-
// Filter Maintenance
|
|
54
|
-
FilterChangeIndication: jest.fn(),
|
|
55
|
-
FilterLifeLevel: jest.fn(),
|
|
56
|
-
// Air Purifier
|
|
28
|
+
// Air Purifier + Filter
|
|
57
29
|
Active: jest.fn(),
|
|
58
30
|
CurrentAirPurifierState: jest.fn(),
|
|
59
31
|
TargetAirPurifierState: jest.fn(),
|
|
60
32
|
RotationSpeed: jest.fn(),
|
|
33
|
+
FilterChangeIndication: jest.fn(),
|
|
34
|
+
FilterLifeLevel: jest.fn(),
|
|
35
|
+
Name: 'Name',
|
|
36
|
+
|
|
61
37
|
// Device Info
|
|
62
38
|
Manufacturer: jest.fn(),
|
|
63
39
|
Model: jest.fn(),
|
|
@@ -80,387 +56,57 @@ describe('AirPurifierAccessory Integration Tests', () => {
|
|
|
80
56
|
(platform as any).api = api;
|
|
81
57
|
});
|
|
82
58
|
|
|
83
|
-
function
|
|
59
|
+
function createMockAirPurifier(overrides: Partial<any> = {}) {
|
|
84
60
|
return {
|
|
85
|
-
deviceName:
|
|
86
|
-
deviceType:
|
|
87
|
-
cid:
|
|
88
|
-
uuid:
|
|
89
|
-
deviceStatus:
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
61
|
+
deviceName: 'Test Air Purifier',
|
|
62
|
+
deviceType: 'Core400S',
|
|
63
|
+
cid: 'test-cid',
|
|
64
|
+
uuid: 'test-uuid',
|
|
65
|
+
deviceStatus: 'on',
|
|
66
|
+
filterLife: 75,
|
|
67
|
+
mode: 'manual',
|
|
68
|
+
speed: 3,
|
|
69
|
+
maxSpeed: 3,
|
|
94
70
|
details: {
|
|
95
|
-
|
|
96
|
-
pm1: config.pm1 !== undefined ? config.pm1 : 15,
|
|
97
|
-
pm10: config.pm10 !== undefined ? config.pm10 : 35,
|
|
98
|
-
filter_life: config.filterLife !== undefined ? config.filterLife : 75,
|
|
99
|
-
...config.details
|
|
71
|
+
filter_life: 75,
|
|
100
72
|
},
|
|
101
|
-
hasFeature: jest.fn().
|
|
102
|
-
return feature === 'air_quality'; // Default to supporting air quality
|
|
103
|
-
}),
|
|
73
|
+
hasFeature: jest.fn().mockReturnValue(true),
|
|
104
74
|
getDetails: jest.fn().mockResolvedValue(true),
|
|
105
75
|
turnOn: jest.fn().mockResolvedValue(true),
|
|
106
76
|
turnOff: jest.fn().mockResolvedValue(true),
|
|
77
|
+
changeFanSpeed: jest.fn().mockResolvedValue(true),
|
|
78
|
+
setMode: jest.fn().mockResolvedValue(true),
|
|
79
|
+
...overrides,
|
|
107
80
|
};
|
|
108
81
|
}
|
|
109
82
|
|
|
110
|
-
|
|
111
|
-
const
|
|
112
|
-
airPurifier: createMockService(),
|
|
113
|
-
airQuality: createMockService(),
|
|
114
|
-
filter: createMockService(),
|
|
115
|
-
info: {
|
|
116
|
-
setCharacteristic: jest.fn().mockReturnThis(),
|
|
117
|
-
},
|
|
118
|
-
};
|
|
83
|
+
it('removes legacy embedded AirQualitySensor and FilterMaintenance services and does not re-add them', () => {
|
|
84
|
+
const mockDevice = createMockAirPurifier();
|
|
119
85
|
|
|
120
|
-
|
|
86
|
+
const airPurifierService = createMockService();
|
|
87
|
+
const legacyAqService = createMockService();
|
|
88
|
+
const legacyFilterService = createMockService();
|
|
89
|
+
const infoService = { setCharacteristic: jest.fn().mockReturnThis() };
|
|
90
|
+
|
|
91
|
+
const accessory = {
|
|
121
92
|
getService: jest.fn((service) => {
|
|
122
|
-
if (service === platform.Service.
|
|
123
|
-
if (service === platform.Service.
|
|
124
|
-
if (service === platform.Service.
|
|
125
|
-
if (service === platform.Service.
|
|
93
|
+
if (service === platform.Service.AccessoryInformation) return infoService;
|
|
94
|
+
if (service === platform.Service.AirPurifier) return airPurifierService;
|
|
95
|
+
if (service === platform.Service.AirQualitySensor) return legacyAqService;
|
|
96
|
+
if (service === platform.Service.FilterMaintenance) return legacyFilterService;
|
|
126
97
|
return null;
|
|
127
98
|
}),
|
|
128
|
-
addService: jest.fn((
|
|
129
|
-
|
|
130
|
-
if (service === platform.Service.FilterMaintenance) return services.filter;
|
|
131
|
-
return services.airPurifier;
|
|
132
|
-
}),
|
|
99
|
+
addService: jest.fn(() => airPurifierService),
|
|
100
|
+
removeService: jest.fn(),
|
|
133
101
|
};
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
describe('Complete Device Setup', () => {
|
|
137
|
-
it('should create all services for a fully featured device', () => {
|
|
138
|
-
const mockDevice = createCompleteAirPurifier();
|
|
139
|
-
const accessory = createMockAccessory();
|
|
140
|
-
|
|
141
|
-
const airPurifier = new AirPurifierAccessory(platform, accessory as any, mockDevice as any);
|
|
142
|
-
|
|
143
|
-
// Verify all services were created
|
|
144
|
-
expect(accessory.addService).toHaveBeenCalledWith(platform.Service.AirQualitySensor);
|
|
145
|
-
expect(accessory.addService).toHaveBeenCalledWith(platform.Service.FilterMaintenance);
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
it('should setup characteristics for all services', () => {
|
|
149
|
-
const mockDevice = createCompleteAirPurifier();
|
|
150
|
-
const accessory = createMockAccessory();
|
|
151
|
-
|
|
152
|
-
new AirPurifierAccessory(platform, accessory as any, mockDevice as any);
|
|
153
|
-
|
|
154
|
-
// Verify characteristics were set up (exact verification depends on implementation details)
|
|
155
|
-
expect(accessory.getService).toHaveBeenCalledWith(platform.Service.AirPurifier);
|
|
156
|
-
});
|
|
157
|
-
});
|
|
158
|
-
|
|
159
|
-
describe('Real-world Scenarios', () => {
|
|
160
|
-
let airPurifier: AirPurifierAccessory;
|
|
161
|
-
let mockDevice: CompleteMockAirPurifier;
|
|
162
|
-
let accessory: any;
|
|
163
|
-
|
|
164
|
-
beforeEach(() => {
|
|
165
|
-
mockDevice = createCompleteAirPurifier();
|
|
166
|
-
accessory = createMockAccessory();
|
|
167
|
-
airPurifier = new AirPurifierAccessory(platform, accessory, mockDevice as any);
|
|
168
|
-
});
|
|
169
|
-
|
|
170
|
-
it('should handle good air quality with healthy filter', async () => {
|
|
171
|
-
mockDevice.airQualityValue = 15; // Good air
|
|
172
|
-
mockDevice.pm1 = 10;
|
|
173
|
-
mockDevice.pm10 = 20;
|
|
174
|
-
mockDevice.filterLife = 80; // Healthy filter
|
|
175
|
-
|
|
176
|
-
const airQuality = await (airPurifier as any).getAirQuality();
|
|
177
|
-
const pm25Density = await (airPurifier as any).getPM25Density();
|
|
178
|
-
const filterChange = await (airPurifier as any).getFilterChangeIndication();
|
|
179
|
-
const filterLife = await (airPurifier as any).getFilterLifeLevel();
|
|
180
|
-
|
|
181
|
-
expect(airQuality).toBe(2); // GOOD
|
|
182
|
-
expect(pm25Density).toBe(15);
|
|
183
|
-
expect(filterChange).toBe(0); // FILTER_OK
|
|
184
|
-
expect(filterLife).toBe(80);
|
|
185
|
-
});
|
|
186
|
-
|
|
187
|
-
it('should handle poor air quality with old filter', async () => {
|
|
188
|
-
mockDevice.airQualityValue = 180; // Very poor air
|
|
189
|
-
mockDevice.pm1 = 120;
|
|
190
|
-
mockDevice.pm10 = 220;
|
|
191
|
-
mockDevice.filterLife = 5; // Old filter
|
|
192
|
-
|
|
193
|
-
const airQuality = await (airPurifier as any).getAirQuality();
|
|
194
|
-
const pm25Density = await (airPurifier as any).getPM25Density();
|
|
195
|
-
const filterChange = await (airPurifier as any).getFilterChangeIndication();
|
|
196
|
-
const filterLife = await (airPurifier as any).getFilterLifeLevel();
|
|
197
|
-
|
|
198
|
-
expect(airQuality).toBe(4); // INFERIOR
|
|
199
|
-
expect(pm25Density).toBe(180);
|
|
200
|
-
expect(filterChange).toBe(1); // CHANGE_FILTER
|
|
201
|
-
expect(filterLife).toBe(5);
|
|
202
|
-
});
|
|
203
|
-
|
|
204
|
-
it('should handle filter replacement scenario', async () => {
|
|
205
|
-
// Old filter with poor air quality
|
|
206
|
-
mockDevice.airQualityValue = 75;
|
|
207
|
-
mockDevice.filterLife = 3;
|
|
208
|
-
|
|
209
|
-
let airQuality = await (airPurifier as any).getAirQuality();
|
|
210
|
-
let filterChange = await (airPurifier as any).getFilterChangeIndication();
|
|
211
|
-
|
|
212
|
-
expect(airQuality).toBe(4); // INFERIOR
|
|
213
|
-
expect(filterChange).toBe(1); // CHANGE_FILTER
|
|
214
|
-
|
|
215
|
-
// Replace filter - air quality should improve over time
|
|
216
|
-
mockDevice.filterLife = 100;
|
|
217
|
-
mockDevice.airQualityValue = 20; // Improved after filter change
|
|
218
|
-
|
|
219
|
-
airQuality = await (airPurifier as any).getAirQuality();
|
|
220
|
-
filterChange = await (airPurifier as any).getFilterChangeIndication();
|
|
221
|
-
|
|
222
|
-
expect(airQuality).toBe(2); // GOOD
|
|
223
|
-
expect(filterChange).toBe(0); // FILTER_OK
|
|
224
|
-
});
|
|
225
|
-
|
|
226
|
-
it('should handle seasonal air quality changes', async () => {
|
|
227
|
-
const seasonalScenarios = [
|
|
228
|
-
{ season: 'Spring', pm25: 30, expected: 2 }, // GOOD - mild pollen
|
|
229
|
-
{ season: 'Summer', pm25: 15, expected: 2 }, // GOOD - clean air
|
|
230
|
-
{ season: 'Fall', pm25: 45, expected: 3 }, // FAIR - more particles
|
|
231
|
-
{ season: 'Winter', pm25: 80, expected: 4 }, // INFERIOR - heating pollution
|
|
232
|
-
];
|
|
233
|
-
|
|
234
|
-
for (const scenario of seasonalScenarios) {
|
|
235
|
-
mockDevice.airQualityValue = scenario.pm25;
|
|
236
|
-
mockDevice.details.air_quality_value = scenario.pm25;
|
|
237
|
-
|
|
238
|
-
const airQuality = await (airPurifier as any).getAirQuality();
|
|
239
|
-
expect(airQuality).toBe(scenario.expected);
|
|
240
|
-
}
|
|
241
|
-
});
|
|
242
|
-
});
|
|
243
|
-
|
|
244
|
-
describe('Device-Specific Behaviors', () => {
|
|
245
|
-
it('should handle Core300S with complete feature set', async () => {
|
|
246
|
-
const core300s = createCompleteAirPurifier({
|
|
247
|
-
deviceType: 'Core300S',
|
|
248
|
-
airQualityValue: 28,
|
|
249
|
-
filterLife: 65,
|
|
250
|
-
details: { filter_life: 65 } // Number format for Core series
|
|
251
|
-
});
|
|
252
102
|
|
|
253
|
-
|
|
254
|
-
const airPurifier = new AirPurifierAccessory(platform, accessory as any, core300s as any);
|
|
103
|
+
new AirPurifierAccessory(platform, accessory as any, mockDevice as any);
|
|
255
104
|
|
|
256
|
-
|
|
257
|
-
|
|
105
|
+
expect(accessory.removeService).toHaveBeenCalledWith(legacyAqService);
|
|
106
|
+
expect(accessory.removeService).toHaveBeenCalledWith(legacyFilterService);
|
|
258
107
|
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
});
|
|
262
|
-
|
|
263
|
-
it('should handle LV-PUR131S with object filter format', async () => {
|
|
264
|
-
const lvPur131s = createCompleteAirPurifier({
|
|
265
|
-
deviceType: 'LV-PUR131S',
|
|
266
|
-
airQualityValue: 35,
|
|
267
|
-
filterLife: 42,
|
|
268
|
-
details: { filter_life: { percent: 42 } } // Object format for LV series
|
|
269
|
-
});
|
|
270
|
-
|
|
271
|
-
const accessory = createMockAccessory();
|
|
272
|
-
const airPurifier = new AirPurifierAccessory(platform, accessory as any, lvPur131s as any);
|
|
273
|
-
|
|
274
|
-
const airQuality = await (airPurifier as any).getAirQuality();
|
|
275
|
-
const filterLife = await (airPurifier as any).getFilterLifeLevel();
|
|
276
|
-
|
|
277
|
-
expect(airQuality).toBe(2); // GOOD
|
|
278
|
-
expect(filterLife).toBe(42);
|
|
279
|
-
});
|
|
280
|
-
|
|
281
|
-
it('should handle LAP-V102S with extended PM data', async () => {
|
|
282
|
-
const lapV102s = createCompleteAirPurifier({
|
|
283
|
-
deviceType: 'LAP-V102S-AASR',
|
|
284
|
-
airQualityValue: 22,
|
|
285
|
-
pm1: 18,
|
|
286
|
-
pm10: 28,
|
|
287
|
-
filterLife: 88,
|
|
288
|
-
details: {
|
|
289
|
-
air_quality_value: 22,
|
|
290
|
-
pm1: 18,
|
|
291
|
-
pm10: 28,
|
|
292
|
-
filter_life: 88
|
|
293
|
-
}
|
|
294
|
-
});
|
|
295
|
-
|
|
296
|
-
const accessory = createMockAccessory();
|
|
297
|
-
const airPurifier = new AirPurifierAccessory(platform, accessory as any, lapV102s as any);
|
|
298
|
-
|
|
299
|
-
const airQuality = await (airPurifier as any).getAirQuality();
|
|
300
|
-
const pm25 = await (airPurifier as any).getPM25Density();
|
|
301
|
-
const pm10 = await (airPurifier as any).getPM10Density();
|
|
302
|
-
|
|
303
|
-
expect(airQuality).toBe(2); // GOOD
|
|
304
|
-
expect(pm25).toBe(22);
|
|
305
|
-
expect(pm10).toBe(28);
|
|
306
|
-
});
|
|
307
|
-
});
|
|
308
|
-
|
|
309
|
-
describe('State Synchronization', () => {
|
|
310
|
-
let airPurifier: AirPurifierAccessory;
|
|
311
|
-
let mockDevice: CompleteMockAirPurifier;
|
|
312
|
-
let characteristics: any;
|
|
313
|
-
|
|
314
|
-
beforeEach(() => {
|
|
315
|
-
characteristics = {
|
|
316
|
-
airQuality: { updateValue: jest.fn() },
|
|
317
|
-
pm25: { updateValue: jest.fn() },
|
|
318
|
-
pm10: { updateValue: jest.fn() },
|
|
319
|
-
filterChange: { updateValue: jest.fn() },
|
|
320
|
-
filterLife: { updateValue: jest.fn() },
|
|
321
|
-
};
|
|
322
|
-
|
|
323
|
-
mockDevice = createCompleteAirPurifier();
|
|
324
|
-
|
|
325
|
-
const accessory = {
|
|
326
|
-
getService: jest.fn(() => ({
|
|
327
|
-
getCharacteristic: jest.fn((char) => {
|
|
328
|
-
if (char === platform.Characteristic.AirQuality) return characteristics.airQuality;
|
|
329
|
-
if (char === platform.Characteristic.PM2_5Density) return characteristics.pm25;
|
|
330
|
-
if (char === platform.Characteristic.PM10Density) return characteristics.pm10;
|
|
331
|
-
if (char === platform.Characteristic.FilterChangeIndication) return characteristics.filterChange;
|
|
332
|
-
if (char === platform.Characteristic.FilterLifeLevel) return characteristics.filterLife;
|
|
333
|
-
return { updateValue: jest.fn() };
|
|
334
|
-
}),
|
|
335
|
-
})),
|
|
336
|
-
addService: jest.fn(() => ({
|
|
337
|
-
getCharacteristic: jest.fn(() => ({ updateValue: jest.fn() })),
|
|
338
|
-
})),
|
|
339
|
-
};
|
|
340
|
-
|
|
341
|
-
airPurifier = new AirPurifierAccessory(platform, accessory as any, mockDevice as any);
|
|
342
|
-
});
|
|
343
|
-
|
|
344
|
-
it('should update all characteristics when device state changes', async () => {
|
|
345
|
-
// Simulate device state change
|
|
346
|
-
mockDevice.airQualityValue = 45;
|
|
347
|
-
mockDevice.pm1 = 35;
|
|
348
|
-
mockDevice.pm10 = 55;
|
|
349
|
-
mockDevice.filterLife = 25;
|
|
350
|
-
|
|
351
|
-
// Trigger update (implementation-dependent)
|
|
352
|
-
await (airPurifier as any).updateDeviceState();
|
|
353
|
-
|
|
354
|
-
// Verify updates were called (exact verification depends on implementation)
|
|
355
|
-
// This is a placeholder for the actual implementation
|
|
356
|
-
expect(mockDevice.getDetails).toHaveBeenCalled();
|
|
357
|
-
});
|
|
358
|
-
|
|
359
|
-
it('should handle partial updates gracefully', async () => {
|
|
360
|
-
// Update only some values
|
|
361
|
-
mockDevice.airQualityValue = 60;
|
|
362
|
-
// Leave other values unchanged
|
|
363
|
-
|
|
364
|
-
const airQuality = await (airPurifier as any).getAirQuality();
|
|
365
|
-
expect(airQuality).toBe(4); // INFERIOR for PM2.5 = 60
|
|
366
|
-
});
|
|
367
|
-
});
|
|
368
|
-
|
|
369
|
-
describe('Error Recovery', () => {
|
|
370
|
-
let airPurifier: AirPurifierAccessory;
|
|
371
|
-
let mockDevice: CompleteMockAirPurifier;
|
|
372
|
-
|
|
373
|
-
beforeEach(() => {
|
|
374
|
-
mockDevice = createCompleteAirPurifier();
|
|
375
|
-
const accessory = createMockAccessory();
|
|
376
|
-
airPurifier = new AirPurifierAccessory(platform, accessory as any, mockDevice as any);
|
|
377
|
-
});
|
|
378
|
-
|
|
379
|
-
it('should handle API timeouts gracefully', async () => {
|
|
380
|
-
mockDevice.getDetails = jest.fn().mockImplementation(() =>
|
|
381
|
-
new Promise((_, reject) => setTimeout(() => reject(new Error('Timeout')), 100))
|
|
382
|
-
);
|
|
383
|
-
|
|
384
|
-
// Should not throw and should use cached values
|
|
385
|
-
const airQuality = await (airPurifier as any).getAirQuality();
|
|
386
|
-
const filterLife = await (airPurifier as any).getFilterLifeLevel();
|
|
387
|
-
|
|
388
|
-
expect(airQuality).toBeDefined();
|
|
389
|
-
expect(filterLife).toBeDefined();
|
|
390
|
-
});
|
|
391
|
-
|
|
392
|
-
it('should handle corrupted device data', async () => {
|
|
393
|
-
mockDevice.details = null as any;
|
|
394
|
-
mockDevice.airQualityValue = NaN;
|
|
395
|
-
mockDevice.filterLife = NaN;
|
|
396
|
-
|
|
397
|
-
const airQuality = await (airPurifier as any).getAirQuality();
|
|
398
|
-
const filterLife = await (airPurifier as any).getFilterLifeLevel();
|
|
399
|
-
|
|
400
|
-
expect(airQuality).toBe(1); // Default to EXCELLENT
|
|
401
|
-
expect(filterLife).toBe(0); // Default to empty
|
|
402
|
-
});
|
|
403
|
-
|
|
404
|
-
it('should recover from network interruptions', async () => {
|
|
405
|
-
// Simulate network failure
|
|
406
|
-
mockDevice.getDetails = jest.fn().mockRejectedValueOnce(new Error('Network error'));
|
|
407
|
-
|
|
408
|
-
// First call should handle error
|
|
409
|
-
await (airPurifier as any).getAirQuality();
|
|
410
|
-
|
|
411
|
-
// Simulate recovery
|
|
412
|
-
mockDevice.getDetails = jest.fn().mockResolvedValue(true);
|
|
413
|
-
mockDevice.airQualityValue = 30;
|
|
414
|
-
|
|
415
|
-
const airQuality = await (airPurifier as any).getAirQuality();
|
|
416
|
-
expect(airQuality).toBe(2); // GOOD
|
|
417
|
-
});
|
|
418
|
-
});
|
|
419
|
-
|
|
420
|
-
describe('Performance and Efficiency', () => {
|
|
421
|
-
it('should not make redundant API calls for multiple characteristic reads', async () => {
|
|
422
|
-
const mockDevice = createCompleteAirPurifier();
|
|
423
|
-
const accessory = createMockAccessory();
|
|
424
|
-
const airPurifier = new AirPurifierAccessory(platform, accessory as any, mockDevice as any);
|
|
425
|
-
|
|
426
|
-
// Read multiple characteristics
|
|
427
|
-
await Promise.all([
|
|
428
|
-
(airPurifier as any).getAirQuality(),
|
|
429
|
-
(airPurifier as any).getPM25Density(),
|
|
430
|
-
(airPurifier as any).getFilterLifeLevel(),
|
|
431
|
-
(airPurifier as any).getFilterChangeIndication(),
|
|
432
|
-
]);
|
|
433
|
-
|
|
434
|
-
// Should use cached data, not make multiple API calls
|
|
435
|
-
expect(mockDevice.getDetails).toHaveBeenCalledTimes(0); // No additional calls beyond setup
|
|
436
|
-
});
|
|
437
|
-
|
|
438
|
-
it('should handle high-frequency updates efficiently', async () => {
|
|
439
|
-
const mockDevice = createCompleteAirPurifier();
|
|
440
|
-
const accessory = createMockAccessory();
|
|
441
|
-
const airPurifier = new AirPurifierAccessory(platform, accessory as any, mockDevice as any);
|
|
442
|
-
|
|
443
|
-
// Simulate rapid updates
|
|
444
|
-
const updates = Array.from({ length: 10 }, (_, i) => ({
|
|
445
|
-
airQualityValue: 20 + i * 5,
|
|
446
|
-
filterLife: 80 - i * 2,
|
|
447
|
-
}));
|
|
448
|
-
|
|
449
|
-
const startTime = Date.now();
|
|
450
|
-
|
|
451
|
-
for (const update of updates) {
|
|
452
|
-
mockDevice.airQualityValue = update.airQualityValue;
|
|
453
|
-
mockDevice.filterLife = update.filterLife;
|
|
454
|
-
|
|
455
|
-
await (airPurifier as any).getAirQuality();
|
|
456
|
-
await (airPurifier as any).getFilterLifeLevel();
|
|
457
|
-
}
|
|
458
|
-
|
|
459
|
-
const endTime = Date.now();
|
|
460
|
-
const duration = endTime - startTime;
|
|
461
|
-
|
|
462
|
-
// Should complete within reasonable time
|
|
463
|
-
expect(duration).toBeLessThan(1000); // Less than 1 second
|
|
464
|
-
});
|
|
108
|
+
expect(accessory.addService).not.toHaveBeenCalledWith(platform.Service.AirQualitySensor);
|
|
109
|
+
expect(accessory.addService).not.toHaveBeenCalledWith(platform.Service.FilterMaintenance);
|
|
465
110
|
});
|
|
466
111
|
});
|
|
112
|
+
|
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
import { PlatformAccessory,
|
|
2
|
-
import { mock, mockDeep } from 'jest-mock-extended';
|
|
1
|
+
import { PlatformAccessory, Logger } from 'homebridge';
|
|
3
2
|
import { BaseAccessory } from '../accessories/base.accessory';
|
|
4
3
|
import { TSVESyncPlatform } from '../platform';
|
|
5
|
-
import {
|
|
6
|
-
import { DeviceCapabilities } from '../types';
|
|
7
|
-
import { PollingManager } from '../utils/polling-manager';
|
|
4
|
+
import { DeviceCapabilities } from '../types/device.types';
|
|
8
5
|
import { PluginLogger } from '../utils/logger';
|
|
9
6
|
import { RetryManager } from '../utils/retry';
|
|
10
7
|
|
|
@@ -17,23 +14,16 @@ class TestAccessory extends BaseAccessory {
|
|
|
17
14
|
this.service = new this.platform.Service.Switch(this.accessory.displayName);
|
|
18
15
|
}
|
|
19
16
|
|
|
20
|
-
protected async updateDeviceSpecificStates(): Promise<void> {
|
|
17
|
+
protected async updateDeviceSpecificStates(_details: any): Promise<void> {
|
|
21
18
|
// Test implementation
|
|
22
19
|
}
|
|
23
20
|
|
|
24
21
|
protected getDeviceCapabilities(): DeviceCapabilities {
|
|
25
22
|
return {
|
|
26
|
-
power: true,
|
|
27
|
-
mode: false,
|
|
28
|
-
speed: false,
|
|
29
|
-
timer: false,
|
|
30
23
|
hasBrightness: false,
|
|
31
24
|
hasColorTemp: false,
|
|
32
25
|
hasColor: false,
|
|
33
26
|
hasSpeed: false,
|
|
34
|
-
hasMode: false,
|
|
35
|
-
hasTimer: false,
|
|
36
|
-
hasSchedule: false,
|
|
37
27
|
hasHumidity: false,
|
|
38
28
|
hasAirQuality: false,
|
|
39
29
|
hasWaterLevel: false,
|
|
@@ -42,23 +32,6 @@ class TestAccessory extends BaseAccessory {
|
|
|
42
32
|
};
|
|
43
33
|
}
|
|
44
34
|
|
|
45
|
-
// Override syncDeviceState to remove retry logic
|
|
46
|
-
public async syncDeviceState(): Promise<boolean> {
|
|
47
|
-
try {
|
|
48
|
-
await this.platform.discoverDevices();
|
|
49
|
-
|
|
50
|
-
if (!this.device.deviceStatus) {
|
|
51
|
-
return false;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
await this.updateDeviceSpecificStates();
|
|
55
|
-
return true;
|
|
56
|
-
} catch (error) {
|
|
57
|
-
(this as any).logger.error('Failed to sync device state', {}, error);
|
|
58
|
-
throw error;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
35
|
// Expose handleDeviceError for testing
|
|
63
36
|
public async testHandleDeviceError(message: string, error: Error | any): Promise<void> {
|
|
64
37
|
return (this as any).handleDeviceError(message, error);
|
|
@@ -257,40 +230,22 @@ describe('BaseAccessory', () => {
|
|
|
257
230
|
});
|
|
258
231
|
|
|
259
232
|
describe('device state management', () => {
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
});
|
|
265
|
-
|
|
266
|
-
it('should sync device state successfully', async () => {
|
|
267
|
-
mockDevice.deviceStatus = 'on';
|
|
268
|
-
mockPlatform.discoverDevices.mockResolvedValue(undefined);
|
|
269
|
-
|
|
270
|
-
await (accessory as any).syncDeviceState();
|
|
271
|
-
expect(mockPlatform.discoverDevices).toHaveBeenCalled();
|
|
233
|
+
it('syncDeviceState calls updateDeviceSpecificStates', async () => {
|
|
234
|
+
const updateSpy = jest.spyOn(accessory as any, 'updateDeviceSpecificStates');
|
|
235
|
+
await accessory.syncDeviceState();
|
|
236
|
+
expect(updateSpy).toHaveBeenCalledWith(mockDevice);
|
|
272
237
|
});
|
|
273
238
|
|
|
274
|
-
it('
|
|
275
|
-
|
|
276
|
-
|
|
239
|
+
it('syncDeviceState swallows update errors and logs', async () => {
|
|
240
|
+
jest.spyOn(accessory as any, 'updateDeviceSpecificStates').mockRejectedValueOnce(new Error('Boom'));
|
|
241
|
+
await accessory.syncDeviceState();
|
|
277
242
|
|
|
278
|
-
await expect((accessory as any).syncDeviceState()).rejects.toThrow(error);
|
|
279
243
|
expect(mockPluginLogger.error).toHaveBeenCalledWith(
|
|
280
244
|
expect.stringContaining('Failed to sync device state'),
|
|
281
245
|
expect.any(Object),
|
|
282
|
-
|
|
246
|
+
expect.any(Error)
|
|
283
247
|
);
|
|
284
248
|
});
|
|
285
|
-
|
|
286
|
-
it('should handle empty device details', async () => {
|
|
287
|
-
mockDevice.deviceStatus = undefined;
|
|
288
|
-
mockPlatform.discoverDevices.mockResolvedValue(undefined);
|
|
289
|
-
|
|
290
|
-
const result = await (accessory as any).syncDeviceState();
|
|
291
|
-
expect(result).toBe(false);
|
|
292
|
-
expect(mockPlatform.discoverDevices).toHaveBeenCalled();
|
|
293
|
-
});
|
|
294
249
|
});
|
|
295
250
|
|
|
296
251
|
describe('utility methods', () => {
|
|
@@ -23,6 +23,8 @@ describe('Fan Device Tests', () => {
|
|
|
23
23
|
RotationDirection: jest.fn(),
|
|
24
24
|
SwingMode: jest.fn(),
|
|
25
25
|
LockPhysicalControls: jest.fn(),
|
|
26
|
+
On: 'On',
|
|
27
|
+
Name: 'Name',
|
|
26
28
|
Manufacturer: jest.fn(),
|
|
27
29
|
Model: jest.fn(),
|
|
28
30
|
SerialNumber: jest.fn(),
|
|
@@ -70,6 +72,15 @@ describe('Fan Device Tests', () => {
|
|
|
70
72
|
uuid: 'test-uuid',
|
|
71
73
|
});
|
|
72
74
|
|
|
75
|
+
const modeCharacteristic = {
|
|
76
|
+
onGet: jest.fn().mockReturnThis(),
|
|
77
|
+
onSet: jest.fn().mockReturnThis(),
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
const modeService = {
|
|
81
|
+
getCharacteristic: jest.fn().mockReturnValue(modeCharacteristic),
|
|
82
|
+
};
|
|
83
|
+
|
|
73
84
|
// Create mock services
|
|
74
85
|
infoService = {
|
|
75
86
|
setCharacteristic: jest.fn().mockReturnThis(),
|
|
@@ -99,6 +110,9 @@ describe('Fan Device Tests', () => {
|
|
|
99
110
|
// Create mock accessory
|
|
100
111
|
accessory = {
|
|
101
112
|
getService: jest.fn((service) => {
|
|
113
|
+
if (service === 'Fan Mode') {
|
|
114
|
+
return modeService;
|
|
115
|
+
}
|
|
102
116
|
if (service === platform.Service.AccessoryInformation) {
|
|
103
117
|
return infoService;
|
|
104
118
|
}
|
|
@@ -136,19 +150,19 @@ describe('Fan Device Tests', () => {
|
|
|
136
150
|
it('should handle rotation direction changes', async () => {
|
|
137
151
|
expect(handlers.setRotationDirection).toBeDefined();
|
|
138
152
|
await handlers.setRotationDirection(1); // CLOCKWISE
|
|
139
|
-
expect(mockFan.
|
|
153
|
+
expect(mockFan.setOscillation).toHaveBeenCalledWith(true);
|
|
140
154
|
|
|
141
155
|
await handlers.setRotationDirection(0); // COUNTER_CLOCKWISE
|
|
142
|
-
expect(mockFan.
|
|
156
|
+
expect(mockFan.setOscillation).toHaveBeenCalledWith(false);
|
|
143
157
|
});
|
|
144
158
|
|
|
145
159
|
it('should handle swing mode changes', async () => {
|
|
146
160
|
expect(handlers.setSwingMode).toBeDefined();
|
|
147
161
|
await handlers.setSwingMode(1); // SWING_ENABLED
|
|
148
|
-
expect(mockFan.
|
|
162
|
+
expect(mockFan.setOscillation).toHaveBeenCalledWith(true);
|
|
149
163
|
|
|
150
164
|
await handlers.setSwingMode(0); // SWING_DISABLED
|
|
151
|
-
expect(mockFan.
|
|
165
|
+
expect(mockFan.setOscillation).toHaveBeenCalledWith(false);
|
|
152
166
|
});
|
|
153
167
|
|
|
154
168
|
it('should handle child lock changes', async () => {
|
|
@@ -160,4 +174,4 @@ describe('Fan Device Tests', () => {
|
|
|
160
174
|
expect(mockFan.setChildLock).toHaveBeenCalledWith(false);
|
|
161
175
|
});
|
|
162
176
|
});
|
|
163
|
-
});
|
|
177
|
+
});
|