summaryrefslogtreecommitdiff
path: root/HutchTests/ContributionCalendarTests.swift
blob: 4ce2f1f46a1d50132b04422e44e193d7e5b2b01c (plain) (blame)
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
import Foundation
import Testing
@testable import Hutch

struct ContributionCalendarTests {

    @Test
    @MainActor
    func contributionCalendarDecodingParsesDatesAndCounts() throws {
        let data = Data(
            """
            {
              "actor": "~ccleberg",
              "from": "2026-03-01",
              "to": "2026-04-15",
              "is_indexed": false,
              "last_polled_at": null,
              "indexing_state": "pending",
              "days": [
                { "date": "2026-03-19", "count": 24, "score": 24.0 },
                { "date": "2026-04-02", "count": 20, "score": 18.5 }
              ]
            }
            """.utf8
        )

        let decoded = try JSONDecoder().decode(ContributionCalendarResponse.self, from: data)

        #expect(decoded.actor == "~ccleberg")
        #expect(decoded.from == ContributionDateParser.parse("2026-03-01"))
        #expect(decoded.to == ContributionDateParser.parse("2026-04-15"))
        #expect(decoded.isIndexed == false)
        #expect(decoded.lastPolledAt == nil)
        #expect(decoded.indexingState == .pending)
        #expect(decoded.days.count == 2)
        #expect(decoded.days[0].date == ContributionDateParser.parse("2026-03-19"))
        #expect(decoded.days[0].count == 24)
        #expect(decoded.days[1].score == 18.5)
    }

    @Test
    @MainActor
    func contributionStatsDecodingParsesSnakeCasePayload() throws {
        let data = Data(
            """
            {
              "actor": "~ccleberg",
              "from": "2026-03-01",
              "to": "2026-04-15",
              "is_indexed": true,
              "last_polled_at": "2026-04-15T15:42:18Z",
              "indexing_state": "indexed",
              "total_events": 126,
              "total_score": 116.75,
              "active_days": 14,
              "longest_streak": 5,
              "current_streak": 0
            }
            """.utf8
        )

        let decoded = try JSONDecoder().decode(ContributionStatsResponse.self, from: data)

        #expect(decoded.totalEvents == 126)
        #expect(decoded.totalScore == 116.75)
        #expect(decoded.activeDays == 14)
        #expect(decoded.longestStreak == 5)
        #expect(decoded.currentStreak == 0)
        #expect(decoded.isIndexed)
        #expect(decoded.indexingState == .indexed)
        #expect(decoded.lastPolledAt == ContributionDateParser.parseTimestamp("2026-04-15T15:42:18Z"))
    }

    @Test
    func contributionDateParserRejectsInvalidDate() {
        #expect(ContributionDateParser.parse("2026-13-40") == nil)
    }

    @Test
    func contributionTimestampParserHandlesFractionalSecondsWithoutTimezone() {
        let parsed = ContributionDateParser.parseTimestamp("2026-04-11T19:26:45.538015")

        #expect(parsed != nil)
    }

    @Test
    func trailingRangeEndsTodayAndStartsOneYearEarlierPlusOneDay() {
        let service = HutchStatsService(configuration: AppConfiguration())
        let endDate = ContributionDateParser.parse("2026-04-11")!
        let range = service.trailingRange(endingOn: endDate)

        #expect(range.lowerBound == ContributionDateParser.parse("2025-04-12"))
        #expect(range.upperBound == endDate)
    }

    @Test
    func contributionIntensityBucketsMatchProductRules() {
        #expect(ContributionIntensity(count: 0) == .empty)
        #expect(ContributionIntensity(count: 1) == .level1)
        #expect(ContributionIntensity(count: 2) == .level2)
        #expect(ContributionIntensity(count: 3) == .level2)
        #expect(ContributionIntensity(count: 4) == .level3)
        #expect(ContributionIntensity(count: 6) == .level3)
        #expect(ContributionIntensity(count: 7) == .level4)
        #expect(ContributionIntensity(count: 12) == .level4)
    }

    @Test
    func weekColumnsGroupConsecutiveDaysIntoSundayBasedWeeks() {
        let days = [
            ContributionDay(date: ContributionDateParser.parse("2026-03-29")!, count: 1, score: 1),
            ContributionDay(date: ContributionDateParser.parse("2026-03-30")!, count: 2, score: 2),
            ContributionDay(date: ContributionDateParser.parse("2026-04-04")!, count: 3, score: 3),
            ContributionDay(date: ContributionDateParser.parse("2026-04-05")!, count: 4, score: 4)
        ]

        let weeks = ContributionCalendarLayout.weekColumns(from: days)

        #expect(weeks.count == 2)
        #expect(weeks[0].startDate == ContributionDateParser.parse("2026-03-29"))
        #expect(weeks[0].days.map(\.count) == [1, 2, 3])
        #expect(weeks[1].startDate == ContributionDateParser.parse("2026-04-05"))
        #expect(weeks[1].days.map(\.count) == [4])
    }

