import Foundation import Testing @testable import PediFoods private func encodedKeys(_ value: T) throws -> Set { let data = try JSONEncoder().encode(value) let object = try JSONSerialization.jsonObject(with: data) as? [String: Any] return Set((object ?? [:]).keys) } @Test("CustomerIdentityUpdatePayload omits profilePicture when it's an empty string") func customerIdentityUpdatePayloadOmitsEmptyProfilePicture() throws { let payload = CustomerIdentityUpdatePayload(name: "Jane", email: nil, phoneNumber: nil, profilePicture: "") let keys = try encodedKeys(payload) #expect(keys.contains("profilePicture") == false) #expect(keys.contains("name")) } @Test("CustomerIdentityUpdatePayload includes profilePicture when it's non-empty") func customerIdentityUpdatePayloadIncludesNonEmptyProfilePicture() throws { let payload = CustomerIdentityUpdatePayload(name: nil, email: nil, phoneNumber: nil, profilePicture: "https://cdn/pic.png") let keys = try encodedKeys(payload) #expect(keys.contains("profilePicture")) } @Test("CustomerIdentityUpdatePayload omits fields that are nil") func customerIdentityUpdatePayloadOmitsNilFields() throws { let payload = CustomerIdentityUpdatePayload(name: "Jane", email: nil, phoneNumber: nil, profilePicture: nil) let keys = try encodedKeys(payload) #expect(keys == ["name"]) } @Test("CustomerAddressPayload maps every field from a CustomerAddress") func customerAddressPayloadMapsFromCustomerAddress() { let address = CustomerAddress( id: "addr-1", label: "Casa", address: "Rua A", number: "100", complement: "Apto 2", neighborhood: "Centro", city: "SP", state: "SP", zipCode: "01000-000", latLong: [-23.5, -46.6], isDefault: true ) let payload = CustomerAddressPayload(from: address) #expect(payload.label == "Casa") #expect(payload.address == "Rua A") #expect(payload.number == "100") #expect(payload.complement == "Apto 2") #expect(payload.neighborhood == "Centro") #expect(payload.city == "SP") #expect(payload.state == "SP") #expect(payload.zipCode == "01000-000") #expect(payload.latLong == [-23.5, -46.6]) } @Test("CustomerAddressPayload encodes latLong under the lat_long snake_case key") func customerAddressPayloadEncodesLatLongSnakeCase() throws { let address = CustomerAddress( id: nil, label: nil, address: nil, number: nil, complement: nil, neighborhood: nil, city: nil, state: nil, zipCode: nil, latLong: [1.0, 2.0], isDefault: nil ) let keys = try encodedKeys(CustomerAddressPayload(from: address)) #expect(keys.contains("lat_long")) #expect(keys.contains("latLong") == false) } @Test("CustomerAttributesUpdatePayload omits attributes when nil") func customerAttributesUpdatePayloadOmitsNilAttributes() throws { let payload = CustomerAttributesUpdatePayload(appVersion: "1.0.0", attributes: nil) let keys = try encodedKeys(payload) #expect(keys == ["appVersion"]) } @Test("CustomerFavoritesMutationResult decodes favorites and an optional store") func customerFavoritesMutationResultDecodes() throws { let json = #"{"favorites":["s1","s2"],"store":{"id":"s1","name":"Loja A"}}"# let result = try JSONDecoder().decode(CustomerFavoritesMutationResult.self, from: Data(json.utf8)) #expect(result.favorites == ["s1", "s2"]) #expect(result.store?.name == "Loja A") }