aboutsummaryrefslogtreecommitdiff
path: root/HutchTests/HomeViewModelTests.swift
blob: 851b5204361d5ff9945ec290ee459654e0d8bc7f (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
import Foundation
import Testing
@testable import Hutch

struct HomeViewModelTests {

    @Test
    func failedBuildsKeepsOnlyFailedAndTimedOutJobs() {
        let jobs = [
            makeJob(id: 1, status: .success, created: Date(timeIntervalSince1970: 10)),
            makeJob(id: 2, status: .failed, created: Date(timeIntervalSince1970: 20)),
            makeJob(id: 3, status: .timeout, created: Date(timeIntervalSince1970: 30)),
            makeJob(id: 4, status: .running, created: Date(timeIntervalSince1970: 40))
        ]

        let failedBuilds = HomeViewModel.failedBuilds(from: jobs)

        #expect(failedBuilds.map(\.job.id) == [3, 2])
    }

    @Test
    func failedBuildsWithinLookbackExcludeOlderFailures() {
        let now = Date(timeIntervalSince1970: 60 * 60 * 24 * 20)
        let recentFailure = HomeBuildItem(
            job: JobSummary(
                id: 1,
                created: now.addingTimeInterval(-(60 * 60 * 24)),
                updated: now.addingTimeInterval(-(60 * 60 * 24)),
                status: .failed,
                note: nil,
                tags: [],
                visibility: nil,
                image: nil,
                tasks: []
            ),
            repositoryName: nil,
            repositoryOwner: nil
        )
        let oldFailure = HomeBuildItem(
            job: JobSummary(
                id: 2,
                created: now.addingTimeInterval(-(60 * 60 * 24 * 10)),
                updated: now.addingTimeInterval(-(60 * 60 * 24 * 10)),
                status: .timeout,
                note: nil,
                tags: [],
                visibility: nil,
                image: nil,
                tasks: []
            ),
            repositoryName: nil,
            repositoryOwner: nil
        )
        let recentSuccess = HomeBuildItem(
            job: JobSummary(
                id: 3,
                created: now.addingTimeInterval(-(60 * 60 * 24)),
                updated: now.addingTimeInterval(-(60 * 60 * 24)),
                status: .success,
                note: nil,
                tags: [],
                visibility: nil,
                image: nil,
                tasks: []
            ),
            repositoryName: nil,
            repositoryOwner: nil
        )

        let filtered = HomeViewModel.failedBuilds(
            in: [recentFailure, oldFailure, recentSuccess],
            lookbackDays: 7,
            now: now,
            calendar: Calendar(identifier: .gregorian)
        )

        #expect(filtered.map(\.job.id) == [1])
    }

    @Test
    func failedBuildLookbackDaysFallsBackToDefaultWhenUnsetOrInvalid() {
        let defaults = UserDefaults(suiteName: #function)!
        defaults.removePersistentDomain(forName: #function)

        #expect(HomeViewModel.failedBuildLookbackDays(defaults: defaults) == HomeViewModel.defaultFailedBuildLookbackDays)

        defaults.set(99, forKey: AppStorageKeys.homeFailedBuildLookbackDays)

        #expect(HomeViewModel.failedBuildLookbackDays(defaults: defaults) == HomeViewModel.defaultFailedBuildLookbackDays)
    }

    @Test
    func deduplicateInboxThreadsCollapsesMessagesIntoOneThreadSummary() {
        let list = InboxMailingListReference(
            id: 1,
            rid: "list",
            name: "hutch-devel",
            owner: Entity(canonicalName: "~owner")
        )
        let first = InboxThreadSummary(
            rootEmailID: 10,
            rootMessageID: "message-1",
            threadRootEmailIDs: [10],
            threadRootMessageIDs: ["message-1"],
            listID: list.id,
            listRID: list.rid,
            listName: list.name,
            listOwner: list.owner,
            subject: "Re: [PATCH] add search",
            latestSender: Entity(canonicalName: "~alice"),
            lastActivityAt: Date(timeIntervalSince1970: 100),
            messageCount: 1,
            repo: "hutch",
            containsPatch: true,
            isUnread: true
        )
        let second = InboxThreadSummary(
            rootEmailID: 11,
            rootMessageID: "message-2",
            threadRootEmailIDs: [11],
            threadRootMessageIDs: ["message-2"],
            listID: list.id,
            listRID: list.rid,
            listName: list.name,
            listOwner: list.owner,
            subject: "[PATCH] add search",
            latestSender: Entity(canonicalName: "~bob"),
            lastActivityAt: Date(timeIntervalSince1970: 200),
            messageCount: 2,
            repo: "hutch",
            containsPatch: true,
            isUnread: true
        )

        let deduplicated = HomeViewModel.deduplicateInboxThreads([first, second])

        #expect(deduplicated.count == 1)
        #expect(deduplicated[0].rootEmailID == 11)
        #expect(deduplicated[0].threadRootEmailIDs == [10, 11])
        #expect(deduplicated[0].threadRootMessageIDs == ["message-1", "message-2"])
        #expect(deduplicated[0].messageCount == 2)
    }

    @Test
    func matchesCurrentUserAssigneeNormalizesCanonicalNameAndUsername() {
        let currentUser = User(
            id: 42,
            created: nil,
            updated: nil,
            username: "owner",
            canonicalName: "~owner",
            email: "[email protected]",
            url: nil,
            location: nil,
            bio: nil,
            avatar: nil,
            pronouns: nil,
            userType: nil,
            receivesPaidServices: nil,
            suspensionNotice: nil
        )

        #expect(HomeViewModel.matchesCurrentUserAssignee(Entity(canonicalName: "~owner"), currentUser: currentUser))
        #expect(HomeViewModel.matchesCurrentUserAssignee(Entity(canonicalName: "owner"), currentUser: currentUser))
        #expect(HomeViewModel.matchesCurrentUserAssignee(Entity(canonicalName: currentUser.username), currentUser: currentUser))
        #expect(HomeViewModel.matchesCurrentUserAssignee(Entity(canonicalName: "~someone-else"), currentUser: currentUser) == false)
    }

    @Test
    func primaryRepositoryReferenceParsesManifestSourceURL() {
        let manifest = """
        image: alpine/latest
        sources:
          - https://git.sr.ht/~owner/hutch
        tasks:
          - true
        """

        let repository = HomeViewModel.primaryRepositoryReference(in: manifest)

        #expect(repository?.ownerCanonicalName == "~owner")
        #expect(repository?.name == "hutch")
    }

    @Test
    func buildItemsAreSortedForTriage() {
        let jobs = [
            makeJob(id: 1, status: .success, created: Date(timeIntervalSince1970: 10)),
            makeJob(id: 2, status: .running, created: Date(timeIntervalSince1970: 20)),
            makeJob(id: 3, status: .failed, created: Date(timeIntervalSince1970: 30))
        ]

        let sorted = HomeViewModel.buildItems(from: jobs)

        #expect(sorted.map(\.job.id) == [3, 2, 1])
    }

    private func makeJob(id: Int, status: JobStatus, created: Date) -> HomeJobPayload {
        HomeJobPayload(
            id: id,
            created: created,
            updated: created,
            status: status,
            note: nil,
            tags: [],
            visibility: nil,
            image: nil,
            tasks: [],
            manifest: nil
        )
    }
}