summaryrefslogtreecommitdiff
path: root/HutchTests/HomeViewModelTests.swift
blob: fe4229f6266288a940fa28ff8ed85becc137a6f5 (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
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) == [2, 3])
    }

    @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 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
        )
    }
}