SPB Git

Repository Public

PortfolioUITests.swift 5.4 KB 84 lines swift

1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Tests/UITests/PortfolioUITests.swift3// Description: Click-through of the Portfolios section on the TEST backend (5058): sidebar, a portfolio row, a new test portfolio, adding and removing a position, the Analysis tab. Everything written is named mac-test-… and deleted at the end.4import XCTest56final class PortfolioUITests: XCTestCase {7    private func launch() 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        app.launchArguments = ["-VQUITest", "-VQInMemory", "-ApplePersistenceIgnoreState", "YES"]12        app.launchEnvironment = ["VQ_DEV_TOKEN": token, "VQ_SERVER": server]13        app.launch()14        XCTAssertTrue(app.windows.firstMatch.waitForExistence(timeout: 20))15        return app16    }17    private func el(_ app: XCUIApplication, _ id: String) -> XCUIElement { app.descendants(matching: .any)[id].firstMatch }18    private func waitGone(_ e: XCUIElement, timeout: TimeInterval = 10) -> Bool {19        let deadline = Date().addingTimeInterval(timeout)20        while e.exists, Date() < deadline { usleep(200_000) }21        return !e.exists22    }23    /// Keys go to the application (the focused field): a field being edited is not always found again by identifier.24    private func replaceText(_ app: XCUIApplication, _ field: XCUIElement, with text: String) {25        app.activate(); field.doubleClick(); usleep(300_000); app.typeText(text + "\r")26    }2728    func testPortfolioListEditorAndAnalysis() throws {29        let app = try launch()30        el(app, "sidebar.portfolio").click()31        // the main portfolio row is always listed first and selects by click32        let main = el(app, "portfolio.row.main")33        XCTAssertTrue(main.waitForExistence(timeout: 15))34        main.click()35        // a new portfolio: created and selected (under -VQUITest it is named "mac-test-ui Portfolio n")36        let before = app.descendants(matching: .any).matching(NSPredicate(format: "identifier BEGINSWITH 'portfolio.row.mac-test-ui'")).count37        el(app, "portfolio.new").click()38        let rows = app.descendants(matching: .any).matching(NSPredicate(format: "identifier BEGINSWITH 'portfolio.row.mac-test-ui'"))39        let deadline = Date().addingTimeInterval(10)40        while rows.count <= before, Date() < deadline { usleep(200_000) }41        XCTAssertGreaterThan(rows.count, before, "the list shows the new portfolio")42        let row = rows.element(boundBy: rows.count - 1)43        XCTAssertTrue(el(app, "portfolio.title").waitForExistence(timeout: 10), "the new portfolio opens")44        main.click(); row.click()45        XCTAssertTrue(el(app, "position.new.quantity").waitForExistence(timeout: 10), "clicking the row opens its holdings")46        // add a position: symbol through the search field, quantity, Add47        let search = app.textFields.matching(NSPredicate(format: "placeholderValue BEGINSWITH 'Add a position'")).firstMatch48        XCTAssertTrue(search.waitForExistence(timeout: 5))49        app.activate(); search.click(); usleep(300_000); app.typeText("AAPL\r")50        XCTAssertTrue(el(app, "position.new.symbol").waitForExistence(timeout: 5), "the picked symbol is shown (window: \(app.windows.firstMatch.title); fields: \(app.textFields.allElementsBoundByIndex.map { ($0.placeholderValue ?? "") + "=" + (($0.value as? String) ?? "") }))")51        let qty = el(app, "position.new.quantity")52        app.activate(); qty.click(); usleep(300_000); app.typeText("3")53        el(app, "position.new.add").click()54        XCTAssertTrue(el(app, "position.qty.AAPL").waitForExistence(timeout: 10), "the position appears in the table")55        // edit in place, then the Analysis tab56        replaceText(app, el(app, "position.qty.AAPL"), with: "4")57        let edited = el(app, "position.qty.AAPL")58        let t0 = Date().addingTimeInterval(5)59        while (edited.value as? String) != "4", Date() < t0 { usleep(200_000) }60        XCTAssertEqual(edited.value as? String, "4", "the quantity is edited in place")61        // the edit is on the window's undo stack: ⌘Z puts the quantity back62        app.activate(); usleep(300_000)63        app.typeKey("z", modifierFlags: .command)64        let q = el(app, "position.qty.AAPL")65        let until = Date().addingTimeInterval(5)66        while (q.value as? String) != "3", Date() < until { usleep(200_000) }67        XCTAssertEqual(q.value as? String, "3", "undo restores the quantity")68        app.buttons["Analysis"].firstMatch.click()69        XCTAssertTrue(el(app, "analysis.run").waitForExistence(timeout: 10), "the Analysis tab offers to run the engines")70        app.buttons["Rebalancing"].firstMatch.click()71        app.buttons["Holdings"].firstMatch.click()72        // remove the position, then the portfolio73        let remove = el(app, "position.remove.AAPL")74        XCTAssertTrue(remove.waitForExistence(timeout: 10))75        remove.click()76        XCTAssertTrue(waitGone(el(app, "position.qty.AAPL")), "it disappears")77        row.rightClick()78        app.menuItems["Delete…"].firstMatch.click()79        app.buttons["Delete Portfolio"].firstMatch.click()80        XCTAssertTrue(waitGone(row), "it disappears")81        sleep(2)   // the deletion reaches the server before the app quits82    }83}84
Files main
Menu

Theme