Skip to content

Commit bce45a7

Browse files
committed
add failing test
1 parent 04f3ab9 commit bce45a7

1 file changed

Lines changed: 71 additions & 42 deletions

File tree

test/unit/app/utils/download-anomalies.spec.ts

Lines changed: 71 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,31 @@ import { describe, expect, it } from 'vitest'
22
import { applyBlocklistCorrection } from '../../../../app/utils/download-anomalies'
33
import type { WeeklyDataPoint } from '../../../../app/types/chart'
44

5+
/** Helper to build a WeeklyDataPoint from a start date and value. */
6+
function week(weekStart: string, value: number): WeeklyDataPoint {
7+
const start = new Date(`${weekStart}T00:00:00Z`)
8+
const end = new Date(start)
9+
end.setUTCDate(end.getUTCDate() + 6)
10+
const weekEnd = end.toISOString().slice(0, 10)
11+
return {
12+
value,
13+
weekKey: `${weekStart}_${weekEnd}`,
14+
weekStart,
15+
weekEnd,
16+
timestampStart: start.getTime(),
17+
timestampEnd: end.getTime(),
18+
}
19+
}
20+
521
describe('applyBlocklistCorrection', () => {
6-
it('treats overlapping weekly buckets as affected anomalies', () => {
7-
const data: WeeklyDataPoint[] = [
8-
{
9-
value: 100,
10-
weekKey: '2022-11-07_2022-11-13',
11-
weekStart: '2022-11-07',
12-
weekEnd: '2022-11-13',
13-
timestampStart: 0,
14-
timestampEnd: 0,
15-
},
16-
{
17-
value: 999,
18-
weekKey: '2022-11-14_2022-11-20',
19-
weekStart: '2022-11-14',
20-
weekEnd: '2022-11-20',
21-
timestampStart: 0,
22-
timestampEnd: 0,
23-
},
24-
{
25-
value: 999,
26-
weekKey: '2022-11-21_2022-11-27',
27-
weekStart: '2022-11-21',
28-
weekEnd: '2022-11-27',
29-
timestampStart: 0,
30-
timestampEnd: 0,
31-
},
32-
{
33-
value: 999,
34-
weekKey: '2022-11-28_2022-12-04',
35-
weekStart: '2022-11-28',
36-
weekEnd: '2022-12-04',
37-
timestampStart: 0,
38-
timestampEnd: 0,
39-
},
40-
{
41-
value: 200,
42-
weekKey: '2022-12-05_2022-12-11',
43-
weekStart: '2022-12-05',
44-
weekEnd: '2022-12-11',
45-
timestampStart: 0,
46-
timestampEnd: 0,
47-
},
22+
// Anomaly Nov 2022: start=2022-11-15, end=2022-11-30
23+
it('corrects weeks overlapping the anomaly', () => {
24+
const data = [
25+
week('2022-11-07', 100),
26+
week('2022-11-14', 999),
27+
week('2022-11-21', 999),
28+
week('2022-11-28', 999),
29+
week('2022-12-05', 200),
4830
]
4931

5032
expect(
@@ -61,4 +43,51 @@ describe('applyBlocklistCorrection', () => {
6143
data[4],
6244
])
6345
})
46+
47+
// Anomaly Jun 2023: start=2023-06-19, end=2023-06-22
48+
it('does not over-correct a week starting on the anomaly end boundary', () => {
49+
const data = [
50+
week('2023-06-01', 500_000),
51+
week('2023-06-08', 500_000),
52+
week('2023-06-15', 10_000_000), // contains spike
53+
week('2023-06-22', 500_000), // starts on anomaly end boundary — normal!
54+
week('2023-06-29', 500_000),
55+
]
56+
57+
const result = applyBlocklistCorrection({
58+
data,
59+
packageName: 'svelte',
60+
granularity: 'weekly',
61+
}) as WeeklyDataPoint[]
62+
63+
// The spike week must be corrected
64+
expect(result[2]!.hasAnomaly).toBe(true)
65+
expect(result[2]!.value).toBeLessThan(1_000_000)
66+
67+
// The boundary week must NOT be modified
68+
expect(result[3]!.value).toBe(500_000)
69+
expect(result[3]!.hasAnomaly).toBeUndefined()
70+
})
71+
72+
it('does not over-correct a week ending on the anomaly start boundary', () => {
73+
const data = [
74+
week('2023-06-13', 500_000), // ends on anomaly start boundary — normal!
75+
week('2023-06-20', 10_000_000), // contains spike
76+
week('2023-06-27', 500_000),
77+
]
78+
79+
const result = applyBlocklistCorrection({
80+
data,
81+
packageName: 'svelte',
82+
granularity: 'weekly',
83+
}) as WeeklyDataPoint[]
84+
85+
// The boundary week must NOT be modified
86+
expect(result[0]!.value).toBe(500_000)
87+
expect(result[0]!.hasAnomaly).toBeUndefined()
88+
89+
// The spike week must be corrected
90+
expect(result[1]!.hasAnomaly).toBe(true)
91+
expect(result[1]!.value).toBeLessThan(1_000_000)
92+
})
6493
})

0 commit comments

Comments
 (0)