SPB Git

Repository Public

ScreensUITests.swift 14.5 KB 339 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : SP ID & Card Vault7// File             : SPVaultUITests/ScreensUITests.swift8// Module           : Tests9// Repository       : https://git.spboucher.ai/sp-id-card-vault10// Project Website  : Not specified11// Created          : 2026-12-0212// Last Modified    : 2026-12-0213//14// Author           : Simon-Pierre Boucher15// Position         : Professor of Finance16// Institution      : Université du Québec en Outaouais (UQO)17// Organization     : SP Boucher18// Website          : https://spboucher.ai19// Git              : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development      : dev@spboucher.ai23// Support          : support@spboucher.ai24// Security/Admin   : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License          : None specified (no license file in this repository)33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import XCTest4243/// UI tests of the security and utility screens. They run against the Debug44/// test harness (`-uiTesting`: temporary vault, scripted key protector, isolated45/// defaults) and only ever use the fictitious demo cards.46@MainActor47final class ScreensUITests: XCTestCase {48    static let recovery = "correct horse battery staple 42"49    static let backupPassphrase = "orbit-lantern-quartz-meadow-71"5051    private var app: XCUIApplication!5253    override func setUp() async throws {54        continueAfterFailure = false55        app = XCUIApplication()56    }5758    private func launch(_ arguments: String...) {59        app.launchArguments = ["-uiTesting"] + arguments60        app.launch()61    }6263    private func element(_ identifier: String) -> XCUIElement {64        app.descendants(matching: .any).matching(identifier: identifier).firstMatch65    }6667    /// Saves a screenshot when `SPV_SHOT_DIR` is set (visual audit runs).68    private func shot(_ name: String) {69        guard let directory = ProcessInfo.processInfo.environment["SPV_SHOT_DIR"] else { return }70        try? FileManager.default.createDirectory(atPath: directory, withIntermediateDirectories: true)71        try? XCUIScreen.main.screenshot().pngRepresentation.write(to: URL(fileURLWithPath: directory).appendingPathComponent("\(name).png"))72    }7374    private func type(_ text: String, into identifier: String) {75        let field = app.secureTextFields[identifier].exists ? app.secureTextFields[identifier] : app.textFields[identifier]76        XCTAssertTrue(field.waitForExistence(timeout: 10), "missing field \(identifier)")77        field.tap()78        field.typeText(text)79    }8081    private func labelled(_ text: String) -> XCUIElement {82        app.descendants(matching: .any).matching(NSPredicate(format: "label CONTAINS %@", text)).firstMatch83    }8485    private func dismissKeyboard() {86        if app.keyboards.buttons["Return"].exists { app.keyboards.buttons["Return"].tap() }87        else if app.keyboards.buttons["return"].exists { app.keyboards.buttons["return"].tap() }88    }8990    private func assertUnlocked(file: StaticString = #filePath, line: UInt = #line) {91        XCTAssertTrue(app.tabBars.firstMatch.waitForExistence(timeout: 20), "the unlocked shell did not appear", file: file, line: line)92        XCTAssertFalse(app.buttons["lock.unlock"].exists, file: file, line: line)93        XCTAssertFalse(app.buttons["onboarding.primary"].exists, file: file, line: line)94    }9596    // MARK: Onboarding9798    func testOnboardingCreatesTheVault() {99        launch()100        let primary = app.buttons["onboarding.primary"]101        XCTAssertTrue(primary.waitForExistence(timeout: 20))102        shot("onboarding-1-welcome")103        primary.tap()104        XCTAssertTrue(labelled("AES-256-GCM").waitForExistence(timeout: 5))105        shot("onboarding-2-protection")106        primary.tap()107        XCTAssertTrue(element("onboarding.biometry.status").waitForExistence(timeout: 5))108        shot("onboarding-3-biometry")109        primary.tap()110111        // Recovery: the button stays disabled until a strong, confirmed passphrase.112        XCTAssertTrue(app.secureTextFields["onboarding.passphrase"].waitForExistence(timeout: 5))113        XCTAssertFalse(primary.isEnabled)114        type("short", into: "onboarding.passphrase")115        XCTAssertFalse(primary.isEnabled)   // too weak116        app.secureTextFields["onboarding.passphrase"].typeText(Self.recovery)117        XCTAssertFalse(primary.isEnabled)   // strong, but not confirmed yet118        type("short" + Self.recovery, into: "onboarding.confirmation")119        dismissKeyboard()120        shot("onboarding-4-recovery")121        XCTAssertTrue(primary.isEnabled)122        primary.tap()123124        let skip = app.buttons["onboarding.skipNotifications"]125        XCTAssertTrue(skip.waitForExistence(timeout: 5))126        shot("onboarding-5-notifications")127        skip.tap()128        assertUnlocked()129    }130131    func testOnboardingRestorePathAsksForABackupFile() {132        launch()133        let restore = app.buttons["onboarding.restore"]134        XCTAssertTrue(restore.waitForExistence(timeout: 20))135        restore.tap()136        let primary = app.buttons["onboarding.primary"]137        primary.tap()   // protection138        primary.tap()   // biometry139        XCTAssertTrue(app.buttons["onboarding.skipRecovery"].waitForExistence(timeout: 5))140        app.buttons["onboarding.skipRecovery"].tap()141        let confirm = app.alerts.buttons["Continue without"]142        XCTAssertTrue(confirm.waitForExistence(timeout: 5))143        shot("onboarding-skip-recovery-warning")144        confirm.tap()145        XCTAssertTrue(app.buttons["onboarding.skipNotifications"].waitForExistence(timeout: 5))146        app.buttons["onboarding.skipNotifications"].tap()147        XCTAssertTrue(labelled("Bring your cards back").waitForExistence(timeout: 5))148        XCTAssertEqual(primary.label, "Choose a backup file…")149        shot("onboarding-6-restore")150        // No vault may exist before a backup has been verified.151        XCTAssertFalse(app.tabBars.firstMatch.exists)152    }153154    // MARK: Lock screen155156    func testFaceIDUnlocksTheVault() {157        launch("-seedDemo", "-biometry", "lock")158        assertUnlocked()   // prompts automatically on appear; the scripted Face ID accepts159    }160161    func testRefusedFaceIDKeepsTheVaultLocked() {162        launch("-seedDemo", "-biometry", "denied")163        XCTAssertTrue(element("lock.message").waitForExistence(timeout: 20))164        XCTAssertTrue(labelled("Face ID did not recognise you").exists)165        let unlock = app.buttons["lock.unlock"]166        XCTAssertTrue(unlock.exists)167        XCTAssertFalse(app.tabBars.firstMatch.exists)168        shot("lock-refused")169        // No automatic retry after a refusal: still locked a moment later.170        sleep(2)171        XCTAssertTrue(unlock.exists)172        XCTAssertFalse(app.tabBars.firstMatch.exists)173        unlock.tap()174        assertUnlocked()175    }176177    func testBiometryLockoutIsExplained() {178        launch("-seedDemo", "-biometry", "lockedOut")179        XCTAssertTrue(labelled("Face ID is locked").waitForExistence(timeout: 20))180        app.buttons["lock.unlock"].tap()181        XCTAssertTrue(labelled("Face ID is locked").exists)182        XCTAssertFalse(app.tabBars.firstMatch.exists)183        shot("lock-lockedout")184    }185186    func testInvalidatedKeyIsRecoveredWithThePassphrase() {187        launch("-seedDemo", "-biometry", "invalidated")188        XCTAssertTrue(app.secureTextFields["lock.recovery.passphrase"].waitForExistence(timeout: 20))189        XCTAssertFalse(app.buttons["lock.unlock"].exists)   // Face ID cannot help: the key is gone190        shot("lock-recovery")191192        type("definitely not the passphrase", into: "lock.recovery.passphrase")193        app.buttons["lock.recovery.submit"].tap()194        XCTAssertTrue(labelled("Wrong passphrase").waitForExistence(timeout: 20))195        XCTAssertFalse(app.tabBars.firstMatch.exists)196        shot("lock-recovery-wrong")197198        let field = app.secureTextFields["lock.recovery.passphrase"]199        field.tap()200        field.typeText(String(repeating: XCUIKeyboardKey.delete.rawValue, count: 40))201        field.typeText(Self.recovery)202        app.buttons["lock.recovery.submit"].tap()203        assertUnlocked()204    }205206    // MARK: Backup207208    func testBackupExportReachesTheExporter() {209        launch("-seedDemo", "-screen", "backup")210        let export = app.buttons["backup.export"]211        XCTAssertTrue(export.waitForExistence(timeout: 20))212        XCTAssertTrue(labelled("Never").exists)213        shot("backup")214        export.tap()   // Face ID again (scripted: accepted), then the passphrase sheet215216        let submit = app.buttons["export.submit"]217        XCTAssertTrue(submit.waitForExistence(timeout: 10))218        XCTAssertFalse(submit.isEnabled)219        type(Self.backupPassphrase, into: "export.passphrase")220        type(Self.backupPassphrase, into: "export.confirmation")221        dismissKeyboard()222        shot("backup-export-sheet")223        XCTAssertTrue(submit.isEnabled)224        submit.tap()225226        let save = app.buttons["backup.save"]227        XCTAssertTrue(save.waitForExistence(timeout: 30))228        XCTAssertFalse(element("backup.last").label.contains("Never"))   // the last-backup date is now set229        shot("backup-ready")230        save.tap()231        // The system document exporter takes over.232        sleep(3)233        shot("backup-exporter")234        let pickerButtons = app.buttons.matching(NSPredicate(format: "label IN {'Save', 'Move', 'Cancel', 'Enregistrer', 'Annuler'}"))235        XCTAssertTrue(pickerButtons.firstMatch.waitForExistence(timeout: 15) || !save.isHittable, "the system exporter did not appear")236    }237238    // MARK: Settings239240    func testSettingsPersistAcrossLaunches() {241        launch("-seedDemo", "-screen", "settings")242        let privacy = app.switches["settings.privacyMode"]243        XCTAssertTrue(privacy.waitForExistence(timeout: 20))244        XCTAssertEqual(privacy.value as? String, "0")245        privacy.switches.firstMatch.exists ? privacy.switches.firstMatch.tap() : privacy.tap()246        XCTAssertEqual(privacy.value as? String, "1")247        app.buttons["settings.appearance.dark"].tap()248        shot("settings")249250        // New process, same vault. The scripted protector starts empty, which is251        // exactly a lost biometric key: recover, then check the preferences.252        app.terminate()253        launch("-keepVault", "-screen", "settings")254        type(Self.recovery, into: "lock.recovery.passphrase")255        app.buttons["lock.recovery.submit"].tap()256        let restored = app.switches["settings.privacyMode"]257        XCTAssertTrue(restored.waitForExistence(timeout: 20))258        XCTAssertEqual(restored.value as? String, "1")259        XCTAssertTrue(app.buttons["settings.appearance.dark"].isSelected)260    }261262    func testSettingsReachesItsSubScreens() {263        launch("-seedDemo", "-screen", "settings")264        XCTAssertTrue(app.buttons["settings.about"].waitForExistence(timeout: 20))265        app.swipeUp()266        app.buttons["settings.about"].tap()267        XCTAssertTrue(element("about.version").waitForExistence(timeout: 5))268        XCTAssertTrue(labelled("Simon-Pierre Boucher").exists)269        shot("about")270        app.buttons["nav.back"].tap()271        app.buttons["settings.categories"].tap()272        XCTAssertTrue(app.buttons["categories.add"].waitForExistence(timeout: 5))273        app.buttons["categories.add"].tap()274        type("Travel", into: "category.name")275        dismissKeyboard()276        shot("category-editor")277        app.buttons["category.save"].tap()278        XCTAssertTrue(labelled("Travel").waitForExistence(timeout: 5))279        shot("categories")280    }281282    // MARK: Search, expiry, security283284    func testSearchFindsCardsLocally() {285        launch("-seedDemo", "-screen", "search")286        let field = app.textFields["search.field"]287        XCTAssertTrue(field.waitForExistence(timeout: 20))288        shot("search-suggestions")289        field.tap()290        field.typeText("4242")291        XCTAssertTrue(app.buttons["search.result.Aurora Infinite"].waitForExistence(timeout: 5))292        XCTAssertFalse(app.buttons["search.result.Passport"].exists)293        shot("search-results")294        app.buttons["search.clear"].tap()295        field.typeText("zzzz")296        XCTAssertTrue(labelled("No card matches").waitForExistence(timeout: 5))297        shot("search-empty")298    }299300    func testExpiryDashboardCountsAndReminderChips() {301        launch("-seedDemo", "-screen", "expirations")302        XCTAssertTrue(element("expiry.count.expired").waitForExistence(timeout: 20))303        XCTAssertEqual(element("expiry.count.expired").label, "Expired: 1")304        XCTAssertEqual(element("expiry.count.soon").label, "Expiring soon: 2")305        XCTAssertEqual(element("expiry.count.valid").label, "Valid: 4")306        let bell = app.buttons["expiry.reminders.Driver's Licence"]307        XCTAssertTrue(bell.exists)308        XCTAssertEqual(bell.value as? String, "3 set")309        bell.tap()310        let chip = app.buttons["expiry.offset.90"]311        XCTAssertTrue(chip.waitForExistence(timeout: 5))312        XCTAssertFalse(chip.isSelected)313        chip.tap()314        XCTAssertTrue(chip.isSelected)315        XCTAssertEqual(bell.value as? String, "4 set")316        shot("expiry-reminders")317    }318319    func testSecurityCenterReportsRealState() {320        launch("-seedDemo", "-screen", "security")321        let headline = element("security.headline")322        XCTAssertTrue(headline.waitForExistence(timeout: 20))323        XCTAssertTrue(headline.label.hasPrefix("1 point needs attention"))   // demo vault has no backup yet324        XCTAssertTrue(labelled("No backup yet").exists)325        XCTAssertTrue(element("security.check.encryption").exists)326        shot("security")327        app.swipeUp()328        app.swipeUp()329        shot("security-2")330        app.swipeUp()331        app.swipeUp()332        XCTAssertTrue(app.buttons["security.recovery.change"].waitForExistence(timeout: 5))333        shot("security-3")334        app.buttons["security.recovery.change"].tap()335        XCTAssertTrue(app.buttons["recovery.submit"].waitForExistence(timeout: 10))336        shot("security-recovery-sheet")337    }338}339
Files main
Menu

Theme