node-red-contrib-octopus-intelligent 1.0.5 → 1.2.0
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 +56 -0
- package/README.md +110 -7
- package/octopus-intelligent.html +138 -127
- package/octopus-intelligent.js +415 -27
- package/package.json +3 -8
- package/__tests__/README.md +0 -220
- package/__tests__/TEST-SUMMARY.md +0 -194
- package/__tests__/charging-now-validation.test.js +0 -495
- package/__tests__/charging-timers.test.js +0 -601
- package/__tests__/data-processing.test.js +0 -478
- package/__tests__/mqtt-refresh-cooldown.test.js +0 -420
- package/__tests__/reconciliation-loop.test.js +0 -474
- package/images/IMAGES_NEEDED.md +0 -64
- package/images/examples/basic-monitoring-flow.png +0 -0
- package/images/examples/homeassistant-device-v1.0.4.png +0 -0
- package/images/examples/homeassistant-device.png +0 -0
- package/images/examples/mqtt-integration-flow.png +0 -0
- package/test-mocks.js +0 -1038
|
@@ -1,601 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Charging Timer Function Tests
|
|
3
|
-
*
|
|
4
|
-
* Tests for setupChargingTimers(), publishChargingState(), clearChargingTimers()
|
|
5
|
-
* and related timer management functions
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
const {
|
|
9
|
-
mockDataWithActiveSlot,
|
|
10
|
-
mockDataWithSlots,
|
|
11
|
-
mockDataWithThreeSlots,
|
|
12
|
-
createMockBroker
|
|
13
|
-
} = require('../test-mocks');
|
|
14
|
-
|
|
15
|
-
describe('Charging Timer Management', () => {
|
|
16
|
-
let mockBroker;
|
|
17
|
-
let chargingNow;
|
|
18
|
-
let preValidationTimer;
|
|
19
|
-
let slotStartTimer;
|
|
20
|
-
let slotEndTimer;
|
|
21
|
-
let cachedSlots;
|
|
22
|
-
|
|
23
|
-
beforeEach(() => {
|
|
24
|
-
jest.clearAllMocks();
|
|
25
|
-
jest.useFakeTimers();
|
|
26
|
-
jest.setSystemTime(new Date('2025-11-29T00:00:00Z'));
|
|
27
|
-
|
|
28
|
-
mockBroker = createMockBroker();
|
|
29
|
-
|
|
30
|
-
// Initialize state
|
|
31
|
-
chargingNow = false;
|
|
32
|
-
preValidationTimer = null;
|
|
33
|
-
slotStartTimer = null;
|
|
34
|
-
slotEndTimer = null;
|
|
35
|
-
cachedSlots = [];
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
afterEach(() => {
|
|
39
|
-
jest.useRealTimers();
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
// ==========================================================================
|
|
43
|
-
// publishChargingState() Tests
|
|
44
|
-
// ==========================================================================
|
|
45
|
-
|
|
46
|
-
describe('publishChargingState()', () => {
|
|
47
|
-
/**
|
|
48
|
-
* @test publishChargingState - Set to Charging
|
|
49
|
-
* @scenario Publishing charging state as ON
|
|
50
|
-
* @given state = true
|
|
51
|
-
* @expect chargingNow = true
|
|
52
|
-
* @expect MQTT publishes "ON" to charging_now topic
|
|
53
|
-
* @expect Retained flag = true
|
|
54
|
-
*/
|
|
55
|
-
test('should set chargingNow to true and publish ON', () => {
|
|
56
|
-
const enableMqtt = true;
|
|
57
|
-
const stateTopic = 'nodered_octopus/A-TEST1234/status';
|
|
58
|
-
|
|
59
|
-
// Simulate publishChargingState(true)
|
|
60
|
-
const state = true;
|
|
61
|
-
chargingNow = state;
|
|
62
|
-
|
|
63
|
-
if (enableMqtt && mockBroker) {
|
|
64
|
-
const payload = state ? "ON" : "OFF";
|
|
65
|
-
mockBroker.client.publish(`${stateTopic}/charging_now`, payload, { retain: true });
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
expect(chargingNow).toBe(true);
|
|
69
|
-
|
|
70
|
-
const published = mockBroker.getPublished();
|
|
71
|
-
expect(published.length).toBe(1);
|
|
72
|
-
expect(published[0].topic).toBe(`${stateTopic}/charging_now`);
|
|
73
|
-
expect(published[0].payload).toBe('ON');
|
|
74
|
-
expect(published[0].options.retain).toBe(true);
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* @test publishChargingState - Set to Not Charging
|
|
79
|
-
* @scenario Publishing charging state as OFF
|
|
80
|
-
* @given state = false
|
|
81
|
-
* @expect chargingNow = false
|
|
82
|
-
* @expect MQTT publishes "OFF" to charging_now topic
|
|
83
|
-
*/
|
|
84
|
-
test('should set chargingNow to false and publish OFF', () => {
|
|
85
|
-
const enableMqtt = true;
|
|
86
|
-
const stateTopic = 'nodered_octopus/A-TEST1234/status';
|
|
87
|
-
|
|
88
|
-
const state = false;
|
|
89
|
-
chargingNow = state;
|
|
90
|
-
|
|
91
|
-
if (enableMqtt && mockBroker) {
|
|
92
|
-
const payload = state ? "ON" : "OFF";
|
|
93
|
-
mockBroker.client.publish(`${stateTopic}/charging_now`, payload, { retain: true });
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
expect(chargingNow).toBe(false);
|
|
97
|
-
|
|
98
|
-
const published = mockBroker.getPublished();
|
|
99
|
-
expect(published[0].payload).toBe('OFF');
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* @test publishChargingState - MQTT Disabled
|
|
104
|
-
* @scenario MQTT is disabled
|
|
105
|
-
* @given state = true, enableMqtt = false
|
|
106
|
-
* @expect chargingNow = true
|
|
107
|
-
* @expect No MQTT publish attempted
|
|
108
|
-
*/
|
|
109
|
-
test('should update state but not publish when MQTT disabled', () => {
|
|
110
|
-
const enableMqtt = false;
|
|
111
|
-
const stateTopic = 'nodered_octopus/A-TEST1234/status';
|
|
112
|
-
|
|
113
|
-
const state = true;
|
|
114
|
-
chargingNow = state;
|
|
115
|
-
|
|
116
|
-
if (enableMqtt && mockBroker) {
|
|
117
|
-
const payload = state ? "ON" : "OFF";
|
|
118
|
-
mockBroker.client.publish(`${stateTopic}/charging_now`, payload, { retain: true });
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
expect(chargingNow).toBe(true);
|
|
122
|
-
|
|
123
|
-
const published = mockBroker.getPublished();
|
|
124
|
-
expect(published.length).toBe(0);
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* @test publishChargingState - Broker Disconnected
|
|
129
|
-
* @scenario MQTT broker is not available
|
|
130
|
-
* @given state = true, broker = null
|
|
131
|
-
* @expect chargingNow = true
|
|
132
|
-
* @expect No errors thrown
|
|
133
|
-
*/
|
|
134
|
-
test('should handle missing broker gracefully', () => {
|
|
135
|
-
const enableMqtt = true;
|
|
136
|
-
const stateTopic = 'nodered_octopus/A-TEST1234/status';
|
|
137
|
-
const broker = null;
|
|
138
|
-
|
|
139
|
-
const state = true;
|
|
140
|
-
chargingNow = state;
|
|
141
|
-
|
|
142
|
-
if (enableMqtt && broker) {
|
|
143
|
-
const payload = state ? "ON" : "OFF";
|
|
144
|
-
broker.client.publish(`${stateTopic}/charging_now`, payload, { retain: true });
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
expect(chargingNow).toBe(true);
|
|
148
|
-
// Should not throw error
|
|
149
|
-
});
|
|
150
|
-
});
|
|
151
|
-
|
|
152
|
-
// ==========================================================================
|
|
153
|
-
// clearChargingTimers() Tests
|
|
154
|
-
// ==========================================================================
|
|
155
|
-
|
|
156
|
-
describe('clearChargingTimers()', () => {
|
|
157
|
-
/**
|
|
158
|
-
* @test clearChargingTimers - Clear All Active Timers
|
|
159
|
-
* @scenario All three timers are set
|
|
160
|
-
* @expect All timers cleared
|
|
161
|
-
* @expect All timer variables set to null
|
|
162
|
-
*/
|
|
163
|
-
test('should clear all active timers', () => {
|
|
164
|
-
// Set up all three timers
|
|
165
|
-
preValidationTimer = setTimeout(() => {}, 10000);
|
|
166
|
-
slotStartTimer = setTimeout(() => {}, 20000);
|
|
167
|
-
slotEndTimer = setTimeout(() => {}, 30000);
|
|
168
|
-
|
|
169
|
-
// Clear all timers
|
|
170
|
-
if (preValidationTimer) {
|
|
171
|
-
clearTimeout(preValidationTimer);
|
|
172
|
-
preValidationTimer = null;
|
|
173
|
-
}
|
|
174
|
-
if (slotStartTimer) {
|
|
175
|
-
clearTimeout(slotStartTimer);
|
|
176
|
-
slotStartTimer = null;
|
|
177
|
-
}
|
|
178
|
-
if (slotEndTimer) {
|
|
179
|
-
clearTimeout(slotEndTimer);
|
|
180
|
-
slotEndTimer = null;
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
expect(preValidationTimer).toBeNull();
|
|
184
|
-
expect(slotStartTimer).toBeNull();
|
|
185
|
-
expect(slotEndTimer).toBeNull();
|
|
186
|
-
});
|
|
187
|
-
|
|
188
|
-
/**
|
|
189
|
-
* @test clearChargingTimers - Clear When No Timers Set
|
|
190
|
-
* @scenario All timer variables already null
|
|
191
|
-
* @expect No errors thrown
|
|
192
|
-
* @expect Timer variables remain null
|
|
193
|
-
*/
|
|
194
|
-
test('should handle clearing when no timers are set', () => {
|
|
195
|
-
expect(preValidationTimer).toBeNull();
|
|
196
|
-
expect(slotStartTimer).toBeNull();
|
|
197
|
-
expect(slotEndTimer).toBeNull();
|
|
198
|
-
|
|
199
|
-
// Attempt to clear
|
|
200
|
-
if (preValidationTimer) {
|
|
201
|
-
clearTimeout(preValidationTimer);
|
|
202
|
-
preValidationTimer = null;
|
|
203
|
-
}
|
|
204
|
-
if (slotStartTimer) {
|
|
205
|
-
clearTimeout(slotStartTimer);
|
|
206
|
-
slotStartTimer = null;
|
|
207
|
-
}
|
|
208
|
-
if (slotEndTimer) {
|
|
209
|
-
clearTimeout(slotEndTimer);
|
|
210
|
-
slotEndTimer = null;
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
// Should not throw, all still null
|
|
214
|
-
expect(preValidationTimer).toBeNull();
|
|
215
|
-
expect(slotStartTimer).toBeNull();
|
|
216
|
-
expect(slotEndTimer).toBeNull();
|
|
217
|
-
});
|
|
218
|
-
|
|
219
|
-
/**
|
|
220
|
-
* @test clearChargingTimers - Clear Partial Timers
|
|
221
|
-
* @scenario Only slotEndTimer is set
|
|
222
|
-
* @expect slotEndTimer cleared and set to null
|
|
223
|
-
* @expect Other timer variables remain null
|
|
224
|
-
*/
|
|
225
|
-
test('should clear only active timers', () => {
|
|
226
|
-
slotEndTimer = setTimeout(() => {}, 30000);
|
|
227
|
-
|
|
228
|
-
if (preValidationTimer) {
|
|
229
|
-
clearTimeout(preValidationTimer);
|
|
230
|
-
preValidationTimer = null;
|
|
231
|
-
}
|
|
232
|
-
if (slotStartTimer) {
|
|
233
|
-
clearTimeout(slotStartTimer);
|
|
234
|
-
slotStartTimer = null;
|
|
235
|
-
}
|
|
236
|
-
if (slotEndTimer) {
|
|
237
|
-
clearTimeout(slotEndTimer);
|
|
238
|
-
slotEndTimer = null;
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
expect(preValidationTimer).toBeNull();
|
|
242
|
-
expect(slotStartTimer).toBeNull();
|
|
243
|
-
expect(slotEndTimer).toBeNull();
|
|
244
|
-
});
|
|
245
|
-
});
|
|
246
|
-
|
|
247
|
-
// ==========================================================================
|
|
248
|
-
// setupChargingTimers() Tests
|
|
249
|
-
// ==========================================================================
|
|
250
|
-
|
|
251
|
-
describe.skip('setupChargingTimers() - Integration Tests', () => {
|
|
252
|
-
/**
|
|
253
|
-
* These tests require module integration and are skipped for now
|
|
254
|
-
* Will be enabled when code is refactored for better testability
|
|
255
|
-
*/
|
|
256
|
-
/**
|
|
257
|
-
* Simplified setupChargingTimers implementation for testing
|
|
258
|
-
*/
|
|
259
|
-
function setupChargingTimers(slots, enableMqtt = true, broker = mockBroker) {
|
|
260
|
-
const stateTopic = 'nodered_octopus/A-TEST1234/status';
|
|
261
|
-
|
|
262
|
-
// Clear existing timers
|
|
263
|
-
if (preValidationTimer) clearTimeout(preValidationTimer);
|
|
264
|
-
if (slotStartTimer) clearTimeout(slotStartTimer);
|
|
265
|
-
if (slotEndTimer) clearTimeout(slotEndTimer);
|
|
266
|
-
|
|
267
|
-
cachedSlots = slots;
|
|
268
|
-
const now = new Date();
|
|
269
|
-
|
|
270
|
-
// Check if currently in a charging slot
|
|
271
|
-
const activeSlot = slots.find(s => {
|
|
272
|
-
const start = new Date(s.startDt);
|
|
273
|
-
const end = new Date(s.endDt);
|
|
274
|
-
return start <= now && end > now;
|
|
275
|
-
});
|
|
276
|
-
|
|
277
|
-
if (activeSlot) {
|
|
278
|
-
// Currently charging
|
|
279
|
-
const newState = true;
|
|
280
|
-
if (chargingNow !== newState) {
|
|
281
|
-
chargingNow = newState;
|
|
282
|
-
if (enableMqtt && broker) {
|
|
283
|
-
const payload = newState ? "ON" : "OFF";
|
|
284
|
-
broker.client.publish(`${stateTopic}/charging_now`, payload, { retain: true });
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
// Set end timer
|
|
289
|
-
const slotEnd = new Date(activeSlot.endDt);
|
|
290
|
-
const msUntilEnd = slotEnd.getTime() - now.getTime();
|
|
291
|
-
if (msUntilEnd > 0) {
|
|
292
|
-
slotEndTimer = setTimeout(() => {
|
|
293
|
-
chargingNow = false;
|
|
294
|
-
if (enableMqtt && broker) {
|
|
295
|
-
broker.client.publish(`${stateTopic}/charging_now`, "OFF", { retain: true });
|
|
296
|
-
}
|
|
297
|
-
}, msUntilEnd);
|
|
298
|
-
}
|
|
299
|
-
} else {
|
|
300
|
-
// Not currently charging
|
|
301
|
-
const newState = false;
|
|
302
|
-
if (chargingNow !== newState) {
|
|
303
|
-
chargingNow = newState;
|
|
304
|
-
if (enableMqtt && broker) {
|
|
305
|
-
const payload = newState ? "ON" : "OFF";
|
|
306
|
-
broker.client.publish(`${stateTopic}/charging_now`, payload, { retain: true });
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
// Find next future slot
|
|
311
|
-
const nextSlot = slots.find(s => new Date(s.startDt) > now);
|
|
312
|
-
if (nextSlot) {
|
|
313
|
-
const slotStart = new Date(nextSlot.startDt);
|
|
314
|
-
const preValidationTime = slotStart.getTime() - 30000; // 30s before
|
|
315
|
-
const msUntilPreValidation = preValidationTime - now.getTime();
|
|
316
|
-
|
|
317
|
-
if (msUntilPreValidation > 0 && msUntilPreValidation < 24 * 60 * 60 * 1000) {
|
|
318
|
-
preValidationTimer = setTimeout(() => {
|
|
319
|
-
// Pre-validation logic would go here
|
|
320
|
-
}, msUntilPreValidation);
|
|
321
|
-
}
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
/**
|
|
327
|
-
* @test setupChargingTimers - Test 3.1: Currently Charging (Active Slot)
|
|
328
|
-
* @scenario Current time is in the middle of a charging slot
|
|
329
|
-
* @given Current time: 2025-11-29T02:00:00Z
|
|
330
|
-
* @given Slots: [{ startDt: "2025-11-29T01:30:00Z", endDt: "2025-11-29T05:30:00Z", deltaKwh: -15.5 }]
|
|
331
|
-
* @expect chargingNow = true
|
|
332
|
-
* @expect MQTT publishes "ON"
|
|
333
|
-
* @expect slotEndTimer set for 05:30:00Z (3.5 hours from now)
|
|
334
|
-
* @expect No pre-validation or start timers
|
|
335
|
-
*/
|
|
336
|
-
test('should set chargingNow=true for active slot', () => {
|
|
337
|
-
jest.setSystemTime(new Date('2025-11-29T02:00:00Z'));
|
|
338
|
-
|
|
339
|
-
const slots = [
|
|
340
|
-
{
|
|
341
|
-
startDt: "2025-11-29 01:30:00+00:00",
|
|
342
|
-
endDt: "2025-11-29 05:30:00+00:00",
|
|
343
|
-
deltaKwh: -15.5,
|
|
344
|
-
meta: { source: "smart-charge" }
|
|
345
|
-
}
|
|
346
|
-
];
|
|
347
|
-
|
|
348
|
-
setupChargingTimers(slots);
|
|
349
|
-
|
|
350
|
-
expect(chargingNow).toBe(true);
|
|
351
|
-
|
|
352
|
-
const published = mockBroker.getPublished();
|
|
353
|
-
expect(published.length).toBeGreaterThan(0);
|
|
354
|
-
expect(published[0].payload).toBe('ON');
|
|
355
|
-
|
|
356
|
-
expect(slotEndTimer).not.toBeNull();
|
|
357
|
-
expect(preValidationTimer).toBeNull();
|
|
358
|
-
expect(slotStartTimer).toBeNull();
|
|
359
|
-
});
|
|
360
|
-
|
|
361
|
-
/**
|
|
362
|
-
* @test setupChargingTimers - Test 3.2: Future Slot (Not Charging)
|
|
363
|
-
* @scenario Next slot is in the future
|
|
364
|
-
* @given Current time: 2025-11-29T00:00:00Z
|
|
365
|
-
* @given Slots: [{ startDt: "2025-11-29T01:30:00Z", endDt: "2025-11-29T05:30:00Z", deltaKwh: -15.5 }]
|
|
366
|
-
* @expect chargingNow = false
|
|
367
|
-
* @expect MQTT publishes "OFF"
|
|
368
|
-
* @expect preValidationTimer set for 01:00:00Z (30s before slot start)
|
|
369
|
-
*/
|
|
370
|
-
test('should set chargingNow=false for future slot', () => {
|
|
371
|
-
jest.setSystemTime(new Date('2025-11-29T00:00:00Z'));
|
|
372
|
-
|
|
373
|
-
const slots = [
|
|
374
|
-
{
|
|
375
|
-
startDt: "2025-11-29 01:30:00+00:00",
|
|
376
|
-
endDt: "2025-11-29 05:30:00+00:00",
|
|
377
|
-
deltaKwh: -15.5,
|
|
378
|
-
meta: { source: "smart-charge" }
|
|
379
|
-
}
|
|
380
|
-
];
|
|
381
|
-
|
|
382
|
-
setupChargingTimers(slots);
|
|
383
|
-
|
|
384
|
-
expect(chargingNow).toBe(false);
|
|
385
|
-
|
|
386
|
-
const published = mockBroker.getPublished();
|
|
387
|
-
expect(published.length).toBeGreaterThan(0);
|
|
388
|
-
expect(published[0].payload).toBe('OFF');
|
|
389
|
-
|
|
390
|
-
expect(preValidationTimer).not.toBeNull();
|
|
391
|
-
expect(slotStartTimer).toBeNull();
|
|
392
|
-
expect(slotEndTimer).toBeNull();
|
|
393
|
-
});
|
|
394
|
-
|
|
395
|
-
/**
|
|
396
|
-
* @test setupChargingTimers - Test 3.3: No Slots (Car Not Plugged In)
|
|
397
|
-
* @scenario No slots in array
|
|
398
|
-
* @given Current time: 2025-11-29T02:00:00Z
|
|
399
|
-
* @given Slots: []
|
|
400
|
-
* @expect chargingNow = false
|
|
401
|
-
* @expect MQTT publishes "OFF"
|
|
402
|
-
* @expect No timers set
|
|
403
|
-
*/
|
|
404
|
-
test('should handle empty slots array', () => {
|
|
405
|
-
jest.setSystemTime(new Date('2025-11-29T02:00:00Z'));
|
|
406
|
-
|
|
407
|
-
const slots = [];
|
|
408
|
-
|
|
409
|
-
setupChargingTimers(slots);
|
|
410
|
-
|
|
411
|
-
expect(chargingNow).toBe(false);
|
|
412
|
-
|
|
413
|
-
const published = mockBroker.getPublished();
|
|
414
|
-
expect(published.length).toBeGreaterThan(0);
|
|
415
|
-
expect(published[0].payload).toBe('OFF');
|
|
416
|
-
|
|
417
|
-
expect(preValidationTimer).toBeNull();
|
|
418
|
-
expect(slotStartTimer).toBeNull();
|
|
419
|
-
expect(slotEndTimer).toBeNull();
|
|
420
|
-
});
|
|
421
|
-
|
|
422
|
-
/**
|
|
423
|
-
* @test setupChargingTimers - Test 3.4: Multiple Slots (First Active)
|
|
424
|
-
* @scenario Multiple slots with first one currently active
|
|
425
|
-
* @given Current time: 2025-11-29T02:00:00Z
|
|
426
|
-
* @given Slots: Two slots (first active, second future)
|
|
427
|
-
* @expect chargingNow = true
|
|
428
|
-
* @expect End timer set for first slot
|
|
429
|
-
* @expect Second slot timer NOT set (too far in future)
|
|
430
|
-
*/
|
|
431
|
-
test('should handle multiple slots with first active', () => {
|
|
432
|
-
jest.setSystemTime(new Date('2025-11-29T02:00:00Z'));
|
|
433
|
-
|
|
434
|
-
const slots = [
|
|
435
|
-
{
|
|
436
|
-
startDt: "2025-11-29 01:30:00+00:00",
|
|
437
|
-
endDt: "2025-11-29 05:30:00+00:00",
|
|
438
|
-
deltaKwh: -15.5,
|
|
439
|
-
meta: { source: "smart-charge" }
|
|
440
|
-
},
|
|
441
|
-
{
|
|
442
|
-
startDt: "2025-11-29 23:30:00+00:00",
|
|
443
|
-
endDt: "2025-11-30 05:30:00+00:00",
|
|
444
|
-
deltaKwh: -18.2,
|
|
445
|
-
meta: { source: "smart-charge" }
|
|
446
|
-
}
|
|
447
|
-
];
|
|
448
|
-
|
|
449
|
-
setupChargingTimers(slots);
|
|
450
|
-
|
|
451
|
-
expect(chargingNow).toBe(true);
|
|
452
|
-
expect(slotEndTimer).not.toBeNull();
|
|
453
|
-
});
|
|
454
|
-
|
|
455
|
-
/**
|
|
456
|
-
* @test setupChargingTimers - Test 3.6: Slot Just Ended
|
|
457
|
-
* @scenario Slot ended 5 seconds ago
|
|
458
|
-
* @given Current time: 2025-11-29T05:30:05Z
|
|
459
|
-
* @given Slots: [{ startDt: "2025-11-29T01:30:00Z", endDt: "2025-11-29T05:30:00Z", deltaKwh: -15.5 }]
|
|
460
|
-
* @expect chargingNow = false (slot already ended)
|
|
461
|
-
* @expect No timers set (slot is in the past)
|
|
462
|
-
*/
|
|
463
|
-
test('should handle slot that just ended', () => {
|
|
464
|
-
jest.setSystemTime(new Date('2025-11-29T05:30:05Z'));
|
|
465
|
-
|
|
466
|
-
const slots = [
|
|
467
|
-
{
|
|
468
|
-
startDt: "2025-11-29 01:30:00+00:00",
|
|
469
|
-
endDt: "2025-11-29 05:30:00+00:00",
|
|
470
|
-
deltaKwh: -15.5,
|
|
471
|
-
meta: { source: "smart-charge" }
|
|
472
|
-
}
|
|
473
|
-
];
|
|
474
|
-
|
|
475
|
-
setupChargingTimers(slots);
|
|
476
|
-
|
|
477
|
-
expect(chargingNow).toBe(false);
|
|
478
|
-
expect(preValidationTimer).toBeNull();
|
|
479
|
-
expect(slotStartTimer).toBeNull();
|
|
480
|
-
expect(slotEndTimer).toBeNull();
|
|
481
|
-
});
|
|
482
|
-
|
|
483
|
-
/**
|
|
484
|
-
* @test setupChargingTimers - Timer Cleanup on Re-run
|
|
485
|
-
* @scenario setupChargingTimers called twice
|
|
486
|
-
* @expect Old timers cleared before new ones set
|
|
487
|
-
*/
|
|
488
|
-
test('should clear old timers when called again', () => {
|
|
489
|
-
jest.setSystemTime(new Date('2025-11-29T00:00:00Z'));
|
|
490
|
-
|
|
491
|
-
const slots = [
|
|
492
|
-
{
|
|
493
|
-
startDt: "2025-11-29 01:30:00+00:00",
|
|
494
|
-
endDt: "2025-11-29 05:30:00+00:00",
|
|
495
|
-
deltaKwh: -15.5,
|
|
496
|
-
meta: { source: "smart-charge" }
|
|
497
|
-
}
|
|
498
|
-
];
|
|
499
|
-
|
|
500
|
-
// First call
|
|
501
|
-
setupChargingTimers(slots);
|
|
502
|
-
const firstTimer = preValidationTimer;
|
|
503
|
-
|
|
504
|
-
// Second call
|
|
505
|
-
setupChargingTimers(slots);
|
|
506
|
-
const secondTimer = preValidationTimer;
|
|
507
|
-
|
|
508
|
-
// Timers should be different (old cleared, new created)
|
|
509
|
-
expect(firstTimer).not.toBe(secondTimer);
|
|
510
|
-
});
|
|
511
|
-
});
|
|
512
|
-
|
|
513
|
-
// ==========================================================================
|
|
514
|
-
// Slot Filtering Tests
|
|
515
|
-
// ==========================================================================
|
|
516
|
-
|
|
517
|
-
describe('Slot Filtering Logic', () => {
|
|
518
|
-
/**
|
|
519
|
-
* @test Slot Filtering - Active and Future Slots
|
|
520
|
-
* @scenario Filter slots to only include active/future
|
|
521
|
-
* @given Current time: 2025-11-29T08:00:00Z
|
|
522
|
-
* @given Slots with past, active, and future times
|
|
523
|
-
* @expect Only slots with endDt > now are included
|
|
524
|
-
*/
|
|
525
|
-
test('should filter out past slots', () => {
|
|
526
|
-
const currentTime = new Date('2025-11-29T08:00:00Z');
|
|
527
|
-
const now = currentTime;
|
|
528
|
-
|
|
529
|
-
const slots = [
|
|
530
|
-
{
|
|
531
|
-
startDt: "2025-11-28 01:30:00+00:00",
|
|
532
|
-
endDt: "2025-11-28 05:30:00+00:00", // Past
|
|
533
|
-
deltaKwh: -12,
|
|
534
|
-
meta: { source: "smart-charge" }
|
|
535
|
-
},
|
|
536
|
-
{
|
|
537
|
-
startDt: "2025-11-29 21:30:00+00:00",
|
|
538
|
-
endDt: "2025-11-29 22:00:00+00:00", // Future
|
|
539
|
-
deltaKwh: 0,
|
|
540
|
-
meta: { source: "smart-charge" }
|
|
541
|
-
},
|
|
542
|
-
{
|
|
543
|
-
startDt: "2025-11-29 23:00:00+00:00",
|
|
544
|
-
endDt: "2025-11-30 04:00:00+00:00", // Future
|
|
545
|
-
deltaKwh: -12,
|
|
546
|
-
meta: { source: "smart-charge" }
|
|
547
|
-
}
|
|
548
|
-
];
|
|
549
|
-
|
|
550
|
-
const activeAndFutureSlots = slots.filter(s => new Date(s.endDt) > now);
|
|
551
|
-
|
|
552
|
-
expect(activeAndFutureSlots.length).toBe(2);
|
|
553
|
-
expect(activeAndFutureSlots[0].endDt).toBe("2025-11-29 22:00:00+00:00");
|
|
554
|
-
expect(activeAndFutureSlots[1].endDt).toBe("2025-11-30 04:00:00+00:00");
|
|
555
|
-
});
|
|
556
|
-
|
|
557
|
-
/**
|
|
558
|
-
* @test Slot Filtering - Total Energy Calculation
|
|
559
|
-
* @scenario Calculate total energy across all active/future slots
|
|
560
|
-
* @given Multiple slots with different deltaKwh values
|
|
561
|
-
* @expect Total energy is sum of all deltaKwh
|
|
562
|
-
*/
|
|
563
|
-
test('should calculate total energy correctly', () => {
|
|
564
|
-
const slots = [
|
|
565
|
-
{ deltaKwh: -10.5 },
|
|
566
|
-
{ deltaKwh: -15.2 },
|
|
567
|
-
{ deltaKwh: -8.3 }
|
|
568
|
-
];
|
|
569
|
-
|
|
570
|
-
const totalEnergy = slots.reduce((sum, s) => sum + (s.deltaKwh || 0), 0);
|
|
571
|
-
|
|
572
|
-
expect(totalEnergy).toBeCloseTo(-34.0, 1);
|
|
573
|
-
});
|
|
574
|
-
|
|
575
|
-
/**
|
|
576
|
-
* @test Slot Filtering - Window Calculation
|
|
577
|
-
* @scenario Calculate overall charging window
|
|
578
|
-
* @given Multiple slots
|
|
579
|
-
* @expect Window start = first slot start
|
|
580
|
-
* @expect Window end = last slot end
|
|
581
|
-
*/
|
|
582
|
-
test('should calculate charging window correctly', () => {
|
|
583
|
-
const slots = [
|
|
584
|
-
{
|
|
585
|
-
startDt: "2025-11-29 21:30:00+00:00",
|
|
586
|
-
endDt: "2025-11-29 22:00:00+00:00"
|
|
587
|
-
},
|
|
588
|
-
{
|
|
589
|
-
startDt: "2025-11-29 23:00:00+00:00",
|
|
590
|
-
endDt: "2025-11-30 04:00:00+00:00"
|
|
591
|
-
}
|
|
592
|
-
];
|
|
593
|
-
|
|
594
|
-
const windowStart = slots.length > 0 ? slots[0].startDt : null;
|
|
595
|
-
const windowEnd = slots.length > 0 ? slots[slots.length - 1].endDt : null;
|
|
596
|
-
|
|
597
|
-
expect(windowStart).toBe("2025-11-29 21:30:00+00:00");
|
|
598
|
-
expect(windowEnd).toBe("2025-11-30 04:00:00+00:00");
|
|
599
|
-
});
|
|
600
|
-
});
|
|
601
|
-
});
|