import Foundation
import Testing
@testable import IrdAPI
/// Pins the generated operation table to the vendored spec.
@Suite struct OperationTableTests {
@Test func everySpecOperationIsGeneratedAndNothingElse() {
let declared = Spec.operations
let generated = Set(IrdOperation.all.map(\.key))
#expect(declared.count == 161, "spec operation count (update README and site when this moves)")
#expect(generated.count == IrdOperation.all.count, "duplicate operations")
#expect(generated == declared, "\n in spec but not generated: \(declared.subtracting(generated).sorted())\n generated but not in spec: \(generated.subtracting(declared).sorted())")
}
@Test func moduleAndNamePairsAreUnique() {
let pairs = IrdOperation.all.map { "\($0.module).\($0.name)" }
#expect(Set(pairs).count == pairs.count)
#expect(Set(IrdOperation.all.map(\.module)).count == 14)
}
@Test func securityMatchesTheSpec() throws {
for op in IrdOperation.all {
let spec = try #require(Spec.operation(op.key))
let schemes = (spec["security"]?.arrayValue ?? []).map { Set(($0.objectValue ?? [:]).keys) }
let expected: IrdSecurity
switch schemes.sorted(by: { $0.count < $1.count }) {
case [[]]: expected = .anonymous
case [[], ["apiKey"]]: expected = .anonymousOrKey
case [["accountJwt"]]: expected = .jwtOnly
case [["apiKey"], ["accountJwt"]], [["accountJwt"], ["apiKey"]]: expected = .keyOrJwt
default: Issue.record("unexpected security for \(op.key): \(schemes)"); continue
}
#expect(op.security == expected, Comment(rawValue: op.key))
}
}
}