forked from AOSSIE-Org/OrgExplorer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalytics.buildTimeSeries.test.js
More file actions
120 lines (93 loc) · 3.88 KB
/
Copy pathanalytics.buildTimeSeries.test.js
File metadata and controls
120 lines (93 loc) · 3.88 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
import { describe, it, expect } from 'vitest'
import { buildTimeSeries } from './analytics'
describe('buildTimeSeries', () => {
it('buckets a plain issue by its created_at month', () => {
const issues = [{ created_at: '2026-03-15T00:00:00Z' }]
const result = buildTimeSeries(issues, 'monthly')
expect(result).toHaveLength(1)
expect(result[0].date).toBe('2026-03')
expect(result[0].issues_created).toBe(1)
expect(result[0].prs_created).toBe(0)
})
it('counts a pull_request item as a PR, not an issue', () => {
const issues = [{ created_at: '2026-03-15T00:00:00Z', pull_request: {} }]
const result = buildTimeSeries(issues, 'monthly')
expect(result[0].prs_created).toBe(1)
expect(result[0].issues_created).toBe(0)
})
it('buckets closed_at into the correct month, separate from created_at', () => {
const issues = [{
created_at: '2026-01-01T00:00:00Z',
closed_at: '2026-02-01T00:00:00Z',
}]
const result = buildTimeSeries(issues, 'monthly')
const jan = result.find(r => r.date === '2026-01')
const feb = result.find(r => r.date === '2026-02')
expect(jan.issues_created).toBe(1)
expect(jan.issues_closed).toBe(0)
expect(feb.issues_closed).toBe(1)
expect(feb.issues_created).toBe(0)
})
it('buckets a merged PR into prs_merged using pull_request.merged_at, not closed_at', () => {
const issues = [{
created_at: '2026-01-01T00:00:00Z',
closed_at: '2026-01-05T00:00:00Z',
pull_request: { merged_at: '2026-01-05T00:00:00Z' },
}]
const result = buildTimeSeries(issues, 'monthly')
const jan = result.find(r => r.date === '2026-01')
expect(jan.prs_created).toBe(1)
expect(jan.prs_closed).toBe(1)
expect(jan.prs_merged).toBe(1)
})
it('does not count prs_merged for a closed-but-not-merged PR', () => {
const issues = [{
created_at: '2026-01-01T00:00:00Z',
closed_at: '2026-01-05T00:00:00Z',
pull_request: { merged_at: null },
}]
const result = buildTimeSeries(issues, 'monthly')
const jan = result.find(r => r.date === '2026-01')
expect(jan.prs_closed).toBe(1)
expect(jan.prs_merged).toBe(0)
})
it('produces one bucket per week when granularity is weekly, distinct from monthly', () => {
const issues = [
{ created_at: '2026-01-01T00:00:00Z' },
{ created_at: '2026-01-08T00:00:00Z' },
]
const weekly = buildTimeSeries(issues, 'weekly')
// Two different weeks -> two buckets, and week-format labels
expect(weekly.length).toBeGreaterThanOrEqual(1)
weekly.forEach(bucket => expect(bucket.date).toMatch(/^\d{4}-W\d{2}$/))
})
it('sorts buckets chronologically', () => {
const issues = [
{ created_at: '2026-03-01T00:00:00Z' },
{ created_at: '2026-01-01T00:00:00Z' },
{ created_at: '2026-02-01T00:00:00Z' },
]
const result = buildTimeSeries(issues, 'monthly')
expect(result.map(r => r.date)).toEqual(['2026-01', '2026-02', '2026-03'])
})
it('returns at most the last 12 buckets when more months are present', () => {
const issues = Array.from({ length: 15 }, (_, i) => ({
created_at: `2025-${String((i % 12) + 1).padStart(2, '0')}-01T00:00:00Z`,
}))
// Construct 15 distinct months across two years to exceed the 12-bucket cap
const spreadIssues = Array.from({ length: 15 }, (_, i) => {
const year = 2025 + Math.floor(i / 12)
const month = (i % 12) + 1
return { created_at: `${year}-${String(month).padStart(2, '0')}-01T00:00:00Z` }
})
const result = buildTimeSeries(spreadIssues, 'monthly')
expect(result.length).toBeLessThanOrEqual(12)
})
it('returns an empty array when given no issues', () => {
expect(buildTimeSeries([], 'monthly')).toEqual([])
})
it('ignores an item with no created_at and no closed_at', () => {
const result = buildTimeSeries([{ created_at: null }], 'monthly')
expect(result).toEqual([])
})
})