Repository Public
1//2// Fabri-Ka iOS v23// File: FabriKaTests/DecodingTests.swift4// Description: décodage de chaque réponse réelle du contrat (fiche, boutique, recherche, facettes, suggestions,5// catégories, annuaire, régions, découverte, hasard, règle « public », sources, inscription, lot).6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9//1011import XCTest12@testable import FabriKa1314final class DecodingTests: XCTestCase {15 func testVersion() throws {16 let v: ServiceInfo = try Fixture.decode("version")17 XCTAssertEqual(v.api, 2)18 XCTAssertTrue(v.ready)19 XCTAssertEqual(v.rules.count, 8)20 XCTAssertGreaterThan(v.products ?? 0, 300_000)21 XCTAssertGreaterThan(v.stores ?? 0, 1_000)22 }2324 func testProductDetail() throws {25 let d: ProductDetail = try Fixture.decode("product")26 XCTAssertEqual(d.card.uid, "cafd5ce66af3bec2d78e")27 XCTAssertEqual(d.card.price, 79)28 XCTAssertNil(d.card.priceMax)29 XCTAssertEqual(d.card.availability, .inStock)30 XCTAssertEqual(d.card.storeId, "lesptitsmosus.com")31 XCTAssertNil(d.card.place, "lieu inconnu : nil plutôt qu'une ville devinée")32 XCTAssertEqual(d.images.count, 10)33 XCTAssertEqual(d.variants.count, 8)34 XCTAssertEqual(d.shownOptions.map(\.name), ["Taille", "Cordons"])35 XCTAssertFalse(d.paragraphs.isEmpty)36 XCTAssertEqual(d.sameStore.count, 8)37 XCTAssertEqual(d.similar.count, 8)38 XCTAssertTrue(d.isPublic)39 XCTAssertNotNil(d.lastSeen)40 let store = try XCTUnwrap(d.store)41 XCTAssertEqual(store.origin, "revendeur")42 XCTAssertTrue(store.isReseller)43 XCTAssertEqual(store.images.count, 3, "révision 3 : trois photos dans le résumé de boutique")44 XCTAssertEqual(store.eyebrow, "Revendeur du Québec")45 XCTAssertEqual(d.card.categoryIcon, "ic-vetements")46 }4748 func testUnlistedProductAcceptsMissingPhotoAndPrice() throws {49 let json = #"{"uid":"abc","title":"Sans photo","price":null,"img":null,"images":[],"public":false,"reason":"sans_photo","reason_label":"aucune photo","store_id":"x.ca","store_name":"X","similar":[]}"#50 let d = try JSONDecoder().decode(ProductDetail.self, from: Data(json.utf8))51 XCTAssertFalse(d.isPublic)52 XCTAssertNil(d.card.price)53 XCTAssertTrue(d.images.isEmpty)54 XCTAssertEqual(d.reasonLabel, "aucune photo")55 XCTAssertEqual(d.card.priceText, "Prix sur demande")56 XCTAssertEqual(d.card.availability, .unknown)57 }5859 func testStoreDetail() throws {60 let d: StoreDetail = try Fixture.decode("boutique")61 XCTAssertEqual(d.store.id, "lesptitsmosus.com")62 XCTAssertTrue(d.catalogue)63 XCTAssertEqual(d.store.n, 3780)64 XCTAssertEqual(d.stats?.products, 3780)65 XCTAssertEqual(d.stats?.priceMin, 0.75)66 XCTAssertEqual(d.stats?.priceMax, 2695)67 XCTAssertEqual(d.contact.email, "bonjour@lesptitsmosus.com")68 XCTAssertEqual(d.website, "https://lesptitsmosus.com")69 XCTAssertEqual(d.categories.first?.label, "Mode & accessoires")70 XCTAssertEqual(d.products?.total, 3780)71 XCTAssertFalse(d.products?.products.isEmpty ?? true)72 XCTAssertEqual(d.facets?.categories.count, 21)73 XCTAssertFalse(d.excluded.isEmpty)74 XCTAssertEqual(d.similar.count, 8)75 XCTAssertNil(d.store.coordinate)76 XCTAssertFalse(d.placeVerified)77 XCTAssertEqual(d.freshness?.status, "ok")78 }7980 func testDirectoryOnlyStore() throws {81 let json = #"{"catalogue":false,"public":false,"id":"ferme.ca","name":"Ferme","url":"https://ferme.ca","region":"Estrie","city":null,"lat":null,"lng":null,"note":"Répertoriée, sans catalogue suivi."}"#82 let d = try JSONDecoder().decode(StoreDetail.self, from: Data(json.utf8))83 XCTAssertFalse(d.catalogue)84 XCTAssertEqual(d.website, "https://ferme.ca")85 XCTAssertEqual(d.store.eyebrow, "Boutique · Estrie")86 }8788 func testSearchText() throws {89 let r: SearchPage = try Fixture.decode("search_texte")90 XCTAssertEqual(r.total, 386)91 XCTAssertEqual(r.pages, 65)92 XCTAssertEqual(r.storesTotal, 77)93 XCTAssertEqual(r.diversity, 3)94 XCTAssertFalse(r.products.isEmpty)95 XCTAssertNil(r.understood)96 XCTAssertNotNil(r.facets)97 for p in r.products {98 XCTAssertNotNil(p.price, "une carte de liste a toujours un prix")99 XCTAssertNotNil(p.img, "une carte de liste a toujours une photo")100 XCTAssertFalse(p.storeName.isEmpty)101 }102 }103104 func testSearchWithTypo() throws {105 let r: SearchPage = try Fixture.decode("search_fautes")106 XCTAssertEqual(r.understood, "bracelet")107 XCTAssertFalse(r.relaxed)108 XCTAssertEqual(r.total, 178)109 }110111 func testSearchFiltersAndPoints() throws {112 let r: SearchPage = try Fixture.decode("search_filtres")113 XCTAssertEqual(r.total, 183)114 XCTAssertEqual(r.unpositioned, 43)115 let first = try XCTUnwrap(r.points.first)116 XCTAssertEqual(first.storeId, "annemariechagnon.com")117 XCTAssertEqual(first.name, "Anne-Marie Chagnon")118 XCTAssertEqual(first.city, "Plateau Mont-Royal")119 XCTAssertEqual(first.n, 47)120 XCTAssertEqual(first.lat, 45.5278, accuracy: 0.0001)121 // somme des n + sans position = total122 XCTAssertEqual(r.points.map(\.n).reduce(0, +) + (r.unpositioned ?? 0), r.total)123 XCTAssertEqual(r.diversity, 0, "tri par prix : diversité désactivée")124 }125126 func testOtherSearchSamples() throws {127 for name in ["search_defaut", "search_hasard", "search_points"] {128 let r: SearchPage = try Fixture.decode(name)129 XCTAssertFalse(r.products.isEmpty, name)130 }131 let c: CountResult = try Fixture.decode("count")132 XCTAssertEqual(c.total, 198)133 XCTAssertEqual(c.stores, 24)134 }135136 func testFacets() throws {137 let f: Facets = try Fixture.decode("facets")138 XCTAssertEqual(f.categories.count, 21)139 XCTAssertEqual(f.regions.count, 17)140 XCTAssertEqual(f.priceMedian, 32)141 XCTAssertEqual(f.buckets.count, 7)142 XCTAssertNil(f.buckets.last?.max, "dernière tranche ouverte : « 1 000 $ et plus »")143 XCTAssertEqual(f.buckets.first?.label, "moins de 10\u{00A0}$")144 XCTAssertEqual(f.total, 67156)145 XCTAssertEqual(f.inStock, 61904)146 XCTAssertFalse(f.subcategories.isEmpty)147 XCTAssertEqual(f.regions.first { $0.label == "Montréal" }?.key, "montreal")148 XCTAssertFalse(f.origins.contains { $0.label.lowercased().contains("certifié") }, "le mot « certifié » n'est plus servi")149 }150151 func testSuggestions() throws {152 let s: Suggestions = try Fixture.decode("suggest")153 XCTAssertEqual(s.q, "chand")154 XCTAssertTrue(s.terms.contains("chandail"))155 XCTAssertFalse(s.subcategories.isEmpty)156 XCTAssertFalse(s.stores.isEmpty)157 XCTAssertFalse(s.products.isEmpty)158 XCTAssertEqual(s.productsTotal, 4874)159 let b: Suggestions = try Fixture.decode("suggest_boutique")160 XCTAssertTrue(b.stores.contains { $0.city != nil })161 XCTAssertFalse(b.isEmpty)162 }163164 func testCategories() throws {165 let c: Categories = try Fixture.decode("categories")166 XCTAssertEqual(c.categories.count, 21)167 XCTAssertEqual(c.categories.last?.key, "autre", "« autre » vient toujours en dernier")168 let mode = try XCTUnwrap(c.categories.first { $0.key == "mode" })169 XCTAssertEqual(mode.label, "Mode & accessoires")170 XCTAssertEqual(mode.images.count, 4)171 XCTAssertFalse(mode.subcategories.isEmpty)172 // chaque catégorie a son enseigne parlante173 for cat in c.categories where cat.key != "autre" { XCTAssertNotEqual(cat.icon, "ic-boutique", cat.key) }174 }175176 func testStoresDirectory() throws {177 let r: StoresPage = try Fixture.decode("boutiques")178 XCTAssertEqual(r.total, 130)179 XCTAssertEqual(r.pages, 33)180 let jodoin = try XCTUnwrap(r.stores.first)181 XCTAssertEqual(jodoin.id, "bijouteriejodoin.com")182 XCTAssertEqual(jodoin.eyebrow, "Revendeur établi à Granby")183 XCTAssertNotNil(jodoin.coordinate)184 XCTAssertEqual(jodoin.thumbnails.count, 3)185 let a: StoresPage = try Fixture.decode("boutiques_annuaire")186 XCTAssertEqual(a.directoryTotal, 182)187 XCTAssertFalse(a.directory.isEmpty)188 XCTAssertNotNil(a.directory.first?.url)189 let p: StoresPage = try Fixture.decode("boutiques_points")190 XCTAssertEqual(p.unpositioned, 304)191 XCTAssertFalse(p.points.isEmpty)192 }193194 func testRegionsAndPlaces() throws {195 let r: Regions = try Fixture.decode("regions")196 XCTAssertEqual(r.regions.count, 17)197 XCTAssertGreaterThan(r.unknownStores ?? 0, 0)198 let estrie: Place = try Fixture.decode("region")199 XCTAssertEqual(estrie.name, "Estrie")200 XCTAssertEqual(estrie.stores, 130)201 XCTAssertFalse(estrie.latest.isEmpty)202 XCTAssertFalse(estrie.topStores.isEmpty)203 XCTAssertFalse(estrie.points.isEmpty)204 XCTAssertNotNil(estrie.categories.first?.sharePct)205 XCTAssertNotNil(estrie.categories.first?.stores, "toujours le nombre de boutiques à côté du chiffre")206 let sherbrooke: Place = try Fixture.decode("ville")207 XCTAssertEqual(sherbrooke.region, "Estrie")208 XCTAssertFalse(sherbrooke.topStores.isEmpty, "une ville rend `all_stores`")209 XCTAssertFalse(sherbrooke.nearbyCities.isEmpty)210 XCTAssertNotNil(sherbrooke.nearbyCities.first?.distanceKm)211 }212213 func testDiscoverAndRandom() throws {214 let d: Discover = try Fixture.decode("discover")215 XCTAssertEqual(d.newTotal, 3800)216 XCTAssertFalse(d.newProducts.isEmpty)217 XCTAssertFalse(d.newStores.isEmpty)218 XCTAssertFalse(d.categories.contains { $0.key == "autre" })219 XCTAssertEqual(d.totalRegions, 17)220 XCTAssertEqual(d.totalStores, 1387)221 let p: RandomFind = try Fixture.decode("random_product")222 XCTAssertEqual(p.kind, "product")223 XCTAssertEqual(p.seed, 2026)224 XCTAssertNotNil(p.product)225 let s: RandomFind = try Fixture.decode("random_store")226 XCTAssertNotNil(s.store)227 XCTAssertFalse(s.products.isEmpty)228 }229230 func testPublicRulesAndSources() throws {231 let p: PublicRules = try Fixture.decode("public")232 XCTAssertEqual(p.rules.count, 8)233 XCTAssertGreaterThan(p.publicProducts ?? 0, 300_000)234 XCTAssertEqual(p.excluded.first?.reason, "sans_photo")235 XCTAssertEqual(p.registry, 4082)236 XCTAssertNotNil(p.meaning)237 let s: Sources = try Fixture.decode("sources")238 XCTAssertEqual(s.followed, 1701)239 XCTAssertEqual(s.platforms.first?.name, "Shopify")240 XCTAssertFalse(s.listedBy.isEmpty)241 XCTAssertNotNil(s.syncs24h)242 }243244 func testInscriptionForm() throws {245 let f: InscriptionForm = try Fixture.decode("inscriptions_form")246 XCTAssertEqual(f.honeypot, "company_url")247 XCTAssertEqual(f.categories.count, 21)248 XCTAssertEqual(f.field("description")?.min, 40)249 XCTAssertNotNil(f.consentLabel)250 XCTAssertEqual(f.stored.count, 10)251 XCTAssertEqual(f.perDay, 3)252 }253254 func testBatch() throws {255 let b: ProductBatch = try Fixture.decode("products_lot")256 XCTAssertEqual(b.entries.map(\.product.uid), ["cafd5ce66af3bec2d78e"])257 XCTAssertEqual(b.entries.first?.requestedUID, "cafd5ce66af3bec2d78e")258 XCTAssertEqual(b.unavailable.first?.uid, "00000000000000000000")259 XCTAssertEqual(b.unavailable.first?.status, "unknown")260 }261262 func testLossyListsSkipBrokenElements() throws {263 let json = #"{"total":2,"products":[{"uid":"a","title":"A","price":"12,5","store_id":"s","store_name":"S","available":1},{"title":"sans uid"},{"uid":"b","title":"B","price":3}]}"#264 let r = try JSONDecoder().decode(SearchPage.self, from: Data(json.utf8))265 XCTAssertEqual(r.products.map(\.uid), ["a", "b"])266 XCTAssertEqual(r.products[0].price, 12.5)267 XCTAssertEqual(r.products[0].availability, .inStock)268 }269}270