SPB Git

Repository Public

AuditorTests.swift 11.7 KB 249 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : ORIX Sheet9// File             : Packages/FeatureAudit/Tests/FeatureAuditTests/AuditorTests.swift10// Module           : FeatureAuditTests11// 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 SheetCore90import SheetUI91import Audit92@testable import FeatureAudit9394@MainActor @Suite struct AuditorTests {95    /// A small model with seeded defects: #DIV/0!, an inconsistent formula, a broken name.96    func model() -> WorkbookSession {97        let s = Fixture.session(["Calc", "Notes"])98        var rows = [["Qty", "Price", "Total"]]99        for r in 2...9 { rows.append([String(r), "1.5", r == 6 ? "=A6*B6*2" : "=A\(r)*B\(r)"]) }100        rows.append(["", "", "=C2/0"])101        Fixture.fill(s, rows)102        s.perform("names") { $0.setDefinedName(DefinedName(name: "Gone", formula: "Missing!$A$1")) }103        return s104    }105106    @Test func stagedRunMatchesTheSinglePassAuditor() throws {107        let s = model()108        let direct = ModelAuditor.audit(workbook: s.workbook, engine: s.engine)109        let seen = Seen()110        let result = AuditRunner.run(workbook: WorkbookSnapshot.make(of: s.workbook), progress: { seen.add($0) })111        let staged = try #require(result)112        #expect(staged.findings.map(\.id) == direct.findings.map(\.id))113        #expect(staged.findings == direct.findings)114        #expect(staged.statistics.formulas == direct.statistics.formulas && staged.statistics.formulas == 9)115        #expect(staged.rules == direct.rules)116        #expect(staged.findings.contains { $0.rule == .errorValue } && staged.findings.contains { $0.rule == .inconsistentFormula } && staged.findings.contains { $0.rule == .brokenName })117        let p = seen.items118        #expect(p.count == AuditRunner.stages.count + 1)119        #expect(p.first?.completed == 0 && p.last?.completed == p.last?.total && p.last?.fraction == 1)120        #expect(Set(AuditRunner.stages.flatMap { $0 }) == Set(AuditRule.allCases))121    }122123    final class Seen: @unchecked Sendable {124        private let lock = NSLock()125        private var list = [AuditRunner.Progress]()126        func add(_ p: AuditRunner.Progress) { lock.lock(); list.append(p); lock.unlock() }127        var items: [AuditRunner.Progress] { lock.lock(); defer { lock.unlock() }; return list }128    }129130    @Test func cancellationStopsBetweenStages() {131        let s = model()132        let counter = Seen()133        let r = AuditRunner.run(workbook: WorkbookSnapshot.make(of: s.workbook), progress: { counter.add($0) }, isCancelled: { counter.items.count >= 3 })134        #expect(r == nil)135        #expect(counter.items.count == 3)136    }137138    @Test func snapshotIsIndependentOfTheLiveWorkbook() {139        let s = model()140        let snap = WorkbookSnapshot.make(of: s.workbook)141        #expect(snap.sheets.map(\.name) == ["Calc", "Notes"] && snap.sheets.map(\.id) == s.workbook.sheets.map(\.id))142        #expect(snap.names.count == 1)143        let live = s.workbook.sheets[0].cell(1, 2)!.formula!, copy = snap.sheets[0].cell(1, 2)!.formula!144        #expect(live !== copy && copy.value == .number(3))145        // editing the live workbook leaves the snapshot alone, and evaluating the snapshot leaves the live engine alone146        s.perform("edit") { $0.set(sheet: 0, row: 1, col: 0, .value(.number(100))) }147        #expect(Fixture.value(s, "C2") == .number(150))148        let engine = CalcEngine(workbook: snap, trustCachedValues: true)149        #expect(snap.sheets[0].value(1, 2) == .number(3))150        engine.set(sheet: 0, row: 1, col: 1, .value(.number(2)))151        _ = engine.recalculate()152        #expect(snap.sheets[0].value(1, 2) == .number(4))153        #expect(Fixture.value(s, "C2") == .number(150) && Fixture.value(s, "B2") == .number(1.5))154    }155156    @Test func backgroundRunDeliversTheReport() async {157        let s = model()158        let m = AuditorModel()159        await withCheckedContinuation { (c: CheckedContinuation<Void, Never>) in160            m.run(s) { ok in #expect(ok); c.resume() }161            #expect(m.isRunning)162        }163        #expect(!m.isRunning && m.progress == nil)164        #expect((m.report?.findings.count ?? 0) >= 3)165        #expect(m.report?.statistics.formulas == 9)166        #expect(!m.isStale(s))167        #expect(m.selectedID == m.visibleFindings.first?.id)168        s.perform("edit") { $0.set(sheet: 0, row: 20, col: 0, .value(.number(1))) }169        #expect(m.isStale(s))170    }171172    @Test func ignoredFindingsPersistInTheWorkbook() throws {173        let s = model()174        let m = AuditorModel.model(for: s)175        m.runNow(s)176        let all = try #require(m.report?.findings)177        let inconsistent = try #require(all.first { $0.rule == .inconsistentFormula })178        var changes = 0179        s.onChange = { changes += 1 }180        m.ignore(inconsistent, in: s)181        #expect(changes == 1)182        m.ignore(rule: .brokenName, in: s)183        #expect(!m.visibleFindings.contains { $0.id == inconsistent.id || $0.rule == .brokenName })184        #expect(m.ignoredCount >= 2)185        // stored as JSON in the attachment, read back by a fresh model186        let data = try #require(s.workbook.attachments[IgnoredFindings.attachmentKey])187        let decoded = try JSONDecoder().decode(IgnoredFindings.self, from: data)188        #expect(decoded.findings == [inconsistent.id] && decoded.rules == ["brokenName"])189        let again = IgnoredFindings.load(from: s.workbook)190        #expect(again == decoded && again.ignores(inconsistent))191        let fresh = AuditorModel()192        fresh.runNow(s)193        #expect(fresh.visibleFindings.map(\.id) == m.visibleFindings.map(\.id))194        #expect(fresh.count(.warning) == m.count(.warning))195        // shown again on demand, exported without them196        m.filter.showIgnored = true197        #expect(m.visibleFindings.count == all.count)198        #expect(m.exportReport?.findings.count == all.count - m.ignoredCount)199        #expect(m.markdown()?.contains("Broken defined name") == false)200        m.restore(inconsistent, in: s)201        #expect(IgnoredFindings.load(from: s.workbook).findings.isEmpty)202        m.restoreAll(in: s)203        #expect(s.workbook.attachments[IgnoredFindings.attachmentKey] == nil)204        #expect(m.markdown()?.contains("Broken defined name") == true)205    }206207    @Test func filterGroupsBySeverityRuleAndSheet() throws {208        let s = model()209        let report = try #require(AuditRunner.run(workbook: WorkbookSnapshot.make(of: s.workbook)))210        var f = FindingsFilter()211        let bySeverity = f.groups(of: report.findings, ignored: IgnoredFindings())212        #expect(bySeverity.map(\.id) == Array(NSOrderedSet(array: report.findings.map(\.severity.rawValue))) as? [String])213        #expect(bySeverity.first?.severity == .error)214        #expect(bySeverity.map(\.findings.count).reduce(0, +) == report.findings.count)215        f.grouping = .rule216        let byRule = f.groups(of: report.findings, ignored: IgnoredFindings())217        #expect(Set(byRule.map(\.id)) == Set(report.findings.map(\.rule.rawValue)))218        #expect(byRule.first?.severity == .error)219        f.grouping = .sheet220        let bySheet = f.groups(of: report.findings, ignored: IgnoredFindings())221        #expect(bySheet.contains { $0.id == "Calc" } && bySheet.contains { $0.id == "" })    // workbook-level findings222        f.severities = [.error]223        #expect(f.apply(to: report.findings, ignored: IgnoredFindings()).allSatisfy { $0.severity == .error })224        f.severities = Set(AuditSeverity.allCases)225        f.search = "div/0"226        let hits = f.apply(to: report.findings, ignored: IgnoredFindings())227        #expect(!hits.isEmpty && hits.allSatisfy { $0.rule == .errorValue })228        f.search = "zzz-nothing"229        #expect(f.groups(of: report.findings, ignored: IgnoredFindings()).isEmpty)230    }231232    @Test func insertReportSheetIsOneUndoStep() throws {233        let s = model()234        let m = AuditorModel()235        m.runNow(s)236        let index = try #require(m.insertReportSheet(in: s))237        #expect(s.workbook.sheets.count == 3 && s.sheetIndex == index)238        let sheet = s.workbook.sheets[index]239        #expect(sheet.name == "Audit Report")240        #expect(sheet.value(0, 0) == .text("Model audit"))241        #expect(sheet.value(4, 0) == .text("Severity"))242        #expect(sheet.value(5, 1).textValue != nil)243        #expect(sheet.cells.maxRow == 4 + (m.report?.findings.count ?? 0))244        #expect(m.insertReportSheet(in: s).map { s.workbook.sheets[$0].name } == "Audit Report 2")245        s.undo(); s.undo()246        #expect(s.workbook.sheets.count == 2)247    }248}249
Files main
Menu

Theme