import Foundation
import Testing
@testable import IrdAPI
@Suite struct CredentialTests {
@Test func inMemoryStoreSavesLoadsAndDeletes() async throws {
let store = InMemoryCredentialStore()
#expect(try await store.token() == nil)
try store.save("ird_a")
#expect(try await store.token() == "ird_a")
try store.delete()
#expect(try await store.token() == nil)
}
@Test func aTokenSavedAfterConstructionIsUsedByTheNextRequest() async throws {
let store = InMemoryCredentialStore()
let http = FakeHTTP(script: [json(200, "{}")])
var c = Configuration()
c.credential = store
c.transport = http
let client = IrdClient(configuration: c)
_ = try await client.raw.request(method: .get, path: "/domains/example")
#expect(http.last?.value(forHTTPHeaderField: "Authorization") == nil)
try store.save("eyJ.jwt")
_ = try await client.raw.request(method: .get, path: "/domains/example")
#expect(http.last?.value(forHTTPHeaderField: "Authorization") == "Bearer eyJ.jwt")
}
@Test func convenienceInitializersWire() async throws {
#expect(try await IrdClient(apiKey: "ird_x").configuration.credential.token() == "ird_x")
#expect(try await IrdClient(credential: StaticCredential("t")).configuration.credential.token() == "t")
#expect(try await IrdClient().configuration.credential.token() == nil)
}
}