import Foundation import Testing @testable import gitbay private func makeClient() throws -> (GitbayClient, StubProtocol.Box) { let box = StubProtocol.box() let client = GitbayClient( instance: try GitbayInstance(url: "https://gitbay.org"), token: "test-token", session: box.session() ) return (client, box) } private let userProfileJSON = """ {"protocol_version":1,"data":{"name":"cmc","kind":"user",\ "description":"Org-Mode · Self-Hosting · Privacy","website":"https://cleberg.net",\ "orgs":[{"name":"krz","role":"admin"},{"name":"audit-labs","role":"admin"}],\ "repos":[{"path":"cmc/notes","visibility":"private","description":"notes",\ "topics":["org","writing"],"license":"MIT","default_branch":"main","updated":"2026-08-28"},\ {"path":"cmc/cv","visibility":"public"}],\ "activity":[{"date":"2026-08-01","count":3},{"date":"2026-08-02","count":12}],\ "activity_total":15},"exit_code":0} """ private let orgProfileJSON = """ {"protocol_version":1,"data":{"name":"krz","kind":"org","description":"warez",\ "members":[{"name":"cmc","role":"admin"}],"repos":[],"activity_total":0},"exit_code":0} """ @MainActor struct ProfileAggregateTests { @Test func aUserProfileCarriesOrgsReposAndActivity() async throws { let (client, stub) = try makeClient() stub.enqueue(.init(status: 200, json: userProfileJSON)) let model = ProfileViewModel(client: client, name: "cmc") await model.load() let profile = try #require(model.state.value) #expect(!profile.isOrg) #expect(profile.orgs?.map(\.name) == ["krz", "audit-labs"]) #expect(profile.repos.map(\.path) == ["cmc/notes", "cmc/cv"]) #expect(profile.repos[0].isPrivate) // Repo rows carry the listing metadata, so a profile lists // repositories the way explore does. #expect(profile.repos[0].topics == ["org", "writing"]) #expect(profile.repos[0].meta == "main · MIT · updated 2026-08-28") // A row the server reported without any of it still renders. #expect(profile.repos[1].topics == nil) #expect(profile.repos[1].meta.isEmpty) #expect(profile.activityTotal == 15) // One read builds the whole page. #expect(stub.seen.count == 1) #expect(stub.seen[0].url.query() == "argv=profile&argv=show&argv=cmc") } @Test func anOrgProfileCarriesMembersInsteadOfOrgs() async throws { let (client, stub) = try makeClient() stub.enqueue(.init(status: 200, json: orgProfileJSON)) let model = ProfileViewModel(client: client, name: "krz") await model.load() let profile = try #require(model.state.value) #expect(profile.isOrg) #expect(profile.members?.map(\.name) == ["cmc"]) #expect(profile.orgs == nil) #expect(profile.repos.isEmpty) } @Test func anUnknownNameIsAnEmptyState() async throws { let (client, stub) = try makeClient() stub.enqueue(.init(status: 404, json: #"{"protocol_version":1,"error":"no user or organization \"nobody\"","exit_code":3}"#)) let model = ProfileViewModel(client: client, name: "nobody") await model.load() guard case .empty = model.state else { Issue.record("expected .empty, got \(model.state)") return } } } @MainActor struct CommitDetailTests { private let commitJSON = """ {"protocol_version":1,"data":{"path":"krz/gitbay",\ "sha":"59ae14400000000000000000000000000000abcd",\ "subject":"control: profile aggregate","message":"Body text.",\ "author_name":"Christian Cleberg","author_email":"hello@cleberg.net",\ "date":"2026-08-28T02:00:00Z",\ "signature":{"state":"verified","signer":"cmc"},\ "checks":[{"context":"ci","state":"success"}],\ "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"},\ "exit_code":0} """ @Test func loadsTheCommitAndParsesItsPatch() async throws { let (client, stub) = try makeClient() stub.enqueue(.init(status: 200, json: commitJSON)) let model = CommitDetailViewModel( client: client, repoPath: "krz/gitbay", sha: "59ae144") await model.load() let commit = try #require(model.state.value) #expect(commit.subject == "control: profile aggregate") #expect(commit.signature.state == .verified) #expect(commit.checks?.first?.state == "success") #expect(commit.shortSHA == "59ae144000") // The patch is parsed by the same parser mr diff uses. let diff = try #require(model.diff) #expect(diff.files.map(\.displayPath) == ["main.go"]) #expect(diff.additions == 1) #expect(stub.seen.first?.url.query() == "argv=repo&argv=commit&argv=krz/gitbay&argv=59ae144") } @Test func anUnknownShaIsAnEmptyState() async throws { let (client, stub) = try makeClient() stub.enqueue(.init(status: 404, json: #"{"protocol_version":1,"error":"no commit \"deadbeef\" in krz/gitbay","exit_code":3}"#)) let model = CommitDetailViewModel( client: client, repoPath: "krz/gitbay", sha: "deadbeef") await model.load() guard case .empty = model.state else { Issue.record("expected .empty, got \(model.state)") return } } }