Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : Immo-Ka iOS9// File : ImmoKaTests/DoorTests.swift10// Description : machine à états de la porte et choix du niveau11// d'animation.12// Module : ImmoKaTests13// Repository : https://git.spboucher.ai/immo-ka-ios-v314// Project Website : https://www.immo-ka.com15// Created : 2026-11-2516// 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 Foundation91import Testing92@testable import ImmoKa9394@Suite("Porte : machine à états et niveaux")95struct DoorTests {96 /// Domaine de réglages jetable : les tests ne modifient jamais les réglages de l'app installée97 /// (un test avait laissé « Aucune » dans le simulateur et coupé la porte de l'app).98 static func scratch() -> UserDefaults {99 let name = "immoka.tests.\(UUID().uuidString)"100 let d = UserDefaults(suiteName: name)!101 d.removePersistentDomain(forName: name)102 return d103 }104105 @Test("séquence complète : de la porte fermée à la fiche")106 func fullSequence() {107 var m = DoorStateMachine()108 #expect(m.state == .closed)109 let path: [(DoorEvent, DoorState)] = [110 (.appear, .idle), (.touchDown, .hover), (.trigger, .opening), (.panelOpened, .open),111 (.reveal, .revealing), (.expand, .navigating), (.finish, .completed), (.reset, .closed),112 ]113 for (event, expected) in path {114 let r1 = m.send(event)115 #expect(r1)116 #expect(m.state == expected)117 }118 }119120 @Test("les transitions hors séquence sont refusées")121 func invalidTransitions() {122 var m = DoorStateMachine()123 let r2 = m.send(.panelOpened)124 #expect(!r2)125 let r3 = m.send(.finish)126 #expect(!r3)127 let r4 = m.send(.interrupt)128 #expect(!r4, "rien à interrompre quand la porte est fermée")129 #expect(m.state == .closed)130 m.send(.trigger)131 let r5 = m.send(.reveal)132 #expect(!r5, "la photo n'est révélée qu'une fois le panneau ouvert")133 let r6 = m.send(.trigger)134 #expect(!r6, "une ouverture en cours ne redémarre pas")135 #expect(m.state == .opening)136 }137138 @Test("interruption à chaque étape animée, puis fin propre")139 func interruption() {140 for steps in [[DoorEvent.trigger], [.trigger, .panelOpened], [.trigger, .panelOpened, .reveal], [.trigger, .panelOpened, .reveal, .expand]] {141 var m = DoorStateMachine()142 steps.forEach { m.send($0) }143 #expect(m.isRunning)144 let r7 = m.send(.interrupt)145 #expect(r7)146 #expect(m.state == .interrupted)147 #expect(!m.isRunning)148 let r8 = m.send(.finish)149 #expect(r8)150 #expect(m.state == .completed)151 }152 }153154 @Test("photo absente : erreur sans blocage, la navigation se termine")155 func missingPhoto() {156 var m = DoorStateMachine()157 m.send(.trigger); m.send(.panelOpened)158 let r9 = m.send(.fail)159 #expect(r9)160 #expect(m.state == .error)161 let r10 = m.send(.finish)162 #expect(r10)163 #expect(m.state == .completed)164 }165166 @Test("invitation et fermeture")167 func ajarAndClose() {168 var m = DoorStateMachine()169 let r11 = m.send(.invite)170 #expect(r11)171 #expect(m.state == .ajar)172 let r12 = m.send(.close)173 #expect(r12)174 #expect(m.state == .closing)175 let r13 = m.send(.closed)176 #expect(r13)177 #expect(m.state == .closed)178 }179180 @Test("niveau selon la fréquence, l'origine, l'accessibilité et le réglage")181 func levelPolicy() {182 typealias P = DoorLevelPolicy183 // automatique, depuis une carte : 3 signatures, 7 standards, puis compact184 #expect(P.level(opensThisSession: 0, reduceMotion: false, preference: .automatic) == .signature)185 #expect(P.level(opensThisSession: 2, reduceMotion: false, preference: .automatic) == .signature)186 #expect(P.level(opensThisSession: 3, reduceMotion: false, preference: .automatic) == .standard)187 #expect(P.level(opensThisSession: 9, reduceMotion: false, preference: .automatic) == .standard)188 #expect(P.level(opensThisSession: 10, reduceMotion: false, preference: .automatic) == .compact)189 // depuis un favori ou une étiquette de carte : standard la première fois, puis compact, jamais signature190 #expect(P.level(opensThisSession: 0, origin: .favorite, reduceMotion: false, preference: .automatic) == .standard)191 #expect(P.level(opensThisSession: 0, origin: .mapPill, reduceMotion: false, preference: .automatic) == .standard)192 #expect(P.level(opensThisSession: 1, origin: .favorite, reduceMotion: false, preference: .automatic) == .compact)193 #expect(P.level(opensThisSession: 11, origin: .mapPill, reduceMotion: false, preference: .automatic) == .compact)194 // Réduire les animations l'emporte, sauf si tout est coupé195 #expect(P.level(opensThisSession: 0, reduceMotion: true, preference: .automatic) == .reduced)196 #expect(P.level(opensThisSession: 0, reduceMotion: true, preference: .discreet) == .reduced)197 #expect(P.level(opensThisSession: 0, reduceMotion: true, preference: .off) == .none)198 // réglages199 #expect(P.level(opensThisSession: 0, reduceMotion: false, preference: .discreet) == .compact)200 #expect(P.level(opensThisSession: 0, reduceMotion: false, preference: .off) == .none)201 // un lien direct n'est jamais animé202 #expect(P.level(opensThisSession: 0, origin: .directLink, reduceMotion: false, preference: .automatic) == .none)203 }204205 @Test("durées du Door Engine 3.0 : les mêmes pistes que le web")206 func brandTimings() {207 let s = DoorLevel.signature.plan, t = DoorLevel.standard.plan, c = DoorLevel.compact.plan208 // signature 1 220 ms, standard 800 ms, discret 300 ms, réduit 140 ms, aucun 0209 #expect(abs(DoorLevel.signature.total - 1.22) < 1e-9 && abs(DoorLevel.standard.total - 0.80) < 1e-9)210 #expect(abs(DoorLevel.compact.total - 0.30) < 1e-9 && abs(DoorLevel.reduced.total - 0.14) < 1e-9 && DoorLevel.none.total == 0)211 // pivotement : 660 ms en signature, 400 ms en standard, jusqu'à 84°212 #expect(abs(DoorLevel.signature.swing - 0.66) < 1e-9 && abs(DoorLevel.standard.swing - 0.40) < 1e-9)213 #expect(DoorLevel.signature.angle == 84 && DoorLevel.compact.angle == 0)214 #expect(!DoorLevel.compact.showsDoor && DoorLevel.signature.showsDoor && DoorLevel.standard.showsDoor)215 // pistes du tableau de MOTION_SYSTEM.md (signature, standard)216 #expect((s.emerge, s.leverStart, s.leverEnd, s.swingStart, s.swingEnd) == (0.24, 0.11, 0.30, 0.18, 0.84))217 #expect((t.emerge, t.leverStart, t.leverEnd, t.swingStart, t.swingEnd) == (0.17, 0.04, 0.23, 0.11, 0.51))218 #expect((s.travelStart, s.travelEnd, s.revealStart, s.revealEnd) == (0.312, 1.10, 0.80, 1.10))219 #expect((t.travelStart, t.travelEnd, t.revealStart, t.revealEnd) == (0.19, 0.71, 0.47, 0.71))220 // la fiche est poussée pendant le pivotement, jamais après : l'animation ne retarde pas la fiche221 #expect(s.pushAt < s.swingEnd && t.pushAt < t.swingEnd && c.pushAt <= 0.07)222 #expect(DoorLevel.reduced.plan.pushAt == 0)223 }224225 @Test("trajectoire du battant : passe par ses points, toujours croissante")226 func swingPath() {227 #expect(SwingPath.value(at: -1) == 0 && SwingPath.value(at: 0) == 0 && SwingPath.value(at: 1) == 1 && SwingPath.value(at: 2) == 1)228 for (t, v) in SwingPath.points { #expect(abs(SwingPath.value(at: t) - v) < 1e-9) }229 #expect(abs(SwingPath.value(at: 0.05) - 0.0175) < 1e-9)230 var last = -1.0231 for i in 0...200 {232 let v = SwingPath.value(at: Double(i) / 200)233 #expect(v >= last)234 last = v235 }236 // la largeur apparente (cosinus) diminue régulièrement : à mi-course on voit déjà nettement l'ouverture237 #expect(cos(86 * SwingPath.value(at: 0.5) * .pi / 180) < 0.7)238 }239240 @Test("courbes de Bézier : bornes et monotonie")241 func bezier() {242 for e in [BezierEase.door, .emerge, .morph, .push, .out, .inOut, .arrive] {243 #expect(e(0) == 0 && e(1) == 1 && e(-1) == 0 && e(2) == 1)244 var last = 0.0245 for i in 0...50 { let v = e(Double(i) / 50); #expect(v >= last - 1e-6); last = v }246 }247 #expect(abs(BezierEase(x1: 0, y1: 0, x2: 1, y2: 1)(0.37) - 0.37) < 1e-4)248 }249250 @Test("chorégraphie : la scène est une fonction du temps")251 func sceneOverTime() {252 let screen = CGSize(width: 402, height: 874)253 let source = CGRect(x: 20, y: 600, width: 286, height: 214)254 let hero = CGRect(x: 0, y: 116, width: 402, height: 320)255 func at(_ t: Double) -> DoorSceneState { DoorSceneState.at(t, level: .signature, source: source, hero: hero, screen: screen) }256 // au geste : la scène est posée sur la photo touchée, rien d'autre n'a bougé257 let s0 = at(0)258 #expect(s0.frame == source && s0.veil == 0 && s0.house == 0 && s0.dim == 0 && s0.angle == 0 && s0.reveal == 0)259 // la maison tient en entier dans la photo260 let box = DoorSceneState.houseBox(in: source)261 #expect(source.contains(box) && box.width == box.height)262 // fin de l'émergence : la maison est posée, la photo autour est dans la pénombre (30 %), porte encore fermée ou presque263 let s1 = at(0.24)264 #expect(abs(s1.house - 1) < 1e-6 && abs(s1.dim - 0.30) < 1e-6 && abs(s1.houseScale - 1) < 1e-6 && s1.angle < 3)265 #expect(s1.frame == source, "la scène ne quitte pas la photo avant le voyage")266 // la béquille s'abaisse avant le pivotement, puis remonte267 #expect(at(0.205).lever < -20 && at(0.31).lever == 0)268 // pivotement : l'angle suit la trajectoire, de 180 à 840 ms269 #expect(abs(at(0.18 + 0.33).angle - 84 * 0.56) < 0.01 && abs(at(0.84).angle - 84) < 1e-6)270 // la page d'origine cède, la fiche est poussée à 378 ms sous un voile plein, puis elle apparaît271 #expect(at(0.17).veil == 0 && abs(at(0.378).veil - 1) < 1e-6 && at(0.61).veil == 0)272 #expect(at(0.3).veil > 0.3 && at(0.3).veil < 1)273 // voyage : la photo va de la carte à la galerie, sans jamais sortir de l'enveloppe des deux cadres274 let mid = at(0.7)275 #expect(mid.frame.width > source.width && mid.frame.width < hero.width && mid.frame.minY < source.minY)276 let end = at(1.10)277 #expect(abs(end.frame.minX - hero.minX) < 0.01 && abs(end.frame.width - hero.width) < 0.01 && abs(end.frame.height - hero.height) < 0.01)278 #expect(end.corner == 0)279 // révélation : l'embrasure s'étend à toute la photo, la maison et la pénombre s'effacent280 #expect(at(0.79).reveal == 0 && abs(end.reveal - 1) < 1e-6 && end.house < 1e-6 && end.dim < 1e-6 && end.rootOpacity == 1)281 // effacement sur la vraie galerie282 #expect(abs(at(1.16).rootOpacity - 0.5) < 0.01 && at(1.23).rootOpacity == 0)283 // discret : la photo voyage de la carte à la galerie, sans maison284 let c = DoorSceneState.at(0.22, level: .compact, source: source, hero: hero, screen: screen)285 #expect(abs(c.frame.width - hero.width) < 0.01 && c.house == 0 && c.angle == 0 && c.dim == 0)286 #expect(DoorSceneState.at(0, level: .compact, source: source, hero: hero, screen: screen).frame == source)287 // réduit : aucun déplacement, un fondu de 140 ms288 let r = DoorSceneState.at(0.07, level: .reduced, source: source, hero: hero, screen: screen)289 #expect(r.frame == hero && r.house == 0 && abs(r.veil - 0.5) < 0.01)290 }291292 @Test("angles des états et géométrie du symbole")293 func geometry() {294 #expect(DoorState.closed.restAngle == 0)295 #expect(DoorState.hover.restAngle == 22)296 #expect(DoorState.ajar.restAngle == 40)297 #expect(DoorState.open.restAngle == 86)298 #expect(BrandGeometry.opening == CGRect(x: 23, y: 28, width: 18, height: 30))299 // projection du battant (système de marque, § 4) : x du bord libre, y haut et bas, sur la grille 64300 let table: [(Double, CGFloat, CGFloat, CGFloat)] = [301 (0, 41.0, 28.0, 58.0), (22, 38.6, 29.6, 56.1), (60, 31.6, 31.3, 54.3), (75, 28.3, 31.6, 54.0), (84, 26.3, 31.7, 53.9),302 ]303 for (angle, x, top, bottom) in table {304 let e = BrandGeometry.freeEdge(angle: angle)305 #expect(abs(23 + e.x - x) < 0.06, "x du bord libre à \(angle)°")306 #expect(abs(28 + e.top - top) < 0.06 && abs(58 - e.bottom - bottom) < 0.06, "hauteur du bord libre à \(angle)°")307 }308 // 40° calculé : 35,6 ; mais le logo au repos garde son battant dessiné à la main (36,5 ; 31,3 → 55,2)309 #expect(abs(23 + BrandGeometry.freeEdge(angle: 40.001).x - 35.6) < 0.06)310 let rest = BrandGeometry.freeEdge(angle: 40)311 #expect(abs(23 + rest.x - 36.5) < 0.001 && abs(rest.top - 3.3) < 0.001 && abs(rest.bottom - 2.8) < 0.001)312 #expect(BrandGeometry.panelShade(angle: 90) == 0.30 && BrandGeometry.panelShade(angle: 0) == 0)313 // les pièces du symbole : le battant fermé remplit l'embrasure, entrouvert il est plus étroit314 let box = CGRect(x: 0, y: 0, width: 64, height: 64)315 let closed = HousePanelShape(angle: 0, micro: true).path(in: box).boundingRect316 #expect(abs(closed.width - BrandGeometry.openingMicro.width) < 0.01)317 let ajar = HousePanelShape(angle: 40).path(in: box).boundingRect318 #expect(abs(ajar.maxX - 36.5) < 0.06 && abs(ajar.minX - 23) < 0.01)319 let body = HouseSilhouetteShape().path(in: box).boundingRect320 #expect(abs(body.minY - 6.5) < 0.01 && abs(body.maxY - 58) < 0.01 && body.minX < 4.2 && body.maxX > 59.8)321 // la silhouette contient les murs et le faîte, pas le ciel au-dessus de l'avant-toit322 let sil = HouseSilhouetteShape().path(in: box)323 #expect(sil.contains(CGPoint(x: 32, y: 20)) && sil.contains(CGPoint(x: 44, y: 11)) && !sil.contains(CGPoint(x: 8, y: 12)))324 }325326 @Test("le niveau compact passe le seuil sans ouvrir de panneau")327 func compactSkipsPanel() {328 var m = DoorStateMachine()329 m.send(.trigger)330 let r = m.send(.expand)331 #expect(r)332 #expect(m.state == .navigating)333 }334335 @Test("interruption pendant l'animation : la fiche est ouverte une seule fois, tout de suite")336 @MainActor337 func interruptNavigatesOnce() async {338 let door = DoorCenter(defaults: Self.scratch())339 var pushes = 0340 door.open(uid: "x:1", image: nil, from: CGRect(x: 20, y: 300, width: 286, height: 214), reduceMotion: false) { pushes += 1 }341 #expect(door.run != nil && door.run?.level == .signature)342 #expect(door.state == .opening)343 #expect(pushes == 0, "la fiche n'est pas encore poussée : la page d'origine cède d'abord")344 door.interrupt()345 #expect(pushes == 1)346 #expect(door.run == nil && door.state == .closed)347 // la fin différée de l'animation annulée ne pousse pas la fiche une seconde fois348 try? await Task.sleep(for: .seconds(1.6))349 #expect(pushes == 1)350 #expect(door.opensThisSession == 1)351 }352353 @Test("animation menée à terme : la fiche est poussée une fois, puis le calque disparaît")354 @MainActor355 func completesAndNavigatesOnce() async {356 let door = DoorCenter(defaults: Self.scratch())357 var pushes = 0358 door.open(uid: "x:2", image: nil, from: CGRect(x: 20, y: 300, width: 286, height: 214), reduceMotion: false) { pushes += 1 }359 // la fiche est poussée à 378 ms, pendant le pivotement ; le calque disparaît à 1 220 ms360 try? await Task.sleep(for: .seconds(0.15))361 #expect(pushes == 0, "la page d'origine n'a pas encore cédé")362 try? await Task.sleep(for: .seconds(0.55))363 #expect(pushes == 1 && door.run != nil)364 try? await Task.sleep(for: .seconds(0.9))365 #expect(pushes == 1)366 #expect(door.run == nil && door.state == .closed)367 }368369 @Test("réglage « Aucune » et lien direct : navigation immédiate, sans calque")370 @MainActor371 func offNavigatesImmediately() {372 let door = DoorCenter(defaults: Self.scratch())373 door.preference = .off374 var pushes = 0375 door.open(uid: "x:3", image: nil, from: CGRect(x: 0, y: 0, width: 200, height: 150), reduceMotion: false) { pushes += 1 }376 #expect(pushes == 1 && door.run == nil)377 door.preference = .automatic378 door.open(uid: "x:4", image: nil, from: CGRect(x: 0, y: 0, width: 200, height: 150), origin: .directLink, reduceMotion: false) { pushes += 1 }379 #expect(pushes == 2 && door.run == nil)380 }381}382