import Foundation
import Testing
@testable import IrdAPI
@Suite struct ErrorsTests {
@Test func helperFlagsReadStatusAndCode() {
#expect(IrdError.api(status: 401, code: "unauthorized", message: "m", body: nil).isAuth)
#expect(IrdError.api(status: 402, code: nil, message: "m", body: nil).isInsufficientCredits)
#expect(IrdError.api(status: 403, code: "totp-required", message: "m", body: nil).needsTOTP)
#expect(IrdError.api(status: 403, code: "invalid-totp", message: "m", body: nil).needsTOTP)
#expect(IrdError.api(status: 403, code: "sensitive-domain-api-key-blocked", message: "m", body: nil).isAPIKeyBlocked)
#expect(IrdError.api(status: 404, code: nil, message: "m", body: nil).isNotFound)
#expect(IrdError.api(status: 429, code: nil, message: "m", body: nil).isRateLimited)
#expect(!IrdError.transport("x").isAuth)
#expect(IrdError.transport("x").status == nil)
}
@Test func descriptionsAreHumanReadable() {
#expect(IrdError.api(status: 402, code: "insufficient_credits", message: "Not enough credits", body: nil).description
== "Not enough credits (insufficient_credits, HTTP 402)")
#expect(IrdError.api(status: 503, code: nil, message: "down", body: nil).description == "down (HTTP 503)")
#expect(IrdError.domainDenied(domain: "x.ro", pattern: "*.ro", op: "POST /tunnels").description
== "Domain gate: x.ro may not be touched by POST /tunnels")
#expect(IrdError.domainDenied(domain: nil, pattern: nil, op: "DELETE /tunnels/{id}").description
== "Domain gate: (unknown domain) may not be touched by DELETE /tunnels/{id}")
#expect(IrdError.timeout(message: "Timed out", last: nil).description == "Timed out")
}
}