Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Tests/CoreTests/TerminalLogicTests.swift3// Description: The Terminal's pure logic: multiple selection and drag reordering of watchlist rows, the change between two statement periods, formula inputs, the statements CSV, palette matching, recent searches and navigation targets.4import Foundation5import Testing6@testable import VQuantCore78@Suite struct TerminalLogicTests {9 let order = ["AAPL", "MSFT", "NVDA", "GOOGL", "AMZN", "SPY"]1011 @Test func clickCommandClickShiftClick() {12 var s = RowSelection()13 s.click("MSFT")14 #expect(s.ids == ["MSFT"] && s.anchor == "MSFT")15 s.toggle("AMZN") // ⌘-click adds16 #expect(s.ids == ["MSFT", "AMZN"] && s.count == 2)17 s.toggle("MSFT") // ⌘-click on a selected row removes it18 #expect(s.ids == ["AMZN"])19 s.click("MSFT"); s.extend(to: "GOOGL", in: order) // ⇧-click selects the range, forwards20 #expect(s.ordered(in: order) == ["MSFT", "NVDA", "GOOGL"])21 s.extend(to: "AAPL", in: order) // …and backwards from the same anchor22 #expect(s.ordered(in: order) == ["AAPL", "MSFT"] && s.anchor == "MSFT")23 var e = RowSelection(); e.extend(to: "NVDA", in: order) // ⇧-click without an anchor is a click24 #expect(e.ids == ["NVDA"])25 }2627 @Test func arrowsPruneAndSelectAll() {28 var s = RowSelection()29 s.move(by: 1, in: order); #expect(s.ids == ["AAPL"])30 s.move(by: 1, in: order); #expect(s.ids == ["MSFT"])31 s.move(by: 1, in: order, extending: true); s.move(by: 1, in: order, extending: true)32 #expect(s.ordered(in: order) == ["MSFT", "NVDA", "GOOGL"])33 s.move(by: -1, in: order, extending: true) // the range shrinks back toward the anchor34 #expect(s.ordered(in: order) == ["MSFT", "NVDA"])35 s.move(by: -9, in: order); #expect(s.ids == ["AAPL"]) // clamped at the ends36 s.selectAll(order); #expect(s.count == 6)37 s.prune(to: ["AAPL", "SPY"]); #expect(s.ids == ["AAPL", "SPY"])38 s.clear(); #expect(s.isEmpty && s.anchor == nil)39 var none = RowSelection(); none.move(by: -1, in: order); #expect(none.ids == ["SPY"])40 none.move(by: 1, in: []); #expect(none.ids == ["SPY"])41 }4243 @Test func reorderKeepsEverySymbolOnce() {44 #expect(ListReorder.move(order, moving: ["SPY"], before: "AAPL") == ["SPY", "AAPL", "MSFT", "NVDA", "GOOGL", "AMZN"])45 #expect(ListReorder.move(order, moving: ["AAPL", "NVDA"], before: nil) == ["MSFT", "GOOGL", "AMZN", "SPY", "AAPL", "NVDA"])46 #expect(ListReorder.move(order, moving: ["AMZN", "MSFT"], before: "NVDA") == ["AAPL", "MSFT", "AMZN", "NVDA", "GOOGL", "SPY"]) // relative order kept47 #expect(ListReorder.move(order, moving: ["MSFT", "NVDA"], before: "NVDA") == order) // onto itself: nothing48 #expect(ListReorder.move(order, moving: ["ZZZ"], before: "AAPL") == order)49 #expect(ListReorder.move(order, moving: ["AAPL"], onto: "NVDA", below: true) == ["MSFT", "NVDA", "AAPL", "GOOGL", "AMZN", "SPY"])50 #expect(ListReorder.move(order, moving: ["AAPL"], onto: "SPY", below: true) == ["MSFT", "NVDA", "GOOGL", "AMZN", "SPY", "AAPL"])51 #expect(ListReorder.move(order, moving: ["AAPL", "GOOGL"], onto: "NVDA", below: true) == ["MSFT", "NVDA", "AAPL", "GOOGL", "AMZN", "SPY"])52 for target in order + ["?"] {53 let r = ListReorder.move(order, moving: ["MSFT", "SPY"], onto: target, below: true)54 #expect(r.sorted() == order.sorted())55 }56 }5758 @Test func periodChange() {59 let c = StatementMath.change(from: 416_161e6, to: 478_834e6, unit: "currency") // AAPL revenue FY2025 → FY2026 (fixture of 5058)60 #expect(c.absolute == 62_673e6)61 #expect(abs((c.relative ?? 0) - 0.1505979656911629) < 1e-12) // the server's own revenueGrowthYoY62 let margin = StatementMath.change(from: 0.4690516410716045, to: 0.48785591666423017, unit: "percent")63 #expect(abs((margin.absolute ?? 0) - 0.018804275592625) < 1e-12 && margin.relative == nil) // points, never a % of a %64 #expect(StatementMath.change(from: -14_264e6, to: 13_700e6, unit: "currency") == .init(absolute: 27_964e6, relative: nil)) // negative base65 #expect(StatementMath.change(from: 0, to: 5, unit: "currency").relative == nil)66 #expect(StatementMath.change(from: nil, to: 5, unit: "currency") == .init(absolute: nil, relative: nil))67 #expect(StatementMath.change(from: 5, to: nil, unit: "ratio") == .init(absolute: nil, relative: nil))68 let ratio = StatementMath.change(from: 0.8, to: 1.0, unit: "ratio")69 #expect(abs((ratio.relative ?? 0) - 0.25) < 1e-12)70 }7172 @Test func formulaInputs() {73 let known: Set<String> = ["ebit", "incomeTaxExpense", "incomeBeforeTax", "totalDebt", "totalStockholdersEquity", "cashAndShortTermInvestments", "netIncome", "revenue", "grossProfit"]74 #expect(StatementMath.inputs(ofFormula: "ebit × (1 − incomeTaxExpense / incomeBeforeTax) / (totalDebt + totalStockholdersEquity − cashAndShortTermInvestments)", known: known)75 == ["ebit", "incomeTaxExpense", "incomeBeforeTax", "totalDebt", "totalStockholdersEquity", "cashAndShortTermInvestments"])76 #expect(StatementMath.inputs(ofFormula: "netIncome / totalStockholdersEquity at period end (null when equity ≤ 0)", known: known) == ["netIncome", "totalStockholdersEquity"])77 #expect(StatementMath.inputs(ofFormula: "revenue[t] / revenue[previous fiscal year] − 1 (null when the base is ≤ 0 or missing)", known: known) == ["revenue"])78 #expect(StatementMath.inputs(ofFormula: "", known: known).isEmpty)79 let s = StatementMath.splitNote("incomeTaxExpense / incomeBeforeTax (null when pre-tax income ≤ 0)")80 #expect(s.expression == "incomeTaxExpense / incomeBeforeTax" && s.note == "null when pre-tax income ≤ 0")81 let plain = StatementMath.splitNote("(cashAndShortTermInvestments + netReceivables) / totalCurrentLiabilities")82 #expect(plain.expression == "(cashAndShortTermInvestments + netReceivables) / totalCurrentLiabilities" && plain.note == nil)83 let tail = StatementMath.splitNote("ebit × (1 − incomeTaxExpense / incomeBeforeTax) / (totalDebt + totalStockholdersEquity − cashAndShortTermInvestments)")84 #expect(tail.note == nil)85 }8687 @Test func statementsCSV() {88 let rows = [89 StatementMath.CSVRow(label: "Revenue", key: "revenue", unit: "currency", values: [478_834e6, 416_161e6], formula: nil),90 StatementMath.CSVRow(label: "Gross margin", key: "grossMargin", unit: "percent", values: [0.5, nil], formula: "grossProfit / revenue"),91 StatementMath.CSVRow(label: "Selling, general", key: "sga", unit: "currency", values: [1.5], formula: nil),92 ]93 let csv = StatementMath.csv(periods: ["FY 2026", "FY 2025"], rows: rows)94 #expect(csv == "line,key,unit,FY 2026,FY 2025,formula\r\nRevenue,revenue,currency,478834000000,416161000000,\r\nGross margin,grossMargin,percent,0.5,,grossProfit / revenue\r\n\"Selling, general\",sga,currency,1.5,,\r\n")95 let cmp = StatementMath.csv(periods: ["FY 2026", "FY 2025"], rows: [rows[0]], compare: (base: 1, value: 0))96 let lines = cmp.components(separatedBy: "\r\n")97 #expect(lines[0] == "line,key,unit,FY 2026,FY 2025,change FY 2025 to FY 2026,change_fraction,formula")98 #expect(lines[1].hasPrefix("Revenue,revenue,currency,478834000000,416161000000,62673000000,0.150597965691"))99 #expect(StatementMath.csv(periods: ["A"], rows: [rows[0]], compare: (base: 4, value: 0)).hasPrefix("line,key,unit,A,formula")) // a compare index out of range is ignored100 }101102 @Test func paletteMatching() {103 #expect(PaletteMatch.score(query: "Statements", title: "Statements") == 1)104 #expect(PaletteMatch.score(query: "stat", title: "Statements") == 0.9)105 #expect(PaletteMatch.score(query: "chart", title: "New Chart Window") == 0.8)106 #expect(PaletteMatch.score(query: "dark", title: "Toggle Theme", keywords: ["dark", "light", "appearance"]) == 0.75)107 #expect(PaletteMatch.score(query: "appear", title: "Toggle Theme", keywords: ["appearance"]) == 0.65)108 #expect(PaletteMatch.score(query: "list", title: "Watchlists") == 0.55)109 #expect(PaletteMatch.score(query: "nwc", title: "New Chart Window") == 0.3)110 #expect(PaletteMatch.score(query: "zz", title: "Statements") == 0)111 #expect(PaletteMatch.score(query: "xq", title: "Research") == 0 && PaletteMatch.score(query: "", title: "Research") == 0)112 #expect(PaletteMatch.score(query: "ETATS", title: "États financiers") == 0.9) // accents and case ignored113 #expect(PaletteMatch.score(query: "aapl", title: "Research") == 0)114 }115116 @Test func recentSearchesAndDestinations() {117 var r = RecentSearches.adding("AAPL", to: [])118 r = RecentSearches.adding("volatilité", to: r)119 r = RecentSearches.adding(" aapl ", to: r) // the same search again moves to the front, once120 #expect(r == ["aapl", "volatilité"])121 #expect(RecentSearches.adding("a", to: r) == r) // a single letter is not kept122 #expect(RecentSearches.adding("VOLATILITE", to: r) == ["VOLATILITE", "aapl"])123 let many = (0..<30).reduce([String]()) { RecentSearches.adding("query \($1)", to: $0) }124 #expect(many.count == RecentSearches.limit && many.first == "query 29")125 // every navigation target of CONTRACT §3 leads somewhere in this app126 for place in ["home", "chat", "terminal", "markets", "charts", "quant", "portfolio", "watchlists", "research", "notes", "history", "statements", "options", "macro", "agent", "usage", "settings", "security"] {127 #expect(PaletteDestination.forPlace(place) != nil, "nav:\(place)")128 }129 #expect(PaletteDestination.forPlace("charts") == .section("charting") && PaletteDestination.forPlace("chat") == .newConversation)130 #expect(PaletteDestination.forPlace("options") == .labTool("options_lab") && PaletteDestination.forPlace("a place added later") == nil)131 }132}133