summaryrefslogtreecommitdiff
path: root/HutchTests/HomeViewModelTests.swift
blob: 72f2e71e4d1e9a70e214930fb0045f375e045c29 (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
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 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
        )
    }
}