gitbayTests/RepoTransferDeleteTests.swift

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

72 lines · 3164 bytes

8 symbols in this file
 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 func posted(_ stub: StubProtocol.Box) throws -> [[String]] {
16    try stub.seen.filter { $0.method == "POST" }.map { seen in
17        let body = try #require(try JSONSerialization.jsonObject(with: seen.body) as? [String: Any])
18        return try #require(body["argv"] as? [String])
19    }
20}
21
22@MainActor
23struct RepoTransferDeleteTests {
24
25    @Test func transferNamesTheNewOwnerAndReturnsTheNewPath() async throws {
26        let (client, stub) = try makeClient()
27        stub.enqueue(.init(status: 200, json: #"{"protocol_version":1,"data":{"repo":"krz/app","was":"cmc/app"},"exit_code":0}"#, match: "cmd"))
28        let model = RepoSettingsViewModel(client: client, repoPath: "cmc/app")
29
30        let moved = await model.transfer(to: " krz ")
31
32        #expect(moved == "krz/app")
33        #expect(try posted(stub) == [["repo", "transfer", "cmc/app", "krz"]])
34    }
35
36    @Test func aRefusedTransferSaysWhy() async throws {
37        let (client, stub) = try makeClient()
38        stub.enqueue(.init(status: 403, json: #"{"protocol_version":1,"error":"you cannot create repositories in krz","exit_code":4}"#, match: "cmd"))
39        let model = RepoSettingsViewModel(client: client, repoPath: "cmc/app")
40
41        #expect(await model.transfer(to: "krz") == nil)
42        #expect(model.actionError == "you cannot create repositories in krz")
43    }
44
45    @Test func deleteSendsYesAndReportsIt() async throws {
46        let (client, stub) = try makeClient()
47        stub.enqueue(.init(status: 200, json: #"{"protocol_version":1,"data":{"deleted":"cmc/app"},"exit_code":0}"#, match: "cmd"))
48        let model = RepoSettingsViewModel(client: client, repoPath: "cmc/app")
49
50        #expect(await model.delete())
51        #expect(try posted(stub) == [["repo", "delete", "cmc/app", "--yes"]])
52    }
53
54    @Test func aRefusedDeleteSaysWhy() async throws {
55        let (client, stub) = try makeClient()
56        stub.enqueue(.init(status: 403, json: #"{"protocol_version":1,"error":"only the owner or an admin can delete cmc/app","exit_code":4}"#, match: "cmd"))
57        let model = RepoSettingsViewModel(client: client, repoPath: "cmc/app")
58
59        #expect(!(await model.delete()))
60        #expect(model.actionError == "only the owner or an admin can delete cmc/app")
61    }
62
63    @Test func aDeleteWhoseRetryFindsNothingIsDone() async throws {
64        let (client, stub) = try makeClient()
65        stub.enqueue(.init(status: 500, json: #"{"protocol_version":1,"error":"removing the directory: busy","exit_code":1}"#, match: "cmd"))
66        stub.enqueue(.init(status: 404, json: #"{"protocol_version":1,"error":"no repository cmc/app","exit_code":3}"#, match: "cmd"))
67        let model = RepoSettingsViewModel(client: client, repoPath: "cmc/app")
68
69        #expect(await model.delete())
70        #expect(model.actionError == nil)
71    }
72}