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 func argvOf(_ seen: StubProtocol.Seen) throws -> ([String], String?) { let body = try #require(try JSONSerialization.jsonObject(with: seen.body) as? [String: Any]) return (try #require(body["argv"] as? [String]), body["stdin"] as? String) } private let keysJSON = """ {"protocol_version":1,"data":[\ {"fingerprint":"SHA256:15jrWGl3s3BB1CeG0z9TfGnyf35l8lcBWoYfA0+IJbY","algo":"ssh-ed25519","scope":"full"}\ ],"exit_code":0} """ private let pgpJSON = """ {"protocol_version":1,"data":[\ {"fingerprint":"3917973fb159bbb86194538569451a517ac0cb37",\ "emails":"[\\"hello@cleberg.net\\"]"}],"exit_code":0} """ private let okJSON = #"{"protocol_version":1,"data":{},"exit_code":0}"# private let orgListJSON = """ {"protocol_version":1,"data":[{"org":"krz","role":"admin"}],"exit_code":0} """ @MainActor struct AccountViewModelTests { private func loadedModel() async throws -> (AccountViewModel, StubProtocol.Box) { let (client, stub) = try makeClient() stub.enqueue(.init(status: 200, json: keysJSON, match: "argv=keys")) stub.enqueue(.init(status: 200, json: pgpJSON, match: "argv=pgp")) stub.enqueue(.init(status: 200, json: orgListJSON, match: "argv=org")) let model = AccountViewModel(client: client) await model.load() return (model, stub) } @Test func loadsBothKeyListsAndDecodesNestedEmails() async throws { let (model, _) = try await loadedModel() let loaded = try #require(model.state.value) #expect(loaded.sshKeys.first?.algo == "ssh-ed25519") // The UID list is JSON-encoded inside the JSON field. #expect(loaded.pgpKeys.first?.emailList == ["hello@cleberg.net"]) } @Test func sshKeyTravelsAsRawStdinWithScope() async throws { let (model, stub) = try await loadedModel() stub.enqueue(.init(status: 200, json: okJSON, match: "cmd")) stub.enqueue(.init(status: 200, json: keysJSON, match: "argv=keys")) stub.enqueue(.init(status: 200, json: pgpJSON, match: "argv=pgp")) stub.enqueue(.init(status: 200, json: orgListJSON, match: "argv=org")) await model.addSSHKey("ssh-ed25519 AAAAC3Nza phone\n", scope: "git") let (argv, stdin) = try argvOf(try #require(stub.seen.first { $0.method == "POST" })) // No --file - here: keys add reads bare stdin. #expect(argv == ["keys", "add", "--scope", "git"]) #expect(stdin == "ssh-ed25519 AAAAC3Nza phone") } @Test func removalsTargetTheFingerprint() async throws { let (model, stub) = try await loadedModel() for _ in 0..<2 { stub.enqueue(.init(status: 200, json: okJSON, match: "cmd")) stub.enqueue(.init(status: 200, json: keysJSON, match: "argv=keys")) stub.enqueue(.init(status: 200, json: pgpJSON, match: "argv=pgp")) stub.enqueue(.init(status: 200, json: orgListJSON, match: "argv=org")) } let loaded = try #require(model.state.value) await model.removeSSHKey(loaded.sshKeys[0]) await model.removePGPKey(loaded.pgpKeys[0]) let writes = try stub.seen.filter { $0.method == "POST" }.map { try argvOf($0).0 } #expect(writes[0] == ["keys", "remove", "SHA256:15jrWGl3s3BB1CeG0z9TfGnyf35l8lcBWoYfA0+IJbY"]) #expect(writes[1] == ["pgp", "remove", "3917973fb159bbb86194538569451a517ac0cb37"]) } @Test func emailAddAndVerifySendTheirCommands() async throws { let (model, stub) = try await loadedModel() for _ in 0..<2 { stub.enqueue(.init(status: 200, json: okJSON, match: "cmd")) stub.enqueue(.init(status: 200, json: keysJSON, match: "argv=keys")) stub.enqueue(.init(status: 200, json: pgpJSON, match: "argv=pgp")) stub.enqueue(.init(status: 200, json: orgListJSON, match: "argv=org")) } await model.addEmail(" claude@cleberg.net ") #expect(model.notice?.contains("on its way") == true) await model.verifyEmail(code: "123456") #expect(model.notice == "Email verified.") let writes = try stub.seen.filter { $0.method == "POST" }.map { try argvOf($0).0 } #expect(writes[0] == ["email", "add", "claude@cleberg.net"]) #expect(writes[1] == ["email", "verify", "123456"]) } @Test func contentValidationUsageErrorsSurfaceVerbatimHere() async throws { let (model, stub) = try await loadedModel() stub.enqueue(.init(status: 400, json: #"{"protocol_version":1,"error":"not a valid public key in authorized_keys format: illegal base64","exit_code":2}"#, match: "cmd")) await model.addSSHKey("garbage", scope: "full") // Exit 2 stays generic app-wide; on this screen it validates // pasted content and the message is for the person. #expect(model.actionError == "not a valid public key in authorized_keys format: illegal base64") } }