import Foundation
import Testing
@testable import IrdAPI
let reg1 = #"{"id":"r1","domain":"a.ro","state":"active","registrant_email_hint":null,"created_at":"2026-01-01T00:00:00Z"}"#
@Suite struct PaginationFlowTests {
@Test func registrationsListAllFollowsTheCursor() async throws {
let (client, http) = captureClient(script: [
json(200, #"{"registrations":[],"next_cursor":"c2"}"#),
json(200, #"{"registrations":[],"next_cursor":null}"#),
])
let all = try await client.registrations.listAll(query: RegistrationListQuery(limit: 50))
#expect(all.isEmpty)
#expect(http.trail == ["GET /domains/registrations?limit=50", "GET /domains/registrations?cursor=c2&limit=50"])
}
@Test func integerCursorsWork() async throws {
let (client, http) = captureClient(script: [
json(200, #"{"entries":[],"next_cursor":41}"#),
json(200, #"{"entries":[],"next_cursor":null}"#),
])
_ = try await client.credits.ledgerAll(query: LedgerQuery(kind: .charge))
#expect(http.trail == ["GET /credits/ledger?kind=charge", "GET /credits/ledger?cursor=41&kind=charge"])
let (c2, h2) = captureClient(script: [json(200, #"{"actions":[],"next_cursor":null}"#)])
_ = try await c2.registrations.listAllActions()
#expect(h2.trail == ["GET /domains/actions"])
}
@Test func directoryAndThreadsUseStringCursors() async throws {
let (client, http) = captureClient(script: [json(200, #"{"entries":[],"next_cursor":null}"#), json(200, #"{"threads":[],"next_cursor":null,"unread_count":0}"#)])
_ = try await client.domains.directoryAll(query: DirectoryQuery(category: .eCommerce, includeTotal: true))
_ = try await client.support.listAllThreads()
#expect(http.trail == ["GET /domains/directory?category=e-commerce&include_total=true", "GET /contact/threads"])
}
@Test func pinsListAllWalksOffsets() async throws {
let (client, http) = captureClient(script: [
json(200, #"{"pins":[{"id":"p1","cid":null,"cid_v1":null,"name":null,"state":"pinned","size_bytes":null,"gateway_url":null,"meta":{},"created_at":null,"updated_at":null,"last_error":null}],"total":1}"#),
])
let pins = try await client.ipfs.listAllPins(query: ListPinsQuery(status: "pinned"))
#expect(pins.map(\.id) == ["p1"])
#expect(http.trail == ["GET /ipfs/pins?status=pinned&limit=100&offset=0"])
let (c2, h2) = captureClient(script: [json(200, #"{"pins":[],"total":0,"limit":2,"offset":0}"#)])
_ = try await c2.ipfs.listAllDomainPins(id: "d1", query: ListDomainPinsQuery(limit: 2))
#expect(h2.trail == ["GET /ipfs/domains/d1/pins?limit=2&offset=0"])
}
}