Repository Public License
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : ORIX Monitor9// File : UITests/ORIXMonitorUITests.swift10// Description : End-to-end interface tests on an isolated instance:11// launch, every page, dashboard editing, processes, alerts,12// reports, settings, menu bar, appearance, window resizing13// and persistence across relaunch.14// Module : UITests15// Repository : https://git.spboucher.ai/orix-monitor16// Created : 2026-11-2117// Last Modified : 2026-11-2118//19// =============================================================================20// AUTHOR & PROFESSIONAL AFFILIATION21// =============================================================================22//23// Author : Simon-Pierre Boucher24// Position : Professor of Finance25// Institution : Université du Québec en Outaouais (UQO)26// Department : Département des sciences administratives27// Organization : SP Boucher28//29// Website : https://spboucher.ai30// Git : https://git.spboucher.ai31//32// =============================================================================33// OFFICIAL CONTACT DIRECTORY34// =============================================================================35//36// General : info@spboucher.ai37// Professional : contact@spboucher.ai38// Direct : simon@spboucher.ai39// Research : research@spboucher.ai40// Development : dev@spboucher.ai41// Support : support@spboucher.ai42// Security/Admin : admin@spboucher.ai43// Notifications : notifications@spboucher.ai44//45// Preferred Contact: support@spboucher.ai46//47// =============================================================================48// COPYRIGHT & INTELLECTUAL PROPERTY49// =============================================================================50//51// Copyright (c) 202652// Simon-Pierre Boucher53//54// All rights reserved where applicable.55//56// This file is governed by the applicable project license,57// copyright notices, institutional agreements and relevant law.58//59// Unless expressly authorized by the applicable license or60// rights holder, copying, redistribution, sublicensing,61// publication and commercial exploitation are not permitted.62//63// Existing third-party rights and notices remain unaffected.64//65// =============================================================================66// CONFIDENTIALITY & SECURITY67// =============================================================================68//69// Classification : Public70// Distribution : Public repository; use is governed by the project license71// Security Contact : admin@spboucher.ai72//73// Access to this source does not independently grant permission74// to redistribute or disclose confidential materials.75//76// Do not embed credentials, private keys, access tokens,77// certificates or confidential personal information.78//79// =============================================================================80// LICENSE & MAINTENANCE81// =============================================================================82//83// License : Proprietary, all rights reserved (see license file)84// License File : LICENSE85// Maintainer : Simon-Pierre Boucher86// Technical Contact: dev@spboucher.ai87//88// =============================================================================89// END OF SP BOUCHER SOURCE HEADER90// =============================================================================9192import XCTest9394@MainActor95final class ORIXMonitorUITests: XCTestCase {96 private var dataDirectory: URL!9798 override func setUp() async throws {99 continueAfterFailure = false100 dataDirectory = FileManager.default.temporaryDirectory.appendingPathComponent("orix-uitest-\(UUID().uuidString)")101 }102103 override func tearDown() async throws {104 try? FileManager.default.removeItem(at: dataDirectory)105 }106107 @discardableResult108 private func launch(section: String = "overview", appearance: String = "dark", extra: [String] = []) -> XCUIApplication {109 let app = XCUIApplication()110 app.launchArguments = ["-ORIXUITest", "YES", "-ORIXDataDir", dataDirectory.path, "-ORIXSection", section, "-ORIXAppearance", appearance,111 "-ORIXInterval", "1", "-ORIXPrivacy", "YES", "-ApplePersistenceIgnoreState", "YES", "-AppleLanguages", "(en)", "-AppleLocale", "en_US"] + extra112 app.launch()113 XCTAssertTrue(app.windows.firstMatch.waitForExistence(timeout: 15), "main window did not appear")114 return app115 }116117 private func page(_ app: XCUIApplication, _ name: String) -> XCUIElement {118 app.descendants(matching: .any).matching(identifier: "page.\(name)").firstMatch119 }120121 private func open(_ app: XCUIApplication, _ section: String) {122 let row = app.descendants(matching: .any).matching(identifier: "sidebar.\(section)").firstMatch123 XCTAssertTrue(row.waitForExistence(timeout: 5), "sidebar row \(section) missing")124 row.click()125 XCTAssertTrue(page(app, section).waitForExistence(timeout: 5), "page \(section) did not open")126 }127128 private func text(_ app: XCUIApplication, containing fragment: String) -> XCUIElement {129 // Page headers are combined accessibility elements, not plain static texts: search every element.130 app.descendants(matching: .any).matching(NSPredicate(format: "value CONTAINS %@ OR label CONTAINS %@", fragment, fragment)).firstMatch131 }132133 /// Quits through the menu command so the app runs its normal shutdown (which flushes buffered samples).134 private func quit(_ app: XCUIApplication) {135 app.activate()136 app.menuBars.menuBarItems["ORIX Monitor"].click()137 let item = app.menuItems.matching(NSPredicate(format: "title BEGINSWITH %@", "Quit")).firstMatch138 XCTAssertTrue(item.waitForExistence(timeout: 5), "no Quit command")139 item.click()140 XCTAssertTrue(app.wait(for: .notRunning, timeout: 15), "app did not quit")141 }142143 func testFirstLaunchShowsDashboardAndStartsSampling() {144 let app = launch()145 XCTAssertTrue(page(app, "overview").waitForExistence(timeout: 5))146 XCTAssertTrue(text(app, containing: "Sampling every").waitForExistence(timeout: 10), "sampler never reported an interval")147 for widget in ["cpu", "memory", "network", "diskActivity", "system"] {148 XCTAssertTrue(app.descendants(matching: .any).matching(identifier: "widget.\(widget)").firstMatch.exists, "widget \(widget) missing")149 }150 // A real reading reaches the sidebar badge within a few samples.151 let badge = app.staticTexts.matching(NSPredicate(format: "value MATCHES %@", "[0-9]+%")).firstMatch152 XCTAssertTrue(badge.waitForExistence(timeout: 10), "no CPU percentage appeared")153 }154155 func testEveryPageOpens() {156 let app = launch()157 let expectations = ["cpu": "100 % = every core busy", "gpu": "About this figure", "memory": "Swap and paging", "disk": "Block devices",158 "network": "Latency test", "processes": "shown of", "power": "Energy by process", "thermal": "Fan speed",159 "history": "How history is kept", "insights": "What is checked", "alerts": "Rules", "reports": "Export"]160 for section in ["cpu", "gpu", "memory", "disk", "network", "processes", "power", "thermal", "history", "insights", "alerts", "reports", "overview"] {161 open(app, section)162 if let fragment = expectations[section] {163 let found = app.descendants(matching: .any).matching(NSPredicate(format: "label CONTAINS[c] %@ OR value CONTAINS[c] %@ OR title CONTAINS[c] %@", fragment, fragment, fragment)).firstMatch164 XCTAssertTrue(found.waitForExistence(timeout: 5), "\(section): “\(fragment)” not found")165 }166 }167 }168169 func testUnavailableSensorsAreDeclaredNotInvented() {170 let app = launch(section: "thermal")171 XCTAssertTrue(page(app, "thermal").waitForExistence(timeout: 5))172 XCTAssertTrue(text(app, containing: "Not available through supported system interfaces.").waitForExistence(timeout: 5))173 XCTAssertFalse(app.staticTexts.matching(NSPredicate(format: "value MATCHES %@", ".*[0-9]+ ?rpm.*")).firstMatch.exists, "a fan speed was displayed")174 }175176 func testDashboardEditingAddsAndRemovesAWidget() {177 let app = launch()178 let overhead = app.descendants(matching: .any).matching(identifier: "widget.overhead").firstMatch179 XCTAssertFalse(overhead.exists)180 app.descendants(matching: .any).matching(identifier: "dashboard.edit").firstMatch.click()181 let add = app.descendants(matching: .any).matching(identifier: "dashboard.add").firstMatch182 XCTAssertTrue(add.waitForExistence(timeout: 3))183 add.click()184 app.menuItems["ORIX Monitor overhead"].click()185 XCTAssertTrue(overhead.waitForExistence(timeout: 5), "widget was not added")186 let remove = app.buttons["Remove ORIX Monitor overhead"]187 XCTAssertTrue(remove.waitForExistence(timeout: 3))188 remove.click()189 XCTAssertTrue(overhead.waitForNonExistence(timeout: 5), "widget was not removed")190 }191192 /// Regression: a narrow window on the Processes page used to abort the app (split-view constraint loop).193 func testProcessesPageSurvivesANarrowWindow() {194 let app = launch(section: "processes", extra: ["-ORIXResizeTo", "860x560"])195 XCTAssertTrue(page(app, "processes").waitForExistence(timeout: 5))196 sleep(8)197 XCTAssertEqual(app.state, .runningForeground, "the app did not survive the narrow Processes page")198 open(app, "history")199 open(app, "processes")200 XCTAssertTrue(app.descendants(matching: .any).matching(identifier: "processes.table").firstMatch.waitForExistence(timeout: 5))201 }202203 func testProcessExplorerListsAndFilters() {204 let app = launch(section: "processes")205 XCTAssertTrue(page(app, "processes").waitForExistence(timeout: 5))206 let table = app.descendants(matching: .any).matching(identifier: "processes.table").firstMatch207 XCTAssertTrue(table.waitForExistence(timeout: 10))208 let count = app.staticTexts.matching(identifier: "processes.count").firstMatch209 XCTAssertTrue(count.waitForExistence(timeout: 10))210 let search = app.searchFields.firstMatch211 XCTAssertTrue(search.waitForExistence(timeout: 5))212 search.click()213 search.typeText("ORIX Monitor")214 XCTAssertTrue(app.staticTexts.matching(NSPredicate(format: "value == %@", "ORIX Monitor")).firstMatch.waitForExistence(timeout: 10), "the app did not find itself")215 search.typeText("-no-such-process-zzz")216 XCTAssertTrue(app.staticTexts.matching(NSPredicate(format: "value BEGINSWITH %@", "0 shown of")).firstMatch.waitForExistence(timeout: 10))217 }218219 func testAlertRuleCanBeAddedAndSnoozed() {220 let app = launch(section: "alerts")221 XCTAssertTrue(page(app, "alerts").waitForExistence(timeout: 5))222 XCTAssertTrue(text(app, containing: "6 rules enabled").waitForExistence(timeout: 5), "default rule set not shown")223 app.descendants(matching: .any).matching(identifier: "alerts.add").firstMatch.click()224 let save = app.buttons["Save"]225 XCTAssertTrue(save.waitForExistence(timeout: 5), "rule editor did not open")226 save.click()227 XCTAssertTrue(text(app, containing: "7 rules enabled").waitForExistence(timeout: 5), "new rule was not counted")228 app.descendants(matching: .any).matching(identifier: "alerts.snooze").firstMatch.click()229 let snooze = app.menuItems["For 15 minutes"]230 XCTAssertTrue(snooze.waitForExistence(timeout: 5), "snooze menu did not open")231 snooze.click()232 XCTAssertTrue(text(app, containing: "Snoozed until").waitForExistence(timeout: 5))233 }234235 func testReportPreviewIsBuiltFromStoredSamples() {236 let app = launch(section: "reports")237 XCTAssertTrue(page(app, "reports").waitForExistence(timeout: 5))238 sleep(6) // let real samples reach the store239 app.descendants(matching: .any).matching(identifier: "reports.refresh").firstMatch.click()240 XCTAssertTrue(text(app, containing: "System Overview Report").waitForExistence(timeout: 10))241 XCTAssertTrue(text(app, containing: "Limitations").waitForExistence(timeout: 5))242 XCTAssertTrue(app.buttons["PDF"].isEnabled && app.buttons["CSV"].isEnabled && app.buttons["JSON"].isEnabled && app.buttons["Markdown"].isEnabled)243 }244245 func testSettingsWindowAndPauseCommand() {246 let app = launch()247 app.activate()248 app.menuBars.menuBarItems["ORIX Monitor"].click()249 let settingsItem = app.menuItems.matching(NSPredicate(format: "title BEGINSWITH %@", "Settings")).firstMatch250 XCTAssertTrue(settingsItem.waitForExistence(timeout: 5), "no Settings command in the app menu")251 settingsItem.click()252 // macOS reopens Settings on the tab used last, so the window itself is the proof that it opened.253 XCTAssertTrue(app.windows["com_apple_SwiftUI_Settings_window"].waitForExistence(timeout: 10), "settings window did not open")254 let window = app.windows["com_apple_SwiftUI_Settings_window"]255 for tab in ["Sampling", "History", "Menu Bar", "Privacy", "About", "General"] {256 let control = window.toolbars.buttons[tab]257 XCTAssertTrue(control.waitForExistence(timeout: 5), "settings tab \(tab) not found")258 control.click()259 usleep(400_000)260 XCTAssertTrue(window.exists, "settings window vanished on tab \(tab)")261 }262 window.buttons["_XCUI:CloseWindow"].click()263 XCTAssertTrue(window.waitForNonExistence(timeout: 5), "settings window did not close")264 app.descendants(matching: .any).matching(identifier: "toolbar.pause").firstMatch.click()265 XCTAssertTrue(text(app, containing: "Sampling paused").waitForExistence(timeout: 5))266 app.descendants(matching: .any).matching(identifier: "toolbar.pause").firstMatch.click()267 XCTAssertTrue(text(app, containing: "Sampling every").waitForExistence(timeout: 5))268 }269270 func testMenuBarExtraOpensPanel() throws {271 let app = launch()272 let item = app.statusItems.firstMatch273 XCTAssertTrue(item.waitForExistence(timeout: 10), "no status item in the menu bar")274 // The item publishes real readings as its accessibility title.275 XCTAssertTrue(app.statusItems.matching(NSPredicate(format: "title CONTAINS %@ OR label CONTAINS %@", "CPU", "CPU")).firstMatch.waitForExistence(timeout: 10),276 "status item carries no CPU reading")277 try XCTSkipUnless(item.isHittable, "macOS hid the status item (menu bar full or behind the notch): the panel cannot be clicked on this Mac")278 item.click()279 let openButton = app.descendants(matching: .any).matching(identifier: "menubar.open").firstMatch280 XCTAssertTrue(openButton.waitForExistence(timeout: 5), "menu bar panel did not open")281 XCTAssertTrue(text(app, containing: "Top processes").exists)282 openButton.click()283 XCTAssertTrue(page(app, "overview").waitForExistence(timeout: 5))284 }285286 func testLightAndDarkAppearance() {287 for appearance in ["light", "dark"] {288 let app = launch(section: "cpu", appearance: appearance)289 XCTAssertTrue(page(app, "cpu").waitForExistence(timeout: 5), "\(appearance) launch failed")290 let shot = XCTAttachment(screenshot: app.windows.firstMatch.screenshot())291 shot.name = "cpu-\(appearance)"292 shot.lifetime = .keepAlways293 add(shot)294 app.terminate()295 }296 }297298 func testWindowResizingKeepsContentReachable() {299 // The app resizes its own live window three seconds after launch (test hook); the layout must reflow, not clip.300 let app = launch(extra: ["-ORIXResizeTo", "900x600"])301 let window = app.windows.firstMatch302 let resized = expectation(for: NSPredicate { _, _ in abs(window.frame.width - 900) < 3 }, evaluatedWith: nil)303 wait(for: [resized], timeout: 20)304 XCTAssertLessThan(window.frame.height, 700)305 XCTAssertTrue(app.descendants(matching: .any).matching(identifier: "widget.cpu").firstMatch.isHittable)306 open(app, "memory")307 XCTAssertTrue(text(app, containing: "Swap used").waitForExistence(timeout: 5))308 open(app, "network")309 XCTAssertTrue(text(app, containing: "Latency test").waitForExistence(timeout: 5))310 }311312 /// Runs Apple's accessibility audit on every page and prints what it reports as "ORIXAUDIT" lines in the test log.313 /// It records rather than fails: the findings are reviewed by hand in docs/reports/accessibility.md.314 func testAccessibilityAuditOfEveryPage() throws {315 var lines: [String] = []316 // One launch per page: the audit is slow, and a fresh instance keeps each page's result independent of the others.317 for section in ["overview", "cpu", "gpu", "memory", "disk", "network", "power", "thermal", "history", "insights", "alerts", "reports"] {318 let app = launch(section: section)319 XCTAssertTrue(page(app, section).waitForExistence(timeout: 10))320 var count = 0321 do {322 try app.performAccessibilityAudit(for: [.contrast, .elementDetection, .hitRegion, .sufficientElementDescription, .parentChild]) { issue in323 count += 1324 lines.append("\(section)\t\(issue.auditType.rawValue)\t\(issue.compactDescription)\t\(issue.element?.label ?? issue.element?.identifier ?? "")")325 return true // handled: keep auditing326 }327 } catch {328 lines.append("\(section)\tERROR\t\(error.localizedDescription)\t")329 }330 lines.append("\(section)\tTOTAL\t\(count)\t")331 // The test runner is sandboxed and cannot write outside its container: results go to the log, one marked line each.332 for line in lines { print("ORIXAUDIT\t\(line)") }333 lines.removeAll()334 app.terminate()335 }336 }337338 func testHistorySurvivesRelaunch() {339 var app = launch()340 XCTAssertTrue(text(app, containing: "Sampling every").waitForExistence(timeout: 10))341 sleep(5)342 quit(app)343 app = launch(section: "history")344 XCTAssertTrue(page(app, "history").waitForExistence(timeout: 5))345 XCTAssertTrue(text(app, containing: "Collected locally since ").waitForExistence(timeout: 15), "history of the first run was not found after relaunch")346 let samples = app.staticTexts.matching(NSPredicate(format: "value MATCHES %@", "[1-9][0-9]*")).firstMatch347 XCTAssertTrue(samples.waitForExistence(timeout: 10), "no stored sample count shown")348 }349}350