    @Test
    func recentWeeksReturnsTrailingWindow() {
        let days = (0..<21).compactMap { offset -> ContributionDay? in
            guard let date = Calendar.contributionCalendar.date(byAdding: .day, value: offset, to: ContributionDateParser.parse("2026-03-01")!) else {
                return nil
            }

            return ContributionDay(date: date, count: offset, score: Double(offset))
        }

        let weeks = ContributionCalendarLayout.recentWeeks(from: days, count: 2)

        #expect(weeks.count == 2)
        #expect(weeks[0].startDate == ContributionDateParser.parse("2026-03-08"))
        #expect(weeks[1].startDate == ContributionDateParser.parse("2026-03-15"))
    }

    @Test
    @MainActor
    func emptyCalendarResponseTreatsActivityAsNotIndexedYet() async {
        let service = MockContributionCalendarService(
            calendarResponses: [.pending(actor: "~alice", year: 2026)],
            statsResponses: [.pending(actor: "~alice", year: 2026)]
        )
        let viewModel = ContributionCalendarViewModel(
            actor: "~alice",
            service: service,
            selectedEndDate: ContributionDateParser.parse("2026-04-11")
        )

        await viewModel.load()

        #expect(viewModel.displayState == .indexing)
        #expect(viewModel.emptyStateTitle == "Indexing Activity")
        #expect(viewModel.emptyStateMessage == "This user’s SourceHut activity is being indexed. Check back soon.")
        #expect(viewModel.loadErrorMessage == nil)
    }

    @Test
    @MainActor
    func indexedEmptyCalendarShowsTrueEmptyState() async {
        let service = MockContributionCalendarService(
            calendarResponses: [.empty(actor: "~alice", year: 2026)],
            statsResponses: [.empty(actor: "~alice", year: 2026)]
        )
        let viewModel = ContributionCalendarViewModel(
            actor: "~alice",
            service: service,
            selectedEndDate: ContributionDateParser.parse("2026-04-11")
        )

        await viewModel.load()

        #expect(viewModel.displayState == .empty)
        #expect(viewModel.emptyStateTitle == "No Contribution Activity")
        #expect(viewModel.emptyStateMessage == "No activity was found for this time range.")
    }

    @Test
    @MainActor
    func errorIndexingStateShowsUnavailableState() async {
        let service = MockContributionCalendarService(
            calendarResponses: [.error(actor: "~alice", year: 2026)],
            statsResponses: [.error(actor: "~alice", year: 2026)]
        )
        let viewModel = ContributionCalendarViewModel(
            actor: "~alice",
            service: service,
            selectedEndDate: ContributionDateParser.parse("2026-04-11")
        )

        await viewModel.load()

        #expect(viewModel.displayState == .unavailable)
        #expect(viewModel.emptyStateTitle == "Activity Unavailable")
        #expect(viewModel.emptyStateMessage == "The contribution graph couldn’t be refreshed right now. Try again later.")
    }

    @Test
    @MainActor
    func populatedStatsExposeLastUpdatedText() async {
        let service = MockContributionCalendarService(
            calendarResponses: [.active(actor: "~alice", date: "2026-03-19", count: 4, score: 4)],
            statsResponses: [.active(actor: "~alice", year: 2026, totalEvents: 4, activeDays: 1, longestStreak: 1)]
        )
        let viewModel = ContributionCalendarViewModel(
            actor: "~alice",
            service: service,
            selectedEndDate: ContributionDateParser.parse("2026-04-11")
        )

        await viewModel.load()

        #expect(viewModel.displayState == .populated)
        #expect(viewModel.lastUpdatedText != nil)
    }
}

private final class MockContributionCalendarService: ContributionCalendarServing, @unchecked Sendable {
    var calendarResponses: [ContributionCalendarResponse]
    var statsResponses: [ContributionStatsResponse]
    private(set) var fetchCalendarCallCount = 0
    private(set) var fetchStatsCallCount = 0

    init(
        calendarResponses: [ContributionCalendarResponse],
        statsResponses: [ContributionStatsResponse]
    ) {
        self.calendarResponses = calendarResponses
        self.statsResponses = statsResponses
    }

    func fetchContributionCalendar(actor: String, endingOn endDate: Date) async throws -> ContributionCalendarResponse {
        fetchCalendarCallCount += 1
        return calendarResponses[min(fetchCalendarCallCount - 1, calendarResponses.count - 1)]
    }

