node-red-contrib-octopus-intelligent 1.0.2 → 1.0.4

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.
@@ -0,0 +1,495 @@
1
+ /**
2
+ * Charging Now Validation Mode Tests
3
+ *
4
+ * Comprehensive tests using real API response structure to verify that
5
+ * charging_now is correctly detected across various scenarios:
6
+ * - 1 slot, 3 slots, 6 slots
7
+ * - Active slot at different positions (first, middle, last)
8
+ * - No active slots (between slots, before all slots)
9
+ * - Validation mode vs normal mode
10
+ *
11
+ * Based on real API response structure from user's system.
12
+ */
13
+
14
+ const {
15
+ mockRealApiResponse_1Slot_Active,
16
+ mockRealApiResponse_1Slot_Future,
17
+ mockRealApiResponse_3Slots_FirstActive,
18
+ mockRealApiResponse_3Slots_MiddleActive,
19
+ mockRealApiResponse_3Slots_LastActive,
20
+ mockRealApiResponse_3Slots_NoneActive,
21
+ mockRealApiResponse_6Slots_FirstActive,
22
+ mockRealApiResponse_6Slots_ThirdActive,
23
+ mockRealApiResponse_6Slots_LastActive,
24
+ mockRealApiResponse_6Slots_NoneActive,
25
+ createMockBroker
26
+ } = require('../test-mocks');
27
+
28
+ describe('Charging Now - Real API Data Tests', () => {
29
+ let mockBroker;
30
+ let chargingNow;
31
+ let cachedSlots;
32
+ let stateCheckInterval;
33
+
34
+ // Mock node for logging
35
+ const mockNode = {
36
+ log: jest.fn(),
37
+ warn: jest.fn()
38
+ };
39
+
40
+ beforeEach(() => {
41
+ jest.clearAllMocks();
42
+ jest.useFakeTimers();
43
+
44
+ mockBroker = createMockBroker();
45
+
46
+ // Initialize state variables
47
+ chargingNow = false;
48
+ cachedSlots = [];
49
+ stateCheckInterval = null;
50
+ });
51
+
52
+ afterEach(() => {
53
+ jest.useRealTimers();
54
+ });
55
+
56
+ // ==========================================================================
57
+ // Helper Functions
58
+ // ==========================================================================
59
+
60
+ function publishChargingState(state) {
61
+ chargingNow = state;
62
+ const enableMqtt = true;
63
+ const stateTopic = 'nodered_octopus/A-TEST1234/status';
64
+
65
+ if (enableMqtt && mockBroker) {
66
+ const payload = state ? "ON" : "OFF";
67
+ mockBroker.client.publish(`${stateTopic}/charging_now`, payload, { retain: true });
68
+ }
69
+ }
70
+
71
+ function setupChargingTimers(slots) {
72
+ cachedSlots = slots;
73
+ const now = new Date();
74
+
75
+ const activeSlot = slots.find(s => {
76
+ const start = new Date(s.startDt);
77
+ const end = new Date(s.endDt);
78
+ return start <= now && end > now;
79
+ });
80
+
81
+ if (activeSlot) {
82
+ const newState = true;
83
+ if (chargingNow !== newState) {
84
+ publishChargingState(newState);
85
+ }
86
+ mockNode.log("Currently charging - end timer set");
87
+ } else {
88
+ const newState = false;
89
+ if (chargingNow !== newState) {
90
+ publishChargingState(newState);
91
+ }
92
+ }
93
+ }
94
+
95
+ // BUGGY version - skips setupChargingTimers in validation mode
96
+ function processDataWithBug(slots, validationMode) {
97
+ if (!validationMode) {
98
+ setupChargingTimers(slots);
99
+ if (!stateCheckInterval) {
100
+ stateCheckInterval = setInterval(() => {}, 10000);
101
+ }
102
+ }
103
+ return { charging_now: chargingNow };
104
+ }
105
+
106
+ // FIXED version - always calls setupChargingTimers
107
+ function processDataFixed(slots, validationMode) {
108
+ setupChargingTimers(slots);
109
+ if (!validationMode && !stateCheckInterval) {
110
+ stateCheckInterval = setInterval(() => {}, 10000);
111
+ }
112
+ return { charging_now: chargingNow };
113
+ }
114
+
115
+ // ==========================================================================
116
+ // Test Matrix: 1 Slot Scenarios
117
+ // ==========================================================================
118
+
119
+ describe('1 Slot Scenarios', () => {
120
+ test('1 slot - ACTIVE (charging now)', () => {
121
+ const mock = mockRealApiResponse_1Slot_Active;
122
+ jest.setSystemTime(new Date(mock.testTime));
123
+
124
+ const slots = mock.data.data.plannedDispatches;
125
+ chargingNow = false;
126
+
127
+ setupChargingTimers(slots);
128
+
129
+ expect(chargingNow).toBe(mock.expectedChargingNow);
130
+ expect(chargingNow).toBe(true);
131
+ });
132
+
133
+ test('1 slot - NOT ACTIVE (future)', () => {
134
+ const mock = mockRealApiResponse_1Slot_Future;
135
+ jest.setSystemTime(new Date(mock.testTime));
136
+
137
+ const slots = mock.data.data.plannedDispatches;
138
+ chargingNow = false;
139
+
140
+ setupChargingTimers(slots);
141
+
142
+ expect(chargingNow).toBe(mock.expectedChargingNow);
143
+ expect(chargingNow).toBe(false);
144
+ });
145
+ });
146
+
147
+ // ==========================================================================
148
+ // Test Matrix: 3 Slots Scenarios
149
+ // ==========================================================================
150
+
151
+ describe('3 Slots Scenarios', () => {
152
+ test('3 slots - FIRST ACTIVE', () => {
153
+ const mock = mockRealApiResponse_3Slots_FirstActive;
154
+ jest.setSystemTime(new Date(mock.testTime));
155
+
156
+ const slots = mock.data.data.plannedDispatches;
157
+ chargingNow = false;
158
+
159
+ setupChargingTimers(slots);
160
+
161
+ expect(chargingNow).toBe(mock.expectedChargingNow);
162
+ expect(chargingNow).toBe(true);
163
+ });
164
+
165
+ test('3 slots - MIDDLE ACTIVE', () => {
166
+ const mock = mockRealApiResponse_3Slots_MiddleActive;
167
+ jest.setSystemTime(new Date(mock.testTime));
168
+
169
+ const slots = mock.data.data.plannedDispatches;
170
+ chargingNow = false;
171
+
172
+ setupChargingTimers(slots);
173
+
174
+ expect(chargingNow).toBe(mock.expectedChargingNow);
175
+ expect(chargingNow).toBe(true);
176
+ });
177
+
178
+ test('3 slots - LAST ACTIVE', () => {
179
+ const mock = mockRealApiResponse_3Slots_LastActive;
180
+ jest.setSystemTime(new Date(mock.testTime));
181
+
182
+ const slots = mock.data.data.plannedDispatches;
183
+ chargingNow = false;
184
+
185
+ setupChargingTimers(slots);
186
+
187
+ expect(chargingNow).toBe(mock.expectedChargingNow);
188
+ expect(chargingNow).toBe(true);
189
+ });
190
+
191
+ test('3 slots - NONE ACTIVE (between slots)', () => {
192
+ const mock = mockRealApiResponse_3Slots_NoneActive;
193
+ jest.setSystemTime(new Date(mock.testTime));
194
+
195
+ const slots = mock.data.data.plannedDispatches;
196
+ chargingNow = false;
197
+
198
+ setupChargingTimers(slots);
199
+
200
+ expect(chargingNow).toBe(mock.expectedChargingNow);
201
+ expect(chargingNow).toBe(false);
202
+ });
203
+ });
204
+
205
+ // ==========================================================================
206
+ // Test Matrix: 6 Slots Scenarios
207
+ // ==========================================================================
208
+
209
+ describe('6 Slots Scenarios', () => {
210
+ test('6 slots - FIRST ACTIVE', () => {
211
+ const mock = mockRealApiResponse_6Slots_FirstActive;
212
+ jest.setSystemTime(new Date(mock.testTime));
213
+
214
+ const slots = mock.data.data.plannedDispatches;
215
+ chargingNow = false;
216
+
217
+ setupChargingTimers(slots);
218
+
219
+ expect(chargingNow).toBe(mock.expectedChargingNow);
220
+ expect(chargingNow).toBe(true);
221
+ });
222
+
223
+ test('6 slots - THIRD ACTIVE (middle)', () => {
224
+ const mock = mockRealApiResponse_6Slots_ThirdActive;
225
+ jest.setSystemTime(new Date(mock.testTime));
226
+
227
+ const slots = mock.data.data.plannedDispatches;
228
+ chargingNow = false;
229
+
230
+ setupChargingTimers(slots);
231
+
232
+ expect(chargingNow).toBe(mock.expectedChargingNow);
233
+ expect(chargingNow).toBe(true);
234
+ });
235
+
236
+ test('6 slots - LAST ACTIVE', () => {
237
+ const mock = mockRealApiResponse_6Slots_LastActive;
238
+ jest.setSystemTime(new Date(mock.testTime));
239
+
240
+ const slots = mock.data.data.plannedDispatches;
241
+ chargingNow = false;
242
+
243
+ setupChargingTimers(slots);
244
+
245
+ expect(chargingNow).toBe(mock.expectedChargingNow);
246
+ expect(chargingNow).toBe(true);
247
+ });
248
+
249
+ test('6 slots - NONE ACTIVE (between slots)', () => {
250
+ const mock = mockRealApiResponse_6Slots_NoneActive;
251
+ jest.setSystemTime(new Date(mock.testTime));
252
+
253
+ const slots = mock.data.data.plannedDispatches;
254
+ chargingNow = false;
255
+
256
+ setupChargingTimers(slots);
257
+
258
+ expect(chargingNow).toBe(mock.expectedChargingNow);
259
+ expect(chargingNow).toBe(false);
260
+ });
261
+ });
262
+
263
+ // ==========================================================================
264
+ // Validation Mode Bug Tests (BEFORE FIX)
265
+ // ==========================================================================
266
+
267
+ describe('BEFORE FIX: Validation Mode Bug', () => {
268
+ test('DEMONSTRATES BUG: validation mode skips setupChargingTimers() with active slot', () => {
269
+ const mock = mockRealApiResponse_3Slots_MiddleActive;
270
+ jest.setSystemTime(new Date(mock.testTime));
271
+
272
+ const slots = mock.data.data.plannedDispatches;
273
+ chargingNow = false;
274
+
275
+ // Process in validation mode with BUGGY version
276
+ const payload = processDataWithBug(slots, true);
277
+
278
+ // BUG: chargingNow remains false even though we're in active slot
279
+ expect(chargingNow).toBe(false); // ❌ Should be true
280
+ expect(payload.charging_now).toBe(false); // ❌ Should be true
281
+
282
+ // MQTT not updated (setupChargingTimers was skipped)
283
+ const published = mockBroker.getPublished();
284
+ expect(published.length).toBe(0);
285
+ });
286
+
287
+ test('COMPARISON: normal mode correctly updates state', () => {
288
+ const mock = mockRealApiResponse_3Slots_MiddleActive;
289
+ jest.setSystemTime(new Date(mock.testTime));
290
+
291
+ const slots = mock.data.data.plannedDispatches;
292
+ chargingNow = false;
293
+
294
+ // Process in NORMAL mode
295
+ const payload = processDataWithBug(slots, false);
296
+
297
+ // Normal mode works correctly
298
+ expect(chargingNow).toBe(true); // ✅ Correct
299
+ expect(payload.charging_now).toBe(true); // ✅ Correct
300
+
301
+ const published = mockBroker.getPublished();
302
+ expect(published.length).toBe(1);
303
+ expect(published[0].payload).toBe('ON');
304
+ });
305
+
306
+ test('BUG AFFECTS ALL SLOT COUNTS: 1 slot validation mode', () => {
307
+ const mock = mockRealApiResponse_1Slot_Active;
308
+ jest.setSystemTime(new Date(mock.testTime));
309
+
310
+ const slots = mock.data.data.plannedDispatches;
311
+ chargingNow = false;
312
+
313
+ const payload = processDataWithBug(slots, true);
314
+
315
+ expect(chargingNow).toBe(false); // ❌ Bug
316
+ });
317
+
318
+ test('BUG AFFECTS ALL SLOT COUNTS: 6 slots validation mode', () => {
319
+ const mock = mockRealApiResponse_6Slots_LastActive;
320
+ jest.setSystemTime(new Date(mock.testTime));
321
+
322
+ const slots = mock.data.data.plannedDispatches;
323
+ chargingNow = false;
324
+
325
+ const payload = processDataWithBug(slots, true);
326
+
327
+ expect(chargingNow).toBe(false); // ❌ Bug
328
+ });
329
+ });
330
+
331
+ // ==========================================================================
332
+ // Fixed Behavior Tests (AFTER FIX)
333
+ // ==========================================================================
334
+
335
+ describe('AFTER FIX: Validation Mode Fixed', () => {
336
+ test('VERIFIES FIX: validation mode now correctly detects 1 active slot', () => {
337
+ const mock = mockRealApiResponse_1Slot_Active;
338
+ jest.setSystemTime(new Date(mock.testTime));
339
+
340
+ const slots = mock.data.data.plannedDispatches;
341
+ chargingNow = false;
342
+
343
+ const payload = processDataFixed(slots, true);
344
+
345
+ expect(chargingNow).toBe(true); // ✅ FIXED
346
+ expect(payload.charging_now).toBe(true); // ✅ FIXED
347
+
348
+ const published = mockBroker.getPublished();
349
+ expect(published.length).toBe(1);
350
+ expect(published[0].payload).toBe('ON');
351
+ });
352
+
353
+ test('VERIFIES FIX: validation mode works with 3 slots - first active', () => {
354
+ const mock = mockRealApiResponse_3Slots_FirstActive;
355
+ jest.setSystemTime(new Date(mock.testTime));
356
+
357
+ const slots = mock.data.data.plannedDispatches;
358
+ chargingNow = false;
359
+
360
+ const payload = processDataFixed(slots, true);
361
+
362
+ expect(chargingNow).toBe(true); // ✅ FIXED
363
+ });
364
+
365
+ test('VERIFIES FIX: validation mode works with 3 slots - middle active', () => {
366
+ const mock = mockRealApiResponse_3Slots_MiddleActive;
367
+ jest.setSystemTime(new Date(mock.testTime));
368
+
369
+ const slots = mock.data.data.plannedDispatches;
370
+ chargingNow = false;
371
+
372
+ const payload = processDataFixed(slots, true);
373
+
374
+ expect(chargingNow).toBe(true); // ✅ FIXED
375
+ });
376
+
377
+ test('VERIFIES FIX: validation mode works with 3 slots - last active', () => {
378
+ const mock = mockRealApiResponse_3Slots_LastActive;
379
+ jest.setSystemTime(new Date(mock.testTime));
380
+
381
+ const slots = mock.data.data.plannedDispatches;
382
+ chargingNow = false;
383
+
384
+ const payload = processDataFixed(slots, true);
385
+
386
+ expect(chargingNow).toBe(true); // ✅ FIXED
387
+ });
388
+
389
+ test('VERIFIES FIX: validation mode works with 6 slots - first active', () => {
390
+ const mock = mockRealApiResponse_6Slots_FirstActive;
391
+ jest.setSystemTime(new Date(mock.testTime));
392
+
393
+ const slots = mock.data.data.plannedDispatches;
394
+ chargingNow = false;
395
+
396
+ const payload = processDataFixed(slots, true);
397
+
398
+ expect(chargingNow).toBe(true); // ✅ FIXED
399
+ });
400
+
401
+ test('VERIFIES FIX: validation mode works with 6 slots - middle active', () => {
402
+ const mock = mockRealApiResponse_6Slots_ThirdActive;
403
+ jest.setSystemTime(new Date(mock.testTime));
404
+
405
+ const slots = mock.data.data.plannedDispatches;
406
+ chargingNow = false;
407
+
408
+ const payload = processDataFixed(slots, true);
409
+
410
+ expect(chargingNow).toBe(true); // ✅ FIXED
411
+ });
412
+
413
+ test('VERIFIES FIX: validation mode works with 6 slots - last active', () => {
414
+ const mock = mockRealApiResponse_6Slots_LastActive;
415
+ jest.setSystemTime(new Date(mock.testTime));
416
+
417
+ const slots = mock.data.data.plannedDispatches;
418
+ chargingNow = false;
419
+
420
+ const payload = processDataFixed(slots, true);
421
+
422
+ expect(chargingNow).toBe(true); // ✅ FIXED
423
+ });
424
+
425
+ test('VERIFIES FIX: validation mode correctly shows not charging when between slots', () => {
426
+ const mock = mockRealApiResponse_3Slots_NoneActive;
427
+ jest.setSystemTime(new Date(mock.testTime));
428
+
429
+ const slots = mock.data.data.plannedDispatches;
430
+ chargingNow = false;
431
+
432
+ const payload = processDataFixed(slots, true);
433
+
434
+ expect(chargingNow).toBe(false); // ✅ Correct
435
+ });
436
+
437
+ test('VERIFIES FIX: reconciliation loop NOT started in validation mode', () => {
438
+ const mock = mockRealApiResponse_1Slot_Active;
439
+ jest.setSystemTime(new Date(mock.testTime));
440
+
441
+ const slots = mock.data.data.plannedDispatches;
442
+ stateCheckInterval = null;
443
+
444
+ processDataFixed(slots, true);
445
+
446
+ expect(stateCheckInterval).toBeNull();
447
+ });
448
+
449
+ test('VERIFIES FIX: reconciliation loop IS started in normal mode', () => {
450
+ const mock = mockRealApiResponse_1Slot_Active;
451
+ jest.setSystemTime(new Date(mock.testTime));
452
+
453
+ const slots = mock.data.data.plannedDispatches;
454
+ stateCheckInterval = null;
455
+
456
+ processDataFixed(slots, false);
457
+
458
+ expect(stateCheckInterval).not.toBeNull();
459
+ });
460
+ });
461
+
462
+ // ==========================================================================
463
+ // Summary Test - Complete Matrix Coverage
464
+ // ==========================================================================
465
+
466
+ describe('Test Matrix Summary', () => {
467
+ test('SUMMARY: All real API scenarios covered', () => {
468
+ const scenarios = [
469
+ { name: '1 slot active', mock: mockRealApiResponse_1Slot_Active, expected: true },
470
+ { name: '1 slot future', mock: mockRealApiResponse_1Slot_Future, expected: false },
471
+ { name: '3 slots first active', mock: mockRealApiResponse_3Slots_FirstActive, expected: true },
472
+ { name: '3 slots middle active', mock: mockRealApiResponse_3Slots_MiddleActive, expected: true },
473
+ { name: '3 slots last active', mock: mockRealApiResponse_3Slots_LastActive, expected: true },
474
+ { name: '3 slots none active', mock: mockRealApiResponse_3Slots_NoneActive, expected: false },
475
+ { name: '6 slots first active', mock: mockRealApiResponse_6Slots_FirstActive, expected: true },
476
+ { name: '6 slots middle active', mock: mockRealApiResponse_6Slots_ThirdActive, expected: true },
477
+ { name: '6 slots last active', mock: mockRealApiResponse_6Slots_LastActive, expected: true },
478
+ { name: '6 slots none active', mock: mockRealApiResponse_6Slots_NoneActive, expected: false }
479
+ ];
480
+
481
+ scenarios.forEach(scenario => {
482
+ jest.setSystemTime(new Date(scenario.mock.testTime));
483
+ const slots = scenario.mock.data.data.plannedDispatches;
484
+ chargingNow = false;
485
+
486
+ setupChargingTimers(slots);
487
+
488
+ expect(chargingNow).toBe(scenario.expected);
489
+ });
490
+
491
+ // Verify test matrix coverage
492
+ expect(scenarios.length).toBe(10); // Complete coverage
493
+ });
494
+ });
495
+ });