Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : Vrai-Prix iOS9// File : VraiPrixTests/StoreTests.swift10// Description : bibliothèque sur l'appareil (favoris, comparateur,11// récents), regroupement de carte, liens profonds.12// Module : VraiPrixTests13// Repository : https://git.spboucher.ai/vrai-prix-ios-v314// Project Website : https://www.vrai-prix.com15// Created : 2026-11-2616// Last Modified : 2026-11-2817//18// =============================================================================19// AUTHOR & PROFESSIONAL AFFILIATION20// =============================================================================21//22// Author : Simon-Pierre Boucher23// Position : Professor of Finance24// Institution : Université du Québec en Outaouais (UQO)25// Department : Département des sciences administratives26// Organization : SP Boucher27//28// Website : https://spboucher.ai29// Git : https://git.spboucher.ai30//31// =============================================================================32// OFFICIAL CONTACT DIRECTORY33// =============================================================================34//35// General : info@spboucher.ai36// Professional : contact@spboucher.ai37// Direct : simon@spboucher.ai38// Research : research@spboucher.ai39// Development : dev@spboucher.ai40// Support : support@spboucher.ai41// Security/Admin : admin@spboucher.ai42// Notifications : notifications@spboucher.ai43//44// Preferred Contact: support@spboucher.ai45//46// =============================================================================47// COPYRIGHT & INTELLECTUAL PROPERTY48// =============================================================================49//50// Copyright (c) 202651// Simon-Pierre Boucher52//53// All rights reserved where applicable.54//55// This file is governed by the applicable project license,56// copyright notices, institutional agreements and relevant law.57//58// Unless expressly authorized by the applicable license or59// rights holder, copying, redistribution, sublicensing,60// publication and commercial exploitation are not permitted.61//62// Existing third-party rights and notices remain unaffected.63//64// =============================================================================65// CONFIDENTIALITY & SECURITY66// =============================================================================67//68// Classification : Public69// Distribution : Public repository; use is governed by the project license70// Security Contact : admin@spboucher.ai71//72// Access to this source does not independently grant permission73// to redistribute or disclose confidential materials.74//75// Do not embed credentials, private keys, access tokens,76// certificates or confidential personal information.77//78// =============================================================================79// LICENSE & MAINTENANCE80// =============================================================================81//82// License : None specified (no license file in this repository)83// Maintainer : Simon-Pierre Boucher84// Technical Contact: dev@spboucher.ai85//86// =============================================================================87// END OF SP BOUCHER SOURCE HEADER88// =============================================================================8990import XCTest91@testable import VraiPrix9293@MainActor94final class StoreTests: XCTestCase {95 /// Ces tests décrivent l'app sous l'API 2 : le niveau est fixé, quel que soit ce que sert la production.96 override func setUp() { ServiceLevel.store(.v2) }9798 private func prop(_ n: Int, kind: SavedProperty.Kind = .unit) -> SavedProperty {99 SavedProperty(kind: kind, ref: "id\(n)", title: "\(n) Rue Test", subtitle: "Québec", value: 400_000, low: 350_000, high: 460_000, price: 430_000, priceLabel: "Vendue")100 }101102 func testFavorites() {103 let l = Library(inMemory: true)104 XCTAssertTrue(l.toggleFavorite(prop(1)))105 XCTAssertTrue(l.isFavorite("unit:id1"))106 XCTAssertTrue(l.toggleFavorite(prop(2)))107 XCTAssertEqual(l.favorites.map(\.ref), ["id2", "id1"], "le plus récent d'abord")108 XCTAssertFalse(l.toggleFavorite(prop(1)))109 XCTAssertEqual(l.favorites.count, 1)110 XCTAssertFalse(l.isFavorite("listing:id2"), "une annonce et une unité de même référence sont distinctes")111 }112113 func testCompareIsLimitedToFour() {114 let l = Library(inMemory: true)115 for i in 1...4 { XCTAssertEqual(l.toggleCompare(prop(i)), .added) }116 XCTAssertEqual(l.toggleCompare(prop(5)), .full)117 XCTAssertEqual(l.compare.count, 4)118 XCTAssertEqual(l.toggleCompare(prop(2)), .removed)119 XCTAssertEqual(l.toggleCompare(prop(5)), .added)120 l.clearCompare(); XCTAssertTrue(l.compare.isEmpty)121 }122123 func testRecentsDeduplicateAndRefreshFavorites() {124 let l = Library(inMemory: true)125 l.toggleFavorite(prop(1))126 for i in 1...30 { l.visit(prop(i)) }127 XCTAssertEqual(l.recents.count, Library.recentLimit)128 XCTAssertEqual(l.recents.first?.ref, "id30")129 var updated = prop(1); updated.value = 999_000130 l.visit(updated)131 XCTAssertEqual(l.recents.first?.ref, "id1")132 XCTAssertEqual(l.recents.filter { $0.ref == "id1" }.count, 1)133 XCTAssertEqual(l.favorites.first?.value, 999_000, "le favori suit la dernière valeur consultée")134 l.eraseAll()135 XCTAssertTrue(l.favorites.isEmpty && l.recents.isEmpty && l.searches.isEmpty)136 }137138 func testSavedGap() {139 XCTAssertEqual(prop(1).gapPct!, 7.5, accuracy: 0.001)140 XCTAssertEqual(prop(1).reading?.verdict, .dessus)141 var served = prop(1); served.verdictKey = "juste"; served.ecartPct = 6.9; served.dansFourchette = true142 XCTAssertEqual(served.reading?.verdict, .juste, "la lecture mémorisée du service prime")143 XCTAssertEqual(prop(1).route, .property("id1"))144 XCTAssertEqual(prop(1, kind: .listing).route, .listing("id1"))145 }146147 // MARK: Carte148149 private func pt(_ i: Int, _ lat: Double, _ lng: Double, _ amount: Double = 400_000) -> MapPoint {150 MapPoint(id: "p\(i)", layer: .sales, lat: lat, lng: lng, amount: amount, title: "", subtitle: "")151 }152153 func testFewPointsStayIndividual() {154 let b = MapBounds(west: -71.3, south: 46.8, east: -71.2, north: 46.9)155 let items = Clusterer.cluster((0..<10).map { pt($0, 46.81 + Double($0) * 0.005, -71.29 + Double($0) * 0.005) }, in: b)156 XCTAssertEqual(items.count, 10)157 XCTAssertTrue(items.allSatisfy { $0.count == 1 })158 }159160 func testManyPointsClusterAndCountIsPreserved() {161 let b = MapBounds(west: -71.3, south: 46.8, east: -71.2, north: 46.9)162 let pts = (0..<600).map { pt($0, 46.8 + Double($0 % 25) * 0.004, -71.3 + Double($0 / 25) * 0.004, Double(300_000 + $0 * 100)) }163 let items = Clusterer.cluster(pts, in: b)164 XCTAssertLessThan(items.count, 120)165 XCTAssertEqual(items.map(\.count).reduce(0, +), 600, "aucun point perdu")166 let pinned = Clusterer.cluster(pts, in: b, pinned: "p300")167 XCTAssertTrue(pinned.contains { if case .single(let p) = $0.kind { return p.id == "p300" }; return false }, "la sélection reste seule")168 XCTAssertEqual(pinned.map(\.count).reduce(0, +), 600)169 }170171 func testCoincidentPointsMergeAndMedian() {172 let b = MapBounds(west: -71.3, south: 46.8, east: -71.2, north: 46.9)173 let items = Clusterer.cluster([pt(1, 46.85, -71.25, 300_000), pt(2, 46.85, -71.25, 500_000), pt(3, 46.86, -71.26)], in: b)174 XCTAssertEqual(items.count, 2)175 XCTAssertTrue(items.contains { if case .cluster(let c, let m) = $0.kind { return c == 2 && m == 400_000 }; return false })176 XCTAssertEqual(Clusterer.median([3, 1, 2]), 2); XCTAssertNil(Clusterer.median([]))177 }178179 func testBoundsClamp() {180 let b = MapBounds(west: -75, south: 45, east: -71, north: 47).clamped(maxSide: 0.5)181 XCTAssertEqual(b.width, 0.5, accuracy: 1e-9); XCTAssertEqual(b.height, 0.5, accuracy: 1e-9)182 XCTAssertEqual((b.west + b.east) / 2, -73, accuracy: 1e-9)183 XCTAssertTrue(MapBounds(west: 0, south: 0, east: 2, north: 2).contains(MapBounds(west: 0.5, south: 0.5, east: 1, north: 1)))184 XCTAssertFalse(MapFilters(type: "condo").keeps(pt(1, 0, 0)))185 XCTAssertFalse(MapFilters(min: 500_000).keeps(pt(1, 0, 0)))186 XCTAssertTrue(MapFilters(min: 300_000, max: 450_000).keeps(pt(1, 0, 0)))187 }188189 // MARK: Liens profonds190191 func testDeepLinks() {192 let m = AppModel(motion: MotionCenter())193 m.handle(url: URL(string: "vraiprix://propriete/123")!)194 XCTAssertEqual(m.paths[.home]?.count, 1)195 m.handle(url: URL(string: "https://www.vrai-prix.com/a-vendre/remax_quebec%3A16616209")!)196 XCTAssertEqual(m.paths[.home]?.count, 2)197 m.handle(url: URL(string: "https://www.vrai-prix.com/ventes")!)198 XCTAssertEqual(m.tab, .sales)199 m.handle(url: URL(string: "vraiprix://municipalite/quebec")!)200 XCTAssertEqual(m.paths[.sales]?.count, 1)201 m.handle(url: URL(string: "vraiprix://inconnu/x")!)202 XCTAssertEqual(m.tab, .sales)203 m.showOnMap(lat: 46.8, lng: -71.2)204 XCTAssertEqual(m.tab, .map); XCTAssertEqual(m.mapFocus?.layer, .sales)205 }206207 func testMotionTriggersAreConsumedOnce() {208 let c = MotionCenter()209 c.arm("unit:1", .reveal)210 XCTAssertEqual(String(describing: c.consume("unit:1")), "reveal")211 XCTAssertEqual(String(describing: c.consume("unit:1")), "direct", "un second affichage est un lien direct")212 }213}214