Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Tests/UITests/ChartUITests.swift3// Description: Click-through of Charting on the backend TEST copy: the sidebar entry, interval and chart type by clicking, a horizontal line drawn with the tool palette (then undone and deleted from the keyboard), the provenance popover, and the question prepared for the research agent.4import XCTest56@MainActor final class ChartUITests: XCTestCase {7 private func launch(_ extra: [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 // -VQXChartNoSync: the clicks below must not write a chart object in the test account12 app.launchArguments = ["-VQUITest", "-VQInMemory", "-ApplePersistenceIgnoreState", "YES", "-VQSymbol", "MSFT", "-VQXChartNoSync", "1"] + extra13 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 private func wait(_ timeout: TimeInterval = 8, _ condition: () -> Bool) -> Bool {21 let deadline = Date().addingTimeInterval(timeout)22 while Date() < deadline { if condition() { return true }; usleep(200_000) }23 return condition()24 }2526 func testChartIntervalTypeDrawingAndProvenance() throws {27 let app = try launch()28 let row = element(app, "sidebar.charting")29 XCTAssertTrue(row.waitForExistence(timeout: 15))30 row.click()31 let canvas = element(app, "chart.canvas")32 XCTAssertTrue(canvas.waitForExistence(timeout: 15), "the chart is shown")33 XCTAssertTrue(wait(20) { canvas.label.contains("1D bars") && !canvas.label.contains("no data") }, "daily bars of MSFT are loaded (\(canvas.label))")3435 // interval by clicking: 1D → 5m → the series is intraday36 element(app, "chart.interval.5m").click()37 XCTAssertTrue(wait(25) { canvas.label.contains("5m bars") && !canvas.label.contains("no data") }, "clicking 5m loads 5-minute bars (\(canvas.label))")38 XCTAssertTrue(element(app, "chart.interval.5m").isSelected)39 element(app, "chart.interval.1D").click()40 XCTAssertTrue(wait(25) { canvas.label.contains("1D bars") }, "clicking 1D returns to daily bars")4142 // chart type by clicking43 let line = element(app, "chart.style.Line")44 line.click()45 XCTAssertTrue(wait { line.isSelected }, "clicking Line selects the line chart")46 element(app, "chart.style.Candles").click()47 XCTAssertTrue(wait { self.element(app, "chart.style.Candles").isSelected })4849 // a horizontal line: pick the tool, click in the plot50 let count = element(app, "chart.drawingCount")51 XCTAssertTrue(count.waitForExistence(timeout: 5), "the drawing count is shown")52 XCTAssertEqual(value(count), "0", "no drawing yet (label \(count.label))")53 element(app, "chart.tool.hline").click()54 XCTAssertTrue(wait { self.element(app, "chart.tool.hline").isSelected }, "the tool is armed")55 canvas.coordinate(withNormalizedOffset: CGVector(dx: 0.45, dy: 0.35)).click()56 XCTAssertTrue(wait { self.value(count) == "1" }, "clicking in the plot with the horizontal-line tool adds a drawing (count \(value(count)))")57 // (the canvas's accessibility value is not asserted: AppKit-backed views report it late or not at all)58 XCTAssertTrue(wait { self.element(app, "chart.tool.pointer").isSelected }, "the pointer comes back after a drawing")59 // undo removes it, redo brings it back, ⌫ deletes the selected drawing60 app.typeKey("z", modifierFlags: .command)61 XCTAssertTrue(wait { self.value(count) == "0" }, "⌘Z undoes the drawing")62 app.typeKey("z", modifierFlags: [.command, .shift])63 XCTAssertTrue(wait { self.value(count) == "1" }, "⇧⌘Z redoes it")64 // a trend line by dragging65 element(app, "chart.tool.trend").click()66 canvas.coordinate(withNormalizedOffset: CGVector(dx: 0.30, dy: 0.50)).press(forDuration: 0.2, thenDragTo: canvas.coordinate(withNormalizedOffset: CGVector(dx: 0.60, dy: 0.30)))67 if wait(5, { self.value(count) == "2" }) {68 app.typeKey(.delete, modifierFlags: [])69 XCTAssertTrue(wait { self.value(count) == "1" }, "⌫ deletes the selected drawing")70 } else {71 // 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)72 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 drew nothing") }73 element(app, "chart.tool.pointer").click()74 }7576 // provenance popover77 element(app, "chart.provenance").click()78 let pop = app.staticTexts["Data provenance"]79 XCTAssertTrue(pop.waitForExistence(timeout: 8), "the provenance popover opens")80 XCTAssertTrue(app.staticTexts["Adjustment applied"].exists && app.staticTexts["Observations"].exists && app.staticTexts["Gaps"].exists)81 app.typeKey(.escape, modifierFlags: [])82 }8384 func testAskTheAgentOpensResearchWithoutSending() throws {85 let app = try launch(["-VQSection", "charting"])86 let canvas = element(app, "chart.canvas")87 XCTAssertTrue(canvas.waitForExistence(timeout: 15))88 XCTAssertTrue(wait(20) { !canvas.label.contains("no data") && canvas.label.contains("bars,") })89 let ask = element(app, "chart.ask")90 XCTAssertTrue(wait(10) { ask.isEnabled })91 ask.click()92 let window = app.windows.firstMatch93 XCTAssertTrue(wait { window.title.hasPrefix("Research") }, "the question is handed to Research (title: \(window.title))")94 }9596 /// A detached chart window has no Research composer: its question goes to a main window.97 func testAskFromDetachedChartWindowReachesAMainWindow() throws {98 let app = try launch(["-VQSection", "charting"])99 XCTAssertTrue(element(app, "chart.canvas").waitForExistence(timeout: 15))100 app.typeKey("n", modifierFlags: [.command, .shift]) // File ▸ New Chart Window101 let detached = app.windows["MSFT"]102 XCTAssertTrue(detached.waitForExistence(timeout: 10), "a chart window opens")103 let canvas = detached.descendants(matching: .any)["chart.canvas"].firstMatch104 XCTAssertTrue(canvas.waitForExistence(timeout: 15))105 XCTAssertTrue(wait(20) { !canvas.label.contains("no data") && canvas.label.contains("bars,") })106 let ask = detached.descendants(matching: .any)["chart.ask"].firstMatch107 XCTAssertTrue(wait(10) { ask.isEnabled })108 ask.click()109 XCTAssertTrue(wait(10) { app.windows.allElementsBoundByIndex.contains { $0.title.hasPrefix("Research") } }, "a main window shows Research with the question")110 XCTAssertTrue(detached.exists, "the chart window stays open")111 }112113 /// A ticker dragged as plain text onto a chart switches the chart to it (here: a compared series of the main114 /// chart, dropped on a detached chart window).115 func testTickerDroppedOnAChartSwitchesItsSymbol() throws {116 let app = try launch(["-VQSection", "charting", "-VQXChartCompare", "NVDA"])117 let chip = element(app, "chart.series.NVDA")118 XCTAssertTrue(chip.waitForExistence(timeout: 25), "the compared series is listed")119 app.typeKey("n", modifierFlags: [.command, .shift])120 let detached = app.windows["MSFT"]121 XCTAssertTrue(detached.waitForExistence(timeout: 10))122 let target = detached.descendants(matching: .any)["chart.canvas"].firstMatch123 XCTAssertTrue(target.waitForExistence(timeout: 15))124 XCTAssertTrue(wait(20) { !target.label.contains("no data") && target.label.contains("bars,") })125 // the two windows overlap at first: move the chart window so the main window's footer is reachable126 let source = app.windows.matching(NSPredicate(format: "title BEGINSWITH 'Charting'")).firstMatch.descendants(matching: .any)["chart.series.NVDA"].firstMatch127 XCTAssertTrue(source.waitForExistence(timeout: 5))128 if !source.isHittable {129 let bar = detached.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0)).withOffset(CGVector(dx: 0, dy: 12))130 bar.press(forDuration: 0.3, thenDragTo: bar.withOffset(CGVector(dx: 0, dy: -260)))131 }132 source.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.5)).press(forDuration: 0.8, thenDragTo: target.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.4)), withVelocity: .slow, thenHoldForDuration: 0.6)133 if !wait(10, { app.windows["NVDA"].exists }) {134 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 dropped nothing (windows: \(app.windows.allElementsBoundByIndex.map { $0.title }))") }135 }136 }137}138