SPB Git

Repository Public

TerminalUITests.swift 10.9 KB 167 lines swift

1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Tests/UITests/TerminalUITests.swift3// Description: Click-through of the Terminal on the VQuant Next TEST backend (5058): the market overview, multiple selection and list management in Watchlists, the command palette from the keyboard, tabs and common size in Statements.4import XCTest56final class TerminalUITests: XCTestCase {7    private func launch(_ arguments: [String] = []) throws -> XCUIApplication {8        let env = ProcessInfo.processInfo.environment9        guard let token = env["VQ_DEV_TOKEN"], let server = env["VQ_SERVER"] else { throw XCTSkip("needs a development session (Scripts/ui-test.sh)") }10        let app = XCUIApplication()11        // no restored window frames: every development copy shares one saved state, and a window restored off-screen cannot be clicked12        app.launchArguments = ["-VQUITest", "-VQInMemory", "-ApplePersistenceIgnoreState", "YES"] + arguments13        app.launchEnvironment = ["VQ_DEV_TOKEN": token, "VQ_SERVER": server]14        app.launch()15        XCTAssertTrue(app.windows.firstMatch.waitForExistence(timeout: 20))16        return app17    }18    private func element(_ app: XCUIApplication, _ id: String) -> XCUIElement { app.descendants(matching: .any)[id].firstMatch }19    private func value(_ e: XCUIElement) -> String { (e.value as? String) ?? "" }20    /// The number of selected rows, as the footer of the watchlist states it ("2 selected", "No selection").21    private func selected(_ e: XCUIElement) -> Int {22        let text = [value(e), e.label].first { !$0.isEmpty } ?? ""23        return text.hasPrefix("No") ? 0 : Int(text.split(separator: " ").first ?? "") ?? -124    }25    private func deleteList(_ app: XCUIApplication, _ name: String) {26        let row = element(app, "watchlist.list.\(name)")27        app.activate()      // the screen is shared with other test runs: come back to the front before a menu28        row.rightClick()29        let item = app.menuItems["Delete…"].firstMatch30        XCTAssertTrue(item.waitForExistence(timeout: 5), "the list's context menu offers to delete it")31        item.click()32        let confirm = app.buttons["Delete Watchlist"].firstMatch33        XCTAssertTrue(confirm.waitForExistence(timeout: 5))34        confirm.click()35        XCTAssertTrue(wait(10) { !row.exists }, "the deleted list is gone")36    }37    private func wait(_ timeout: TimeInterval = 8, _ condition: () -> Bool) -> Bool {38        let end = Date().addingTimeInterval(timeout)39        while Date() < end { if condition() { return true }; usleep(200_000) }40        return condition()41    }4243    func testOverviewShowsAnIndexAndOpensItsChart() throws {44        let app = try launch()45        element(app, "sidebar.overview").click()46        let index = element(app, "overview.item.^GSPC")47        XCTAssertTrue(index.waitForExistence(timeout: 20), "the S&P 500 is among the indices of the overview")48        XCTAssertTrue(element(app, "overview.session").exists, "the session of the market is stated")49        XCTAssertTrue(element(app, "overview.yieldcurve").exists, "the Treasury curve is drawn")50        index.click()51        XCTAssertTrue(wait { app.windows.firstMatch.title.contains("^GSPC") }, "clicking an index opens it in Charting (title: \(app.windows.firstMatch.title))")52    }5354    func testWatchlistSelectionAndListLifecycle() throws {55        let app = try launch(["-VQXWatchlist", "kit-test-watchlist"])56        element(app, "sidebar.watchlists").click()57        let first = element(app, "watchlist.row.AAPL"), second = element(app, "watchlist.row.NVDA")58        XCTAssertTrue(first.waitForExistence(timeout: 20), "the synced test watchlist shows its symbols")59        let count = element(app, "watchlist.selection")60        first.click()61        XCTAssertTrue(wait { self.selected(count) == 1 }, "a click selects one row (stated: \(selected(count)))")62        XCUIElement.perform(withKeyModifiers: .command) { second.click() }63        XCTAssertTrue(wait { self.selected(count) == 2 }, "⌘-click adds a second row (stated: \(selected(count)))")64        XCTAssertEqual(value(first), "selected"); XCTAssertEqual(value(second), "selected")65        let third = element(app, "watchlist.row.AMZN")66        XCUIElement.perform(withKeyModifiers: .shift) { third.click() }67        XCTAssertTrue(wait { self.selected(count) == 3 }, "⇧-click extends from the last clicked row, NVDA, to AMZN (stated: \(selected(count)))")6869        // another list opened by a click loads its own quotes (the row then carries the company name)70        app.activate()71        element(app, "watchlist.list.Tech").click()72        XCTAssertTrue(wait { !self.element(app, "watchlist.row.NVDA").exists }, "the other list replaces the first")73        let tech = element(app, "watchlist.row.MSFT")74        XCTAssertTrue(wait(15) { tech.exists && tech.label.contains("Microsoft") }, "the quotes of the list opened by click are shown (\(tech.label))")75        XCTAssertTrue(wait { self.selected(count) == 0 }, "the selection does not follow to another list")7677        // create a list, see it open, delete it78        let name = "mac-test-\(Int(Date().timeIntervalSince1970) % 100_000)"79        element(app, "watchlist.new").click()80        let field = element(app, "watchlist.new.field")81        XCTAssertTrue(field.waitForExistence(timeout: 5))82        field.click(); field.typeText(name + "\r")83        let row = element(app, "watchlist.list.\(name)")84        XCTAssertTrue(row.waitForExistence(timeout: 10), "the new list appears among the watchlists")85        let title = element(app, "watchlist.title")86        XCTAssertTrue(wait { self.value(title) == name || title.label == name }, "the new list is open")87        deleteList(app, name)88    }8990    /// In a list of its own (`mac-test-…`, deleted at the end): symbols added from the field, a row dragged to91    /// another place, a selected row removed.92    func testWatchlistAddReorderRemove() throws {93        let app = try launch()94        element(app, "sidebar.watchlists").click()95        let name = "mac-test-order-\(Int(Date().timeIntervalSince1970) % 100_000)"96        let new = element(app, "watchlist.new")97        XCTAssertTrue(new.waitForExistence(timeout: 20))98        new.click()99        let field = element(app, "watchlist.new.field")100        XCTAssertTrue(field.waitForExistence(timeout: 5))101        field.click(); field.typeText(name + "\r")102        XCTAssertTrue(element(app, "watchlist.list.\(name)").waitForExistence(timeout: 10))103        let add = app.textFields.matching(NSPredicate(format: "placeholderValue == 'Add a symbol'")).firstMatch104        XCTAssertTrue(add.waitForExistence(timeout: 5))105        for s in ["AAPL", "MSFT", "NVDA"] {106            add.click(); add.typeText(s + "\r")107            XCTAssertTrue(element(app, "watchlist.row.\(s)").waitForExistence(timeout: 10), "\(s) is added")108        }109        let aapl = element(app, "watchlist.row.AAPL"), msft = element(app, "watchlist.row.MSFT"), nvda = element(app, "watchlist.row.NVDA")110        XCTAssertTrue(aapl.frame.minY < msft.frame.minY && msft.frame.minY < nvda.frame.minY, "symbols are listed in the order they were added")111        // drag the first row onto the lower half of the last one: it becomes the last112        let from = aapl.coordinate(withNormalizedOffset: CGVector(dx: 0.1, dy: 0.5)), to = nvda.coordinate(withNormalizedOffset: CGVector(dx: 0.1, dy: 0.8))113        from.press(forDuration: 0.6, thenDragTo: to, withVelocity: .slow, thenHoldForDuration: 0.4)114        if !wait(6, { msft.frame.minY < nvda.frame.minY && nvda.frame.minY < aapl.frame.minY }) {115            XCTExpectFailure("synthesized drags do not start an AppKit/SwiftUI drag session on macOS 27; this path was verified with real pointer events (docs/TEST_REPORT.md, evidence P)") { XCTFail("the synthesized drag moved nothing") }116        }117        // remove one selected row118        msft.click()119        element(app, "watchlist.remove").click()120        XCTAssertTrue(wait(10) { !msft.exists }, "the selected symbol is removed")121        XCTAssertTrue(aapl.exists && nvda.exists)122        deleteList(app, name)123    }124125    func testPaletteOpensATickerFromTheKeyboard() throws {126        let app = try launch()127        element(app, "sidebar.overview").click()128        app.typeKey("k", modifierFlags: .command)129        let field = element(app, "palette.field")130        XCTAssertTrue(field.waitForExistence(timeout: 10), "⌘K opens the palette")131        XCTAssertTrue(element(app, "palette.row.section:statements").exists, "local commands are listed before anything is typed")132        field.typeText("MSFT")133        let hit = element(app, "palette.row.symbol:MSFT")134        XCTAssertTrue(hit.waitForExistence(timeout: 10), "the ticker is offered")135        XCTAssertTrue(wait { self.value(hit) == "highlighted" }, "the first result is highlighted")136        field.typeKey(.downArrow, modifierFlags: []); field.typeKey(.upArrow, modifierFlags: [])137        XCTAssertTrue(wait { self.value(hit) == "highlighted" }, "↓ then ↑ comes back to the first result")138        field.typeText("\r")139        XCTAssertTrue(wait { app.windows.firstMatch.title.hasPrefix("Charting") && app.windows.firstMatch.title.contains("MSFT") }, "Return opens MSFT in Charting (title: \(app.windows.firstMatch.title))")140        XCTAssertFalse(field.exists, "the palette closes")141        // escape closes without acting142        app.typeKey("k", modifierFlags: .command)143        XCTAssertTrue(field.waitForExistence(timeout: 5))144        field.typeKey(.escape, modifierFlags: [])145        XCTAssertTrue(wait { !field.exists })146    }147148    func testStatementsTabsCommonSizeAndInspector() throws {149        let app = try launch(["-VQSymbol", "AAPL"])150        element(app, "sidebar.statements").click()151        XCTAssertTrue(element(app, "statements.row.revenue").waitForExistence(timeout: 25), "the income statement of AAPL is shown")152        app.buttons["Balance sheet"].firstMatch.click()153        XCTAssertTrue(element(app, "statements.row.totalAssets").waitForExistence(timeout: 10), "the balance sheet tab shows its lines")154        let caption = element(app, "statements.caption")155        element(app, "statements.commonsize").click()156        XCTAssertTrue(wait { (self.value(caption) + caption.label).contains("Common size") }, "common size is on (caption: \(value(caption)) \(caption.label))")157        XCTAssertFalse(element(app, "statements.row.totalAssets").exists, "the base of the common-size view is not a line of it")158        // a derived line shows its formula159        element(app, "statements.row.totalDebt").click()160        let formula = element(app, "statements.inspector.formula")161        XCTAssertTrue(formula.waitForExistence(timeout: 5))162        XCTAssertTrue((value(formula) + formula.label).contains("totalDebt / totalAssets"), "the inspector shows the calculation (\(value(formula)) \(formula.label))")163        element(app, "statements.compare").click()164        XCTAssertTrue(element(app, "statements.compare.from").waitForExistence(timeout: 5), "comparing shows the two period pickers")165    }166}167
Files main
Menu

Theme