| @@ -0,0 +1,123 @@ |
| 1 | import Foundation |
| 2 | import Testing |
| 3 | @testable import gitbay |
| 4 | |
| 5 | private 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 | |
| 15 | private func argvOf(_ seen: StubProtocol.Seen) throws -> [String] { |
| 16 | let body = try #require(try JSONSerialization.jsonObject(with: seen.body) as? [String: Any]) |
| 17 | return try #require(body["argv"] as? [String]) |
| 18 | } |
| 19 | |
| 20 | private let ok = """ |
| 21 | {"protocol_version":1,"exit_code":0} |
| 22 | """ |
| 23 | |
| 24 | @MainActor |
| 25 | struct BuildCancelTests { |
| 26 | |
| 27 | /// The wire status is "pending", not "queued" — the refusal message |
| 28 | /// says "queued" but the value never does. |
| 29 | @Test func onlyPendingAndRunningBuildsAreCancellable() { |
| 30 | #expect(BuildDetailViewModel.isCancellable(status: "pending")) |
| 31 | #expect(BuildDetailViewModel.isCancellable(status: "running")) |
| 32 | for status in ["success", "failure", "cancelled", "queued", ""] { |
| 33 | #expect(BuildDetailViewModel.isCancellable(status: status) == false, |
| 34 | "\(status) should not be cancellable") |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | @Test func cancelSendsTheBuildNumber() async throws { |
| 39 | let (client, stub) = try makeClient() |
| 40 | // The view model's own load response, then the write, then a reload. |
| 41 | let model = BuildDetailViewModel(client: client, repoPath: "krz/gitbay", number: 966) |
| 42 | stub.enqueue(.init(status: 200, json: ok)) |
| 43 | stub.enqueue(.init(status: 200, json: ok)) |
| 44 | await model.cancel() |
| 45 | |
| 46 | let write = try #require(stub.seen.first { $0.method == "POST" }) |
| 47 | #expect(try argvOf(write) == ["build", "cancel", "krz/gitbay", "966"]) |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | @MainActor |
| 52 | struct ReleaseDeleteTests { |
| 53 | |
| 54 | /// `--yes` is required by the command, not optional. |
| 55 | @Test func deleteSendsTheConfirmationFlag() async throws { |
| 56 | let (client, stub) = try makeClient() |
| 57 | let model = ReleaseDetailViewModel(client: client, repoPath: "krz/gitbay", tag: "v1.0.0") |
| 58 | stub.enqueue(.init(status: 200, json: ok)) |
| 59 | let deleted = await model.delete() |
| 60 | |
| 61 | #expect(deleted) |
| 62 | let write = try #require(stub.seen.first { $0.method == "POST" }) |
| 63 | #expect(try argvOf(write) == ["release", "delete", "krz/gitbay", "v1.0.0", "--yes"]) |
| 64 | } |
| 65 | |
| 66 | @Test func aRefusedDeleteReturnsFalseAndSurfaces() async throws { |
| 67 | let (client, stub) = try makeClient() |
| 68 | let model = ReleaseDetailViewModel(client: client, repoPath: "krz/gitbay", tag: "v1.0.0") |
| 69 | stub.enqueue(.init(status: 200, json: """ |
| 70 | {"protocol_version":1,"error":"deleting a release needs write access","exit_code":4} |
| 71 | """)) |
| 72 | let deleted = await model.delete() |
| 73 | |
| 74 | #expect(deleted == false) |
| 75 | #expect(model.actionError?.isEmpty == false) |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | @MainActor |
| 80 | struct OrgCreateRenameTests { |
| 81 | |
| 82 | /// Both names are positional, OLD first. Swapping them renames the |
| 83 | /// wrong way with no error at all. |
| 84 | @Test func renameSendsOldThenNew() async throws { |
| 85 | let (client, stub) = try makeClient() |
| 86 | let model = OrgViewModel(client: client, orgName: "krz") |
| 87 | stub.enqueue(.init(status: 200, json: ok)) |
| 88 | stub.enqueue(.init(status: 200, json: ok)) |
| 89 | _ = await model.rename(to: "kerouac") |
| 90 | |
| 91 | let write = try #require(stub.seen.first { $0.method == "POST" }) |
| 92 | #expect(try argvOf(write) == ["org", "rename", "krz", "kerouac"]) |
| 93 | } |
| 94 | |
| 95 | @Test func aBlankRenameSendsNothing() async throws { |
| 96 | let (client, stub) = try makeClient() |
| 97 | let model = OrgViewModel(client: client, orgName: "krz") |
| 98 | let renamed = await model.rename(to: " ") |
| 99 | |
| 100 | #expect(renamed == false) |
| 101 | #expect(stub.seen.filter { $0.method == "POST" }.isEmpty) |
| 102 | } |
| 103 | |
| 104 | @Test func createSendsTheName() async throws { |
| 105 | let (client, stub) = try makeClient() |
| 106 | stub.enqueue(.init(status: 200, json: ok)) |
| 107 | stub.enqueue(.init(status: 200, json: ok)) |
| 108 | let model = ProfileViewModel(client: client, name: "cmc") |
| 109 | _ = await model.createOrg("newco") |
| 110 | |
| 111 | let write = try #require(stub.seen.first { $0.method == "POST" }) |
| 112 | #expect(try argvOf(write) == ["org", "create", "newco"]) |
| 113 | } |
| 114 | |
| 115 | @Test func aBlankCreateSendsNothing() async throws { |
| 116 | let (client, stub) = try makeClient() |
| 117 | let model = ProfileViewModel(client: client, name: "cmc") |
| 118 | let created = await model.createOrg(" ") |
| 119 | |
| 120 | #expect(created == false) |
| 121 | #expect(stub.seen.filter { $0.method == "POST" }.isEmpty) |
| 122 | } |
| 123 | } |