-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathenergy-chart-options.test.ts
More file actions
609 lines (524 loc) · 19.2 KB
/
energy-chart-options.test.ts
File metadata and controls
609 lines (524 loc) · 19.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
import { assert, describe, it } from "vitest";
import type { BarSeriesOption, LineSeriesOption } from "echarts/charts";
import {
computeUntrackedConsumption,
fillDataGapsAndRoundCaps,
fillLineGaps,
getCompareTransform,
getSuggestedMax,
} from "../../../../../../src/panels/lovelace/cards/energy/common/energy-chart-options";
// Helper to get x value from either [x,y] or {value: [x,y]} format
function getX(item: any): number {
return item?.value?.[0] ?? item?.[0];
}
// Helper to get y value from either [x,y] or {value: [x,y]} format
function getY(item: any): number {
return item?.value?.[1] ?? item?.[1];
}
describe("getSuggestedMax", () => {
it("returns end date unchanged for 5minute period", () => {
const end = new Date("2024-03-15T14:37:22.000");
const result = getSuggestedMax("5minute", end, false);
assert.equal(result.getTime(), end.getTime());
});
it("returns end date unchanged when noRounding is true", () => {
const end = new Date("2024-03-15T14:37:22.000");
const result = getSuggestedMax("hour", end, true);
assert.equal(result.getTime(), end.getTime());
});
it("rounds down to start of hour for hour period", () => {
const end = new Date("2024-03-15T14:37:22.000");
const result = getSuggestedMax("hour", end, false);
assert.equal(result.getMinutes(), 0);
assert.equal(result.getSeconds(), 0);
assert.equal(result.getMilliseconds(), 0);
assert.equal(result.getHours(), 14);
});
it("rounds down to start of day for day period", () => {
const end = new Date("2024-03-15T14:37:22.000");
const result = getSuggestedMax("day", end, false);
assert.equal(result.getHours(), 0);
assert.equal(result.getMinutes(), 0);
assert.equal(result.getDate(), 15);
});
it("rounds down to start of day for week period", () => {
const end = new Date("2024-03-15T14:37:22.000");
const result = getSuggestedMax("week", end, false);
assert.equal(result.getHours(), 0);
assert.equal(result.getMinutes(), 0);
assert.equal(result.getDate(), 15);
});
it("rounds down to start of month for month period", () => {
const end = new Date("2024-03-15T14:37:22.000");
const result = getSuggestedMax("month", end, false);
assert.equal(result.getDate(), 1);
assert.equal(result.getHours(), 0);
assert.equal(result.getMonth(), 2); // March = 2
});
it("corrects DST edge case when hour is 0 for day period", () => {
// Simulate a time that lands exactly on midnight (e.g. DST adjustment)
const end = new Date("2024-03-15T00:00:00.000");
const result = getSuggestedMax("day", end, false);
// Should subtract an hour first, landing on previous day
assert.equal(result.getDate(), 14);
assert.equal(result.getHours(), 0);
});
it("does not apply DST correction when hour is nonzero", () => {
const end = new Date("2024-03-15T10:30:00.000");
const result = getSuggestedMax("day", end, false);
assert.equal(result.getDate(), 15);
assert.equal(result.getHours(), 0);
});
it("does not mutate the input date", () => {
const end = new Date("2024-03-15T14:37:22.000");
const originalTime = end.getTime();
getSuggestedMax("month", end, false);
assert.equal(end.getTime(), originalTime);
});
});
describe("fillLineGaps", () => {
it("fills gaps in datasets with missing timestamps", () => {
const datasets: LineSeriesOption[] = [
{
type: "line",
data: [
[1000, 10],
[3000, 30],
],
},
{
type: "line",
data: [
[1000, 100],
[2000, 200],
[3000, 300],
],
},
];
const result = fillLineGaps(datasets);
// First dataset should have gap at 2000 filled with 0
assert.equal(result[0].data!.length, 3);
assert.equal(getX(result[0].data![0]), 1000);
assert.equal(getY(result[0].data![0]), 10);
assert.equal(getX(result[0].data![1]), 2000);
assert.equal(getY(result[0].data![1]), 0);
assert.equal(getX(result[0].data![2]), 3000);
assert.equal(getY(result[0].data![2]), 30);
// Second dataset should be unchanged
assert.equal(result[1].data!.length, 3);
assert.equal(getX(result[1].data![0]), 1000);
assert.equal(getY(result[1].data![0]), 100);
assert.equal(getX(result[1].data![1]), 2000);
assert.equal(getY(result[1].data![1]), 200);
assert.equal(getX(result[1].data![2]), 3000);
assert.equal(getY(result[1].data![2]), 300);
});
it("handles unsorted data from multiple sources", () => {
// This is the bug we're fixing: when multiple power sources are combined,
// the data may not be in chronological order
const datasets: LineSeriesOption[] = [
{
type: "line",
data: [
[3000, 30],
[1000, 10],
[2000, 20],
],
},
];
const result = fillLineGaps(datasets);
// Data should be sorted by timestamp
assert.equal(result[0].data!.length, 3);
assert.equal(getX(result[0].data![0]), 1000);
assert.equal(getY(result[0].data![0]), 10);
assert.equal(getX(result[0].data![1]), 2000);
assert.equal(getY(result[0].data![1]), 20);
assert.equal(getX(result[0].data![2]), 3000);
assert.equal(getY(result[0].data![2]), 30);
});
it("handles multiple datasets with unsorted data", () => {
const datasets: LineSeriesOption[] = [
{
type: "line",
data: [
[3000, 30],
[1000, 10],
],
},
{
type: "line",
data: [
[2000, 200],
[1000, 100],
[3000, 300],
],
},
];
const result = fillLineGaps(datasets);
// First dataset should be sorted and have gap at 2000 filled
assert.equal(result[0].data!.length, 3);
assert.equal(getX(result[0].data![0]), 1000);
assert.equal(getY(result[0].data![0]), 10);
assert.equal(getX(result[0].data![1]), 2000);
assert.equal(getY(result[0].data![1]), 0);
assert.equal(getX(result[0].data![2]), 3000);
assert.equal(getY(result[0].data![2]), 30);
// Second dataset should be sorted
assert.equal(result[1].data!.length, 3);
assert.equal(getX(result[1].data![0]), 1000);
assert.equal(getY(result[1].data![0]), 100);
assert.equal(getX(result[1].data![1]), 2000);
assert.equal(getY(result[1].data![1]), 200);
assert.equal(getX(result[1].data![2]), 3000);
assert.equal(getY(result[1].data![2]), 300);
});
it("handles data with object format (LineDataItemOption)", () => {
const datasets: LineSeriesOption[] = [
{
type: "line",
data: [{ value: [3000, 30] }, { value: [1000, 10] }],
},
];
const result = fillLineGaps(datasets);
assert.equal(result[0].data!.length, 2);
assert.equal(getX(result[0].data![0]), 1000);
assert.equal(getY(result[0].data![0]), 10);
assert.equal(getX(result[0].data![1]), 3000);
assert.equal(getY(result[0].data![1]), 30);
});
it("returns empty array for empty datasets", () => {
const datasets: LineSeriesOption[] = [
{
type: "line",
data: [],
},
];
const result = fillLineGaps(datasets);
assert.deepEqual(result[0].data, []);
});
it("handles already sorted data with no gaps", () => {
const datasets: LineSeriesOption[] = [
{
type: "line",
data: [
[1000, 10],
[2000, 20],
[3000, 30],
],
},
];
const result = fillLineGaps(datasets);
assert.equal(result[0].data!.length, 3);
assert.equal(getX(result[0].data![0]), 1000);
assert.equal(getY(result[0].data![0]), 10);
assert.equal(getX(result[0].data![1]), 2000);
assert.equal(getY(result[0].data![1]), 20);
assert.equal(getX(result[0].data![2]), 3000);
assert.equal(getY(result[0].data![2]), 30);
});
it("preserves original data item properties", () => {
const datasets: LineSeriesOption[] = [
{
type: "line",
data: [
{ value: [2000, 20], itemStyle: { color: "red" } },
{ value: [1000, 10], itemStyle: { color: "blue" } },
],
},
];
const result = fillLineGaps(datasets);
// First item should be the one with timestamp 1000
const firstItem = result[0].data![0] as any;
assert.equal(getX(firstItem), 1000);
assert.equal(firstItem.itemStyle.color, "blue");
// Second item should be the one with timestamp 2000
const secondItem = result[0].data![1] as any;
assert.equal(getX(secondItem), 2000);
assert.equal(secondItem.itemStyle.color, "red");
});
});
// Helper to get bar data item
function getBarItem(dataset: BarSeriesOption, index: number): any {
const dp = dataset.data![index];
return dp && typeof dp === "object" && "value" in dp ? dp : { value: dp };
}
describe("fillDataGapsAndRoundCaps", () => {
it("fills missing buckets with zero values", () => {
// When a dataset has entries at some but not all bucket positions,
// the function splices in zero-value entries to align them
const datasets: BarSeriesOption[] = [
{
type: "bar",
stack: "a",
data: [
[1000, 10],
[3000, 30],
],
},
{
type: "bar",
stack: "a",
data: [
[1000, 100],
[2000, 200],
[3000, 300],
],
},
];
fillDataGapsAndRoundCaps(datasets);
// First dataset should now have 3 entries with bucket 2000 filled in
assert.equal(datasets[0].data!.length, 3);
const filled = getBarItem(datasets[0], 1);
assert.equal(filled.value[0], 2000);
assert.equal(filled.value[1], 0);
assert.equal(filled.itemStyle.borderWidth, 0);
});
it("sets borderWidth 0 on zero-value existing entries", () => {
const datasets: BarSeriesOption[] = [
{
type: "bar",
stack: "a",
data: [
[1000, 0],
[2000, 5],
],
},
];
fillDataGapsAndRoundCaps(datasets);
const zeroItem = getBarItem(datasets[0], 0);
assert.equal(zeroItem.itemStyle.borderWidth, 0);
});
it("rounds caps on top positive bar in stack", () => {
const datasets: BarSeriesOption[] = [
{
type: "bar",
stack: "a",
data: [[1000, 10]],
},
{
type: "bar",
stack: "a",
data: [[1000, 20]],
},
];
fillDataGapsAndRoundCaps(datasets);
// Last dataset (topmost positive) gets rounded top caps
// Iteration is reverse so first positive hit from the end gets the cap
const topItem = getBarItem(datasets[1], 0);
assert.deepEqual(topItem.itemStyle.borderRadius, [4, 4, 0, 0]);
// Bottom dataset should NOT have rounded caps
const bottomItem = getBarItem(datasets[0], 0);
assert.equal(bottomItem.itemStyle?.borderRadius, undefined);
});
it("rounds caps on bottom negative bar in stack", () => {
const datasets: BarSeriesOption[] = [
{
type: "bar",
stack: "a",
data: [[1000, -10]],
},
{
type: "bar",
stack: "a",
data: [[1000, -20]],
},
];
fillDataGapsAndRoundCaps(datasets);
// Last dataset (bottommost negative) gets rounded bottom caps
const bottomItem = getBarItem(datasets[1], 0);
assert.deepEqual(bottomItem.itemStyle.borderRadius, [0, 0, 4, 4]);
// First dataset should NOT have rounded caps
const topItem = getBarItem(datasets[0], 0);
assert.equal(topItem.itemStyle?.borderRadius, undefined);
});
it("handles different stacks independently", () => {
const datasets: BarSeriesOption[] = [
{
type: "bar",
stack: "a",
data: [[1000, 10]],
},
{
type: "bar",
stack: "b",
data: [[1000, 20]],
},
];
fillDataGapsAndRoundCaps(datasets);
// Both should get caps since they're in different stacks
const itemA = getBarItem(datasets[0], 0);
assert.deepEqual(itemA.itemStyle.borderRadius, [4, 4, 0, 0]);
const itemB = getBarItem(datasets[1], 0);
assert.deepEqual(itemB.itemStyle.borderRadius, [4, 4, 0, 0]);
});
it("handles object-format data items", () => {
const datasets: BarSeriesOption[] = [
{
type: "bar",
stack: "a",
data: [{ value: [1000, 10] }],
},
{
type: "bar",
stack: "a",
data: [{ value: [2000, 20] }],
},
];
fillDataGapsAndRoundCaps(datasets);
// Both datasets should now have 2 entries
assert.equal(datasets[0].data!.length, 2);
assert.equal(datasets[1].data!.length, 2);
});
it("handles empty datasets", () => {
const datasets: BarSeriesOption[] = [
{
type: "bar",
stack: "a",
data: [],
},
];
fillDataGapsAndRoundCaps(datasets);
assert.equal(datasets[0].data!.length, 0);
});
});
describe("getCompareTransform", () => {
it("returns identity transform when no compareStart", () => {
const start = new Date("2024-03-01");
const transform = getCompareTransform(start);
const testDate = new Date("2024-03-15T12:00:00");
assert.equal(transform(testDate).getTime(), testDate.getTime());
});
it("returns identity transform when compareStart is undefined", () => {
const start = new Date("2024-03-01");
const transform = getCompareTransform(start, undefined);
const testDate = new Date("2024-03-15T12:00:00");
assert.equal(transform(testDate).getTime(), testDate.getTime());
});
it("shifts by years when start is at year boundary", () => {
const start = new Date("2024-01-01T00:00:00");
const compareStart = new Date("2023-01-01T00:00:00");
const transform = getCompareTransform(start, compareStart);
const testDate = new Date("2023-06-15T12:00:00");
const result = transform(testDate);
// Should shift forward by 1 year
assert.equal(result.getFullYear(), 2024);
assert.equal(result.getMonth(), 5); // June
assert.equal(result.getDate(), 15);
});
it("shifts by months when start is at month boundary", () => {
const start = new Date("2024-03-01T00:00:00");
const compareStart = new Date("2024-01-01T00:00:00");
const transform = getCompareTransform(start, compareStart);
const testDate = new Date("2024-01-15T12:00:00");
const result = transform(testDate);
// Should shift forward by 2 months
assert.equal(result.getMonth(), 2); // March
assert.equal(result.getDate(), 15);
});
it("shifts by days when start is at day boundary", () => {
const start = new Date("2024-03-15T00:00:00");
const compareStart = new Date("2024-03-08T00:00:00");
const transform = getCompareTransform(start, compareStart);
const testDate = new Date("2024-03-08T14:30:00");
const result = transform(testDate);
// Should shift forward by 7 days
assert.equal(result.getDate(), 15);
assert.equal(result.getHours(), 14);
assert.equal(result.getMinutes(), 30);
});
it("falls back to millisecond offset for non-aligned starts", () => {
const start = new Date("2024-03-15T10:30:00");
const compareStart = new Date("2024-03-14T10:30:00");
const transform = getCompareTransform(start, compareStart);
const testDate = new Date("2024-03-14T12:00:00");
const result = transform(testDate);
const expectedOffset = start.getTime() - compareStart.getTime();
assert.equal(result.getTime(), testDate.getTime() + expectedOffset);
});
it("prefers year shift over month shift when both apply", () => {
// Jan 1 is both start-of-year and start-of-month
const start = new Date("2024-01-01T00:00:00");
const compareStart = new Date("2022-01-01T00:00:00");
const transform = getCompareTransform(start, compareStart);
const testDate = new Date("2022-07-01T00:00:00");
const result = transform(testDate);
// Should shift by 2 years (year check comes first)
assert.equal(result.getFullYear(), 2024);
assert.equal(result.getMonth(), 6); // July
});
it("uses month shift when start is at month but not year boundary", () => {
const start = new Date("2024-06-01T00:00:00");
const compareStart = new Date("2024-03-01T00:00:00");
const transform = getCompareTransform(start, compareStart);
const testDate = new Date("2024-03-20T08:00:00");
const result = transform(testDate);
// Should shift by 3 months
assert.equal(result.getMonth(), 5); // June
assert.equal(result.getDate(), 20);
});
});
describe("computeUntrackedConsumption", () => {
it("returns positive untracked when grid exceeds devices", () => {
const usedTotal = { 1000: 5, 2000: 3 };
const deviceTotal = { 1000: 2, 2000: 1 };
const result = computeUntrackedConsumption(usedTotal, deviceTotal);
assert.deepEqual(result.values, { 1000: 3, 2000: 2 });
assert.deepEqual(result.rawNegatives, {});
});
it("clamps negative untracked to zero and records raw negatives", () => {
// Device sensors report more than the integer grid meter
const usedTotal = { 1000: 0, 2000: 1 };
const deviceTotal = { 1000: 0.3, 2000: 1.7 };
const result = computeUntrackedConsumption(usedTotal, deviceTotal);
assert.equal(result.values[1000], 0);
assert.equal(result.values[2000], 0);
assert.approximately(result.rawNegatives[1000], -0.3, 0.001);
assert.approximately(result.rawNegatives[2000], -0.7, 0.001);
});
it("returns zero when grid equals devices", () => {
const usedTotal = { 1000: 2.5 };
const deviceTotal = { 1000: 2.5 };
const result = computeUntrackedConsumption(usedTotal, deviceTotal);
assert.equal(result.values[1000], 0);
assert.deepEqual(result.rawNegatives, {});
});
it("returns full grid value when no device data exists for timestamp", () => {
const usedTotal = { 1000: 4 };
const deviceTotal = {};
const result = computeUntrackedConsumption(usedTotal, deviceTotal);
assert.equal(result.values[1000], 4);
assert.deepEqual(result.rawNegatives, {});
});
it("ignores device timestamps not present in usedTotal", () => {
const usedTotal = { 1000: 2 };
const deviceTotal = { 1000: 1, 9999: 5 };
const result = computeUntrackedConsumption(usedTotal, deviceTotal);
assert.deepEqual(result.values, { 1000: 1 });
assert.deepEqual(result.rawNegatives, {});
});
it("handles mixed positive and negative across timestamps", () => {
const usedTotal = { 1000: 0, 2000: 3, 3000: 1 };
const deviceTotal = { 1000: 0.5, 2000: 1, 3000: 2 };
const result = computeUntrackedConsumption(usedTotal, deviceTotal);
assert.equal(result.values[1000], 0); // clamped
assert.equal(result.values[2000], 2); // positive
assert.equal(result.values[3000], 0); // clamped
assert.approximately(result.rawNegatives[1000], -0.5, 0.001);
assert.approximately(result.rawNegatives[3000], -1, 0.001);
assert.isUndefined(result.rawNegatives[2000]); // positive, not in rawNegatives
});
it("returns empty result for empty inputs", () => {
const result = computeUntrackedConsumption({}, {});
assert.deepEqual(result.values, {});
assert.deepEqual(result.rawNegatives, {});
});
it("does not mutate input objects", () => {
const usedTotal = { 1000: 5 };
const deviceTotal = { 1000: 2 };
const usedCopy = { ...usedTotal };
const deviceCopy = { ...deviceTotal };
computeUntrackedConsumption(usedTotal, deviceTotal);
assert.deepEqual(usedTotal, usedCopy);
assert.deepEqual(deviceTotal, deviceCopy);
});
});