-
-
Notifications
You must be signed in to change notification settings - Fork 433
Expand file tree
/
Copy pathchart-data-buckets.spec.ts
More file actions
234 lines (193 loc) · 7.32 KB
/
chart-data-buckets.spec.ts
File metadata and controls
234 lines (193 loc) · 7.32 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
import { describe, expect, it } from 'vitest'
import {
fillPartialBucket,
buildDailyEvolution,
buildWeeklyEvolution,
buildMonthlyEvolution,
buildYearlyEvolution,
} from '~/utils/chart-data-buckets'
describe('fillPartialBucket', () => {
it('scales proportionally', () => {
expect(fillPartialBucket(100, 3, 7)).toBe(Math.round((100 * 7) / 3))
})
it('returns raw value when bucket is full', () => {
expect(fillPartialBucket(100, 7, 7)).toBe(100)
})
it('returns raw value when actualDays is 0', () => {
expect(fillPartialBucket(0, 0, 7)).toBe(0)
})
})
describe('buildDailyEvolution', () => {
it('adds timestamps and preserves values', () => {
const result = buildDailyEvolution([
{ day: '2025-03-01', value: 10 },
{ day: '2025-03-02', value: 20 },
])
expect(result).toEqual([
{ day: '2025-03-01', value: 10, timestamp: Date.UTC(2025, 2, 1) },
{ day: '2025-03-02', value: 20, timestamp: Date.UTC(2025, 2, 2) },
])
})
it('sorts by day', () => {
const result = buildDailyEvolution([
{ day: '2025-03-03', value: 3 },
{ day: '2025-03-01', value: 1 },
{ day: '2025-03-02', value: 2 },
])
expect(result.map(r => r.day)).toEqual(['2025-03-01', '2025-03-02', '2025-03-03'])
})
it('returns empty array for empty input', () => {
expect(buildDailyEvolution([])).toEqual([])
})
})
describe('buildWeeklyEvolution', () => {
it('groups daily data into 7-day buckets aligned from end', () => {
const daily = Array.from({ length: 14 }, (_, i) => ({
day: `2025-03-${String(i + 1).padStart(2, '0')}`,
value: 10,
}))
const result = buildWeeklyEvolution(daily, '2025-03-01', '2025-03-14')
expect(result).toHaveLength(2)
expect(result[0]!.value).toBe(70)
expect(result[0]!.weekStart).toBe('2025-03-01')
expect(result[0]!.weekEnd).toBe('2025-03-07')
expect(result[1]!.value).toBe(70)
expect(result[1]!.weekStart).toBe('2025-03-08')
expect(result[1]!.weekEnd).toBe('2025-03-14')
})
it('scales up partial first bucket proportionally', () => {
const daily = Array.from({ length: 10 }, (_, i) => ({
day: `2025-03-${String(i + 1).padStart(2, '0')}`,
value: 10,
}))
const result = buildWeeklyEvolution(daily, '2025-03-01', '2025-03-10')
expect(result).toHaveLength(2)
expect(result[0]!.value).toBe(Math.round((30 * 7) / 3))
expect(result[0]!.weekStart).toBe('2025-03-01')
expect(result[0]!.weekEnd).toBe('2025-03-03')
expect(result[1]!.value).toBe(70)
expect(result[1]!.weekStart).toBe('2025-03-04')
expect(result[1]!.weekEnd).toBe('2025-03-10')
})
it('always aligns from rangeEnd, even with trailing zeros', () => {
const daily = [
{ day: '2025-03-01', value: 10 },
{ day: '2025-03-02', value: 10 },
{ day: '2025-03-03', value: 10 },
{ day: '2025-03-04', value: 10 },
{ day: '2025-03-05', value: 10 },
{ day: '2025-03-06', value: 10 },
{ day: '2025-03-07', value: 10 },
{ day: '2025-03-08', value: 0 },
{ day: '2025-03-09', value: 0 },
]
const result = buildWeeklyEvolution(daily, '2025-03-01', '2025-03-09')
// Bucket 0: 03-03..03-09 = 50, Bucket 1: 03-01..03-02 (partial, scaled)
expect(result).toHaveLength(2)
expect(result[0]!.weekStart).toBe('2025-03-01')
expect(result[0]!.weekEnd).toBe('2025-03-02')
expect(result[0]!.value).toBe(Math.round((20 * 7) / 2))
expect(result[1]!.weekStart).toBe('2025-03-03')
expect(result[1]!.weekEnd).toBe('2025-03-09')
expect(result[1]!.value).toBe(50)
})
it('returns empty array for empty input', () => {
expect(buildWeeklyEvolution([], '2025-03-01', '2025-03-14')).toEqual([])
})
})
describe('buildMonthlyEvolution', () => {
it('aggregates daily data by month', () => {
const result = buildMonthlyEvolution([
{ day: '2025-01-15', value: 10 },
{ day: '2025-01-20', value: 5 },
{ day: '2025-02-10', value: 20 },
])
expect(result).toEqual([
{ month: '2025-01', value: 15, timestamp: Date.UTC(2025, 0, 1) },
{ month: '2025-02', value: 20, timestamp: Date.UTC(2025, 1, 1) },
])
})
it('sorts by month', () => {
const result = buildMonthlyEvolution([
{ day: '2025-03-01', value: 1 },
{ day: '2025-01-01', value: 1 },
{ day: '2025-02-01', value: 1 },
])
expect(result.map(r => r.month)).toEqual(['2025-01', '2025-02', '2025-03'])
})
it('returns empty array for empty input', () => {
expect(buildMonthlyEvolution([])).toEqual([])
})
it('scales single month partial on both ends without double-scaling', () => {
// 2025-01-10 to 2025-01-20 = 11 days out of 31
const daily = Array.from({ length: 11 }, (_, i) => ({
day: `2025-01-${String(i + 10).padStart(2, '0')}`,
value: 10,
}))
const result = buildMonthlyEvolution(daily, '2025-01-10', '2025-01-20')
expect(result).toHaveLength(1)
expect(result[0]!.value).toBe(Math.round((110 * 31) / 11))
})
it('scales up partial first month', () => {
const daily = Array.from({ length: 14 }, (_, i) => ({
day: `2025-03-${String(i + 18).padStart(2, '0')}`,
value: 10,
}))
const result = buildMonthlyEvolution(daily, '2025-03-18', '2025-03-31')
expect(result).toHaveLength(1)
expect(result[0]!.value).toBe(Math.round((140 * 31) / 14))
})
it('scales up partial last month', () => {
const daily = [
...Array.from({ length: 31 }, (_, i) => ({
day: `2025-01-${String(i + 1).padStart(2, '0')}`,
value: 10,
})),
...Array.from({ length: 9 }, (_, i) => ({
day: `2025-02-${String(i + 1).padStart(2, '0')}`,
value: 10,
})),
]
const result = buildMonthlyEvolution(daily, '2025-01-01', '2025-02-09')
expect(result).toHaveLength(2)
expect(result[0]!.value).toBe(310)
expect(result[1]!.value).toBe(Math.round((90 * 28) / 9))
})
})
describe('buildYearlyEvolution', () => {
it('aggregates daily data by year', () => {
const result = buildYearlyEvolution([
{ day: '2024-06-15', value: 100 },
{ day: '2024-12-01', value: 50 },
{ day: '2025-03-01', value: 200 },
])
expect(result).toEqual([
{ year: '2024', value: 150, timestamp: Date.UTC(2024, 0, 1) },
{ year: '2025', value: 200, timestamp: Date.UTC(2025, 0, 1) },
])
})
it('returns empty array for empty input', () => {
expect(buildYearlyEvolution([])).toEqual([])
})
it('scales single year partial on both ends without double-scaling', () => {
// 2025-04-01 to 2025-06-30 = 91 days out of 365
const daily = Array.from({ length: 91 }, (_, i) => {
const d = new Date(Date.UTC(2025, 3, 1))
d.setUTCDate(d.getUTCDate() + i)
return { day: d.toISOString().slice(0, 10), value: 10 }
})
const result = buildYearlyEvolution(daily, '2025-04-01', '2025-06-30')
expect(result).toHaveLength(1)
expect(result[0]!.value).toBe(Math.round((910 * 365) / 91))
})
it('scales up partial first year', () => {
const daily = Array.from({ length: 184 }, (_, i) => {
const d = new Date(Date.UTC(2025, 6, 1))
d.setUTCDate(d.getUTCDate() + i)
return { day: d.toISOString().slice(0, 10), value: 10 }
})
const result = buildYearlyEvolution(daily, '2025-07-01', '2025-12-31')
expect(result).toHaveLength(1)
expect(result[0]!.value).toBe(Math.round((1840 * 365) / 184))
})
})