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.
package/test-mocks.js ADDED
@@ -0,0 +1,1038 @@
1
+ /**
2
+ * Mock API Responses for Unit Testing
3
+ *
4
+ * These are sanitized mock responses based on actual Octopus Energy API structure.
5
+ * All tokens, API keys, and account numbers are FAKE and for testing only.
6
+ */
7
+
8
+ // ============================================================================
9
+ // 1. AUTHENTICATION RESPONSES
10
+ // ============================================================================
11
+
12
+ /**
13
+ * Successful authentication response
14
+ * @returns {Object} Mock authentication response with fake JWT token
15
+ */
16
+ const mockAuthSuccess = {
17
+ status: 200,
18
+ data: {
19
+ data: {
20
+ obtainKrakenToken: {
21
+ token: "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.FAKE_TOKEN_PAYLOAD.FAKE_SIGNATURE"
22
+ }
23
+ }
24
+ }
25
+ };
26
+
27
+ /**
28
+ * Authentication failure - invalid API key
29
+ * @returns {Object} Mock error response
30
+ */
31
+ const mockAuthFailure = {
32
+ status: 200,
33
+ data: {
34
+ errors: [
35
+ {
36
+ message: "Invalid API key provided",
37
+ locations: [{ line: 1, column: 1 }],
38
+ path: ["obtainKrakenToken"]
39
+ }
40
+ ]
41
+ }
42
+ };
43
+
44
+ // ============================================================================
45
+ // 2. DATA QUERY RESPONSES (plannedDispatches + vehicleChargingPreferences)
46
+ // ============================================================================
47
+
48
+ /**
49
+ * Data query with 2 active/future slots (based on real example)
50
+ * Scenario: Two smart-charge slots scheduled for tonight
51
+ */
52
+ const mockDataWithSlots = {
53
+ status: 200,
54
+ data: {
55
+ data: {
56
+ plannedDispatches: [
57
+ {
58
+ startDt: "2025-11-29 21:30:00+00:00",
59
+ endDt: "2025-11-29 22:00:00+00:00",
60
+ deltaKwh: 0,
61
+ meta: {
62
+ source: "smart-charge"
63
+ }
64
+ },
65
+ {
66
+ startDt: "2025-11-29 23:00:00+00:00",
67
+ endDt: "2025-11-30 04:00:00+00:00",
68
+ deltaKwh: -12,
69
+ meta: {
70
+ source: "smart-charge"
71
+ }
72
+ }
73
+ ],
74
+ vehicleChargingPreferences: {
75
+ weekdayTargetSoc: 80,
76
+ weekdayTargetTime: "04:00",
77
+ weekendTargetSoc: 80,
78
+ weekendTargetTime: "04:00"
79
+ }
80
+ }
81
+ }
82
+ };
83
+
84
+ /**
85
+ * Data query with active slot (currently charging)
86
+ * Scenario: One slot currently active, started 30 minutes ago
87
+ * NOTE: This is legacy mock data, see mockRealApiResponse_* below for real API structure
88
+ */
89
+ const mockDataWithActiveSlot = {
90
+ status: 200,
91
+ data: {
92
+ data: {
93
+ plannedDispatches: [
94
+ {
95
+ startDt: "2025-11-29 01:30:00+00:00",
96
+ endDt: "2025-11-29 05:30:00+00:00",
97
+ deltaKwh: -15.5,
98
+ meta: {
99
+ source: "smart-charge"
100
+ }
101
+ }
102
+ ],
103
+ vehicleChargingPreferences: {
104
+ weekdayTargetSoc: 80,
105
+ weekdayTargetTime: "08:00",
106
+ weekendTargetSoc: 80,
107
+ weekendTargetTime: "08:00"
108
+ }
109
+ }
110
+ }
111
+ };
112
+
113
+ // ============================================================================
114
+ // 6. REAL API RESPONSE TEST DATA (Based on User's Actual Data)
115
+ // ============================================================================
116
+
117
+ /**
118
+ * REAL API RESPONSE - 1 Slot: Currently Active
119
+ * Test Time: 2025-12-10T03:15:00Z (middle of slot)
120
+ * Slot: 03:00 - 03:30 (ACTIVE at test time)
121
+ * Expected charging_now: true
122
+ */
123
+ const mockRealApiResponse_1Slot_Active = {
124
+ status: 200,
125
+ data: {
126
+ data: {
127
+ plannedDispatches: [
128
+ {
129
+ startDt: "2025-12-10 03:00:00+00:00",
130
+ endDt: "2025-12-10 03:30:00+00:00",
131
+ deltaKwh: -1,
132
+ meta: { source: "smart-charge" }
133
+ }
134
+ ],
135
+ vehicleChargingPreferences: {
136
+ weekdayTargetSoc: 80,
137
+ weekdayTargetTime: "08:00",
138
+ weekendTargetSoc: 80,
139
+ weekendTargetTime: "08:00"
140
+ }
141
+ }
142
+ },
143
+ testTime: "2025-12-10T03:15:00Z", // 15 minutes into the slot
144
+ expectedChargingNow: true
145
+ };
146
+
147
+ /**
148
+ * REAL API RESPONSE - 1 Slot: Not Active (Future)
149
+ * Test Time: 2025-12-10T02:00:00Z (before slot)
150
+ * Slot: 03:00 - 03:30 (NOT ACTIVE at test time)
151
+ * Expected charging_now: false
152
+ */
153
+ const mockRealApiResponse_1Slot_Future = {
154
+ status: 200,
155
+ data: {
156
+ data: {
157
+ plannedDispatches: [
158
+ {
159
+ startDt: "2025-12-10 03:00:00+00:00",
160
+ endDt: "2025-12-10 03:30:00+00:00",
161
+ deltaKwh: -1,
162
+ meta: { source: "smart-charge" }
163
+ }
164
+ ],
165
+ vehicleChargingPreferences: {
166
+ weekdayTargetSoc: 80,
167
+ weekdayTargetTime: "08:00",
168
+ weekendTargetSoc: 80,
169
+ weekendTargetTime: "08:00"
170
+ }
171
+ }
172
+ },
173
+ testTime: "2025-12-10T02:00:00Z", // 1 hour before slot starts
174
+ expectedChargingNow: false
175
+ };
176
+
177
+ /**
178
+ * REAL API RESPONSE - 3 Slots: First Active
179
+ * Test Time: 2025-12-10T01:15:00Z
180
+ * Slot 1: 01:00 - 02:00 (ACTIVE)
181
+ * Slot 2: 03:00 - 04:00 (future)
182
+ * Slot 3: 05:00 - 06:00 (future)
183
+ * Expected charging_now: true
184
+ */
185
+ const mockRealApiResponse_3Slots_FirstActive = {
186
+ status: 200,
187
+ data: {
188
+ data: {
189
+ plannedDispatches: [
190
+ {
191
+ startDt: "2025-12-10 01:00:00+00:00",
192
+ endDt: "2025-12-10 02:00:00+00:00",
193
+ deltaKwh: -5,
194
+ meta: { source: "smart-charge" }
195
+ },
196
+ {
197
+ startDt: "2025-12-10 03:00:00+00:00",
198
+ endDt: "2025-12-10 04:00:00+00:00",
199
+ deltaKwh: -5,
200
+ meta: { source: "smart-charge" }
201
+ },
202
+ {
203
+ startDt: "2025-12-10 05:00:00+00:00",
204
+ endDt: "2025-12-10 06:00:00+00:00",
205
+ deltaKwh: -5,
206
+ meta: { source: "smart-charge" }
207
+ }
208
+ ],
209
+ vehicleChargingPreferences: {
210
+ weekdayTargetSoc: 80,
211
+ weekdayTargetTime: "08:00",
212
+ weekendTargetSoc: 80,
213
+ weekendTargetTime: "08:00"
214
+ }
215
+ }
216
+ },
217
+ testTime: "2025-12-10T01:15:00Z", // 15 minutes into first slot
218
+ expectedChargingNow: true
219
+ };
220
+
221
+ /**
222
+ * REAL API RESPONSE - 3 Slots: Middle Active
223
+ * Test Time: 2025-12-10T03:30:00Z
224
+ * Slot 1: 01:00 - 02:00 (past)
225
+ * Slot 2: 03:00 - 04:00 (ACTIVE)
226
+ * Slot 3: 05:00 - 06:00 (future)
227
+ * Expected charging_now: true
228
+ */
229
+ const mockRealApiResponse_3Slots_MiddleActive = {
230
+ status: 200,
231
+ data: {
232
+ data: {
233
+ plannedDispatches: [
234
+ {
235
+ startDt: "2025-12-10 01:00:00+00:00",
236
+ endDt: "2025-12-10 02:00:00+00:00",
237
+ deltaKwh: -5,
238
+ meta: { source: "smart-charge" }
239
+ },
240
+ {
241
+ startDt: "2025-12-10 03:00:00+00:00",
242
+ endDt: "2025-12-10 04:00:00+00:00",
243
+ deltaKwh: -5,
244
+ meta: { source: "smart-charge" }
245
+ },
246
+ {
247
+ startDt: "2025-12-10 05:00:00+00:00",
248
+ endDt: "2025-12-10 06:00:00+00:00",
249
+ deltaKwh: -5,
250
+ meta: { source: "smart-charge" }
251
+ }
252
+ ],
253
+ vehicleChargingPreferences: {
254
+ weekdayTargetSoc: 80,
255
+ weekdayTargetTime: "08:00",
256
+ weekendTargetSoc: 80,
257
+ weekendTargetTime: "08:00"
258
+ }
259
+ }
260
+ },
261
+ testTime: "2025-12-10T03:30:00Z", // 30 minutes into middle slot
262
+ expectedChargingNow: true
263
+ };
264
+
265
+ /**
266
+ * REAL API RESPONSE - 3 Slots: Last Active
267
+ * Test Time: 2025-12-10T05:45:00Z
268
+ * Slot 1: 01:00 - 02:00 (past)
269
+ * Slot 2: 03:00 - 04:00 (past)
270
+ * Slot 3: 05:00 - 06:00 (ACTIVE)
271
+ * Expected charging_now: true
272
+ */
273
+ const mockRealApiResponse_3Slots_LastActive = {
274
+ status: 200,
275
+ data: {
276
+ data: {
277
+ plannedDispatches: [
278
+ {
279
+ startDt: "2025-12-10 01:00:00+00:00",
280
+ endDt: "2025-12-10 02:00:00+00:00",
281
+ deltaKwh: -5,
282
+ meta: { source: "smart-charge" }
283
+ },
284
+ {
285
+ startDt: "2025-12-10 03:00:00+00:00",
286
+ endDt: "2025-12-10 04:00:00+00:00",
287
+ deltaKwh: -5,
288
+ meta: { source: "smart-charge" }
289
+ },
290
+ {
291
+ startDt: "2025-12-10 05:00:00+00:00",
292
+ endDt: "2025-12-10 06:00:00+00:00",
293
+ deltaKwh: -5,
294
+ meta: { source: "smart-charge" }
295
+ }
296
+ ],
297
+ vehicleChargingPreferences: {
298
+ weekdayTargetSoc: 80,
299
+ weekdayTargetTime: "08:00",
300
+ weekendTargetSoc: 80,
301
+ weekendTargetTime: "08:00"
302
+ }
303
+ }
304
+ },
305
+ testTime: "2025-12-10T05:45:00Z", // 45 minutes into last slot
306
+ expectedChargingNow: true
307
+ };
308
+
309
+ /**
310
+ * REAL API RESPONSE - 3 Slots: None Active (Between Slots)
311
+ * Test Time: 2025-12-10T02:30:00Z
312
+ * Slot 1: 01:00 - 02:00 (past)
313
+ * Slot 2: 03:00 - 04:00 (future)
314
+ * Slot 3: 05:00 - 06:00 (future)
315
+ * Expected charging_now: false
316
+ */
317
+ const mockRealApiResponse_3Slots_NoneActive = {
318
+ status: 200,
319
+ data: {
320
+ data: {
321
+ plannedDispatches: [
322
+ {
323
+ startDt: "2025-12-10 01:00:00+00:00",
324
+ endDt: "2025-12-10 02:00:00+00:00",
325
+ deltaKwh: -5,
326
+ meta: { source: "smart-charge" }
327
+ },
328
+ {
329
+ startDt: "2025-12-10 03:00:00+00:00",
330
+ endDt: "2025-12-10 04:00:00+00:00",
331
+ deltaKwh: -5,
332
+ meta: { source: "smart-charge" }
333
+ },
334
+ {
335
+ startDt: "2025-12-10 05:00:00+00:00",
336
+ endDt: "2025-12-10 06:00:00+00:00",
337
+ deltaKwh: -5,
338
+ meta: { source: "smart-charge" }
339
+ }
340
+ ],
341
+ vehicleChargingPreferences: {
342
+ weekdayTargetSoc: 80,
343
+ weekdayTargetTime: "08:00",
344
+ weekendTargetSoc: 80,
345
+ weekendTargetTime: "08:00"
346
+ }
347
+ }
348
+ },
349
+ testTime: "2025-12-10T02:30:00Z", // Between slot 1 and slot 2
350
+ expectedChargingNow: false
351
+ };
352
+
353
+ /**
354
+ * REAL API RESPONSE - 6 Slots: First Active
355
+ * Test Time: 2025-12-10T00:30:00Z
356
+ * Expected charging_now: true
357
+ */
358
+ const mockRealApiResponse_6Slots_FirstActive = {
359
+ status: 200,
360
+ data: {
361
+ data: {
362
+ plannedDispatches: [
363
+ {
364
+ startDt: "2025-12-10 00:00:00+00:00",
365
+ endDt: "2025-12-10 01:00:00+00:00",
366
+ deltaKwh: -3,
367
+ meta: { source: "smart-charge" }
368
+ },
369
+ {
370
+ startDt: "2025-12-10 02:00:00+00:00",
371
+ endDt: "2025-12-10 03:00:00+00:00",
372
+ deltaKwh: -3,
373
+ meta: { source: "smart-charge" }
374
+ },
375
+ {
376
+ startDt: "2025-12-10 04:00:00+00:00",
377
+ endDt: "2025-12-10 05:00:00+00:00",
378
+ deltaKwh: -3,
379
+ meta: { source: "smart-charge" }
380
+ },
381
+ {
382
+ startDt: "2025-12-10 06:00:00+00:00",
383
+ endDt: "2025-12-10 07:00:00+00:00",
384
+ deltaKwh: -3,
385
+ meta: { source: "smart-charge" }
386
+ },
387
+ {
388
+ startDt: "2025-12-10 08:00:00+00:00",
389
+ endDt: "2025-12-10 09:00:00+00:00",
390
+ deltaKwh: -3,
391
+ meta: { source: "smart-charge" }
392
+ },
393
+ {
394
+ startDt: "2025-12-10 10:00:00+00:00",
395
+ endDt: "2025-12-10 11:00:00+00:00",
396
+ deltaKwh: -3,
397
+ meta: { source: "bump-charge" }
398
+ }
399
+ ],
400
+ vehicleChargingPreferences: {
401
+ weekdayTargetSoc: 90,
402
+ weekdayTargetTime: "07:00",
403
+ weekendTargetSoc: 90,
404
+ weekendTargetTime: "07:00"
405
+ }
406
+ }
407
+ },
408
+ testTime: "2025-12-10T00:30:00Z", // 30 minutes into first slot
409
+ expectedChargingNow: true
410
+ };
411
+
412
+ /**
413
+ * REAL API RESPONSE - 6 Slots: Third Active (Middle)
414
+ * Test Time: 2025-12-10T04:30:00Z
415
+ * Expected charging_now: true
416
+ */
417
+ const mockRealApiResponse_6Slots_ThirdActive = {
418
+ status: 200,
419
+ data: {
420
+ data: {
421
+ plannedDispatches: [
422
+ {
423
+ startDt: "2025-12-10 00:00:00+00:00",
424
+ endDt: "2025-12-10 01:00:00+00:00",
425
+ deltaKwh: -3,
426
+ meta: { source: "smart-charge" }
427
+ },
428
+ {
429
+ startDt: "2025-12-10 02:00:00+00:00",
430
+ endDt: "2025-12-10 03:00:00+00:00",
431
+ deltaKwh: -3,
432
+ meta: { source: "smart-charge" }
433
+ },
434
+ {
435
+ startDt: "2025-12-10 04:00:00+00:00",
436
+ endDt: "2025-12-10 05:00:00+00:00",
437
+ deltaKwh: -3,
438
+ meta: { source: "smart-charge" }
439
+ },
440
+ {
441
+ startDt: "2025-12-10 06:00:00+00:00",
442
+ endDt: "2025-12-10 07:00:00+00:00",
443
+ deltaKwh: -3,
444
+ meta: { source: "smart-charge" }
445
+ },
446
+ {
447
+ startDt: "2025-12-10 08:00:00+00:00",
448
+ endDt: "2025-12-10 09:00:00+00:00",
449
+ deltaKwh: -3,
450
+ meta: { source: "smart-charge" }
451
+ },
452
+ {
453
+ startDt: "2025-12-10 10:00:00+00:00",
454
+ endDt: "2025-12-10 11:00:00+00:00",
455
+ deltaKwh: -3,
456
+ meta: { source: "bump-charge" }
457
+ }
458
+ ],
459
+ vehicleChargingPreferences: {
460
+ weekdayTargetSoc: 90,
461
+ weekdayTargetTime: "07:00",
462
+ weekendTargetSoc: 90,
463
+ weekendTargetTime: "07:00"
464
+ }
465
+ }
466
+ },
467
+ testTime: "2025-12-10T04:30:00Z", // 30 minutes into third slot
468
+ expectedChargingNow: true
469
+ };
470
+
471
+ /**
472
+ * REAL API RESPONSE - 6 Slots: Last Active
473
+ * Test Time: 2025-12-10T10:45:00Z
474
+ * Expected charging_now: true
475
+ */
476
+ const mockRealApiResponse_6Slots_LastActive = {
477
+ status: 200,
478
+ data: {
479
+ data: {
480
+ plannedDispatches: [
481
+ {
482
+ startDt: "2025-12-10 00:00:00+00:00",
483
+ endDt: "2025-12-10 01:00:00+00:00",
484
+ deltaKwh: -3,
485
+ meta: { source: "smart-charge" }
486
+ },
487
+ {
488
+ startDt: "2025-12-10 02:00:00+00:00",
489
+ endDt: "2025-12-10 03:00:00+00:00",
490
+ deltaKwh: -3,
491
+ meta: { source: "smart-charge" }
492
+ },
493
+ {
494
+ startDt: "2025-12-10 04:00:00+00:00",
495
+ endDt: "2025-12-10 05:00:00+00:00",
496
+ deltaKwh: -3,
497
+ meta: { source: "smart-charge" }
498
+ },
499
+ {
500
+ startDt: "2025-12-10 06:00:00+00:00",
501
+ endDt: "2025-12-10 07:00:00+00:00",
502
+ deltaKwh: -3,
503
+ meta: { source: "smart-charge" }
504
+ },
505
+ {
506
+ startDt: "2025-12-10 08:00:00+00:00",
507
+ endDt: "2025-12-10 09:00:00+00:00",
508
+ deltaKwh: -3,
509
+ meta: { source: "smart-charge" }
510
+ },
511
+ {
512
+ startDt: "2025-12-10 10:00:00+00:00",
513
+ endDt: "2025-12-10 11:00:00+00:00",
514
+ deltaKwh: -3,
515
+ meta: { source: "bump-charge" }
516
+ }
517
+ ],
518
+ vehicleChargingPreferences: {
519
+ weekdayTargetSoc: 90,
520
+ weekdayTargetTime: "07:00",
521
+ weekendTargetSoc: 90,
522
+ weekendTargetTime: "07:00"
523
+ }
524
+ }
525
+ },
526
+ testTime: "2025-12-10T10:45:00Z", // 45 minutes into last slot
527
+ expectedChargingNow: true
528
+ };
529
+
530
+ /**
531
+ * REAL API RESPONSE - 6 Slots: None Active (Between Slots)
532
+ * Test Time: 2025-12-10T03:30:00Z
533
+ * Expected charging_now: false
534
+ */
535
+ const mockRealApiResponse_6Slots_NoneActive = {
536
+ status: 200,
537
+ data: {
538
+ data: {
539
+ plannedDispatches: [
540
+ {
541
+ startDt: "2025-12-10 00:00:00+00:00",
542
+ endDt: "2025-12-10 01:00:00+00:00",
543
+ deltaKwh: -3,
544
+ meta: { source: "smart-charge" }
545
+ },
546
+ {
547
+ startDt: "2025-12-10 02:00:00+00:00",
548
+ endDt: "2025-12-10 03:00:00+00:00",
549
+ deltaKwh: -3,
550
+ meta: { source: "smart-charge" }
551
+ },
552
+ {
553
+ startDt: "2025-12-10 04:00:00+00:00",
554
+ endDt: "2025-12-10 05:00:00+00:00",
555
+ deltaKwh: -3,
556
+ meta: { source: "smart-charge" }
557
+ },
558
+ {
559
+ startDt: "2025-12-10 06:00:00+00:00",
560
+ endDt: "2025-12-10 07:00:00+00:00",
561
+ deltaKwh: -3,
562
+ meta: { source: "smart-charge" }
563
+ },
564
+ {
565
+ startDt: "2025-12-10 08:00:00+00:00",
566
+ endDt: "2025-12-10 09:00:00+00:00",
567
+ deltaKwh: -3,
568
+ meta: { source: "smart-charge" }
569
+ },
570
+ {
571
+ startDt: "2025-12-10 10:00:00+00:00",
572
+ endDt: "2025-12-10 11:00:00+00:00",
573
+ deltaKwh: -3,
574
+ meta: { source: "bump-charge" }
575
+ }
576
+ ],
577
+ vehicleChargingPreferences: {
578
+ weekdayTargetSoc: 90,
579
+ weekdayTargetTime: "07:00",
580
+ weekendTargetSoc: 90,
581
+ weekendTargetTime: "07:00"
582
+ }
583
+ }
584
+ },
585
+ testTime: "2025-12-10T03:30:00Z", // Between slot 2 and slot 3
586
+ expectedChargingNow: false
587
+ };
588
+
589
+ /**
590
+ * Data query with no slots
591
+ * Scenario: Car not plugged in or no charging scheduled
592
+ * TODO: Update with actual API response structure when available
593
+ */
594
+ const mockDataNoSlots = {
595
+ status: 200,
596
+ data: {
597
+ data: {
598
+ plannedDispatches: [],
599
+ vehicleChargingPreferences: {
600
+ weekdayTargetSoc: 80,
601
+ weekdayTargetTime: "08:00",
602
+ weekendTargetSoc: 80,
603
+ weekendTargetTime: "08:00"
604
+ }
605
+ }
606
+ }
607
+ };
608
+
609
+ /**
610
+ * Data query with 3 slots (testing multiple slot handling)
611
+ * Scenario: Complex charging schedule with bump charge
612
+ */
613
+ const mockDataWithThreeSlots = {
614
+ status: 200,
615
+ data: {
616
+ data: {
617
+ plannedDispatches: [
618
+ {
619
+ startDt: "2025-11-29 01:30:00+00:00",
620
+ endDt: "2025-11-29 03:30:00+00:00",
621
+ deltaKwh: -10.2,
622
+ meta: {
623
+ source: "smart-charge"
624
+ }
625
+ },
626
+ {
627
+ startDt: "2025-11-29 12:00:00+00:00",
628
+ endDt: "2025-11-29 13:00:00+00:00",
629
+ deltaKwh: -5.5,
630
+ meta: {
631
+ source: "bump-charge"
632
+ }
633
+ },
634
+ {
635
+ startDt: "2025-11-30 02:00:00+00:00",
636
+ endDt: "2025-11-30 05:30:00+00:00",
637
+ deltaKwh: -18.7,
638
+ meta: {
639
+ source: "smart-charge"
640
+ }
641
+ }
642
+ ],
643
+ vehicleChargingPreferences: {
644
+ weekdayTargetSoc: 90,
645
+ weekdayTargetTime: "07:00",
646
+ weekendTargetSoc: 90,
647
+ weekendTargetTime: "07:00"
648
+ }
649
+ }
650
+ }
651
+ };
652
+
653
+ /**
654
+ * Data query with past slots (should be filtered out)
655
+ * Scenario: Old slots that have already completed
656
+ */
657
+ const mockDataWithPastSlots = {
658
+ status: 200,
659
+ data: {
660
+ data: {
661
+ plannedDispatches: [
662
+ {
663
+ startDt: "2025-11-28 01:30:00+00:00",
664
+ endDt: "2025-11-28 05:30:00+00:00",
665
+ deltaKwh: -12,
666
+ meta: {
667
+ source: "smart-charge"
668
+ }
669
+ }
670
+ ],
671
+ vehicleChargingPreferences: {
672
+ weekdayTargetSoc: 80,
673
+ weekdayTargetTime: "08:00",
674
+ weekendTargetSoc: 80,
675
+ weekendTargetTime: "08:00"
676
+ }
677
+ }
678
+ }
679
+ };
680
+
681
+ // ============================================================================
682
+ // 3. PREFERENCE MUTATION RESPONSES
683
+ // ============================================================================
684
+
685
+ /**
686
+ * Successful preference mutation
687
+ * TODO: Update with actual API response structure when available
688
+ */
689
+ const mockMutationSuccess = {
690
+ status: 200,
691
+ data: {
692
+ data: {
693
+ setVehicleChargePreferences: {
694
+ __typename: "VehicleChargingPreferences"
695
+ }
696
+ }
697
+ }
698
+ };
699
+
700
+ /**
701
+ * Preference mutation failure
702
+ * TODO: Update with actual API response structure when available
703
+ */
704
+ const mockMutationFailure = {
705
+ status: 200,
706
+ data: {
707
+ errors: [
708
+ {
709
+ message: "Failed to update preferences",
710
+ locations: [{ line: 1, column: 1 }],
711
+ path: ["setVehicleChargePreferences"]
712
+ }
713
+ ]
714
+ }
715
+ };
716
+
717
+ // ============================================================================
718
+ // 4. ERROR RESPONSES
719
+ // ============================================================================
720
+
721
+ /**
722
+ * Network timeout error
723
+ */
724
+ const mockNetworkTimeout = {
725
+ code: "ECONNABORTED",
726
+ message: "timeout of 10000ms exceeded"
727
+ };
728
+
729
+ /**
730
+ * Invalid account number
731
+ * TODO: Update with actual API response structure when available
732
+ */
733
+ const mockInvalidAccount = {
734
+ status: 200,
735
+ data: {
736
+ errors: [
737
+ {
738
+ message: "Account not found",
739
+ locations: [{ line: 1, column: 1 }]
740
+ }
741
+ ]
742
+ }
743
+ };
744
+
745
+ /**
746
+ * Rate limiting error
747
+ */
748
+ const mockRateLimitError = {
749
+ status: 429,
750
+ statusText: "Too Many Requests",
751
+ data: {
752
+ error: "Rate limit exceeded"
753
+ }
754
+ };
755
+
756
+ // ============================================================================
757
+ // 5. EXPECTED PAYLOADS (Node-RED msg.payload format)
758
+ // ============================================================================
759
+
760
+ /**
761
+ * Expected payload format with 2 slots (based on user's example)
762
+ * This is what fetchData() should produce
763
+ */
764
+ const expectedPayloadTwoSlots = {
765
+ next_start: "2025-11-29 21:30:00+00:00",
766
+ total_energy: -12,
767
+ next_kwh: "0.00",
768
+ next_source: "smart-charge",
769
+ confirmed_limit: 80,
770
+ confirmed_time: "04:00",
771
+ pending_limit: 80,
772
+ pending_time: "04:00",
773
+ charging_now: false,
774
+ slot1_start: "2025-11-29 21:30:00+00:00",
775
+ slot1_end: "2025-11-29 22:00:00+00:00",
776
+ slot2_start: "2025-11-29 23:00:00+00:00",
777
+ slot2_end: "2025-11-30 04:00:00+00:00",
778
+ slot3_start: null,
779
+ slot3_end: null,
780
+ window_start: "2025-11-29 21:30:00+00:00",
781
+ window_end: "2025-11-30 04:00:00+00:00",
782
+ next_start_raw: "2025-11-29 21:30:00+00:00",
783
+ slot1_start_raw: "2025-11-29 21:30:00+00:00",
784
+ slot1_end_raw: "2025-11-29 22:00:00+00:00",
785
+ slot2_start_raw: "2025-11-29 23:00:00+00:00",
786
+ slot2_end_raw: "2025-11-30 04:00:00+00:00",
787
+ slot3_start_raw: null,
788
+ slot3_end_raw: null,
789
+ window_start_raw: "2025-11-29 21:30:00+00:00",
790
+ window_end_raw: "2025-11-30 04:00:00+00:00"
791
+ };
792
+
793
+ /**
794
+ * Expected payload with active slot (charging_now = true)
795
+ * Current time: 2025-11-29T02:00:00Z (in the middle of slot)
796
+ */
797
+ const expectedPayloadActiveSlot = {
798
+ next_start: "2025-11-29 01:30:00+00:00",
799
+ total_energy: -15.5,
800
+ next_kwh: "-15.50",
801
+ next_source: "smart-charge",
802
+ confirmed_limit: 80,
803
+ confirmed_time: "08:00",
804
+ pending_limit: 80,
805
+ pending_time: "08:00",
806
+ charging_now: true, // KEY DIFFERENCE
807
+ slot1_start: "2025-11-29 01:30:00+00:00",
808
+ slot1_end: "2025-11-29 05:30:00+00:00",
809
+ slot2_start: null,
810
+ slot2_end: null,
811
+ slot3_start: null,
812
+ slot3_end: null,
813
+ window_start: "2025-11-29 01:30:00+00:00",
814
+ window_end: "2025-11-29 05:30:00+00:00",
815
+ next_start_raw: "2025-11-29 01:30:00+00:00",
816
+ slot1_start_raw: "2025-11-29 01:30:00+00:00",
817
+ slot1_end_raw: "2025-11-29 05:30:00+00:00",
818
+ slot2_start_raw: null,
819
+ slot2_end_raw: null,
820
+ slot3_start_raw: null,
821
+ slot3_end_raw: null,
822
+ window_start_raw: "2025-11-29 01:30:00+00:00",
823
+ window_end_raw: "2025-11-29 05:30:00+00:00"
824
+ };
825
+
826
+ /**
827
+ * Expected payload with no slots
828
+ */
829
+ const expectedPayloadNoSlots = {
830
+ next_start: null,
831
+ total_energy: 0,
832
+ next_kwh: "0",
833
+ next_source: "unknown",
834
+ confirmed_limit: 80,
835
+ confirmed_time: "08:00",
836
+ pending_limit: 80,
837
+ pending_time: "08:00",
838
+ charging_now: false,
839
+ slot1_start: null,
840
+ slot1_end: null,
841
+ slot2_start: null,
842
+ slot2_end: null,
843
+ slot3_start: null,
844
+ slot3_end: null,
845
+ window_start: null,
846
+ window_end: null,
847
+ next_start_raw: null,
848
+ slot1_start_raw: null,
849
+ slot1_end_raw: null,
850
+ slot2_start_raw: null,
851
+ slot2_end_raw: null,
852
+ slot3_start_raw: null,
853
+ slot3_end_raw: null,
854
+ window_start_raw: null,
855
+ window_end_raw: null
856
+ };
857
+
858
+ // ============================================================================
859
+ // 6. HELPER FUNCTIONS FOR TESTS
860
+ // ============================================================================
861
+
862
+ /**
863
+ * Create a mock axios instance for testing
864
+ * @param {Array} responses - Array of mock responses in order
865
+ * @returns {Object} Mock axios with post method
866
+ */
867
+ function createMockAxios(responses) {
868
+ let callCount = 0;
869
+ return {
870
+ post: jest.fn(() => {
871
+ const response = responses[callCount] || responses[responses.length - 1];
872
+ callCount++;
873
+ return Promise.resolve(response);
874
+ })
875
+ };
876
+ }
877
+
878
+ /**
879
+ * Create a mock MQTT broker for testing
880
+ * @returns {Object} Mock broker with publish/subscribe methods
881
+ */
882
+ function createMockBroker() {
883
+ const published = [];
884
+ return {
885
+ client: {
886
+ publish: jest.fn((topic, payload, options) => {
887
+ published.push({ topic, payload, options });
888
+ }),
889
+ subscribe: jest.fn()
890
+ },
891
+ register: jest.fn(),
892
+ subscribe: jest.fn(),
893
+ getPublished: () => published
894
+ };
895
+ }
896
+
897
+ /**
898
+ * Create a mock Node-RED node
899
+ * @returns {Object} Mock node with all required methods
900
+ */
901
+ function createMockNode() {
902
+ const sent = [];
903
+ const errors = [];
904
+ const warnings = [];
905
+ const logs = [];
906
+ const statuses = [];
907
+
908
+ return {
909
+ status: jest.fn((status) => statuses.push(status)),
910
+ send: jest.fn((msg) => sent.push(msg)),
911
+ error: jest.fn((error) => errors.push(error)),
912
+ warn: jest.fn((warning) => warnings.push(warning)),
913
+ log: jest.fn((log) => logs.push(log)),
914
+ on: jest.fn(),
915
+ getSent: () => sent,
916
+ getErrors: () => errors,
917
+ getWarnings: () => warnings,
918
+ getLogs: () => logs,
919
+ getStatuses: () => statuses
920
+ };
921
+ }
922
+
923
+ /**
924
+ * Generate mock data with an active slot (currently charging)
925
+ * Slot: NOW - 30 minutes to NOW + 3.5 hours
926
+ * This ensures the test always has an active slot regardless of when it runs
927
+ * @returns {Object} Mock API response with active slot
928
+ */
929
+ function generateMockDataWithActiveSlot() {
930
+ const now = new Date();
931
+ const startTime = new Date(now.getTime() - 30 * 60 * 1000); // 30 min ago
932
+ const endTime = new Date(now.getTime() + 3.5 * 60 * 60 * 1000); // 3.5 hrs from now
933
+
934
+ return {
935
+ status: 200,
936
+ data: {
937
+ data: {
938
+ plannedDispatches: [
939
+ {
940
+ startDt: startTime.toISOString().replace('T', ' ').replace('Z', '+00:00'),
941
+ endDt: endTime.toISOString().replace('T', ' ').replace('Z', '+00:00'),
942
+ deltaKwh: -15.5,
943
+ meta: { source: "smart-charge" }
944
+ }
945
+ ],
946
+ vehicleChargingPreferences: {
947
+ weekdayTargetSoc: 80,
948
+ weekdayTargetTime: "08:00",
949
+ weekendTargetSoc: 80,
950
+ weekendTargetTime: "08:00"
951
+ }
952
+ }
953
+ }
954
+ };
955
+ }
956
+
957
+ /**
958
+ * Generate mock data with future slot only (not currently charging)
959
+ * Slot: NOW + 2 hours to NOW + 6 hours
960
+ * @returns {Object} Mock API response with future slot
961
+ */
962
+ function generateMockDataWithFutureSlot() {
963
+ const now = new Date();
964
+ const startTime = new Date(now.getTime() + 2 * 60 * 60 * 1000); // 2 hrs from now
965
+ const endTime = new Date(now.getTime() + 6 * 60 * 60 * 1000); // 6 hrs from now
966
+
967
+ return {
968
+ status: 200,
969
+ data: {
970
+ data: {
971
+ plannedDispatches: [
972
+ {
973
+ startDt: startTime.toISOString().replace('T', ' ').replace('Z', '+00:00'),
974
+ endDt: endTime.toISOString().replace('T', ' ').replace('Z', '+00:00'),
975
+ deltaKwh: -15.5,
976
+ meta: { source: "smart-charge" }
977
+ }
978
+ ],
979
+ vehicleChargingPreferences: {
980
+ weekdayTargetSoc: 80,
981
+ weekdayTargetTime: "08:00",
982
+ weekendTargetSoc: 80,
983
+ weekendTargetTime: "08:00"
984
+ }
985
+ }
986
+ }
987
+ };
988
+ }
989
+
990
+ // ============================================================================
991
+ // EXPORTS
992
+ // ============================================================================
993
+
994
+ module.exports = {
995
+ // Auth responses
996
+ mockAuthSuccess,
997
+ mockAuthFailure,
998
+
999
+ // Data query responses
1000
+ mockDataWithSlots,
1001
+ mockDataWithActiveSlot,
1002
+ mockDataNoSlots,
1003
+ mockDataWithThreeSlots,
1004
+ mockDataWithPastSlots,
1005
+
1006
+ // Real API response test data (based on user's actual data)
1007
+ mockRealApiResponse_1Slot_Active,
1008
+ mockRealApiResponse_1Slot_Future,
1009
+ mockRealApiResponse_3Slots_FirstActive,
1010
+ mockRealApiResponse_3Slots_MiddleActive,
1011
+ mockRealApiResponse_3Slots_LastActive,
1012
+ mockRealApiResponse_3Slots_NoneActive,
1013
+ mockRealApiResponse_6Slots_FirstActive,
1014
+ mockRealApiResponse_6Slots_ThirdActive,
1015
+ mockRealApiResponse_6Slots_LastActive,
1016
+ mockRealApiResponse_6Slots_NoneActive,
1017
+
1018
+ // Mutation responses
1019
+ mockMutationSuccess,
1020
+ mockMutationFailure,
1021
+
1022
+ // Error responses
1023
+ mockNetworkTimeout,
1024
+ mockInvalidAccount,
1025
+ mockRateLimitError,
1026
+
1027
+ // Expected payloads
1028
+ expectedPayloadTwoSlots,
1029
+ expectedPayloadActiveSlot,
1030
+ expectedPayloadNoSlots,
1031
+
1032
+ // Helper functions
1033
+ createMockAxios,
1034
+ createMockBroker,
1035
+ createMockNode,
1036
+ generateMockDataWithActiveSlot,
1037
+ generateMockDataWithFutureSlot
1038
+ };