krz/hutch
an ios client for sourcehut
clone: git clone https://gitbay.org/krz/hutch.git
v2.15.1: HutchTests/HomeViewModelTests.swift · raw
1import Foundation
2import Testing
3@testable import Hutch
4
5struct HomeViewModelTests {
6
7 @Test
8 func failedBuildsKeepsOnlyFailedAndTimedOutJobs() {
9 let jobs = [
10 makeJob(id: 1, status: .success, created: Date(timeIntervalSince1970: 10)),
11 makeJob(id: 2, status: .failed, created: Date(timeIntervalSince1970: 20)),
12 makeJob(id: 3, status: .timeout, created: Date(timeIntervalSince1970: 30)),
13 makeJob(id: 4, status: .running, created: Date(timeIntervalSince1970: 40))
14 ]
15
16 let failedBuilds = HomeViewModel.failedBuilds(from: jobs)
17
18 #expect(failedBuilds.map(\.job.id) == [2, 3])
19 }
20
21 @Test
22 func matchesCurrentUserAssigneeNormalizesCanonicalNameAndUsername() {
23 let currentUser = User(
24 id: 42,
25 created: nil,
26 updated: nil,
27 username: "owner",
28 canonicalName: "~owner",
29 email: "owner@example.com",
30 url: nil,
31 location: nil,
32 bio: nil,
33 avatar: nil,
34 pronouns: nil,
35 userType: nil,
36 receivesPaidServices: nil,
37 suspensionNotice: nil
38 )
39
40 #expect(HomeViewModel.matchesCurrentUserAssignee(Entity(canonicalName: "~owner"), currentUser: currentUser))
41 #expect(HomeViewModel.matchesCurrentUserAssignee(Entity(canonicalName: "owner"), currentUser: currentUser))
42 #expect(HomeViewModel.matchesCurrentUserAssignee(Entity(canonicalName: currentUser.username), currentUser: currentUser))
43 #expect(HomeViewModel.matchesCurrentUserAssignee(Entity(canonicalName: "~someone-else"), currentUser: currentUser) == false)
44 }
45
46 @Test
47 func primaryRepositoryReferenceParsesManifestSourceURL() {
48 let manifest = """
49 image: alpine/latest
50 sources:
51 - https://git.sr.ht/~owner/hutch
52 tasks:
53 - true
54 """
55
56 let repository = HomeViewModel.primaryRepositoryReference(in: manifest)
57
58 #expect(repository?.ownerCanonicalName == "~owner")
59 #expect(repository?.name == "hutch")
60 }
61
62 @Test
63 func buildItemsAreSortedForTriage() {
64 let jobs = [
65 makeJob(id: 1, status: .success, created: Date(timeIntervalSince1970: 10)),
66 makeJob(id: 2, status: .running, created: Date(timeIntervalSince1970: 20)),
67 makeJob(id: 3, status: .failed, created: Date(timeIntervalSince1970: 30))
68 ]
69
70 let sorted = HomeViewModel.buildItems(from: jobs)
71
72 #expect(sorted.map(\.job.id) == [3, 2, 1])
73 }
74
75 private func makeJob(id: Int, status: JobStatus, created: Date) -> HomeJobPayload {
76 HomeJobPayload(
77 id: id,
78 created: created,
79 updated: created,
80 status: status,
81 note: nil,
82 tags: [],
83 visibility: nil,
84 image: nil,
85 tasks: [],
86 manifest: nil
87 )
88 }
89}