    func fetchContributionStats(actor: String, endingOn endDate: Date) async throws -> ContributionStatsResponse {
        fetchStatsCallCount += 1
        return statsResponses[min(fetchStatsCallCount - 1, statsResponses.count - 1)]
    }
}

private extension ContributionCalendarResponse {
    static func empty(actor: String, year: Int) -> Self {
        let from = ContributionDateParser.parse("\(year)-01-01")!
        let to = ContributionDateParser.parse("\(year)-01-07")!
        return ContributionCalendarResponse(
            actor: actor,
            from: from,
            to: to,
            isIndexed: true,
            lastPolledAt: ContributionDateParser.parseTimestamp("\(year)-01-07T12:00:00Z"),
            indexingState: .indexed,
            days: (1...7).map { day in
                ContributionDay(
                    date: ContributionDateParser.parse("\(year)-01-0\(day)")!,
                    count: 0,
                    score: 0
                )
            }
        )
    }

    static func active(actor: String, date: String, count: Int, score: Double) -> Self {
        let resolvedDate = ContributionDateParser.parse(date)!
        return ContributionCalendarResponse(
            actor: actor,
            from: resolvedDate,
            to: resolvedDate,
            isIndexed: true,
            lastPolledAt: ContributionDateParser.parseTimestamp("2026-03-19T12:00:00Z"),
            indexingState: .indexed,
            days: [ContributionDay(date: resolvedDate, count: count, score: score)]
        )
    }

    static func pending(actor: String, year: Int) -> Self {
        let from = ContributionDateParser.parse("\(year)-01-01")!
        let to = ContributionDateParser.parse("\(year)-01-07")!
        return ContributionCalendarResponse(
            actor: actor,
            from: from,
            to: to,
            isIndexed: false,
            lastPolledAt: nil,
            indexingState: .pending,
            days: (1...7).map { day in
                ContributionDay(
                    date: ContributionDateParser.parse("\(year)-01-0\(day)")!,
                    count: 0,
                    score: 0
                )
            }
        )
    }

    static func error(actor: String, year: Int) -> Self {
        let from = ContributionDateParser.parse("\(year)-01-01")!
        let to = ContributionDateParser.parse("\(year)-01-07")!
        return ContributionCalendarResponse(
            actor: actor,
            from: from,
            to: to,
            isIndexed: false,
            lastPolledAt: nil,
            indexingState: .error,
            days: (1...7).map { day in
                ContributionDay(
                    date: ContributionDateParser.parse("\(year)-01-0\(day)")!,
                    count: 0,
                    score: 0
                )
            }
        )
    }
}

private extension ContributionStatsResponse {
    static func empty(actor: String, year: Int) -> Self {
        ContributionStatsResponse(
            actor: actor,
            from: ContributionDateParser.parse("\(year)-01-01")!,
            to: ContributionDateParser.parse("\(year)-01-07")!,
            isIndexed: true,
            lastPolledAt: ContributionDateParser.parseTimestamp("\(year)-01-07T12:00:00Z"),
            indexingState: .indexed,
            totalEvents: 0,
            totalScore: 0,
            activeDays: 0,
            longestStreak: 0,
            currentStreak: 0
        )
    }

    static func active(actor: String, year: Int, totalEvents: Int, activeDays: Int, longestStreak: Int) -> Self {
        ContributionStatsResponse(
            actor: actor,
            from: ContributionDateParser.parse("\(year)-01-01")!,
            to: ContributionDateParser.parse("\(year)-01-07")!,
            isIndexed: true,
            lastPolledAt: ContributionDateParser.parseTimestamp("\(year)-01-07T12:00:00Z"),
            indexingState: .indexed,
            totalEvents: totalEvents,
            totalScore: Double(totalEvents),
            activeDays: activeDays,
            longestStreak: longestStreak,
            currentStreak: 0
        )
    }

    static func pending(actor: String, year: Int) -> Self {
        ContributionStatsResponse(
            actor: actor,
            from: ContributionDateParser.parse("\(year)-01-01")!,
            to: ContributionDateParser.parse("\(year)-01-07")!,
            isIndexed: false,
            lastPolledAt: nil,
            indexingState: .pending,
            totalEvents: 0,
            totalScore: 0,
            activeDays: 0,
            longestStreak: 0,
            currentStreak: 0
        )
    }

    static func error(actor: String, year: Int) -> Self {
        ContributionStatsResponse(
            actor: actor,
            from: ContributionDateParser.parse("\(year)-01-01")!,
            to: ContributionDateParser.parse("\(year)-01-07")!,
            isIndexed: false,
            lastPolledAt: nil,
            indexingState: .error,
            totalEvents: 0,
            totalScore: 0,
            activeDays: 0,
            longestStreak: 0,
            currentStreak: 0
        )
    }
}