node-red-contrib-octopus-intelligent 1.0.3 → 1.0.5

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,478 @@
1
+ /**
2
+ * Data Processing Tests
3
+ *
4
+ * Tests for how API responses are processed into Node-RED payloads
5
+ * Tests buildDefaultPayload() and payload construction logic
6
+ */
7
+
8
+ const {
9
+ mockDataWithSlots,
10
+ mockDataWithActiveSlot,
11
+ mockDataNoSlots,
12
+ mockDataWithThreeSlots,
13
+ expectedPayloadTwoSlots,
14
+ expectedPayloadActiveSlot,
15
+ expectedPayloadNoSlots
16
+ } = require('../test-mocks');
17
+
18
+ describe('Data Processing', () => {
19
+ // ==========================================================================
20
+ // buildDefaultPayload() Tests
21
+ // ==========================================================================
22
+
23
+ describe('buildDefaultPayload()', () => {
24
+ /**
25
+ * @test buildDefaultPayload - Build Default Payload
26
+ * @scenario Creating default payload structure when errors occur
27
+ * @given Current confirmed/pending state exists
28
+ * @expect Payload with all null slot fields and zero energy
29
+ */
30
+ test('should create default payload with current state', () => {
31
+ const confirmedLimit = 80;
32
+ const confirmedTime = "08:00";
33
+ const pendingLimit = 80;
34
+ const pendingTime = "08:00";
35
+ const chargingNow = false;
36
+
37
+ function buildDefaultPayload() {
38
+ return {
39
+ next_start: null,
40
+ total_energy: 0,
41
+ next_kwh: "0",
42
+ next_source: "unknown",
43
+ confirmed_limit: confirmedLimit,
44
+ confirmed_time: confirmedTime,
45
+ pending_limit: pendingLimit,
46
+ pending_time: pendingTime,
47
+ charging_now: chargingNow,
48
+ slot1_start: null,
49
+ slot1_end: null,
50
+ slot2_start: null,
51
+ slot2_end: null,
52
+ slot3_start: null,
53
+ slot3_end: null,
54
+ window_start: null,
55
+ window_end: null,
56
+ next_start_raw: null,
57
+ slot1_start_raw: null,
58
+ slot1_end_raw: null,
59
+ slot2_start_raw: null,
60
+ slot2_end_raw: null,
61
+ slot3_start_raw: null,
62
+ slot3_end_raw: null,
63
+ window_start_raw: null,
64
+ window_end_raw: null
65
+ };
66
+ }
67
+
68
+ const payload = buildDefaultPayload();
69
+
70
+ expect(payload.next_start).toBeNull();
71
+ expect(payload.total_energy).toBe(0);
72
+ expect(payload.next_kwh).toBe("0");
73
+ expect(payload.next_source).toBe("unknown");
74
+ expect(payload.confirmed_limit).toBe(80);
75
+ expect(payload.confirmed_time).toBe("08:00");
76
+ expect(payload.charging_now).toBe(false);
77
+
78
+ // All slot fields should be null
79
+ expect(payload.slot1_start).toBeNull();
80
+ expect(payload.slot1_end).toBeNull();
81
+ expect(payload.slot2_start).toBeNull();
82
+ expect(payload.slot2_end).toBeNull();
83
+ expect(payload.slot3_start).toBeNull();
84
+ expect(payload.slot3_end).toBeNull();
85
+ });
86
+
87
+ /**
88
+ * @test buildDefaultPayload - Preserves User State
89
+ * @scenario Default payload preserves confirmed and pending values
90
+ * @given User has set preferences to different values
91
+ * @expect Payload includes current confirmed/pending state
92
+ */
93
+ test('should preserve confirmed and pending state', () => {
94
+ const confirmedLimit = 85;
95
+ const confirmedTime = "07:30";
96
+ const pendingLimit = 90;
97
+ const pendingTime = "06:00";
98
+ const chargingNow = true;
99
+
100
+ function buildDefaultPayload() {
101
+ return {
102
+ next_start: null,
103
+ total_energy: 0,
104
+ next_kwh: "0",
105
+ next_source: "unknown",
106
+ confirmed_limit: confirmedLimit,
107
+ confirmed_time: confirmedTime,
108
+ pending_limit: pendingLimit,
109
+ pending_time: pendingTime,
110
+ charging_now: chargingNow,
111
+ slot1_start: null,
112
+ slot1_end: null,
113
+ slot2_start: null,
114
+ slot2_end: null,
115
+ slot3_start: null,
116
+ slot3_end: null,
117
+ window_start: null,
118
+ window_end: null,
119
+ next_start_raw: null,
120
+ slot1_start_raw: null,
121
+ slot1_end_raw: null,
122
+ slot2_start_raw: null,
123
+ slot2_end_raw: null,
124
+ slot3_start_raw: null,
125
+ slot3_end_raw: null,
126
+ window_start_raw: null,
127
+ window_end_raw: null
128
+ };
129
+ }
130
+
131
+ const payload = buildDefaultPayload();
132
+
133
+ expect(payload.confirmed_limit).toBe(85);
134
+ expect(payload.confirmed_time).toBe("07:30");
135
+ expect(payload.pending_limit).toBe(90);
136
+ expect(payload.pending_time).toBe("06:00");
137
+ expect(payload.charging_now).toBe(true);
138
+ });
139
+ });
140
+
141
+ // ==========================================================================
142
+ // Payload Construction from API Response
143
+ // ==========================================================================
144
+
145
+ describe('Payload Construction', () => {
146
+ /**
147
+ * Simulate the payload building logic from fetchData()
148
+ */
149
+ function buildPayloadFromSlots(slots, prefs, currentTime, chargingNow) {
150
+ const now = new Date(currentTime);
151
+ const activeAndFutureSlots = slots.filter(s => new Date(s.endDt) > now);
152
+ const nextSlot = activeAndFutureSlots[0] || null;
153
+ const totalEnergy = activeAndFutureSlots.reduce((sum, s) => sum + (s.deltaKwh || 0), 0);
154
+
155
+ return {
156
+ next_start: nextSlot ? nextSlot.startDt : null,
157
+ total_energy: parseFloat(totalEnergy.toFixed(2)),
158
+ next_kwh: nextSlot ? nextSlot.deltaKwh.toFixed(2) : "0",
159
+ next_source: nextSlot && nextSlot.meta ? nextSlot.meta.source : "unknown",
160
+ confirmed_limit: prefs.weekdayTargetSoc,
161
+ confirmed_time: prefs.weekdayTargetTime,
162
+ pending_limit: prefs.weekdayTargetSoc,
163
+ pending_time: prefs.weekdayTargetTime,
164
+ charging_now: chargingNow,
165
+ slot1_start: activeAndFutureSlots[0] ? activeAndFutureSlots[0].startDt : null,
166
+ slot1_end: activeAndFutureSlots[0] ? activeAndFutureSlots[0].endDt : null,
167
+ slot2_start: activeAndFutureSlots[1] ? activeAndFutureSlots[1].startDt : null,
168
+ slot2_end: activeAndFutureSlots[1] ? activeAndFutureSlots[1].endDt : null,
169
+ slot3_start: activeAndFutureSlots[2] ? activeAndFutureSlots[2].startDt : null,
170
+ slot3_end: activeAndFutureSlots[2] ? activeAndFutureSlots[2].endDt : null,
171
+ window_start: activeAndFutureSlots.length > 0 ? activeAndFutureSlots[0].startDt : null,
172
+ window_end: activeAndFutureSlots.length > 0 ? activeAndFutureSlots[activeAndFutureSlots.length - 1].endDt : null,
173
+ next_start_raw: nextSlot ? nextSlot.startDt : null,
174
+ slot1_start_raw: activeAndFutureSlots[0] ? activeAndFutureSlots[0].startDt : null,
175
+ slot1_end_raw: activeAndFutureSlots[0] ? activeAndFutureSlots[0].endDt : null,
176
+ slot2_start_raw: activeAndFutureSlots[1] ? activeAndFutureSlots[1].startDt : null,
177
+ slot2_end_raw: activeAndFutureSlots[1] ? activeAndFutureSlots[1].endDt : null,
178
+ slot3_start_raw: activeAndFutureSlots[2] ? activeAndFutureSlots[2].startDt : null,
179
+ slot3_end_raw: activeAndFutureSlots[2] ? activeAndFutureSlots[2].endDt : null,
180
+ window_start_raw: activeAndFutureSlots.length > 0 ? activeAndFutureSlots[0].startDt : null,
181
+ window_end_raw: activeAndFutureSlots.length > 0 ? activeAndFutureSlots[activeAndFutureSlots.length - 1].endDt : null
182
+ };
183
+ }
184
+
185
+ /**
186
+ * @test Payload Construction - Two Future Slots
187
+ * @scenario Processing API response with 2 future slots
188
+ * @given mockDataWithSlots (2 slots for tonight)
189
+ * @given Current time: 2025-11-29T08:00:00Z (before slots)
190
+ * @expect Payload matches expectedPayloadTwoSlots structure
191
+ */
192
+ test('should build correct payload with two future slots', () => {
193
+ const apiData = mockDataWithSlots.data.data;
194
+ const currentTime = '2025-11-29T08:00:00Z';
195
+ const chargingNow = false;
196
+
197
+ const payload = buildPayloadFromSlots(
198
+ apiData.plannedDispatches,
199
+ apiData.vehicleChargingPreferences,
200
+ currentTime,
201
+ chargingNow
202
+ );
203
+
204
+ // Verify key fields
205
+ expect(payload.next_start).toBe("2025-11-29 21:30:00+00:00");
206
+ expect(payload.total_energy).toBe(-12);
207
+ expect(payload.next_kwh).toBe("0.00");
208
+ expect(payload.next_source).toBe("smart-charge");
209
+ expect(payload.charging_now).toBe(false);
210
+
211
+ // Verify slot assignments
212
+ expect(payload.slot1_start).toBe("2025-11-29 21:30:00+00:00");
213
+ expect(payload.slot1_end).toBe("2025-11-29 22:00:00+00:00");
214
+ expect(payload.slot2_start).toBe("2025-11-29 23:00:00+00:00");
215
+ expect(payload.slot2_end).toBe("2025-11-30 04:00:00+00:00");
216
+ expect(payload.slot3_start).toBeNull();
217
+ expect(payload.slot3_end).toBeNull();
218
+
219
+ // Verify window
220
+ expect(payload.window_start).toBe("2025-11-29 21:30:00+00:00");
221
+ expect(payload.window_end).toBe("2025-11-30 04:00:00+00:00");
222
+
223
+ // Verify raw timestamps match
224
+ expect(payload.next_start_raw).toBe(payload.next_start);
225
+ expect(payload.slot1_start_raw).toBe(payload.slot1_start);
226
+ });
227
+
228
+ /**
229
+ * @test Payload Construction - Active Slot
230
+ * @scenario Processing API response with currently active slot
231
+ * @given mockDataWithActiveSlot (slot from 01:30 to 05:30)
232
+ * @given Current time: 2025-11-29T02:00:00Z (in middle of slot)
233
+ * @expect Payload shows charging_now = true
234
+ */
235
+ test('should build correct payload with active slot', () => {
236
+ const apiData = mockDataWithActiveSlot.data.data;
237
+ const currentTime = '2025-11-29T02:00:00Z';
238
+ const chargingNow = true; // Active slot detected
239
+
240
+ const payload = buildPayloadFromSlots(
241
+ apiData.plannedDispatches,
242
+ apiData.vehicleChargingPreferences,
243
+ currentTime,
244
+ chargingNow
245
+ );
246
+
247
+ expect(payload.next_start).toBe("2025-11-29 01:30:00+00:00");
248
+ expect(payload.total_energy).toBe(-15.5);
249
+ expect(payload.next_kwh).toBe("-15.50");
250
+ expect(payload.next_source).toBe("smart-charge");
251
+ expect(payload.charging_now).toBe(true);
252
+
253
+ expect(payload.slot1_start).toBe("2025-11-29 01:30:00+00:00");
254
+ expect(payload.slot1_end).toBe("2025-11-29 05:30:00+00:00");
255
+ expect(payload.slot2_start).toBeNull();
256
+ expect(payload.slot2_end).toBeNull();
257
+ });
258
+
259
+ /**
260
+ * @test Payload Construction - No Slots
261
+ * @scenario Processing API response with no slots
262
+ * @given mockDataNoSlots (empty plannedDispatches)
263
+ * @expect All slot fields are null
264
+ * @expect total_energy = 0
265
+ * @expect charging_now = false
266
+ */
267
+ test('should build correct payload with no slots', () => {
268
+ const apiData = mockDataNoSlots.data.data;
269
+ const currentTime = '2025-11-29T08:00:00Z';
270
+ const chargingNow = false;
271
+
272
+ const payload = buildPayloadFromSlots(
273
+ apiData.plannedDispatches,
274
+ apiData.vehicleChargingPreferences,
275
+ currentTime,
276
+ chargingNow
277
+ );
278
+
279
+ expect(payload.next_start).toBeNull();
280
+ expect(payload.total_energy).toBe(0);
281
+ expect(payload.next_kwh).toBe("0");
282
+ expect(payload.next_source).toBe("unknown");
283
+ expect(payload.charging_now).toBe(false);
284
+
285
+ expect(payload.slot1_start).toBeNull();
286
+ expect(payload.slot1_end).toBeNull();
287
+ expect(payload.slot2_start).toBeNull();
288
+ expect(payload.slot2_end).toBeNull();
289
+ expect(payload.slot3_start).toBeNull();
290
+ expect(payload.slot3_end).toBeNull();
291
+
292
+ expect(payload.window_start).toBeNull();
293
+ expect(payload.window_end).toBeNull();
294
+ });
295
+
296
+ /**
297
+ * @test Payload Construction - Three Slots
298
+ * @scenario Processing API response with 3 slots
299
+ * @given mockDataWithThreeSlots
300
+ * @given Current time before all slots
301
+ * @expect All three slots populated
302
+ * @expect Total energy is sum of all slots
303
+ */
304
+ test('should build correct payload with three slots', () => {
305
+ const apiData = mockDataWithThreeSlots.data.data;
306
+ const currentTime = '2025-11-29T00:00:00Z';
307
+ const chargingNow = false;
308
+
309
+ const payload = buildPayloadFromSlots(
310
+ apiData.plannedDispatches,
311
+ apiData.vehicleChargingPreferences,
312
+ currentTime,
313
+ chargingNow
314
+ );
315
+
316
+ // Should have all 3 slots
317
+ expect(payload.slot1_start).toBe("2025-11-29 01:30:00+00:00");
318
+ expect(payload.slot1_end).toBe("2025-11-29 03:30:00+00:00");
319
+ expect(payload.slot2_start).toBe("2025-11-29 12:00:00+00:00");
320
+ expect(payload.slot2_end).toBe("2025-11-29 13:00:00+00:00");
321
+ expect(payload.slot3_start).toBe("2025-11-30 02:00:00+00:00");
322
+ expect(payload.slot3_end).toBe("2025-11-30 05:30:00+00:00");
323
+
324
+ // Total energy: -10.2 + -5.5 + -18.7 = -34.4
325
+ expect(payload.total_energy).toBeCloseTo(-34.4, 1);
326
+
327
+ // Window should span from first to last slot
328
+ expect(payload.window_start).toBe("2025-11-29 01:30:00+00:00");
329
+ expect(payload.window_end).toBe("2025-11-30 05:30:00+00:00");
330
+
331
+ // Next slot details
332
+ expect(payload.next_start).toBe("2025-11-29 01:30:00+00:00");
333
+ expect(payload.next_kwh).toBe("-10.20");
334
+ expect(payload.next_source).toBe("smart-charge");
335
+ });
336
+
337
+ /**
338
+ * @test Payload Construction - Past Slots Filtered Out
339
+ * @scenario API returns past slots that should be ignored
340
+ * @given Current time: 2025-11-29T08:00:00Z
341
+ * @given Slot from yesterday (already completed)
342
+ * @expect Past slot filtered out
343
+ * @expect Payload shows no slots
344
+ */
345
+ test('should filter out past slots', () => {
346
+ const currentTime = '2025-11-29T08:00:00Z';
347
+ const chargingNow = false;
348
+
349
+ const slots = [
350
+ {
351
+ startDt: "2025-11-28 01:30:00+00:00",
352
+ endDt: "2025-11-28 05:30:00+00:00", // Yesterday - already ended
353
+ deltaKwh: -12,
354
+ meta: { source: "smart-charge" }
355
+ }
356
+ ];
357
+
358
+ const prefs = {
359
+ weekdayTargetSoc: 80,
360
+ weekdayTargetTime: "08:00"
361
+ };
362
+
363
+ const payload = buildPayloadFromSlots(slots, prefs, currentTime, chargingNow);
364
+
365
+ // Past slot should be filtered out
366
+ expect(payload.next_start).toBeNull();
367
+ expect(payload.total_energy).toBe(0);
368
+ expect(payload.slot1_start).toBeNull();
369
+ expect(payload.charging_now).toBe(false);
370
+ });
371
+
372
+ /**
373
+ * @test Payload Construction - Mixed Past and Future Slots
374
+ * @scenario API returns mix of past and future slots
375
+ * @given Current time: 2025-11-29T08:00:00Z
376
+ * @given One past slot, two future slots
377
+ * @expect Only future slots included
378
+ * @expect Total energy only counts future slots
379
+ */
380
+ test('should only include future slots when mixed with past', () => {
381
+ const currentTime = '2025-11-29T08:00:00Z';
382
+ const chargingNow = false;
383
+
384
+ const slots = [
385
+ {
386
+ startDt: "2025-11-28 01:30:00+00:00",
387
+ endDt: "2025-11-28 05:30:00+00:00", // Past
388
+ deltaKwh: -100, // Should not be counted
389
+ meta: { source: "smart-charge" }
390
+ },
391
+ {
392
+ startDt: "2025-11-29 21:30:00+00:00",
393
+ endDt: "2025-11-29 22:00:00+00:00", // Future
394
+ deltaKwh: -10,
395
+ meta: { source: "smart-charge" }
396
+ },
397
+ {
398
+ startDt: "2025-11-29 23:00:00+00:00",
399
+ endDt: "2025-11-30 04:00:00+00:00", // Future
400
+ deltaKwh: -15,
401
+ meta: { source: "smart-charge" }
402
+ }
403
+ ];
404
+
405
+ const prefs = {
406
+ weekdayTargetSoc: 80,
407
+ weekdayTargetTime: "04:00"
408
+ };
409
+
410
+ const payload = buildPayloadFromSlots(slots, prefs, currentTime, chargingNow);
411
+
412
+ // Should only have 2 future slots
413
+ expect(payload.slot1_start).toBe("2025-11-29 21:30:00+00:00");
414
+ expect(payload.slot2_start).toBe("2025-11-29 23:00:00+00:00");
415
+ expect(payload.slot3_start).toBeNull();
416
+
417
+ // Total energy should be -10 + -15 = -25 (not including -100 from past)
418
+ expect(payload.total_energy).toBe(-25);
419
+
420
+ // Next slot should be first future slot
421
+ expect(payload.next_start).toBe("2025-11-29 21:30:00+00:00");
422
+ expect(payload.next_kwh).toBe("-10.00");
423
+ });
424
+
425
+ /**
426
+ * @test Payload Construction - Preferences Extraction
427
+ * @scenario Verify preferences are correctly extracted
428
+ * @given API response with preferences
429
+ * @expect confirmed_limit and confirmed_time populated
430
+ */
431
+ test('should extract preferences correctly', () => {
432
+ const apiData = mockDataWithSlots.data.data;
433
+ const currentTime = '2025-11-29T08:00:00Z';
434
+ const chargingNow = false;
435
+
436
+ const payload = buildPayloadFromSlots(
437
+ apiData.plannedDispatches,
438
+ apiData.vehicleChargingPreferences,
439
+ currentTime,
440
+ chargingNow
441
+ );
442
+
443
+ expect(payload.confirmed_limit).toBe(80);
444
+ expect(payload.confirmed_time).toBe("04:00");
445
+ expect(payload.pending_limit).toBe(80);
446
+ expect(payload.pending_time).toBe("04:00");
447
+ });
448
+
449
+ /**
450
+ * @test Payload Construction - Bump Charge Source
451
+ * @scenario Verify bump-charge source is captured
452
+ * @given Slot with meta.source = "bump-charge"
453
+ * @expect next_source = "bump-charge"
454
+ */
455
+ test('should capture bump-charge source correctly', () => {
456
+ const currentTime = '2025-11-29T00:00:00Z';
457
+ const chargingNow = false;
458
+
459
+ const slots = [
460
+ {
461
+ startDt: "2025-11-29 12:00:00+00:00",
462
+ endDt: "2025-11-29 13:00:00+00:00",
463
+ deltaKwh: -5.5,
464
+ meta: { source: "bump-charge" }
465
+ }
466
+ ];
467
+
468
+ const prefs = {
469
+ weekdayTargetSoc: 90,
470
+ weekdayTargetTime: "07:00"
471
+ };
472
+
473
+ const payload = buildPayloadFromSlots(slots, prefs, currentTime, chargingNow);
474
+
475
+ expect(payload.next_source).toBe("bump-charge");
476
+ });
477
+ });
478
+ });