SPB Git

Repository Public

DoorTests.swift 16.2 KB 344 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : Lou-Ka iOS9// File             : LouKaTests/DoorTests.swift10// Description      : machine à états de la porte et choix du niveau11//                    d'animation.12// Module           : LouKaTests13// Repository       : https://git.spboucher.ai/lou-ka-ios-v314// Project Website  : https://www.lou-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 LouKa9394@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 = "louka.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 pastille de carte : standard 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: 11, origin: .mapPill, reduceMotion: false, preference: .automatic) == .compact)193        // Réduire les animations l'emporte, sauf si tout est coupé194        #expect(P.level(opensThisSession: 0, reduceMotion: true, preference: .automatic) == .reduced)195        #expect(P.level(opensThisSession: 0, reduceMotion: true, preference: .discreet) == .reduced)196        #expect(P.level(opensThisSession: 0, reduceMotion: true, preference: .off) == .none)197        // réglages198        #expect(P.level(opensThisSession: 0, reduceMotion: false, preference: .discreet) == .compact)199        #expect(P.level(opensThisSession: 0, reduceMotion: false, preference: .off) == .none)200        // un lien direct n'est jamais animé201        #expect(P.level(opensThisSession: 0, origin: .directLink, reduceMotion: false, preference: .automatic) == .none)202    }203204    @Test("durées et angle du système de marque (porte de la transition v2)")205    func brandTimings() {206        let s = DoorLevel.signature, t = DoorLevel.standard, c = DoorLevel.compact207        #expect((s.emerge, s.swingDelay, s.swing, s.reveal, s.settle, s.angle) == (0.22, 0.16, 0.64, 0.30, 0.12, 86))208        #expect((t.emerge, t.swingDelay, t.swing, t.reveal, t.settle, t.angle) == (0.16, 0.10, 0.40, 0.24, 0.09, 86))209        #expect((c.reveal, c.settle, c.angle) == (0.22, 0.08, 0))210        #expect(!c.showsDoor && s.showsDoor && t.showsDoor)211        #expect(DoorLevel.reduced.settle == 0.14)212        #expect(DoorLevel.none.total == 0)213        // signature 1 180 ms, standard 790 ms (plan du Door Engine 3.0), effacement compris ; le passage part 40 ms avant la fin du pivotement214        #expect(abs(s.total - 1.18) < 0.0001 && abs(t.total - 0.79) < 0.0001)215        #expect(abs(s.revealStart - 0.76) < 0.0001 && abs(t.revealStart - 0.46) < 0.0001)216    }217218    @Test("trajectoire du battant : passe par ses points, toujours croissante")219    func swingPath() {220        #expect(SwingPath.value(at: -1) == 0 && SwingPath.value(at: 0) == 0 && SwingPath.value(at: 1) == 1 && SwingPath.value(at: 2) == 1)221        for (t, v) in SwingPath.points { #expect(abs(SwingPath.value(at: t) - v) < 1e-9) }222        #expect(abs(SwingPath.value(at: 0.05) - 0.0175) < 1e-9)223        var last = -1.0224        for i in 0...200 {225            let v = SwingPath.value(at: Double(i) / 200)226            #expect(v >= last)227            last = v228        }229        // la largeur apparente (cosinus) diminue régulièrement : à mi-course on voit déjà nettement l'ouverture230        #expect(cos(86 * SwingPath.value(at: 0.5) * .pi / 180) < 0.7)231    }232233    @Test("courbes de Bézier : bornes et monotonie")234    func bezier() {235        for e in [BezierEase.door, .emerge, .morph, .push, .out] {236            #expect(e(0) == 0 && e(1) == 1 && e(-1) == 0 && e(2) == 1)237            var last = 0.0238            for i in 0...50 { let v = e(Double(i) / 50); #expect(v >= last - 1e-6); last = v }239        }240        #expect(abs(BezierEase(x1: 0, y1: 0, x2: 1, y2: 1)(0.37) - 0.37) < 1e-4)241    }242243    @Test("chorégraphie : la scène est une fonction du temps")244    func sceneOverTime() {245        let screen = CGSize(width: 402, height: 874)246        let source = CGRect(x: 20, y: 600, width: 286, height: 214)247        let hero = CGRect(x: 0, y: 116, width: 402, height: 320)248        let door = DoorSceneState.doorRect(in: screen)249        // une vraie porte : largeur = 54 % de la hauteur, hauteur = 62 % de l'écran, au centre, un peu au-dessus du milieu250        #expect(abs(door.height - 542) < 1 && abs(door.width / door.height - 0.54) < 0.01)251        #expect(abs(door.midX - 201) < 1 && door.midY < 437)252        func at(_ t: Double) -> DoorSceneState { DoorSceneState.at(t, level: .signature, source: source, hero: hero, screen: screen) }253        // au toucher : porte fermée, à l'échelle où elle tient dans la photo, centrée dessus, invisible254        let s0 = at(0)255        #expect(s0.angle == 0 && s0.rigOpacity == 0 && s0.veil == 0)256        #expect(abs(s0.rigScale - 214 * 0.94 / door.height) < 0.001)257        #expect(abs(s0.rigOffset.width - (source.midX - door.midX)) < 0.01 && abs(s0.rigOffset.height - (source.midY - door.midY)) < 0.01)258        // fin de l'émergence : au centre, pleine taille ; la béquille vient de jouer259        let s1 = at(0.22)260        #expect(abs(s1.rigScale - 1) < 0.001 && abs(s1.rigOffset.width) < 0.01 && s1.rigOpacity == 1)261        #expect(at(0.185).lever < -24 && at(0.34).lever == 0)262        // pivotement : l'angle suit la trajectoire, on avance vers la porte263        #expect(abs(at(0.16 + 0.32).angle - 86 * 0.56) < 0.01)264        #expect(at(0.5).push > 1.02 && at(0.5).push < 1.07)265        // passage du seuil : la boîte va de la porte à la photo de la fiche, la quincaillerie s'efface266        #expect(at(0.75).rig == door && at(0.75).hardware == 1)267        let end = at(0.76 + 0.30)268        #expect(abs(end.rig.minX - hero.minX) < 0.01 && abs(end.rig.width - hero.width) < 0.01 && abs(end.rig.height - hero.height) < 0.01)269        #expect(end.hardware == 0 && end.inset == 0 && abs(end.push - 1) < 0.001 && end.rootOpacity == 1)270        // effacement271        #expect(abs(at(1.12).rootOpacity - 0.5) < 0.01 && at(1.19).rootOpacity == 0)272        // compact : la photo touchée s'étend jusqu'à la photo de la fiche, sans porte273        let c = DoorSceneState.at(0.07 + 0.22, level: .compact, source: source, hero: hero, screen: screen)274        #expect(abs(c.rig.width - hero.width) < 0.01 && c.hardware == 0 && c.angle == 0)275        #expect(DoorSceneState.at(0, level: .compact, source: source, hero: hero, screen: screen).rig == source)276    }277278    @Test("angles des états et géométrie du symbole")279    func geometry() {280        #expect(DoorState.closed.restAngle == 0)281        #expect(DoorState.hover.restAngle == 16)282        #expect(DoorState.ajar.restAngle == 40)283        #expect(DoorState.open.restAngle == 86)284        #expect(abs(DoorGeometry.aspect - 0.69) < 0.005)285        #expect(abs(DoorGeometry.frame - 0.128) < 0.001)286    }287288    @Test("le niveau compact passe le seuil sans ouvrir de panneau")289    func compactSkipsPanel() {290        var m = DoorStateMachine()291        m.send(.trigger)292        let r = m.send(.expand)293        #expect(r)294        #expect(m.state == .navigating)295    }296297    @Test("interruption pendant l'animation : la fiche est ouverte une seule fois, tout de suite")298    @MainActor299    func interruptNavigatesOnce() async {300        let door = DoorCenter(defaults: Self.scratch())301        var pushes = 0302        door.open(uid: "x:1", image: nil, from: CGRect(x: 20, y: 300, width: 286, height: 214), reduceMotion: false) { pushes += 1 }303        #expect(door.run != nil && door.run?.level == .signature)304        #expect(door.state == .opening)305        #expect(pushes == 0, "la fiche n'est pas encore poussée : la porte s'ouvre")306        door.interrupt()307        #expect(pushes == 1)308        #expect(door.run == nil && door.state == .closed)309        // la fin différée de l'animation annulée ne pousse pas la fiche une seconde fois310        try? await Task.sleep(for: .seconds(1.6))311        #expect(pushes == 1)312        #expect(door.opensThisSession == 1)313    }314315    @Test("animation menée à terme : la fiche est poussée une fois, puis le calque disparaît")316    @MainActor317    func completesAndNavigatesOnce() async {318        let door = DoorCenter(defaults: Self.scratch())319        var pushes = 0320        door.open(uid: "x:2", image: nil, from: CGRect(x: 20, y: 300, width: 286, height: 214), reduceMotion: false) { pushes += 1 }321        // la fiche est poussée au passage du seuil (760 ms), le calque disparaît à 1 180 ms322        try? await Task.sleep(for: .seconds(0.6))323        #expect(pushes == 0, "le battant pivote encore")324        try? await Task.sleep(for: .seconds(0.4))325        #expect(pushes == 1 && door.run != nil)326        try? await Task.sleep(for: .seconds(0.6))327        #expect(pushes == 1)328        #expect(door.run == nil && door.state == .closed)329    }330331    @Test("réglage « Aucune » et lien direct : navigation immédiate, sans calque")332    @MainActor333    func offNavigatesImmediately() {334        let door = DoorCenter(defaults: Self.scratch())335        door.preference = .off336        var pushes = 0337        door.open(uid: "x:3", image: nil, from: CGRect(x: 0, y: 0, width: 200, height: 150), reduceMotion: false) { pushes += 1 }338        #expect(pushes == 1 && door.run == nil)339        door.preference = .automatic340        door.open(uid: "x:4", image: nil, from: CGRect(x: 0, y: 0, width: 200, height: 150), origin: .directLink, reduceMotion: false) { pushes += 1 }341        #expect(pushes == 2 && door.run == nil)342    }343}344
Files main
Menu

Theme