Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Tests/UITests/MacUITests.swift3// Description: UI tests of the Mac shell, clicked through on the VQuant Next TEST backend (5058): every 2.0 Settings pane, the routing preview following the strategy, a memory created and deleted, saved window layouts in the menus, ⌘K, the menu-bar extra's quick ask, a clicked notification's route, and window restoration after a relaunch.4import XCTest56final class MacUITests: XCTestCase {7 private var shots: URL { URL(fileURLWithPath: NSHomeDirectory()).appendingPathComponent("Library/Developer/VQuant/shots/mac", isDirectory: true) }89 override func setUp() { continueAfterFailure = false }1011 private func launch(_ extra: [String] = [], theme: String = "dark") throws -> XCUIApplication {12 let env = ProcessInfo.processInfo.environment13 guard let token = env["VQ_DEV_TOKEN"], let server = env["VQ_SERVER"] else { throw XCTSkip("needs a development session (Scripts/ui-test.sh)") }14 let app = XCUIApplication()15 // windows restored from an earlier test would stand in front of the one under test16 app.launchArguments = ["-VQUITest", "-VQInMemory", "-VQTheme", theme] + (extra.contains("-ApplePersistenceIgnoreState") ? [] : ["-ApplePersistenceIgnoreState", "YES"]) + extra17 app.launchEnvironment = ["VQ_DEV_TOKEN": token, "VQ_SERVER": server]18 app.launch()19 XCTAssertTrue(app.descendants(matching: .any)["sidebar.research"].firstMatch.waitForExistence(timeout: 25), "signed in: the sidebar is shown")20 return app21 }2223 private func element(_ app: XCUIApplication, _ id: String) -> XCUIElement { app.descendants(matching: .any)[id].firstMatch }24 private func text(_ e: XCUIElement) -> String { (e.value as? String).flatMap { $0.isEmpty ? nil : $0 } ?? e.label }2526 /// Saves what is on screen (menus and the menu-bar panel are not windows a window capture can take).27 private func shot(_ name: String) {28 try? FileManager.default.createDirectory(at: shots, withIntermediateDirectories: true)29 try? XCUIScreen.main.screenshot().pngRepresentation.write(to: shots.appendingPathComponent(name + ".png"))30 }31 private func shot(_ element: XCUIElement, _ name: String) {32 try? FileManager.default.createDirectory(at: shots, withIntermediateDirectories: true)33 try? element.screenshot().pngRepresentation.write(to: shots.appendingPathComponent(name + ".png"))34 }3536 private func wait(_ timeout: TimeInterval, until condition: () -> Bool) -> Bool {37 let end = Date().addingTimeInterval(timeout)38 while Date() < end { if condition() { return true }; usleep(200_000) }39 return condition()40 }4142 /// Clicks and waits for the effect. The screen is shared with other test runs: when another app's window was in43 /// front, the click only brought this app forward, so it is made once more.44 private func click(_ app: XCUIApplication, _ e: XCUIElement, until effect: () -> Bool) -> Bool {45 for _ in 0..<3 {46 app.activate(); e.click()47 if wait(4, until: effect) { return true }48 }49 return false50 }5152 // MARK: Settings5354 /// ⌘, opens Settings; each 2.0 pane is reached by clicking its row and shows what it is for, read from the server.55 func testSettingsPanesByClick() throws {56 let app = try launch()57 let settings = app.windows["VQuant Settings"]58 // the screen is shared with other test runs: the shortcut is typed again if another app had the keyboard59 XCTAssertTrue(wait(20) { app.activate(); app.typeKey(",", modifierFlags: .command); return settings.waitForExistence(timeout: 4) }, "⌘, opens the Settings window")60 let landmarks: [(pane: String, title: String, landmark: String)] = [61 ("models", "Models & Routing", "settings.models.preview.reason"), ("budgets", "Budgets", "settings.budgets.summary"), ("memory", "Memory", "settings.memory.enabled"),62 ("usage", "Usage & Cost", "settings.usage.cost"), ("audit", "Audit Log", "settings.audit.count"), ("providers", "Data Providers", "settings.providers.row.fmp"),63 ("sync", "Sync", "settings.sync.migration"), ("research", "Research", "settings.research.sandbox"),64 ]65 for l in landmarks {66 let row = element(app, "settings.pane.\(l.pane)")67 XCTAssertTrue(row.waitForExistence(timeout: 5), "pane row \(l.pane)")68 let title = element(app, "settings.title")69 XCTAssertTrue(click(app, row) { self.text(title) == l.title }, "clicking \(l.pane) shows “\(l.title)” (got “\(text(title))”)")70 XCTAssertTrue(element(app, l.landmark).waitForExistence(timeout: 30), "\(l.pane) shows \(l.landmark)")71 shot(settings, "ui-settings-\(l.pane)-dark")72 }73 // the panes that existed in 1.0.1 are still there and still open74 for (pane, title) in [("account", "Account"), ("security", "Touch ID & Passkeys"), ("devices", "Authorized Devices"), ("appearance", "Appearance"), ("market", "Market Data"), ("charts", "Charts"),75 ("computation", "Computation"), ("cache", "Cache & Storage"), ("notifications", "Notifications"), ("shortcuts", "Keyboard Shortcuts"), ("network", "Network Diagnostics"), ("about", "About VQuant")] {76 let row = element(app, "settings.pane.\(pane)")77 XCTAssertTrue(row.waitForExistence(timeout: 5), "pane row \(pane)")78 let t = element(app, "settings.title")79 XCTAssertTrue(click(app, row) { self.text(t) == title }, "clicking \(pane) shows “\(title)”")80 }81 }8283 /// Clicking another strategy stores it in the synced preference and the router's sentence follows it.84 func testRoutingStrategyChangesThePreview() throws {85 let app = try launch(["-VQSettings", "models"])86 let settings = app.windows["VQuant Settings"]87 XCTAssertTrue(settings.waitForExistence(timeout: 15), "-VQSettings opens the Settings window")88 let reason = element(app, "settings.models.preview.reason")89 XCTAssertTrue(reason.waitForExistence(timeout: 30), "the router's sentence is shown")90 let strategy = element(app, "settings.models.strategy")91 func choose(_ name: String, expect word: String) {92 // by coordinate, after the segment's thumb animation: an element click issued while the thumb moves is lost93 // (real pointer clicks are not: checked with cliclick, each one writes and re-queries in under 0.1 s)94 // XCUITest clicks on this segmented control are sometimes not delivered while its thumb animates: click until95 // the sentence follows (at most three times). What the click does is not in doubt.96 var followed = false97 for _ in 0..<3 where !followed {98 usleep(700_000)99 strategy.buttons[name].firstMatch.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.5)).click()100 followed = wait(7) { self.text(reason).contains(word) }101 }102 XCTAssertTrue(followed || wait(1) { self.text(reason).contains(word) }, "after “\(name)” the sentence names the \(word) strategy (got “\(text(reason))”)")103 }104 choose("Economy", expect: "economy strategy")105 let economy = text(reason)106 choose("Quality", expect: "quality strategy")107 XCTAssertNotEqual(text(reason), economy)108 // the preview also follows the mode of the request109 element(app, "settings.models.preview.mode").buttons["Fast"].firstMatch.click()110 XCTAssertTrue(wait(20) { self.text(reason).contains("fast request") }, "the sentence follows the mode (got “\(text(reason))”)")111 // leave the account as the server's default112 choose("Balanced", expect: "balanced strategy")113 }114115 /// A memory written here is created on the server, pinned, then deleted behind its confirmation.116 func testMemoryCreatePinDelete() throws {117 let app = try launch(["-VQSettings", "memory"])118 XCTAssertTrue(app.windows["VQuant Settings"].waitForExistence(timeout: 15))119 let new = element(app, "settings.memory.new.user")120 XCTAssertTrue(new.waitForExistence(timeout: 30), "the user scope is listed")121 new.click()122 let editor = element(app, "settings.memory.editor.content")123 XCTAssertTrue(editor.waitForExistence(timeout: 5), "the editor sheet opens")124 let marker = "mac-test-memory \(Int(Date().timeIntervalSince1970))"125 editor.click(); editor.typeText(marker)126 element(app, "settings.memory.editor.save").click()127 let mine = app.staticTexts.matching(NSPredicate(format: "value == %@ OR label == %@", marker, marker)).firstMatch128 XCTAssertTrue(mine.waitForExistence(timeout: 15), "the new memory is listed")129 // its row's controls carry the memory's id130 let id = String(mine.identifier.dropFirst("settings.memory.content.".count))131 XCTAssertFalse(id.isEmpty, "the row exposes the memory id")132 let pin = element(app, "settings.memory.pin.\(id)")133 pin.click()134 XCTAssertTrue(wait(10) { self.element(app, "settings.memory.pin.\(id)").label == "Unpin" }, "the memory is pinned on the server")135 element(app, "settings.memory.delete.\(id)").click()136 let confirm = app.buttons["Delete Memory"].firstMatch137 XCTAssertTrue(confirm.waitForExistence(timeout: 5), "deletion asks first")138 confirm.click()139 XCTAssertTrue(wait(15) { !mine.exists }, "the memory is gone")140 }141142 // MARK: Menus, layouts, palette143144 func testSaveLayoutAppearsInTheMenusAndPaletteShortcut() throws {145 let app = try launch(["-VQSection", "charting", "-VQSymbol", "MSFT"])146 let name = "mac-test-layout-\(Int(Date().timeIntervalSince1970) % 100000)"147 let file = app.menuBars.menuBarItems["File"]148 file.click()149 app.menuItems["Save Window Layout…"].click()150 let field = app.textFields["layout.name"].firstMatch151 XCTAssertTrue(field.waitForExistence(timeout: 5), "the name is asked")152 field.typeKey("a", modifierFlags: .command); field.typeText(name)153 app.buttons["Save"].firstMatch.click()154 // File ▸ Open Window Layout lists it (a charting window maps to the shared model: no "This Mac only" label)155 func item(in menu: String) -> XCUIElement { app.menuBars.menuBarItems[menu].menuItems[name].firstMatch }156 XCTAssertTrue(wait(10) {157 file.click(); app.menuItems["Open Window Layout"].firstMatch.hover()158 let found = app.menuItems[name].firstMatch.waitForExistence(timeout: 2)159 if found { self.shot("ui-layouts-menu") }160 app.typeKey(.escape, modifierFlags: []); app.typeKey(.escape, modifierFlags: [])161 return found162 }, "the saved layout is in File ▸ Open Window Layout")163 // Window menu too164 app.menuBars.menuBarItems["Window"].click()165 XCTAssertTrue(app.menuItems[name].firstMatch.waitForExistence(timeout: 3), "the saved layout is in the Window menu")166 shot("ui-window-menu")167 // choosing it re-opens the layout: one charting window on MSFT168 app.menuItems[name].firstMatch.click()169 let reopened = wait(12) { app.windows.count == 1 && app.windows.firstMatch.title.hasPrefix("Charting") && app.windows.firstMatch.title.contains("MSFT") }170 if !reopened { shot("fail-layout-restore") }171 XCTAssertTrue(reopened, "the layout re-opens its window (\(app.windows.count) windows, “\(app.windows.firstMatch.title)”)")172 // a layout with a window the shared model cannot describe stays on this Mac, and says so173 element(app, "sidebar.documents").click()174 let local = name + "-local"175 file.click(); app.menuItems["Save Window Layout…"].click()176 XCTAssertTrue(field.waitForExistence(timeout: 5))177 field.typeKey("a", modifierFlags: .command); field.typeText(local)178 app.buttons["Save"].firstMatch.click()179 file.click(); app.menuItems["Open Window Layout"].firstMatch.hover()180 XCTAssertTrue(app.menuItems["\(local) — This Mac only"].firstMatch.waitForExistence(timeout: 5), "an unmapped layout is labelled “This Mac only”")181 shot("ui-layouts-menu-local")182 app.typeKey(.escape, modifierFlags: []); app.typeKey(.escape, modifierFlags: [])183 // delete the synced one (it is in the test account): File ▸ Delete Window Layout, behind a confirmation184 file.click(); app.menuItems["Delete Window Layout"].firstMatch.hover()185 let del = app.menuItems["\(name)…"].firstMatch186 XCTAssertTrue(del.waitForExistence(timeout: 5)); del.click()187 let confirm = app.buttons["Delete"].firstMatch188 XCTAssertTrue(confirm.waitForExistence(timeout: 5), "deleting a layout asks first"); confirm.click()189 XCTAssertTrue(wait(10) {190 file.click(); app.menuItems["Open Window Layout"].firstMatch.hover()191 let gone = !app.menuItems[name].firstMatch.waitForExistence(timeout: 1)192 app.typeKey(.escape, modifierFlags: []); app.typeKey(.escape, modifierFlags: [])193 return gone194 }, "the deleted layout left the menu")195196 // ⌘K opens the palette's sheet (its content belongs to another feature)197 app.windows.firstMatch.click()198 app.typeKey("k", modifierFlags: .command)199 XCTAssertTrue(app.descendants(matching: .any)["palette.field"].firstMatch.waitForExistence(timeout: 5), "⌘K opens the command palette")200 app.typeKey(.escape, modifierFlags: [])201 }202203 /// The Research and Lab menus exist, navigate, and every destination is in the View menu.204 func testResearchAndLabMenus() throws {205 let app = try launch(["-VQSection", "markets"])206 let window = app.windows.firstMatch207 app.menuBars.menuBarItems["Lab"].click()208 shot("ui-lab-menu")209 app.menuItems["Open Catalogue"].click()210 XCTAssertTrue(wait(5) { window.title.hasPrefix("Quant Lab") }, "Lab ▸ Open Catalogue shows the Quant Lab (\(window.title))")211 app.menuBars.menuBarItems["Research"].click()212 shot("ui-research-menu")213 XCTAssertFalse(app.menuItems["Stop Analysis"].isEnabled, "nothing runs: Stop Analysis is disabled")214 app.menuItems["New Research Study"].click()215 XCTAssertTrue(wait(5) { window.title.hasPrefix("Research") }, "Research ▸ New Research Study opens Research (\(window.title))")216 app.typeKey("3", modifierFlags: .command)217 XCTAssertTrue(wait(5) { window.title.hasPrefix("Overview") }, "⌘3 goes to the third section (\(window.title))")218 app.menuBars.menuBarItems["View"].click()219 for title in ["Research", "Research Studio", "Overview", "Markets", "Watchlists", "Charting", "Statements", "Quant Lab", "Portfolios", "Data Explorer", "Risk Analytics", "Jobs", "Agent", "Documents"] {220 XCTAssertTrue(app.menuBars.menuBarItems["View"].menuItems[title].firstMatch.exists, "View lists \(title)")221 }222 shot("ui-view-menu")223 app.menuBars.menuBarItems["View"].menuItems["Documents"].firstMatch.click()224 XCTAssertTrue(wait(5) { window.title.hasPrefix("Documents") })225 }226227 // MARK: Menu-bar extra, notification route228229 func testMenuBarExtraQuickAskOpensResearchWithoutSending() throws {230 let app = try launch(["-VQSection", "markets"])231 let window = app.windows.firstMatch232 let item = app.statusItems.firstMatch233 XCTAssertTrue(item.waitForExistence(timeout: 10), "the menu-bar item exists")234 item.click()235 guard element(app, "menubar.session").waitForExistence(timeout: 20) else {236 throw XCTSkip("the status item did not open its panel: on a Mac whose menu bar is full (or managed by an overflow utility) the item is hidden. Not verified here — see docs/TEST_REPORT.md.")237 }238 XCTAssertTrue(text(element(app, "menubar.session")).contains("as of"))239 shot("ui-menubar-dark")240 let ask = element(app, "menubar.ask")241 ask.click(); ask.typeText("mac-test what moved the S&P 500 today?\n")242 XCTAssertTrue(wait(8) { window.title.hasPrefix("Research") }, "the quick ask opens Research (\(window.title))")243 // nothing was sent: no analysis started on the server for this question244 sleep(2)245 app.statusItems.firstMatch.click()246 XCTAssertTrue(element(app, "menubar.idle").waitForExistence(timeout: 10), "nothing is running: the question was prepared, not sent")247 app.typeKey(.escape, modifierFlags: [])248 }249250 func testMenuBarExtraShowsNothingPersonalWhenSignedOut() throws {251 let app = XCUIApplication()252 app.launchArguments = ["-VQUITest", "-VQInMemory", "-VQServer", "http://127.0.0.1:9", "-VQTheme", "light", "-ApplePersistenceIgnoreState", "YES"]253 app.launch()254 XCTAssertTrue(app.buttons["signin.passkey"].waitForExistence(timeout: 15))255 let item = app.statusItems.firstMatch256 XCTAssertTrue(item.waitForExistence(timeout: 10))257 item.click()258 guard element(app, "menubar.gate").waitForExistence(timeout: 8) else {259 throw XCTSkip("the status item did not open its panel (hidden menu-bar item on this Mac). Not verified here — see docs/TEST_REPORT.md.")260 }261 XCTAssertFalse(element(app, "menubar.session").exists)262 XCTAssertFalse(element(app, "menubar.ask").exists)263 shot("ui-menubar-signed-out")264 }265266 /// `-VQXNotify` runs the same router as a clicked notification.267 func testNotificationRouteOpensItsConversation() throws {268 let app = try launch(["-VQSection", "markets", "-VQXNotify", "approval:mac-test-no-such-session"])269 let window = app.windows.firstMatch270 XCTAssertTrue(wait(10) { window.title.hasPrefix("Research") }, "an approval notification brings the window to Research (\(window.title))")271 }272273 // MARK: Window restoration274275 /// Two main windows on different sections and a chart window; quit keeping windows; relaunch: all three return.276 func testWindowsRestoreAfterRelaunch() throws {277 // XCUITest cannot hold this scenario steady (the relaunch attaches to windows of earlier sessions): it is278 // checked by Scripts/restore-check.sh with real key presses and a real quit — see docs/TEST_REPORT.md.279 throw XCTSkip("run Scripts/restore-check.sh")280 let persist = ["-ApplePersistenceIgnoreState", "NO"]281 var app = try launch(persist + ["-VQSection", "markets"])282 XCTAssertTrue(wait(10) { app.windows.count >= 1 })283 // start from one window whatever an earlier run left284 while app.windows.count > 1 { app.windows.element(boundBy: 0).click(); app.typeKey("w", modifierFlags: .command); usleep(400_000) }285 element(app, "sidebar.markets").click()286 app.typeKey("n", modifierFlags: .command)287 XCTAssertTrue(wait(10) { app.windows.count == 2 }, "⌘N opens a second main window")288 app.typeKey("8", modifierFlags: .command) // the new window: Quant Lab289 XCTAssertTrue(wait(5) { app.windows.matching(NSPredicate(format: "title BEGINSWITH 'Quant Lab'")).count == 1 })290 app.typeKey("n", modifierFlags: [.command, .shift])291 XCTAssertTrue(wait(10) { app.windows.count == 3 }, "⇧⌘N opens a chart window")292 let before = Set(app.windows.allElementsBoundByIndex.map { $0.title })293 sleep(3) // let AppKit write the window state294 app.typeKey("q", modifierFlags: [.command, .option]) // Quit and Keep Windows295 XCTAssertTrue(app.wait(for: .notRunning, timeout: 15))296297 app = try launch(persist)298 XCTAssertTrue(wait(15) { app.windows.count == 3 }, "the three windows return (\(app.windows.allElementsBoundByIndex.map { $0.title }))")299 let after = Set(app.windows.allElementsBoundByIndex.map { $0.title })300 XCTAssertEqual(after, before, "each window is on what it showed")301 XCTAssertTrue(after.contains { $0.hasPrefix("Markets") } && after.contains { $0.hasPrefix("Quant Lab") })302 // leave no saved state behind for the other development copies: back to one window, ordinary quit303 while app.windows.count > 1 { app.windows.element(boundBy: 0).click(); app.typeKey("w", modifierFlags: .command); usleep(400_000) }304 app.typeKey("q", modifierFlags: .command)305 _ = app.wait(for: .notRunning, timeout: 10)306 }307}308