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 bookmarksJSON = """ {"protocol_version":1,"data":[\ {"path":"krz/gitbay","description":"A CLI-first git forge.","visibility":"public","bookmarks":3},\ {"path":"cmc/notes","description":"","visibility":"private","bookmarks":1}\ ],"exit_code":0} """ @MainActor struct BookmarkListViewModelTests { @Test func listsBookmarkedRepositories() async throws { let (client, stub) = try makeClient() stub.enqueue(.init(status: 200, json: bookmarksJSON)) let model = BookmarkListViewModel(client: client) await model.load() let repos = try #require(model.state.value) #expect(repos.map(\.path) == ["krz/gitbay", "cmc/notes"]) #expect(repos[1].isPrivate) #expect(stub.seen.first?.url.query() == "argv=repo&argv=bookmarks") } @Test func noBookmarksIsAnEmptyStateNotAFailure() async throws { let (client, stub) = try makeClient() stub.enqueue(.init(status: 200, json: #"{"protocol_version":1,"data":[],"exit_code":0}"#)) let model = BookmarkListViewModel(client: client) await model.load() guard case .empty = model.state else { Issue.record("expected an empty state, got \(model.state)") return } } }