aboutsummaryrefslogtreecommitdiff
path: root/HutchTests/SRHTClientTests.swift
blob: 12d6d0f3be443da6ebed421f703c448855135480 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import Foundation
import Testing
@testable import Hutch

struct SRHTClientTests {

    @Test
    @MainActor
    func fetchTextRejectsUnexpectedAuthenticatedURL() async throws {
        let client = SRHTClient(token: "test-token")
        let url = try #require(URL(string: "https://example.com/build-log"))

        do {
            _ = try await client.fetchText(url: url)
            Issue.record("Expected fetchText(url:) to reject non-sr.ht URLs.")
        } catch let error as SRHTError {
            guard case .invalidAuthenticatedURL(let rejectedURL) = error else {
                Issue.record("Expected invalidAuthenticatedURL error, got \(error).")
                return
            }

            #expect(rejectedURL == url)
        } catch {
            Issue.record("Expected SRHTError.invalidAuthenticatedURL, got \(error).")
        }
    }

    @Test
    func graphQLErrorUserFacingMessagePreservesValidationDetails() {
        let error = SRHTError.graphQLErrors([
            GraphQLError(message: "A tracker named bugs already exists", locations: nil)
        ])

        #expect(error.userFacingMessage == "A tracker named bugs already exists")
    }

    @Test
    func graphQLErrorUserFacingMessageClassifiesNotFoundResponses() {
        let error = SRHTError.graphQLErrors([
            GraphQLError(message: "reference not found", locations: nil)
        ])

        #expect(error.userFacingMessage == "That content is no longer available.")
        #expect(error.matchesGraphQLErrorClassification(.missingReference))
    }

    @Test
    func graphQLErrorUserFacingMessageClassifiesServiceProvisioningFailures() {
        let error = SRHTError.graphQLErrors([
            GraphQLError(message: "No such repository or user found", locations: nil)
        ])

        #expect(error.userFacingMessage == "That account needs to activate this SourceHut service before this action can succeed.")
        #expect(error.matchesGraphQLErrorClassification(.serviceNotProvisioned))
    }
}