gitbayTests/ProfileCommitTests.swift

v1.0.7
gitbay-ios/gitbayTests/ProfileCommitTests.swift history · blame · raw

150 lines · 6238 bytes

  1import Foundation
  2import Testing
  3@testable import gitbay
  4
  5private func makeClient() throws -> (GitbayClient, StubProtocol.Box) {
  6    let box = StubProtocol.box()
  7    let client = GitbayClient(
  8        instance: try GitbayInstance(url: "https://gitbay.org"),
  9        token: "test-token",
 10        session: box.session()
 11    )
 12    return (client, box)
 13}
 14
 15private let userProfileJSON = """
 16    {"protocol_version":1,"data":{"name":"cmc","kind":"user",\
 17    "description":"Org-Mode · Self-Hosting · Privacy","website":"https://cleberg.net",\
 18    "about":"* Christian\\n\\nOrg-Mode.","about_format":"org",\
 19    "links":[{"label":"Tor","url":"http://example.onion/"},{"url":"https://krz.sh"}],\
 20    "orgs":[{"name":"krz","role":"admin"},{"name":"audit-labs","role":"admin"}],\
 21    "repos":[{"path":"cmc/notes","visibility":"private","description":"notes",\
 22    "topics":["org","writing"],"license":"MIT","default_branch":"main","updated":"2026-08-28"},\
 23    {"path":"cmc/cv","visibility":"public"}],\
 24    "activity":[{"date":"2026-08-01","count":3},{"date":"2026-08-02","count":12}],\
 25    "activity_total":15},"exit_code":0}
 26    """
 27
 28private let orgProfileJSON = """
 29    {"protocol_version":1,"data":{"name":"krz","kind":"org","description":"warez",\
 30    "members":[{"name":"cmc","role":"admin"}],"repos":[],"activity_total":0},"exit_code":0}
 31    """
 32
 33@MainActor
 34struct ProfileAggregateTests {
 35
 36    @Test func aUserProfileCarriesOrgsReposAndActivity() async throws {
 37        let (client, stub) = try makeClient()
 38        stub.enqueue(.init(status: 200, json: userProfileJSON))
 39        let model = ProfileViewModel(client: client, name: "cmc")
 40
 41        await model.load()
 42
 43        let profile = try #require(model.state.value)
 44        #expect(!profile.isOrg)
 45        #expect(profile.orgs?.map(\.name) == ["krz", "audit-labs"])
 46        #expect(profile.repos.map(\.path) == ["cmc/notes", "cmc/cv"])
 47        #expect(profile.repos[0].isPrivate)
 48        // Repo rows carry the listing metadata, so a profile lists
 49        // repositories the way explore does.
 50        #expect(profile.repos[0].topics == ["org", "writing"])
 51        #expect(profile.repos[0].meta == "main · MIT · updated 2026-08-28")
 52        // A row the server reported without any of it still renders.
 53        #expect(profile.repos[1].topics == nil)
 54        #expect(profile.repos[1].meta.isEmpty)
 55        #expect(profile.about == "* Christian\n\nOrg-Mode.")
 56        // The format picks the filename the about text renders under.
 57        #expect(profile.aboutFile == "about.org")
 58        #expect(profile.links?.map(\.title) == ["Tor", "https://krz.sh"])
 59        #expect(profile.activityTotal == 15)
 60        // One read builds the whole page.
 61        #expect(stub.seen.count == 1)
 62        let first = try #require(stub.seen.first)
 63        #expect(first.url.query() == "argv=profile&argv=show&argv=cmc")
 64    }
 65
 66    @Test func anOrgProfileCarriesMembersInsteadOfOrgs() async throws {
 67        let (client, stub) = try makeClient()
 68        stub.enqueue(.init(status: 200, json: orgProfileJSON))
 69        let model = ProfileViewModel(client: client, name: "krz")
 70
 71        await model.load()
 72
 73        let profile = try #require(model.state.value)
 74        #expect(profile.isOrg)
 75        #expect(profile.members?.map(\.name) == ["cmc"])
 76        #expect(profile.orgs == nil)
 77        #expect(profile.repos.isEmpty)
 78        // A profile with no about text and no links renders neither.
 79        #expect(profile.about == nil)
 80        #expect(profile.links == nil)
 81        // Markdown is the default for anything but org.
 82        #expect(profile.aboutFile == "about.md")
 83    }
 84
 85    @Test func anUnknownNameIsAnEmptyState() async throws {
 86        let (client, stub) = try makeClient()
 87        stub.enqueue(.init(status: 404, json:
 88            #"{"protocol_version":1,"error":"no user or organization \"nobody\"","exit_code":3}"#))
 89        let model = ProfileViewModel(client: client, name: "nobody")
 90
 91        await model.load()
 92
 93        guard case .empty = model.state else {
 94            Issue.record("expected .empty, got \(model.state)")
 95            return
 96        }
 97    }
 98}
 99
100@MainActor
101struct CommitDetailTests {
102
103    private let commitJSON = """
104        {"protocol_version":1,"data":{"path":"krz/gitbay",\
105        "sha":"59ae14400000000000000000000000000000abcd",\
106        "subject":"control: profile aggregate","message":"Body text.",\
107        "author_name":"Christian Cleberg","author_email":"hello@cleberg.net",\
108        "date":"2026-08-28T02:00:00Z",\
109        "signature":{"state":"verified","signer":"cmc"},\
110        "checks":[{"context":"ci","state":"success"}],\
111        "diff":"diff --git a/main.go b/main.go\\n--- a/main.go\\n+++ b/main.go\\n@@ -1,2 +1,3 @@\\n package main\\n+// added\\n"},\
112        "exit_code":0}
113        """
114
115    @Test func loadsTheCommitAndParsesItsPatch() async throws {
116        let (client, stub) = try makeClient()
117        stub.enqueue(.init(status: 200, json: commitJSON))
118        let model = CommitDetailViewModel(
119            client: client, repoPath: "krz/gitbay", sha: "59ae144")
120
121        await model.load()
122
123        let commit = try #require(model.state.value)
124        #expect(commit.subject == "control: profile aggregate")
125        #expect(commit.signature.state == .verified)
126        #expect(commit.checks?.first?.state == "success")
127        #expect(commit.shortSHA == "59ae144000")
128        // The patch is parsed by the same parser mr diff uses.
129        let diff = try #require(model.diff)
130        #expect(diff.files.map(\.displayPath) == ["main.go"])
131        #expect(diff.additions == 1)
132        #expect(stub.seen.first?.url.query() ==
133            "argv=repo&argv=commit&argv=krz/gitbay&argv=59ae144")
134    }
135
136    @Test func anUnknownShaIsAnEmptyState() async throws {
137        let (client, stub) = try makeClient()
138        stub.enqueue(.init(status: 404, json:
139            #"{"protocol_version":1,"error":"no commit \"deadbeef\" in krz/gitbay","exit_code":3}"#))
140        let model = CommitDetailViewModel(
141            client: client, repoPath: "krz/gitbay", sha: "deadbeef")
142
143        await model.load()
144
145        guard case .empty = model.state else {
146            Issue.record("expected .empty, got \(model.state)")
147            return
148        }
149    }
150}