import Foundation
import Testing
@testable import IrdAPI
/// Executable full-coverage inventory: every generated method is invoked against a
/// recording transport answering the synthesised success body, and must issue exactly
/// one request with the exact method and URL. Decoding through the typed model proves
/// every response type reads what the spec describes.
@Suite struct CoverageTests {
static let fixtures: [String: JSONValue] = {
let url = Bundle.module.url(forResource: "synth", withExtension: "json", subdirectory: "Fixtures")!
return try! JSONValue.parse(try! Data(contentsOf: url)).objectValue!
}()
@Test func theInventoryCoversEveryOperation() {
#expect(GeneratedCoverage.all.count == 161)
#expect(Set(GeneratedCoverage.all.map(\.key)).count == 161)
#expect(Set(GeneratedCoverage.all.map(\.key)) == Spec.operations)
}
@Test(arguments: GeneratedCoverage.all.map(\.key))
func everyOperationHitsTheExactMethodAndURL(key: String) async throws {
let c = try #require(GeneratedCoverage.all.first { $0.key == key })
let reply = Self.fixtures[key]?["response"]?["full"]
let body = try reply.map { try $0.serialized() } ?? Data()
let (client, http) = captureClient(script: [HTTPResponse(status: c.status, body: body)])
try await c.call(client)
#expect(http.count == 1, "exactly one request")
let r = try #require(http.last)
#expect(r.httpMethod == c.method)
#expect(r.url?.absoluteString == c.url)
#expect(r.value(forHTTPHeaderField: "Authorization") == "Bearer ird_k" || key.hasPrefix("POST /auth/device/"))
#expect((r.httpBody != nil) == c.hasBody, "body presence")
if c.hasBody { #expect(r.value(forHTTPHeaderField: "Content-Type") == "application/json") }
}
}