gitbayTests/ExploreRefLogTests.swift
96 lines · 3455 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 logJSON = """
16 {"protocol_version":1,"data":[\
17 {"sha":"aaaaaaaaaabbbbbbbbbb","subject":"only on side","author_name":"cmc",\
18 "author_email":"c@x.test","date":"2026-08-28T02:00:00Z",\
19 "signature":{"state":"unsigned"}}],"exit_code":0}
20 """
21
22@MainActor
23struct LogAtRefTests {
24
25 @Test func noRefReadsTheDefaultBranch() async throws {
26 let (client, stub) = try makeClient()
27 stub.enqueue(.init(status: 200, json: logJSON))
28 let model = LogViewModel(client: client, repoPath: "krz/gitbay")
29
30 await model.load()
31
32 #expect(stub.seen.first?.url.query() == "argv=repo&argv=log&argv=krz/gitbay")
33 }
34
35 @Test func aRefIsForwardedToTheCommand() async throws {
36 let (client, stub) = try makeClient()
37 stub.enqueue(.init(status: 200, json: logJSON))
38 let model = LogViewModel(client: client, repoPath: "krz/gitbay", ref: "side")
39
40 await model.load()
41
42 #expect(model.state.value?.first?.subject == "only on side")
43 #expect(stub.seen.first?.url.query() ==
44 "argv=repo&argv=log&argv=krz/gitbay&argv=--ref&argv=side")
45 }
46
47 @Test func anUnknownRefIsAnEmptyState() async throws {
48 let (client, stub) = try makeClient()
49 stub.enqueue(.init(status: 404, json:
50 #"{"protocol_version":1,"error":"no ref \"nope\" in krz/gitbay","exit_code":3}"#))
51 let model = LogViewModel(client: client, repoPath: "krz/gitbay", ref: "nope")
52
53 await model.load()
54
55 guard case .empty = model.state else {
56 Issue.record("expected .empty, got \(model.state)")
57 return
58 }
59 }
60}
61
62@MainActor
63struct ExploreTests {
64
65 @Test func exploreListsPublicReposAndPages() async throws {
66 let (client, stub) = try makeClient()
67 stub.enqueue(.init(status: 200, json: """
68 {"protocol_version":1,"data":{"items":[\
69 {"path":"krz/gitbay","description":"a forge","topics":["cli","forge"]},\
70 {"path":"krz/old","archived":true}],"next":"ZXhwbG9yZTpr"},"exit_code":0}
71 """))
72 stub.enqueue(.init(status: 200, json: """
73 {"protocol_version":1,"data":{"items":[{"path":"krz/space-wiki"}]},"exit_code":0}
74 """))
75 let list = PagedListModel<PublicRepo>(
76 client: client, argv: ["explore"], emptyMessage: "none")
77
78 await list.reload()
79 #expect(list.state.value?.map(\.path) == ["krz/gitbay", "krz/old"])
80 #expect(list.state.value?[0].topics == ["cli", "forge"])
81 #expect(list.state.value?[1].isArchived == true)
82 #expect(list.hasMore)
83
84 await list.loadMore()
85 #expect(list.state.value?.count == 3)
86 #expect(!list.hasMore)
87 #expect(stub.seen[1].url.query()?.contains("argv=--cursor&argv=ZXhwbG9yZTpr") == true)
88 }
89
90 @Test func ownerAndNameSplitForDisplay() {
91 let repo = PublicRepo(path: "audit-labs/audit-tools",
92 description: nil, archived: nil, topics: nil)
93 #expect(repo.owner == "audit-labs")
94 #expect(repo.name == "audit-tools")
95 }
96}