Repository Public
1//2// SP Chat iOS3// File: SPChat-iOS/UITests/SPChatUITests.swift4// Description: UI tests driving the real app against the backend development server: sign-in screen, a full chat, navigation, iPad layout, rotation.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8//910import XCTest1112/// Run with Scripts/test-live.sh uitest — the runner receives SPCHAT_BASE_URL and SPCHAT_DEBUG_SESSION (a session of the13/// *_dev database). Without them only the signed-out tests run.14final class SPChatUITests: XCTestCase {15 private var baseURL: String? { ProcessInfo.processInfo.environment["SPCHAT_BASE_URL"] }16 private var token: String? { ProcessInfo.processInfo.environment["SPCHAT_DEBUG_SESSION"] }17 private var isPad: Bool { UIDevice.current.userInterfaceIdiom == .pad }1819 override func setUp() { continueAfterFailure = false }20 override func tearDown() {21 if XCUIDevice.shared.orientation != .portrait { XCUIDevice.shared.orientation = .portrait; Thread.sleep(forTimeInterval: 1.5) }22 }2324 private func launch(signedIn: Bool, theme: String? = nil) throws -> XCUIApplication {25 let app = XCUIApplication()26 app.launchEnvironment["SPCHAT_UI_TESTS"] = "1"27 app.launchEnvironment["SPCHAT_UI_MODEL"] = "claude-haiku-5-5"28 if let theme { app.launchEnvironment["SPCHAT_UI_THEME"] = theme }29 if let baseURL { app.launchEnvironment["SPCHAT_BASE_URL"] = baseURL }30 if signedIn {31 guard baseURL != nil, let token else { throw XCTSkip("serveur de développement non fourni (Scripts/test-live.sh uitest)") }32 app.launchEnvironment["SPCHAT_DEBUG_SESSION"] = token33 }34 app.launchArguments += ["-AppleLanguages", "(fr)", "-AppleLocale", "fr_CA"]35 app.launch()36 return app37 }3839 private func attach(_ app: XCUIApplication, _ name: String) {40 let a = XCTAttachment(screenshot: app.screenshot()); a.name = name; a.lifetime = .keepAlways; add(a)41 }4243 // MARK: signed out4445 func testSignInScreenOffersPasskeyAndEnrolment() throws {46 let app = try launch(signedIn: false)47 let passkey = app.buttons["signin.passkey"]48 XCTAssertTrue(passkey.waitForExistence(timeout: 10))49 XCTAssertTrue(passkey.isHittable)50 XCTAssertFalse(app.textFields["composer.field"].exists, "aucun contenu privé sans session")51 attach(app, "connexion")52 app.buttons["signin.enrol"].tap()53 let code = app.textFields["enrol.code"]54 XCTAssertTrue(code.waitForExistence(timeout: 5))55 XCTAssertFalse(app.buttons["Continuer"].isEnabled, "un code trop court ne peut pas être soumis")56 code.tap(); code.typeText("ABCD")57 XCTAssertFalse(app.buttons["Continuer"].isEnabled)58 app.buttons["Annuler"].tap()59 XCTAssertTrue(passkey.waitForExistence(timeout: 5))60 }6162 // MARK: signed in6364 func testSendMessageStreamsAnAnswerThenManageTheConversation() throws {65 let app = try launch(signedIn: true)66 let field = app.textFields["composer.field"]67 XCTAssertTrue(field.waitForExistence(timeout: 15), "la session de test ouvre directement une nouvelle conversation")68 XCTAssertTrue(app.buttons["composer.mic"].exists, "champ vide : dictée proposée")69 XCTAssertTrue(app.buttons["chat.modelButton"].label.contains("Haiku"))70 XCTAssertTrue(app.buttons["composer.web"].exists && app.buttons["composer.tools"].exists && app.buttons["composer.options"].exists, "rangée de contrôles du compositeur")7172 let marker = "uitest-\(Int(Date().timeIntervalSince1970) % 100_000)"73 field.tap()74 field.typeText("Réponds exactement : pong \(marker)")75 let send = app.buttons["composer.send"]76 XCTAssertTrue(send.waitForExistence(timeout: 3) && send.isEnabled)77 send.tap()78 // the answer arrives by streaming; the stop button is replaced by the microphone when the turn is over79 let answer = app.staticTexts.containing(NSPredicate(format: "label CONTAINS[c] %@", "pong \(marker)")).element(boundBy: 1)80 XCTAssertTrue(answer.waitForExistence(timeout: 60), "la réponse du modèle s'affiche")81 XCTAssertTrue(app.buttons["composer.mic"].waitForExistence(timeout: 30), "fin de génération")82 XCTAssertTrue(app.buttons["Copier la réponse"].waitForExistence(timeout: 10))83 XCTAssertTrue(app.buttons["Régénérer"].exists)84 attach(app, "conversation")8586 // model picker87 app.buttons["chat.modelButton"].tap()88 XCTAssertTrue(app.navigationBars["Modèle"].waitForExistence(timeout: 5))89 XCTAssertTrue(app.descendants(matching: .any).matching(NSPredicate(format: "identifier BEGINSWITH %@", "model.")).firstMatch.waitForExistence(timeout: 10), "les modèles vérifiés sont listés")90 attach(app, "modeles")91 app.navigationBars["Modèle"].buttons["OK"].tap()9293 // the conversation is in the list (stack on iPhone, sidebar on iPad)94 if !isPad { app.navigationBars.buttons.element(boundBy: 0).tap() }95 let row = app.staticTexts.containing(NSPredicate(format: "label CONTAINS[c] %@", "pong")).firstMatch96 XCTAssertTrue(app.buttons["list.new"].waitForExistence(timeout: 10))97 attach(app, "liste")9899 // global search finds the message on the server100 app.buttons["list.search"].tap()101 let search = app.searchFields.firstMatch102 XCTAssertTrue(search.waitForExistence(timeout: 5))103 search.tap(); search.typeText(marker)104 XCTAssertTrue(app.buttons["search.hit"].firstMatch.waitForExistence(timeout: 20), "la recherche serveur retrouve le message")105 app.buttons["search.hit"].firstMatch.tap()106 XCTAssertTrue(app.textFields["composer.field"].waitForExistence(timeout: 10))107 XCTAssertTrue(app.staticTexts.containing(NSPredicate(format: "label CONTAINS[c] %@", marker)).firstMatch.waitForExistence(timeout: 10))108 _ = row109110 // delete it from the conversation menu: cleans the dev database and proves the destructive path asks first111 app.buttons["chat.menu"].tap()112 app.buttons["Supprimer"].firstMatch.tap()113 let confirm = app.buttons["Supprimer"].firstMatch114 XCTAssertTrue(confirm.waitForExistence(timeout: 5))115 confirm.tap()116 XCTAssertTrue(app.staticTexts["Que veux-tu explorer ?"].waitForExistence(timeout: 15), "après suppression : nouvelle conversation vide")117 }118119 func testRichAnswerRendersNatively() throws {120 let app = try launch(signedIn: true)121 let field = app.textFields["composer.field"]122 XCTAssertTrue(field.waitForExistence(timeout: 15))123 field.tap()124 field.typeText("Sans introduction, donne dans cet ordre : un titre de niveau 2 « Démo », une liste de 2 puces, un tableau Markdown 3 colonnes × 2 lignes (Ville, Pays, Population), un bloc de code Swift de 4 lignes, puis la formule de la somme des n premiers entiers en LaTeX entre $$ et une phrase contenant la formule en ligne $e^{i\\pi}+1=0$.")125 app.buttons["composer.send"].tap()126 XCTAssertTrue(app.buttons["composer.mic"].waitForExistence(timeout: 90), "fin de génération")127 XCTAssertTrue(app.buttons["Copier le code"].waitForExistence(timeout: 5), "bloc de code natif avec bouton de copie")128 XCTAssertTrue(app.staticTexts["swift"].exists, "étiquette de langage")129 XCTAssertTrue(app.descendants(matching: .any).matching(NSPredicate(format: "label BEGINSWITH %@", "Tableau, 2 lignes")).firstMatch.exists, "tableau natif")130 XCTAssertTrue(app.descendants(matching: .any).matching(NSPredicate(format: "label BEGINSWITH %@", "Équation")).firstMatch.exists, "équation composée nativement")131 XCTAssertEqual(app.webViews.count, 0, "aucune vue web dans l'interface")132 attach(app, "riche-bas")133 app.swipeDown()134 attach(app, "riche-haut")135 app.buttons["chat.menu"].tap()136 app.buttons["Supprimer"].firstMatch.tap()137 app.buttons["Supprimer"].firstMatch.tap()138 XCTAssertTrue(app.staticTexts["Que veux-tu explorer ?"].waitForExistence(timeout: 15))139 }140141 func testSettingsRotationAndDarkTheme() throws {142 let app = try launch(signedIn: true, theme: "dark")143 XCTAssertTrue(app.textFields["composer.field"].waitForExistence(timeout: 15))144 attach(app, "sombre")145 XCUIDevice.shared.orientation = .landscapeLeft146 Thread.sleep(forTimeInterval: 1.5)147 XCTAssertTrue(app.textFields["composer.field"].waitForExistence(timeout: 5))148 XCTAssertTrue(app.textFields["composer.field"].isHittable, "le compositeur reste utilisable en paysage")149 attach(app, "paysage")150 XCUIDevice.shared.orientation = .portrait151 Thread.sleep(forTimeInterval: 1.5)152153 if !isPad { app.navigationBars.buttons.element(boundBy: 0).tap() }154 XCTAssertTrue(app.buttons["list.settings"].waitForExistence(timeout: 10))155 app.buttons["list.settings"].tap()156 XCTAssertTrue(app.navigationBars["Réglages"].waitForExistence(timeout: 5))157 XCTAssertTrue(app.staticTexts["Sécurité et clés d’accès"].exists)158 attach(app, "reglages")159 app.staticTexts["Sécurité et clés d’accès"].tap()160 XCTAssertTrue(app.staticTexts["Clés d’accès"].waitForExistence(timeout: 10) || app.staticTexts["CLÉS D’ACCÈS"].waitForExistence(timeout: 2))161 // the development account holds many test passkeys: the sessions section may start below the fold162 var scrolls = 0163 while !app.staticTexts["ici"].waitForExistence(timeout: 2) && scrolls < 10 { app.swipeUp(); scrolls += 1 }164 XCTAssertTrue(app.staticTexts["ici"].exists, "la session de cet appareil est listée")165 app.navigationBars["Sécurité"].buttons.firstMatch.tap()166 XCTAssertTrue(app.switches.firstMatch.waitForExistence(timeout: 5))167 var swipes = 0168 // the settings form is the front-most scroll container (on iPad the sidebar list is another one, behind the sheet)169 let form = app.collectionViews.element(boundBy: app.collectionViews.count - 1)170 let signOut = app.buttons.matching(NSPredicate(format: "identifier == %@ OR label == %@", "settings.signout", "Se déconnecter")).firstMatch171 if !isPad { // on iPad the form sheet shares the screen with the sidebar list; the scroll check runs on iPhone172 while !signOut.exists && swipes < 8 { form.swipeUp(); swipes += 1 }173 XCTAssertTrue(signOut.exists, "déconnexion accessible au bas des réglages")174 }175 app.buttons["settings.done"].tap()176 }177178 func testIPadShowsSidebarAndConversationSideBySide() throws {179 guard isPad else { throw XCTSkip("iPad seulement") }180 let app = try launch(signedIn: true)181 XCUIDevice.shared.orientation = .landscapeLeft182 Thread.sleep(forTimeInterval: 1.5)183 XCTAssertTrue(app.textFields["composer.field"].waitForExistence(timeout: 15))184 XCTAssertTrue(app.buttons["list.new"].waitForExistence(timeout: 10), "barre latérale visible en même temps que la conversation")185 XCTAssertTrue(app.buttons["list.new"].isHittable && app.textFields["composer.field"].isHittable)186 attach(app, "ipad")187 // a new conversation from the sidebar keeps both columns on screen188 // (the ⌘N / ⌘⇧F / ⌘. commands are declared in SPChatApp.commands; XCUITest cannot synthesise hardware key189 // commands on the iPad simulator, so they are checked by hand — see docs/IMPLEMENTATION_STATUS.md)190 app.buttons["list.new"].tap()191 XCTAssertTrue(app.staticTexts["Que veux-tu explorer ?"].waitForExistence(timeout: 5))192 let field = app.textFields["composer.field"]193 field.tap(); field.typeText("saisie iPad")194 XCTAssertEqual(field.value as? String, "saisie iPad")195 XCTAssertTrue(app.buttons["list.new"].isHittable)196 }197}198