SPB Git

Repository Public

ContextTests.swift 12.1 KB 251 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : ORIX Sheet9// File             : Packages/SheetIntelligence/Tests/SheetIntelligenceTests/ContextTests.swift10// Module           : SheetIntelligenceTests11// Repository       : https://git.spboucher.ai/orix-sheet12// Created          : 2026-11-2113// Last Modified    : 2026-11-2114//15// =============================================================================16// AUTHOR & PROFESSIONAL AFFILIATION17// =============================================================================18//19// Author           : Simon-Pierre Boucher20// Position         : Professor of Finance21// Institution      : Université du Québec en Outaouais (UQO)22// Department       : Département des sciences administratives23// Organization     : SP Boucher24//25// Website          : https://spboucher.ai26// Git              : https://git.spboucher.ai27//28// =============================================================================29// OFFICIAL CONTACT DIRECTORY30// =============================================================================31//32// General          : info@spboucher.ai33// Professional     : contact@spboucher.ai34// Direct           : simon@spboucher.ai35// Research         : research@spboucher.ai36// Development      : dev@spboucher.ai37// Support          : support@spboucher.ai38// Security/Admin   : admin@spboucher.ai39// Notifications    : notifications@spboucher.ai40//41// Preferred Contact: support@spboucher.ai42//43// =============================================================================44// COPYRIGHT & INTELLECTUAL PROPERTY45// =============================================================================46//47// Copyright (c) 202648// Simon-Pierre Boucher49//50// All rights reserved where applicable.51//52// This file is governed by the applicable project license,53// copyright notices, institutional agreements and relevant law.54//55// Unless expressly authorized by the applicable license or56// rights holder, copying, redistribution, sublicensing,57// publication and commercial exploitation are not permitted.58//59// Existing third-party rights and notices remain unaffected.60//61// =============================================================================62// CONFIDENTIALITY & SECURITY63// =============================================================================64//65// Classification   : Public66// Distribution     : Public repository; use is governed by the project license67// Security Contact : admin@spboucher.ai68//69// Access to this source does not independently grant permission70// to redistribute or disclose confidential materials.71//72// Do not embed credentials, private keys, access tokens,73// certificates or confidential personal information.74//75// =============================================================================76// LICENSE & MAINTENANCE77// =============================================================================78//79// License          : None specified (no license file in this repository)80// Maintainer       : Simon-Pierre Boucher81// Technical Contact: dev@spboucher.ai82//83// =============================================================================84// END OF SP BOUCHER SOURCE HEADER85// =============================================================================8687import Foundation88import Testing89import SheetCore90@testable import SheetIntelligence9192@Suite struct ContextTests {93    @Test func sheetContextDescribesTheDataBlock() {94        let c = Fixtures.context(active: "C5")95        #expect(c.sheetName == "Sales")96        #expect(c.otherSheets == ["Params"])97        #expect(c.usedRange == "A1:F21")98        #expect(c.region == "A1:F21")99        #expect(c.hasHeader)100        #expect(c.firstDataRow == 2 && c.lastDataRow == 21)101        #expect(c.columns.map(\.name) == ["Region", "Product", "Units", "Unit Price", "Revenue", "Date"])102        #expect(c.columns.map(\.type) == ["text", "text", "integer", "number", "number", "date"])103        #expect(c.sampleRows.count == 5)104        #expect(c.sampleRows[0].first == "2" && c.sampleRows[0][1] == "North")105        #expect(c.definedNames.map(\.name) == ["TaxRate"])106        #expect(c.activeCellContent == String(Fixtures.units(3)))107        let text = c.rendered()108        #expect(text.contains("- C: \"Units\" (integer), data in C2:C21"))109        #expect(text.contains("Active cell: C5"))110        #expect(text.contains("TaxRate"))111    }112113    @Test func theLargestBlockIsDescribedWhenTheActiveCellSitsInASmallOne() {114        let (wb, engine) = Fixtures.sales()115        engine.set(sheet: 0, "I3", .value(.text("Summary")))116        engine.set(sheet: 0, "I4", .value(.text("West revenue")))117        _ = engine.recalculate()118        let c = ContextBuilder.sheetContext(workbook: wb, sheet: 0, selection: RangeRef(a1: "J4")!, active: CellRef(a1: "J4")!)119        #expect(c.region == "A1:F21")120        #expect(c.activeBlock == "I3:I4")121        #expect(c.columns.count == 6)122        #expect(c.rendered().contains("The active cell belongs to another, smaller block: I3:I4."))123        #expect(c.nearbyLabels.contains("I4: West revenue"))124        // an explicit multi-cell selection wins125        let s = ContextBuilder.sheetContext(workbook: wb, sheet: 0, selection: RangeRef(a1: "I3:I4")!, active: CellRef(a1: "I3")!)126        #expect(s.region == "I3:I4" && s.activeBlock == nil)127    }128129    @Test func renderedContextRespectsTheHardCap() {130        var limits = ContextLimits()131        for cap in [120, 400, 1000, 2400, 6000] {132            limits.maxCharacters = cap133            let c = Fixtures.context(active: "C5", limits: limits)134            #expect(c.rendered().count <= cap)135            #expect(c.rendered(includeStructure: true).count <= cap)136            #expect(c.rendered(maxCharacters: 300, includeStructure: true).count <= 300)137        }138        limits.maxCharacters = 120139        #expect(Fixtures.context(limits: limits).rendered().hasSuffix("[context truncated]"))140    }141142    @Test func wideAndLongCellsAreBounded() {143        let wb = Workbook(sheetNames: ["Wide"])144        let engine = CalcEngine(workbook: wb)145        for c in 0..<80 {146            engine.set(sheet: 0, row: 0, col: c, .value(.text("Header number \(c) " + String(repeating: "x", count: 200))))147            for r in 1..<200 { engine.set(sheet: 0, row: r, col: c, .value(.text(String(repeating: "secret ", count: 50)))) }148        }149        let limits = ContextLimits()150        let c = ContextBuilder.sheetContext(workbook: wb, sheet: 0, selection: RangeRef(CellRef(0, 0)), active: CellRef(0, 0), limits: limits)151        #expect(c.columns.count == limits.maxColumns)152        #expect(c.sampleRows.count == limits.sampleRows)153        #expect(c.sampleRows.allSatisfy { $0.allSatisfy { $0.count <= limits.maxCellCharacters } })154        #expect(c.columns.allSatisfy { $0.name.count <= limits.maxCellCharacters })155        #expect(c.rendered().count <= limits.maxCharacters)156        // 80 × 199 data cells exist; at most sampleRows × maxColumns of them are in the context157        #expect(c.rendered().components(separatedBy: "secret").count - 1 <= limits.sampleRows * limits.maxColumns * 5)158    }159160    @Test func sampleValuesCanBeExcluded() {161        var limits = ContextLimits()162        limits.includeSampleValues = false163        let c = Fixtures.context(active: "C5", limits: limits)164        #expect(c.sampleRows.isEmpty)165        #expect(c.nearbyLabels.isEmpty)166        #expect(c.activeCellContent == "(a value)")167        let text = c.rendered(includeStructure: true)168        #expect(!text.contains("North") && !text.contains("Widget"))169        #expect(text.contains("\"Region\" (text)"))170        let (wb, _) = Fixtures.sales()171        let p = ContextBuilder.tableProfile(workbook: wb, sheet: 0, range: RangeRef(a1: "A1:F21")!, limits: limits)172        #expect(p.columns.allSatisfy { $0.topValues.isEmpty })173    }174175    @Test func snapshotIsIndependentOfTheLiveWorkbook() {176        let (wb, engine) = Fixtures.sales()177        let c = ContextBuilder.sheetContext(workbook: wb, sheet: 0, selection: RangeRef(a1: "H2")!, active: CellRef(a1: "H2")!)178        engine.set(sheet: 0, row: 1, col: 2, .value(.number(1_000_000)))179        _ = engine.recalculate()180        let v = FormulaValidator.validate("=C2", engine: c.engine, sheet: c.sheetIndex, at: c.active)181        #expect(v.value == .number(Double(Fixtures.units(0))))182        #expect(c.snapshot !== wb)183    }184185    @Test func factsFindInputsOutputsAndPatterns() {186        let c = Fixtures.context()187        #expect(c.facts.formulaCount == 20)188        #expect(c.facts.keyFormulas.count == 1)189        #expect(c.facts.keyFormulas[0].formula == "=C2*D2")190        #expect(c.facts.keyFormulas[0].count == 20)191        #expect(c.facts.keyFormulas[0].extent == "E2:E21")192        #expect(c.facts.keyFormulas[0].label?.contains("Revenue") == true)193        #expect(c.facts.inputs.map(\.address) == ["C2:C21", "D2:D21"])194        #expect(c.facts.inputs[0].content == "20 values")195        #expect(c.facts.outputs.count == 20)196        let doc = SheetDocumenter.markdown(c, audit: [])197        #expect(doc.contains("| E2:E21 |") && doc.contains("`=C2*D2`"))198        #expect(doc.contains("The auditor reported no findings."))199        #expect(!doc.contains("Purpose"))200        #expect(SheetDocumenter.markdown(c, audit: [], purpose: "Likely a sales log.").contains(SheetDocumenter.purposeHeading + "\n\nLikely a sales log."))201    }202203    @Test func formulaContextCarriesTheEngineSteps() throws {204        let (wb, engine) = Fixtures.sales()205        engine.set(sheet: 0, "H2", .formula(try FormulaParser.parse("=IF(SUM(E2:E21)>1000,E2*TaxRate,0)")))206        _ = engine.recalculate()207        let f = try #require(ContextBuilder.formulaContext(engine: engine, sheet: 0, cell: CellRef(a1: "H2")!))208        #expect(f.cell == "H2" && f.sheet == "Sales")209        #expect(f.formula == "=IF(SUM(E2:E21)>1000,E2*TaxRate,0)")210        #expect(f.result == Value.number(Fixtures.revenue(0) * 0.15).description)211        #expect(!f.steps.isEmpty)212        #expect(f.functions.map(\.name).contains("SUM"))213        #expect(f.references.contains { $0.address.contains("E2") && $0.label?.contains("Revenue") == true })214        let text = f.rendered()215        #expect(text.contains("Evaluation steps computed by the engine"))216        #expect(f.rendered(maxCharacters: 200).count <= 200)217        #expect(ContextBuilder.formulaContext(engine: engine, sheet: 0, cell: CellRef(a1: "A2")!) == nil)218        _ = wb219    }220221    @Test func tableProfileIsComputedDeterministically() {222        let p = Fixtures.profile()223        #expect(p.rowCount == 20 && p.columns.count == 6 && p.hasHeader)224        let units = p.column(named: "units")!225        let values = (0..<20).map { Double(Fixtures.units($0)) }226        #expect(units.type == "integer" && units.count == 20 && units.missing == 0)227        #expect(units.min == values.min() && units.max == values.max())228        #expect(abs(units.mean! - values.reduce(0, +) / 20) < 1e-12)229        #expect(units.q1 != nil && units.q3 != nil)230        let region = p.column(named: "Region")!231        #expect(region.topValues.count == 3 && region.topValues[0] == .init(value: "North", count: 5))232        let date = p.column(named: "Date")!233        #expect(date.type == "date" && date.earliest == "2026-01-01" && date.min == nil)234        #expect(p.rendered().contains("\"Units\" (column C, integer): 20 values, 0 missing (0%)"))235        #expect(p.rendered(maxCharacters: 250).count <= 250)236        #expect(p.numbers.contains(20) && p.numbers.contains(units.mean!))237    }238239    @Test func profileCountsMissingValues() {240        let (wb, engine) = Fixtures.sales()241        engine.clear(sheet: 0, range: RangeRef(a1: "C3:C4")!)242        engine.set(sheet: 0, "C5", .value(.text("n/a")))243        _ = engine.recalculate()244        let p = ContextBuilder.tableProfile(workbook: wb, sheet: 0, range: RangeRef(a1: "A1:F21")!)245        let units = p.column(named: "Units")!246        #expect(units.missing == 3)247        #expect(units.missingPercent == 15)248        #expect(units.type == "mixed")249    }250}251
Files main
Menu

Theme