This commit is contained in:
Daniel Arantes Loverde
2026-04-16 14:50:07 -03:00
parent 7f7d414e6c
commit 0ebb854213
20 changed files with 871 additions and 216 deletions

View File

@@ -0,0 +1,49 @@
import Foundation
import Testing
@testable import PediFoods
@Test("Customer profile decodes persisted favorite store ids")
func customerProfileDecodesFavoriteStoreIds() throws {
let json = """
{
"error": false,
"result": {
"id": "cust_1",
"name": "Daniel",
"email": "daniel@example.com",
"phoneNumber": "+5511999999999",
"favorites": ["store_a", "store_b"],
"address_book": []
}
}
"""
let envelope = try JSONDecoder().decode(ApiEnvelope<CustomerProfile>.self, from: Data(json.utf8))
#expect(envelope.result?.favorites == ["store_a", "store_b"])
}
@Test("Favorite mutation decodes updated favorites array")
func favoriteMutationDecodesUpdatedFavoritesArray() throws {
let json = """
{
"error": false,
"result": {
"favorites": ["store_a"],
"store": {
"id": "store_a",
"name": "CPS Drinks",
"category": "Doces & Bolos",
"rating": 4.8,
"totalReviews": 12,
"isOpen": true,
"statusLabel": "Aberto"
}
}
}
"""
let envelope = try JSONDecoder().decode(ApiEnvelope<CustomerFavoritesMutationResult>.self, from: Data(json.utf8))
#expect(envelope.result?.favorites == ["store_a"])
#expect(envelope.result?.store?.id == "store_a")
#expect(envelope.result?.store?.name == "CPS Drinks")
}

View File

@@ -0,0 +1,117 @@
import Testing
@testable import PediFoods
@Test("Store catalog normalizer makes IDs non-empty and unique")
func storeCatalogNormalizerMakesIdsUnique() {
let catalog = [
StoreCatalogCategory(
id: "",
name: "Pizzas",
isPizzaCategory: true,
pizzaConfig: StorePizzaConfig(
sizes: [
StorePizzaSize(id: "", name: "Grande", slices: 8, maxFlavors: 2),
StorePizzaSize(id: "", name: "Familia", slices: 12, maxFlavors: 3)
],
doughs: [
StorePizzaDough(id: "massa", name: "Tradicional", active: true),
StorePizzaDough(id: "massa", name: "Fina", active: true)
],
crusts: [
StorePizzaCrust(id: "", name: "Cheddar", active: true, priceModifier: 5),
StorePizzaCrust(id: "", name: "Catupiry", active: true, priceModifier: 6)
]
),
products: [
StoreCatalogProduct(
id: "",
type: "pizza",
name: "Calabresa",
description: nil,
image: nil,
price: 10,
originalPrice: nil,
pizzaPrices: [:],
addonGroups: [
StoreAddonGroup(
id: "",
name: "Extras",
minSelectors: nil,
maxSelectors: nil,
items: [
StoreAddonItem(id: "", name: "Bacon", price: 2),
StoreAddonItem(id: "", name: "Bacon em dobro", price: 4)
]
),
StoreAddonGroup(
id: "",
name: "Molhos",
minSelectors: nil,
maxSelectors: nil,
items: [
StoreAddonItem(id: "", name: "Alho", price: 1)
]
)
]
),
StoreCatalogProduct(
id: "",
type: "pizza",
name: "Mussarela",
description: nil,
image: nil,
price: 12,
originalPrice: nil,
pizzaPrices: [:],
addonGroups: []
)
]
),
StoreCatalogCategory(
id: "",
name: "Bebidas",
isPizzaCategory: false,
pizzaConfig: nil,
products: [
StoreCatalogProduct(
id: "",
type: nil,
name: "Refrigerante",
description: nil,
image: nil,
price: 7,
originalPrice: nil,
pizzaPrices: [:],
addonGroups: []
)
]
)
]
let normalized = StoreCatalogNormalizer.sanitize(categories: catalog, storeId: "store-1")
let categoryIds = normalized.map(\.id)
#expect(Set(categoryIds).count == categoryIds.count)
#expect(categoryIds.allSatisfy { $0.isEmpty == false })
let firstCategory = normalized[0]
let productIds = firstCategory.products.map(\.id)
#expect(Set(productIds).count == productIds.count)
#expect(productIds.allSatisfy { $0.isEmpty == false })
let addonGroupIds = firstCategory.products[0].addonGroups.map(\.id)
#expect(Set(addonGroupIds).count == addonGroupIds.count)
#expect(addonGroupIds.allSatisfy { $0.isEmpty == false })
let addonItemIds = firstCategory.products[0].addonGroups.flatMap(\.items).map(\.id)
#expect(Set(addonItemIds).count == addonItemIds.count)
#expect(addonItemIds.allSatisfy { $0.isEmpty == false })
let sizeIds = firstCategory.pizzaConfig?.sizes.map(\.id) ?? []
let doughIds = firstCategory.pizzaConfig?.doughs.map(\.id) ?? []
let crustIds = firstCategory.pizzaConfig?.crusts.map(\.id) ?? []
#expect(Set(sizeIds).count == sizeIds.count)
#expect(Set(doughIds).count == doughIds.count)
#expect(Set(crustIds).count == crustIds.count)
}