import Foundation
import Testing
@testable import IrdAPI
func registryAction(_ state: String) -> HTTPResponse {
json(200, #"{"action":{"id":"a1","seq":1,"type":"renew","state":"\#(state)","registration_id":"r1","charged":"12.00","refunded":false,"error":null,"error_code":null,"created_at":"2026-09-08T10:00:00Z"}}"#)
}
func vpsAction(_ state: String) -> HTTPResponse {
json(200, #"{"action":{"id":"a1","type":"create","state":"\#(state)","instance_id":"i1","error":null,"created_at":"2026-09-08T10:00:00Z"}}"#)
}
@Suite struct ActionsFlowTests {
@Test func registryActionsPollUntilTerminal() async throws {
let (client, http) = captureClient(script: [registryAction("queued"), registryAction("submitted"), registryAction("succeeded")])
let action = try await client.registrations.waitForAction(id: "a1")
#expect(action.state == .succeeded)
#expect(action.charged == "12.00")
#expect(http.trail == ["GET /domains/actions/a1", "GET /domains/actions/a1", "GET /domains/actions/a1"])
}
@Test func failedActionsAreReturnedNotThrown() async throws {
let (client, _) = captureClient(script: [vpsAction("claimed"), vpsAction("failed")])
let action = try await client.vps.waitForAction(id: "a1")
#expect(action.state == .failed)
}
@Test func unknownStatesKeepPolling() async throws {
let (client, http) = captureClient(script: [vpsAction("brand-new-state"), vpsAction("cancelled")])
let action = try await client.vps.waitForAction(id: "a1")
#expect(action.state == .cancelled)
#expect(http.count == 2)
}
}