Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : ORIX Sheet9// File : Packages/SheetUI/Tests/SheetUITests/SessionTests.swift10// Module : SheetUITests11// 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 SheetUI9192@Suite struct AxisLayoutTests {93 @Test func offsetsAndIndices() {94 let l = AxisLayout(count: 1_048_576, defaultSize: 20, custom: [2: 50, 10: 5], hidden: IndexSet([4, 5]))95 #expect(l.offset(0) == 0)96 #expect(l.offset(3) == 20 + 20 + 50)97 #expect(l.size(4) == 0)98 #expect(l.offset(6) == 90 + 20) // rows 4 and 5 hidden99 #expect(l.index(at: 45) == 2)100 #expect(l.index(at: 89.9) == 2)101 #expect(l.index(at: 90) == 3)102 #expect(l.index(at: 110) == 6)103 #expect(l.total == Double(1_048_576) * 20 + 30 - 15 - 40)104 #expect(l.index(at: l.total + 100) == 1_048_575)105 #expect(l.nextVisible(from: 3, step: 1) == 6)106 for i in stride(from: 0, to: 1_000_000, by: 77_777) { #expect(l.index(at: l.offset(i) + 0.5) == i || l.size(i) == 0) }107 }108}109110@MainActor @Suite struct SessionTests {111 func make() -> WorkbookSession {112 let s = WorkbookSession(workbook: Workbook(sheetNames: ["Sheet1"]), locale: Locale(identifier: "en_US"))113 return s114 }115 func type(_ s: WorkbookSession, _ a1: String, _ text: String) {116 s.select(cell: CellRef(a1: a1)!)117 s.beginEdit(text: text)118 #expect(s.commitEdit(move: nil))119 }120121 @Test func editCommitUndo() {122 let s = make()123 type(s, "A1", "2"); type(s, "A2", "=A1*21")124 #expect(s.sheet.value(1, 0) == .number(42))125 s.undo()126 #expect(s.sheet.value(1, 0) == .blank)127 s.redo()128 #expect(s.sheet.value(1, 0) == .number(42))129 s.select(cell: CellRef(a1: "B1")!)130 s.beginEdit(text: "=1+")131 #expect(!s.commitEdit(move: nil))132 #expect(s.isEditing && s.editError != nil)133 s.cancelEdit()134 #expect(s.sheet.value(0, 1) == .blank)135 }136137 @Test func navigation() {138 let s = make()139 for (i, v) in ["1", "2", "3"].enumerated() { type(s, "B\(i + 2)", v) }140 s.select(cell: CellRef(a1: "B2")!)141 s.moveToEdge(.down)142 #expect(s.selection.active == CellRef(a1: "B4")!)143 s.moveToEdge(.down)144 #expect(s.selection.active.row == Limits.maxRows - 1)145 s.moveToEdge(.up)146 #expect(s.selection.active == CellRef(a1: "B4")!)147 s.move(.right, extend: true); s.move(.up, extend: true)148 #expect(s.selection.primary == RangeRef(a1: "B3:C4")!)149 s.select(cell: CellRef(a1: "B3")!)150 s.selectAll()151 #expect(s.selection.primary == RangeRef(a1: "B2:B4")!)152 s.selectAll()153 #expect(s.selection.primary.isWholeColumns && s.selection.primary.isWholeRows)154 #expect(s.goTo("D7:E9"))155 #expect(s.selection.primary == RangeRef(a1: "D7:E9")!)156 #expect(!s.goTo("not a ref !!"))157 }158159 @Test func mergeAwareSelection() {160 let s = make()161 s.select(RangeRef(a1: "B2:C3")!)162 s.toggleMerge()163 s.select(cell: CellRef(a1: "C3")!)164 #expect(s.selection.active == CellRef(a1: "B2")!)165 #expect(s.selection.primary == RangeRef(a1: "B2:C3")!)166 s.move(.right)167 #expect(s.selection.active == CellRef(a1: "D2")!)168 }169170 @Test func pointingAndReferences() {171 let s = make()172 s.select(cell: CellRef(a1: "C1")!)173 s.beginEdit(text: "=SUM(")174 #expect(s.canPointReference)175 s.pointReference(RangeRef(a1: "A1")!)176 s.pointReference(RangeRef(a1: "A1:A3")!)177 #expect(s.editText == "=SUM(A1:A3")178 s.userEdited("=SUM(A1:A3)+B2*\"A9\"")179 let refs = s.editReferences180 #expect(refs.map(\.range) == [RangeRef(a1: "A1:A3")!, RangeRef(a1: "B2")!])181 #expect(s.commitEdit(move: nil))182 #expect(ReferenceScanner.scan("=LOG10(5)+Sheet2!$B$4+'My S'!A:B+XYZ1").map(\.range.a1) == ["B4", "A:B"])183 }184185 @Test func summaryFormatAndFind() {186 let s = make()187 for (i, v) in ["10", "20", "abc", "30"].enumerated() { type(s, "A\(i + 1)", v) }188 s.select(RangeRef(a1: "A1:A4")!)189 let sum = s.selectionSummary190 #expect(sum.count == 4 && sum.numericCount == 3 && sum.sum == 60 && sum.min == 10 && sum.max == 30)191 s.toggleBold()192 #expect(s.activeStyle.bold)193 s.applyBorders(.outline)194 #expect(s.workbook.styles[s.sheet.styleID(0, 0)].borders.top != nil)195 #expect(s.workbook.styles[s.sheet.styleID(1, 0)].borders.top == nil)196 #expect(s.find("ab").count == 1)197 #expect(s.replaceAll("abc", with: "xyz") == 1)198 #expect(s.sheet.value(2, 0) == .text("xyz"))199 s.sortSelection(ascending: false)200 #expect(s.sheet.value(0, 0) == .text("xyz"))201 }202203 @Test func backgroundRecalculation() async throws {204 let s = make()205 s.syncCalcThreshold = 100206 s.perform("fill") { e in207 for r in 0..<5000 { e.set(sheet: 0, row: r, col: 0, .value(.number(Double(r)))); e.setFormula(sheet: 0, row: r, col: 1, "=A\(r + 1)*2") }208 }209 var waited = 0210 while s.isCalculating, waited < 400 { try await Task.sleep(for: .milliseconds(10)); waited += 1 }211 #expect(!s.isCalculating)212 #expect(s.sheet.value(4999, 1) == .number(9998))213 // an edit during a background pass cancels and restarts it214 s.perform("edit") { $0.set(sheet: 0, row: 0, col: 0, .value(.number(7))) }215 #expect(s.sheet.value(0, 1) == .number(14))216 }217218 @Test func printPagination() {219 let s = make()220 s.perform("fill") { e in for r in 0..<120 { for c in 0..<12 { e.set(sheet: 0, row: r, col: c, .value(.number(Double(r * c)))) } } }221 let layout = PrintLayout(workbook: s.workbook, sheetIndex: 0)222 #expect(layout.pages.count == 8) // 12 cols × 80 pt = 960 pt → 2 bands; 120 rows × 20 pt = 2400 pt → 4 bands (Letter portrait)223 #expect(layout.pages.first?.rows.lowerBound == 0)224 let pdf = PrintRenderer.pdfData(session: s)225 #expect(pdf.count > 1000)226 #expect(String(decoding: pdf.prefix(5), as: UTF8.self) == "%PDF-")227 s.perform("fit") { $0.editMeta(sheet: 0) { $0.print.fitToWidth = 1 } }228 #expect(PrintLayout(workbook: s.workbook, sheetIndex: 0).pages.count < 8)229 }230}231