a native ios client for gitbay

client ios swift

https://gitbay.org

gitbayTests/ProfileCommitTests.swift

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

138 lines · 5536 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    "orgs":[{"name":"krz","role":"admin"},{"name":"audit-labs","role":"admin"}],\
 19    "repos":[{"path":"cmc/notes","visibility":"private","description":"notes",\
 20    "topics":["org","writing"],"license":"MIT","default_branch":"main","updated":"2026-08-28"},\
 21    {"path":"cmc/cv","visibility":"public"}],\
 22    "activity":[{"date":"2026-08-01","count":3},{"date":"2026-08-02","count":12}],\
 23    "activity_total":15},"exit_code":0}
 24    """
 25
 26private let orgProfileJSON = """
 27    {"protocol_version":1,"data":{"name":"krz","kind":"org","description":"warez",\
 28    "members":[{"name":"cmc","role":"admin"}],"repos":[],"activity_total":0},"exit_code":0}
 29    """
 30
 31@MainActor
 32struct ProfileAggregateTests {
 33
 34    @Test func aUserProfileCarriesOrgsReposAndActivity() async throws {
 35        let (client, stub) = try makeClient()
 36        stub.enqueue(.init(status: 200, json: userProfileJSON))
 37        let model = ProfileViewModel(client: client, name: "cmc")
 38
 39        await model.load()
 40
 41        let profile = try #require(model.state.value)
 42        #expect(!profile.isOrg)
 43        #expect(profile.orgs?.map(\.name) == ["krz", "audit-labs"])
 44        #expect(profile.repos.map(\.path) == ["cmc/notes", "cmc/cv"])
 45        #expect(profile.repos[0].isPrivate)
 46        // Repo rows carry the listing metadata, so a profile lists
 47        // repositories the way explore does.
 48        #expect(profile.repos[0].topics == ["org", "writing"])
 49        #expect(profile.repos[0].meta == "main · MIT · updated 2026-08-28")
 50        // A row the server reported without any of it still renders.
 51        #expect(profile.repos[1].topics == nil)
 52        #expect(profile.repos[1].meta.isEmpty)
 53        #expect(profile.activityTotal == 15)
 54        // One read builds the whole page.
 55        #expect(stub.seen.count == 1)
 56        #expect(stub.seen[0].url.query() == "argv=profile&argv=show&argv=cmc")
 57    }
 58
 59    @Test func anOrgProfileCarriesMembersInsteadOfOrgs() async throws {
 60        let (client, stub) = try makeClient()
 61        stub.enqueue(.init(status: 200, json: orgProfileJSON))
 62        let model = ProfileViewModel(client: client, name: "krz")
 63
 64        await model.load()
 65
 66        let profile = try #require(model.state.value)
 67        #expect(profile.isOrg)
 68        #expect(profile.members?.map(\.name) == ["cmc"])
 69        #expect(profile.orgs == nil)
 70        #expect(profile.repos.isEmpty)
 71    }
 72
 73    @Test func anUnknownNameIsAnEmptyState() async throws {
 74        let (client, stub) = try makeClient()
 75        stub.enqueue(.init(status: 404, json:
 76            #"{"protocol_version":1,"error":"no user or organization \"nobody\"","exit_code":3}"#))
 77        let model = ProfileViewModel(client: client, name: "nobody")
 78
 79        await model.load()
 80
 81        guard case .empty = model.state else {
 82            Issue.record("expected .empty, got \(model.state)")
 83            return
 84        }
 85    }
 86}
 87
 88@MainActor
 89struct CommitDetailTests {
 90
 91    private let commitJSON = """
 92        {"protocol_version":1,"data":{"path":"krz/gitbay",\
 93        "sha":"59ae14400000000000000000000000000000abcd",\
 94        "subject":"control: profile aggregate","message":"Body text.",\
 95        "author_name":"Christian Cleberg","author_email":"hello@cleberg.net",\
 96        "date":"2026-08-28T02:00:00Z",\
 97        "signature":{"state":"verified","signer":"cmc"},\
 98        "checks":[{"context":"ci","state":"success"}],\
 99        "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"},\
100        "exit_code":0}
101        """
102
103    @Test func loadsTheCommitAndParsesItsPatch() async throws {
104        let (client, stub) = try makeClient()
105        stub.enqueue(.init(status: 200, json: commitJSON))
106        let model = CommitDetailViewModel(
107            client: client, repoPath: "krz/gitbay", sha: "59ae144")
108
109        await model.load()
110
111        let commit = try #require(model.state.value)
112        #expect(commit.subject == "control: profile aggregate")
113        #expect(commit.signature.state == .verified)
114        #expect(commit.checks?.first?.state == "success")
115        #expect(commit.shortSHA == "59ae144000")
116        // The patch is parsed by the same parser mr diff uses.
117        let diff = try #require(model.diff)
118        #expect(diff.files.map(\.displayPath) == ["main.go"])
119        #expect(diff.additions == 1)
120        #expect(stub.seen.first?.url.query() ==
121            "argv=repo&argv=commit&argv=krz/gitbay&argv=59ae144")
122    }
123
124    @Test func anUnknownShaIsAnEmptyState() async throws {
125        let (client, stub) = try makeClient()
126        stub.enqueue(.init(status: 404, json:
127            #"{"protocol_version":1,"error":"no commit \"deadbeef\" in krz/gitbay","exit_code":3}"#))
128        let model = CommitDetailViewModel(
129            client: client, repoPath: "krz/gitbay", sha: "deadbeef")
130
131        await model.load()
132
133        guard case .empty = model.state else {
134            Issue.record("expected .empty, got \(model.state)")
135            return
136        }
137    }
138}