Repository Public
1// SP Notes iOS2// File: UITests/HostedSectionsLiveUITests.swift3// Description: Opens on screen every hosted screen of SPNotesFeatures and every note-menu sheet, signed in against the4// INTEGRATION server (never production): `TEST_RUNNER_SPNOTES_LIVE_URL` + `TEST_RUNNER_SPNOTES_LIVE_TOKEN`5// (`Scripts/test-live.sh ui`). Skipped without them. The app seeds a few rows itself (`SPNOTES_LIVE_SEED=1`,6// App/LiveSeed.swift). Nothing paid is triggered: no question is sent, no synopsis, no summary, no transcription.7//8// Author: Simon-Pierre Boucher9// Contact: contact@spboucher.ai10import XCTest1112final class HostedSectionsLiveUITests: XCTestCase {13 private var app: XCUIApplication!14 private var appearance = "light"15 private var isPad: Bool { UIDevice.current.userInterfaceIdiom == .pad }1617 static let sections = ["brief", "ask", "search-server", "databases", "canvas", "graph", "constellation", "timeline", "health", "research"]18 static let noteA = "5eed1111-0000-4000-8000-000000000001"1920 override func setUp() { continueAfterFailure = true }2122 private func launch(appearance: String) throws {23 let environment = ProcessInfo.processInfo.environment24 guard let url = environment["SPNOTES_LIVE_URL"], let token = environment["SPNOTES_LIVE_TOKEN"], !url.isEmpty, token.count >= 40 else {25 throw XCTSkip("no integration server given (Scripts/test-live.sh ui)")26 }27 self.appearance = appearance28 if isPad { XCUIDevice.shared.orientation = .landscapeLeft }29 app = XCUIApplication()30 app.launchEnvironment = ["SPNOTES_BASE_URL": url, "SPNOTES_TOKEN": token, "SPNOTES_DATA_DIR": "temp", "SPNOTES_LIVE_SEED": "1"]31 app.launchArguments = ["-appearance", appearance, "-lock.enabled", "NO", "-AppleLanguages", "(fr)", "-AppleLocale", "fr_CA"]32 app.launch()33 let state = element("live-seed-state")34 XCTAssertTrue(state.waitForExistence(timeout: 120), "the seed finished")35 XCTAssertEqual(state.label, "done", "seed problems: \(state.label)")36 if isPad, element("note-window-close").exists { element("note-window-close").tap() }37 }3839 private func element(_ id: String) -> XCUIElement { app.descendants(matching: .any).matching(identifier: id).firstMatch }4041 private func shot(_ name: String) {42 let attachment = XCTAttachment(screenshot: XCUIScreen.main.screenshot())43 attachment.name = "hosted-\(name)-\(isPad ? "ipad" : "iphone")-\(appearance)"44 attachment.lifetime = .keepAlways45 add(attachment)46 }4748 /// Opens a hosted section from « Plus » (iPhone) or the sidebar (iPad). False when its entry is not listed.49 @discardableResult50 private func open(section id: String) -> Bool {51 let entry: XCUIElement52 if isPad {53 entry = element("sidebar-hosted-\(id)")54 for _ in 0..<5 where !(entry.exists && entry.isHittable) { element("sidebar-all").swipeUp() }55 } else {56 let plus = app.tabBars.buttons["Plus"]57 XCTAssertTrue(plus.waitForExistence(timeout: 15))58 plus.tap()59 // back to the root of the tab60 for _ in 0..<3 where !element("more-tasks").exists && app.navigationBars.buttons.firstMatch.exists { app.navigationBars.buttons.element(boundBy: 0).tap() }61 entry = element("more-hosted-\(id)")62 for _ in 0..<8 where !(entry.exists && entry.isHittable) { app.swipeUp(velocity: .slow) }63 }64 guard entry.waitForExistence(timeout: 5) else { XCTFail("the section \(id) is not listed"); return false }65 entry.tap()66 XCTAssertTrue(element("hosted-\(id)").waitForExistence(timeout: 15), "the screen of \(id) is on screen")67 XCTAssertFalse(app.staticTexts["Section indisponible"].exists)68 // content arrives from the server: give it a moment, then something readable must be there69 sleep(4)70 XCTAssertGreaterThan(app.staticTexts.count, 1, "\(id) shows text")71 XCTAssertEqual(app.state, .runningForeground, "\(id) did not crash the app")72 return true73 }7475 private func run(sections: [String]) throws {76 for id in sections {77 guard open(section: id) else { continue }78 shot(id)79 switch id {80 case "databases":81 let database = app.descendants(matching: .any).matching(NSPredicate(format: "identifier BEGINSWITH 'database-'")).firstMatch82 XCTAssertTrue(database.waitForExistence(timeout: 10), "the seeded database is listed")83 if database.exists {84 database.tap()85 let row = app.descendants(matching: .any).matching(NSPredicate(format: "label CONTAINS %@ OR value CONTAINS %@", "12 rue Laurel", "12 rue Laurel")).firstMatch86 XCTAssertTrue(row.waitForExistence(timeout: 20), "the rows of the database are shown")87 shot("database")88 if row.exists, row.isHittable {89 row.tap()90 sleep(3)91 shot("database-row")92 XCTAssertEqual(app.state, .runningForeground)93 // a row editor is a sheet on compact: close it whatever its close control is called94 for label in ["Fermer", "OK", "Terminé", "Annuler"] where app.buttons[label].firstMatch.exists && app.buttons[label].firstMatch.isHittable { app.buttons[label].firstMatch.tap(); break }95 }96 if element("databases-back").exists, element("databases-back").isHittable { element("databases-back").tap() }97 }98 case "canvas":99 let canvas = element("canvas-5eed1111-0000-4000-8000-000000000004")100 XCTAssertTrue(canvas.waitForExistence(timeout: 10), "the seeded canvas is listed")101 if canvas.exists {102 canvas.tap()103 sleep(4)104 XCTAssertEqual(app.state, .runningForeground)105 shot("canvas-open")106 if element("canvas-back").exists, element("canvas-back").isHittable { element("canvas-back").tap() }107 }108 case "ask":109 // the model picker, without sending anything (nothing paid)110 let models = app.buttons.matching(NSPredicate(format: "label CONTAINS[c] 'modèle' OR label CONTAINS[c] 'Auto'")).firstMatch111 if models.waitForExistence(timeout: 5), models.isHittable {112 models.tap()113 sleep(3)114 shot("model-picker")115 XCTAssertEqual(app.state, .runningForeground)116 for label in ["Fermer", "OK", "Terminé", "Annuler"] where app.buttons[label].firstMatch.exists && app.buttons[label].firstMatch.isHittable { app.buttons[label].firstMatch.tap(); break }117 if !element("hosted-ask").isHittable { app.swipeDown(velocity: .fast) }118 } else {119 XCTFail("the model picker control was not found on the Ask screen")120 }121 default: break122 }123 }124 }125126 func testEveryHostedSectionOpensLight() throws {127 try launch(appearance: "light")128 try run(sections: Self.sections)129 }130131 func testASubsetOpensDark() throws {132 try launch(appearance: "dark")133 try run(sections: ["brief", "graph", "timeline", "health"])134 }135136 func testNoteMenuSheets() throws {137 try launch(appearance: "light")138 XCUIDevice.shared.system.open(URL(string: "spnotes://note/\(Self.noteA)")!)139 let menu = element("note-menu")140 XCTAssertTrue(menu.waitForExistence(timeout: 20), "the seeded note opened")141 for (entry, name) in [("menu-outline", "outline"), ("menu-timeline", "note-timeline"), ("menu-ask", "note-ask")] {142 menu.tap()143 let button = element(entry)144 XCTAssertTrue(button.waitForExistence(timeout: 5), "\(entry) is offered (the server has the feature)")145 guard button.exists else { app.tap(); continue }146 button.tap()147 XCTAssertTrue(element("note-feature-sheet").waitForExistence(timeout: 10), "\(name) sheet")148 sleep(4)149 XCTAssertEqual(app.state, .runningForeground, "\(name) did not crash")150 shot(name)151 if name == "note-timeline" {152 // the time machine of the page (reads stored versions: nothing paid)153 let machine = app.buttons.matching(NSPredicate(format: "label CONTAINS[c] 'temps' OR label CONTAINS[c] 'machine'")).firstMatch154 if machine.exists, machine.isHittable { machine.tap(); sleep(3); shot("time-machine"); XCTAssertEqual(app.state, .runningForeground) }155 else { XCTFail("the time machine control was not found on the page timeline") }156 }157 let close = element("note-feature-close")158 if !(close.exists && close.isHittable) { app.swipeDown(velocity: .fast) }159 XCTAssertTrue(close.waitForExistence(timeout: 5))160 close.tap()161 XCTAssertTrue(menu.waitForExistence(timeout: 10))162 }163 }164}165