krz/hutch

an ios client for sourcehut

clone: git clone https://gitbay.org/krz/hutch.git

v3.1.5: HutchTests/SRHTClientTests.swift · raw

 1import Foundation
 2import Testing
 3@testable import Hutch
 4
 5struct SRHTClientTests {
 6
 7    @Test
 8    @MainActor
 9    func fetchTextRejectsUnexpectedAuthenticatedURL() async throws {
10        let client = SRHTClient(token: "test-token")
11        let url = try #require(URL(string: "https://example.com/build-log"))
12
13        do {
14            _ = try await client.fetchText(url: url)
15            Issue.record("Expected fetchText(url:) to reject non-sr.ht URLs.")
16        } catch let error as SRHTError {
17            guard case .invalidAuthenticatedURL(let rejectedURL) = error else {
18                Issue.record("Expected invalidAuthenticatedURL error, got \(error).")
19                return
20            }
21
22            #expect(rejectedURL == url)
23        } catch {
24            Issue.record("Expected SRHTError.invalidAuthenticatedURL, got \(error).")
25        }
26    }
27
28    @Test
29    func graphQLErrorUserFacingMessagePreservesValidationDetails() {
30        let error = SRHTError.graphQLErrors([
31            GraphQLError(message: "A tracker named bugs already exists", locations: nil)
32        ])
33
34        #expect(error.userFacingMessage == "A tracker named bugs already exists")
35    }
36
37    @Test
38    func graphQLErrorUserFacingMessageClassifiesNotFoundResponses() {
39        let error = SRHTError.graphQLErrors([
40            GraphQLError(message: "reference not found", locations: nil)
41        ])
42
43        #expect(error.userFacingMessage == "That content is no longer available.")
44        #expect(error.matchesGraphQLErrorClassification(.missingReference))
45    }
46
47    @Test
48    func graphQLErrorUserFacingMessageClassifiesServiceProvisioningFailures() {
49        let error = SRHTError.graphQLErrors([
50            GraphQLError(message: "No such repository or user found", locations: nil)
51        ])
52
53        #expect(error.userFacingMessage == "That account needs to activate this SourceHut service before this action can succeed.")
54        #expect(error.matchesGraphQLErrorClassification(.serviceNotProvisioned))
55    }
56}