gitbayTests/BookmarkTests.swift

v1.8.0
gitbay-ios/gitbayTests/BookmarkTests.swift history · blame · raw

50 lines · 1630 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 bookmarksJSON = """
16    {"protocol_version":1,"data":[\
17    {"path":"krz/gitbay","description":"A CLI-first git forge.","visibility":"public","bookmarks":3},\
18    {"path":"cmc/notes","description":"","visibility":"private","bookmarks":1}\
19    ],"exit_code":0}
20    """
21
22@MainActor
23struct BookmarkListViewModelTests {
24
25    @Test func listsBookmarkedRepositories() async throws {
26        let (client, stub) = try makeClient()
27        stub.enqueue(.init(status: 200, json: bookmarksJSON))
28        let model = BookmarkListViewModel(client: client)
29
30        await model.load()
31
32        let repos = try #require(model.state.value)
33        #expect(repos.map(\.path) == ["krz/gitbay", "cmc/notes"])
34        #expect(repos[1].isPrivate)
35        #expect(stub.seen.first?.url.query() == "argv=repo&argv=bookmarks")
36    }
37
38    @Test func noBookmarksIsAnEmptyStateNotAFailure() async throws {
39        let (client, stub) = try makeClient()
40        stub.enqueue(.init(status: 200, json: #"{"protocol_version":1,"data":[],"exit_code":0}"#))
41        let model = BookmarkListViewModel(client: client)
42
43        await model.load()
44
45        guard case .empty = model.state else {
46            Issue.record("expected an empty state, got \(model.state)")
47            return
48        }
49    }
50}