Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128//// ATRepoServiceTests.swift// CoreATProtocol//
import Foundationimport Testingimport NetworkingKit@testable import CoreATProtocol
private struct StubRecordValue: Decodable, Sendable, Equatable { let type: String let note: String let amount: Int
enum CodingKeys: String, CodingKey { case type = "$type" case note, amount }}
@Suite("ATRepoService models")struct ATRepoServiceModelTests {
@Test("ATRecord decodes a getRecord response with a caller-supplied value type") func decodeRecord() throws { let json = """ { "uri": "at://did:plc:abc123/com.example.test/self", "cid": "bafyreib2rxk3rh6kzwq", "value": { "$type": "com.example.test", "note": "hello", "amount": 21 } } """ let record = try JSONDecoder.atDecoder.decode(ATRecord<StubRecordValue>.self, from: Data(json.utf8))
#expect(record.uri == "at://did:plc:abc123/com.example.test/self") #expect(record.cid == "bafyreib2rxk3rh6kzwq") #expect(record.value == StubRecordValue(type: "com.example.test", note: "hello", amount: 21)) }
@Test("ATRecord decodes when cid is absent") func decodeRecordWithoutCID() throws { let json = """ { "uri": "at://did:plc:abc123/com.example.test/self", "value": { "$type": "com.example.test", "note": "x", "amount": 1 } } """ let record = try JSONDecoder.atDecoder.decode(ATRecord<StubRecordValue>.self, from: Data(json.utf8)) #expect(record.cid == nil) }
@Test("ATRecordList decodes records and cursor") func decodeRecordList() throws { let json = """ { "records": [ { "uri": "at://did:plc:abc123/com.example.test/1", "cid": "bafyone", "value": { "$type": "com.example.test", "note": "first", "amount": 1 } }, { "uri": "at://did:plc:abc123/com.example.test/2", "cid": "bafytwo", "value": { "$type": "com.example.test", "note": "second", "amount": 2 } } ], "cursor": "next-page" } """ let list = try JSONDecoder.atDecoder.decode(ATRecordList<StubRecordValue>.self, from: Data(json.utf8))
#expect(list.records.count == 2) #expect(list.records[1].value.note == "second") #expect(list.cursor == "next-page") }
@Test("ATPutRecordResponse decodes with and without validationStatus") func decodePutRecordResponse() throws { let withStatus = """ { "uri": "at://did:plc:abc/com.example/self", "cid": "bafy", "validationStatus": "unknown" } """ let withoutStatus = """ { "uri": "at://did:plc:abc/com.example/self", "cid": "bafy" } """
let first = try JSONDecoder.atDecoder.decode(ATPutRecordResponse.self, from: Data(withStatus.utf8)) let second = try JSONDecoder.atDecoder.decode(ATPutRecordResponse.self, from: Data(withoutStatus.utf8))
#expect(first.validationStatus == "unknown") #expect(second.validationStatus == nil) }}
@Suite("AT Protocol error introspection")struct ATProtoErrorNameTests {
@Test("atProtoErrorName extracts the XRPC error name from a NetworkError body") func errorNameFromNetworkError() { let body = Data(#"{"error":"RecordNotFound","message":"Could not locate record"}"#.utf8) let error = NetworkError.statusCode(StatusCode(rawValue: 400), data: body, request: nil)
#expect(error.atProtoErrorName == "RecordNotFound") }
@Test("atProtoErrorName extracts the XRPC error name from an AtError-wrapped NetworkError") func errorNameFromAtError() { let body = Data(#"{"error":"RecordNotFound","message":"Could not locate record"}"#.utf8) let error = AtError.network(.statusCode(StatusCode(rawValue: 400), data: body, request: nil))
#expect(error.atProtoErrorName == "RecordNotFound") }
@Test("atProtoErrorName is nil for non-XRPC errors") func errorNameForUnrelatedError() { #expect(IdentityError.invalidHandle.atProtoErrorName == nil)
let nonJSONBody = Data("plain text".utf8) let error = NetworkError.statusCode(StatusCode(rawValue: 500), data: nonJSONBody, request: nil) #expect(error.atProtoErrorName == nil) }}