Repository Public
1//2// SP Chat iOS3// File: SPChat-iOS/UITests/NextGenUITests.swift4// Description: UI tests of version 2.0 against the backend DEVELOPMENT server (Scripts/test-live.sh uitest): model5// sheet and OpenRouter turn with web search, Explorer, providers, contextual panels, command center,6// constellation, Arena — on iPhone and iPad. Screenshots are attached for docs/img/v2.7//8// Author: Simon-Pierre Boucher9// Contact: contact@spboucher.ai10//1112import XCTest1314class NextGenUITestCase: XCTestCase {15 var baseURL: String? { ProcessInfo.processInfo.environment["SPCHAT_BASE_URL"] }16 var token: String? { ProcessInfo.processInfo.environment["SPCHAT_DEBUG_SESSION"] }17 var demoConversation: String? { ProcessInfo.processInfo.environment["SPCHAT_DEMO_CONVERSATION"].flatMap { $0.isEmpty ? nil : $0 } }18 var theme: String { ProcessInfo.processInfo.environment["SPCHAT_UI_THEME"] ?? "dark" }19 var isPad: Bool { UIDevice.current.userInterfaceIdiom == .pad }2021 override func setUp() { continueAfterFailure = false }2223 func launch(link: String? = nil, model: String = "claude-haiku-5-5", extra: [String: String] = [:]) throws -> XCUIApplication {24 guard let baseURL, let token else { throw XCTSkip("serveur de développement non fourni (Scripts/test-live.sh uitest)") }25 let app = XCUIApplication()26 app.launchEnvironment["SPCHAT_UI_TESTS"] = "1"27 app.launchEnvironment["SPCHAT_UI_MODEL"] = model28 app.launchEnvironment["SPCHAT_UI_THEME"] = theme29 app.launchEnvironment["SPCHAT_BASE_URL"] = baseURL30 app.launchEnvironment["SPCHAT_DEBUG_SESSION"] = token31 if let link { app.launchEnvironment["SPCHAT_UI_LINK"] = link }32 for (k, v) in extra { app.launchEnvironment[k] = v }33 app.launchArguments += ["-AppleLanguages", "(fr)", "-AppleLocale", "fr_CA"]34 app.launch()35 return app36 }3738 func shot(_ app: XCUIApplication, _ name: String) {39 let a = XCTAttachment(screenshot: app.screenshot()); a.name = "v2-\(name)"; a.lifetime = .keepAlways; add(a)40 }4142 func element(_ app: XCUIApplication, _ id: String) -> XCUIElement { app.descendants(matching: .any).matching(identifier: id).firstMatch }4344 /// The simulator's first-run keyboard tip covers the bottom of the screen.45 func dismissKeyboardTip(_ app: XCUIApplication) {46 let c = app.buttons["Continue"]; if c.waitForExistence(timeout: 1) { c.tap() }47 let d = app.buttons["Continuer"]; if d.exists { d.tap() }48 }4950 func deleteOpenConversation(_ app: XCUIApplication) {51 app.buttons["chat.menu"].tap()52 app.buttons["Supprimer"].firstMatch.tap()53 let confirm = app.buttons["Supprimer"].firstMatch54 XCTAssertTrue(confirm.waitForExistence(timeout: 5)); confirm.tap()55 XCTAssertTrue(app.staticTexts["Que veux-tu explorer ?"].waitForExistence(timeout: 15))56 }57}5859final class NextGenUITests: NextGenUITestCase {60 func testModelSheetThenOpenRouterTurnWithWebSearchShowsSourcesAndFollowUps() throws {61 let app = try launch()62 let field = app.textFields["composer.field"]63 XCTAssertTrue(field.waitForExistence(timeout: 20))64 let chip = app.buttons["chat.modelButton"]65 XCTAssertTrue(chip.label.contains("Claude Haiku 5.5"), "nom exact du modèle dans la puce : \(chip.label)")6667 // model sheet: instant search over the whole catalogue, then Model Morphing in the composer chip68 chip.tap()69 XCTAssertTrue(app.navigationBars["Modèle"].waitForExistence(timeout: 5))70 XCTAssertTrue(app.descendants(matching: .any).matching(NSPredicate(format: "identifier BEGINSWITH %@", "model.or:")).firstMatch.waitForExistence(timeout: 15), "le catalogue OpenRouter est listé")71 shot(app, "model-sheet")72 let search = app.searchFields.matching(NSPredicate(format: "placeholderValue BEGINSWITH %@", "Rechercher parmi")).firstMatch // on iPad the sidebar has its own search field73 XCTAssertTrue(search.waitForExistence(timeout: 5))74 search.tap(); search.typeText("qwen3 mlx")75 dismissKeyboardTip(app)76 let local = element(app, "model.local:maclustr-qwen3")77 XCTAssertTrue(local.waitForExistence(timeout: 10), "le modèle de la route locale (MacLustr AI) est listé")78 XCTAssertTrue(app.staticTexts["LOCAL"].exists || local.label.contains("locale") || local.label.contains("local"), "badge de route LOCAL : \(local.label)")79 shot(app, "model-sheet-local")80 search.typeText(String(repeating: XCUIKeyboardKey.delete.rawValue, count: 9) + "gemini 4 neon")81 let neon = element(app, "model.or:google/gemini-4-neon")82 XCTAssertTrue(neon.waitForExistence(timeout: 10), "recherche instantanée dans le catalogue")83 XCTAssertTrue(neon.label.contains("OpenRouter") || app.staticTexts["OPENROUTER"].exists || neon.label.contains("catalogue"), "route et état du modèle visibles : \(neon.label)")84 shot(app, "model-sheet-search")85 neon.tap()86 XCTAssertTrue(field.waitForExistence(timeout: 5))87 XCTAssertTrue(chip.label.contains("Gemini 4 Neon"), "la puce du compositeur a changé de modèle : \(chip.label)")8889 // composer: web search toggle and tool picker90 let web = app.buttons["composer.web"]91 XCTAssertEqual(web.value as? String, "désactivée")92 web.tap()93 XCTAssertEqual(web.value as? String, "activée")94 app.buttons["composer.tools"].tap()95 XCTAssertTrue(app.buttons["Python"].waitForExistence(timeout: 5) || app.switches["Python"].exists || app.staticTexts["Python"].exists, "sélecteur d'outils du tour")96 shot(app, "composer-tools")97 app.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.12)).tap()9899 // quick commands100 field.tap(); field.typeText("/")101 dismissKeyboardTip(app)102 XCTAssertTrue(element(app, "composer.suggestion").waitForExistence(timeout: 5), "commandes rapides")103 shot(app, "composer-commands")104 field.typeText(XCUIKeyboardKey.delete.rawValue)105106 let marker = "uitest-\(Int(Date().timeIntervalSince1970) % 100_000)"107 field.typeText("Quelle est la capitale de l'Australie ? Réponds en trois phrases complètes d'au moins 400 caractères au total, avec la source. (\(marker))")108 shot(app, "composer-ready")109 app.buttons["composer.send"].tap()110 XCTAssertTrue(app.buttons["composer.stop"].waitForExistence(timeout: 10), "génération en cours")111 shot(app, "chat-streaming")112 XCTAssertTrue(element(app, "message.source").waitForExistence(timeout: 120), "cartes de sources de la recherche web")113 XCTAssertTrue(app.buttons["composer.mic"].waitForExistence(timeout: 120), "fin de génération")114 XCTAssertTrue(element(app, "message.tool").exists, "ligne d'appel d'outil")115 XCTAssertTrue(app.descendants(matching: .any).matching(NSPredicate(format: "label CONTAINS %@", "via OpenRouter")).firstMatch.exists, "route de la réponse")116 XCTAssertTrue(app.buttons["Copier la réponse"].waitForExistence(timeout: 10) && app.buttons["Régénérer"].exists && app.buttons["Lentilles de réponse"].exists)117 shot(app, "chat-sources")118 XCTAssertTrue(element(app, "message.followup").waitForExistence(timeout: 40), "suggestions de suite")119 app.swipeUp()120 shot(app, "chat-followups")121 XCTAssertEqual(app.webViews.count, 0, "aucune vue web")122 if demoConversation != nil { deleteOpenConversation(app) } // kept once: it becomes the demo conversation of the read-only tests123 }124125 func testExplorerSearchFiltersDetailAndCompare() throws {126 let app = try launch(link: "spchat://explorer")127 XCTAssertTrue(app.navigationBars["Explorateur de modèles"].waitForExistence(timeout: 20))128 let rows = app.descendants(matching: .any).matching(identifier: "explorer.row")129 XCTAssertTrue(rows.firstMatch.waitForExistence(timeout: 20))130 shot(app, "explorer")131 app.buttons["explorer.filters"].tap()132 XCTAssertTrue(app.navigationBars["Filtres"].waitForExistence(timeout: 5))133 shot(app, "explorer-filters")134 let free = app.switches["Gratuits"]135 var scrolls = 0136 while !(free.exists && free.isHittable) && scrolls < 8 { app.swipeUp(); scrolls += 1 }137 free.switches.firstMatch.exists ? free.switches.firstMatch.tap() : free.tap()138 app.navigationBars["Filtres"].buttons.matching(NSPredicate(format: "label BEGINSWITH %@", "Voir")).firstMatch.tap()139 XCTAssertTrue(app.staticTexts.matching(NSPredicate(format: "label CONTAINS %@", "gratuit")).firstMatch.waitForExistence(timeout: 5), "le filtre « gratuit » ne garde que des modèles à 0 $")140 XCTAssertTrue(app.buttons.matching(NSPredicate(format: "label BEGINSWITH %@", "Filtres ·")).firstMatch.exists, "le nombre de filtres actifs est affiché")141 shot(app, "explorer-free")142 app.buttons["explorer.filters"].tap()143 app.navigationBars["Filtres"].buttons["Effacer"].tap()144 app.navigationBars["Filtres"].buttons.matching(NSPredicate(format: "label BEGINSWITH %@", "Voir")).firstMatch.tap()145146 let search = app.searchFields["Nom, créateur ou identifiant"]147 XCTAssertTrue(search.waitForExistence(timeout: 5))148 search.tap(); search.typeText("gemini 4 neon")149 dismissKeyboardTip(app)150 XCTAssertTrue(rows.firstMatch.waitForExistence(timeout: 5))151 rows.firstMatch.tap()152 XCTAssertTrue(element(app, "model.detail").waitForExistence(timeout: 10))153 XCTAssertTrue(app.staticTexts["Catalogue OpenRouter · non testé par le banc"].waitForExistence(timeout: 5), "un modèle du catalogue est dit non testé")154 XCTAssertTrue(app.staticTexts["Points de terminaison en direct"].waitForExistence(timeout: 30), "Live Model Intelligence chargée")155 shot(app, "explorer-detail")156 element(app, "model.detail").swipeUp(); shot(app, "explorer-detail-2")157 app.navigationBars["Gemini 4 Neon"].buttons.element(boundBy: 0).tap()158159 // compare two models from the list160 search.tap()161 search.typeText(String(repeating: XCUIKeyboardKey.delete.rawValue, count: 20) + "claude opus")162 XCTAssertTrue(rows.element(boundBy: 1).waitForExistence(timeout: 5))163 let swipeAction = app.buttons.matching(NSPredicate(format: "label == %@ AND identifier != %@", "Comparer", "explorer.compare")).firstMatch164 for i in 0..<2 { rows.element(boundBy: i).swipeLeft(); XCTAssertTrue(swipeAction.waitForExistence(timeout: 3)); swipeAction.tap() }165 let compare = app.buttons["explorer.compare"]166 XCTAssertTrue(compare.waitForExistence(timeout: 5) && compare.isEnabled)167 compare.tap()168 XCTAssertTrue(app.navigationBars["Comparaison"].waitForExistence(timeout: 5))169 XCTAssertTrue(app.staticTexts["Prix entrée"].exists && app.staticTexts["Contexte"].exists)170 shot(app, "explorer-compare")171 }172173 func testProvidersTestAKeyAndRefuseAnInvalidOne() throws {174 let app = try launch(link: "spchat://settings")175 XCTAssertTrue(app.navigationBars["Réglages"].waitForExistence(timeout: 20))176 element(app, "settings.providers").tap()177 XCTAssertTrue(app.navigationBars["Fournisseurs"].waitForExistence(timeout: 5))178 let test = app.buttons["providers.test.openrouter"]179 XCTAssertTrue(test.waitForExistence(timeout: 15), "OpenRouter est configuré sur le serveur de dev")180 XCTAssertTrue(app.staticTexts["Anthropic"].exists)181 XCTAssertTrue(app.buttons["providers.test.anthropic"].exists, "« Tester » est offert aussi sur les fournisseurs directs")182 test.tap()183 XCTAssertTrue(element(app, "providers.result").waitForExistence(timeout: 30), "résultat du test de la clé")184 shot(app, "providers")185 // a key is typed in a secure field and refused by the provider: nothing changes, nothing is displayed186 var scrolls = 0187 let setKey = app.buttons["providers.setKey.tavily"]188 while !(setKey.exists && setKey.isHittable) && scrolls < 8 { app.swipeUp(); scrolls += 1 }189 XCTAssertTrue(app.staticTexts["Tavily"].exists, "Tavily (outil de recherche web) est listé")190 XCTAssertTrue(app.buttons["providers.setKey.local"].exists && app.staticTexts["Inférence locale MacLustr"].exists, "la route locale MacLustr AI est gérée comme OpenRouter")191 shot(app, "providers-local")192 setKey.tap()193 let secure = app.secureTextFields["providers.keyField"]194 XCTAssertTrue(secure.waitForExistence(timeout: 5), "la clé se saisit dans un champ sécurisé")195 secure.tap(); secure.typeText("tvly-uitest-invalid-0000000000")196 XCTAssertFalse(app.staticTexts.matching(NSPredicate(format: "label CONTAINS %@", "tvly-uitest")).firstMatch.exists, "la clé n'est jamais affichée")197 shot(app, "providers-key")198 app.buttons["providers.saveKey"].tap()199 XCTAssertTrue(app.staticTexts.matching(NSPredicate(format: "label CONTAINS[c] %@", "refusée")).firstMatch.waitForExistence(timeout: 40), "clé refusée par le fournisseur")200 XCTAssertFalse(app.staticTexts.matching(NSPredicate(format: "label CONTAINS %@", "tvly-uitest")).firstMatch.exists)201 }202203 func testPanelsOfAStoredConversation() throws {204 guard let id = demoConversation else { throw XCTSkip("aucune conversation de démonstration (lancer d'abord le test du tour OpenRouter)") }205 let app = try launch(link: "spchat://c/\(id)?panel=timeline")206 XCTAssertTrue(element(app, "timeline.event").waitForExistence(timeout: 25), "chronologie des événements enregistrés")207 shot(app, "timeline")208 app.buttons["timeline.play"].tap()209 shot(app, "timeline-replay")210 app.buttons["Fermer"].firstMatch.tap()211 XCTAssertTrue(element(app, "message.source").waitForExistence(timeout: 10), "la conversation enregistrée montre ses sources")212 shot(app, "chat-stored")213214 app.buttons["chat.menu"].tap(); app.buttons["Inspecteur de contexte"].tap()215 XCTAssertTrue(element(app, "context.gauge").waitForExistence(timeout: 15))216 XCTAssertTrue(app.staticTexts.matching(NSPredicate(format: "label CONTAINS[c] %@", "estimation")).firstMatch.exists, "l'estimation est étiquetée comme telle")217 shot(app, "context")218 app.buttons["Fermer"].firstMatch.tap()219220 app.buttons["chat.menu"].tap(); app.buttons["Branches"].tap()221 XCTAssertTrue(element(app, "branches.node").waitForExistence(timeout: 15))222 shot(app, "branches")223 app.buttons["Fermer"].firstMatch.tap()224225 app.buttons["Lentilles de réponse"].firstMatch.tap()226 XCTAssertTrue(element(app, "lens.kind.summary").waitForExistence(timeout: 15) && element(app, "lens.kind.verify").exists, "les cinq lentilles")227 shot(app, "lens")228 }229230 func testCommandCenterOpensConstellation() throws {231 let app = try launch()232 XCTAssertTrue(app.textFields["composer.field"].waitForExistence(timeout: 20))233 app.buttons["chat.commandCenter"].tap()234 let field = element(app, "command.field")235 XCTAssertTrue(field.waitForExistence(timeout: 5))236 dismissKeyboardTip(app)237 shot(app, "command-center")238 field.tap(); field.typeText("constell")239 let item = element(app, "command.item")240 XCTAssertTrue(item.waitForExistence(timeout: 5))241 shot(app, "command-center-search")242 item.tap()243 XCTAssertTrue(element(app, "constellation.canvas").waitForExistence(timeout: 20), "le centre de commandes ouvre la constellation")244 Thread.sleep(forTimeInterval: 2.5)245 shot(app, "constellation")246 XCTAssertEqual(app.webViews.count, 0)247 }248249 func testArenaComparesTwoRoutes() throws {250 let app = try launch(link: "spchat://arena?text=Explique%20en%20deux%20phrases%20la%20diff%C3%A9rence%20entre%20TCP%20et%20UDP.")251 let start = app.buttons["arena.start"]252 XCTAssertTrue(start.waitForExistence(timeout: 20))253 shot(app, "arena-intro")254 start.tap()255 XCTAssertTrue(element(app, "arena.column").waitForExistence(timeout: 20))256 shot(app, "arena-running")257 let prefer = app.buttons["arena.prefer"].firstMatch258 XCTAssertTrue(prefer.waitForExistence(timeout: 150), "au moins une réponse terminée avec ses mesures")259 XCTAssertTrue(app.descendants(matching: .any).matching(NSPredicate(format: "label CONTAINS %@ OR label CONTAINS %@", "facturé", "estimé")).firstMatch.waitForExistence(timeout: 60), "coût étiqueté facturé ou estimé")260 Thread.sleep(forTimeInterval: 4)261 shot(app, "arena-done")262 prefer.tap()263 shot(app, "arena-preferred")264 }265}266