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