Repository Public
1import Foundation2import Testing3@testable import SheetIntelligence45@Suite struct PostCheckTests {6 // MARK: numbers78 @Test func numbersFromTheProfileAreVerified() {9 let p = Fixtures.profile()10 let units = p.column(named: "Units")!11 let text = "The table has 20 rows and 6 columns. Units range from \(NumberVerifier.format(units.min!)) to \(NumberVerifier.format(units.max!)), with a mean of \(NumberVerifier.format(units.mean!)). Dates run from 2026-01-01 onwards. North appears 5 times."12 let d = Intelligence.verify(description: text, profile: p)13 #expect(d.unverifiedNumbers.isEmpty)14 #expect(d.verifiedNumberCount == 6)15 }1617 @Test func inventedNumbersAreFlagged() {18 let p = Fixtures.profile()19 let d = Intelligence.verify(description: "Revenue grew 37.5% and totals 1,234,567 across 20 rows; about 42 outliers.", profile: p)20 #expect(d.unverifiedNumbers == ["37.5%", "1,234,567", "42"])21 #expect(d.verifiedNumberCount == 1)22 }2324 @Test func roundedNumbersMatchTheirSource() {25 let r = NumberVerifier.check("mean 15.3, about 15, exactly 15.2857, wrong 15.4", allowed: [15.285714])26 #expect(r.unverified == ["15.4"])27 #expect(r.verifiedCount == 3)28 }2930 @Test func percentagesMatchFractions() {31 #expect(NumberVerifier.check("15% of rows, 15 % too", allowed: [0.15]).unverified.isEmpty)32 #expect(NumberVerifier.check("100% complete", allowed: []).unverified.isEmpty)33 #expect(NumberVerifier.check("16% of rows", allowed: [0.15]).unverified == ["16%"])34 }3536 @Test func localisedNumberFormatsAreRead() {37 #expect(NumberVerifier.check("total 1,234.5", allowed: [1234.5]).unverified.isEmpty)38 #expect(NumberVerifier.check("total de 1 234,5", allowed: [1234.5]).unverified.isEmpty)39 #expect(NumberVerifier.check("total de 1\u{202F}234,5", allowed: [1234.5]).unverified.isEmpty)40 #expect(NumberVerifier.check("moyenne de 15,29", allowed: [15.285714]).unverified.isEmpty)41 #expect(NumberVerifier.check("value 1,234", allowed: [1.234]).unverified.isEmpty)42 #expect(NumberVerifier.check("value 1,234", allowed: [1234]).unverified.isEmpty)43 #expect(NumberVerifier.check("minimum of -12.5 and −3", allowed: [-12.5, -3]).unverified.isEmpty)44 #expect(NumberVerifier.check("range 10-12", allowed: [10, 12]).unverified.isEmpty)45 }4647 @Test func identifiersAndListMarkersAreNotNumbers() {48 let text = "1. Column B2 holds Q1 data.\n2. See A1:D100 and the 3D view.\n 3) Done."49 #expect(NumberVerifier.tokens(in: text).isEmpty)50 #expect(NumberVerifier.tokens(in: "Sales 2024 grew", ignoring: ["Sales 2024"]).isEmpty)51 #expect(NumberVerifier.tokens(in: "In 2024 it grew").map(\.text) == ["2024"])52 }5354 @Test func formatIsStable() {55 #expect(NumberVerifier.format(20) == "20")56 #expect(NumberVerifier.format(15.285714) == "15.2857")57 #expect(NumberVerifier.format(1234.5678) == "1234.57")58 #expect(NumberVerifier.format(0.5) == "0.5")59 #expect(NumberVerifier.format(.nan) == "n/a")60 }6162 // MARK: findings6364 static let findings = [65 AuditFindingSummary(id: "errorValue:Sales!H4", rule: "Error value", severity: "error", location: "Sales!H4", evidence: "#DIV/0!", explanation: "e", recommendedAction: "a"),66 AuditFindingSummary(id: "hardcodedConstant:Sales!E9", rule: "Hardcoded constant in a formula", severity: "warning", location: "Sales!E9", evidence: "=C9*14", explanation: "e", recommendedAction: "a"),67 ]6869 @Test func inventedFindingIdsAreRejected() {70 let raw = PostChecks.RawReview(overview: " Two issues. ", items: [71 .init(findingId: "hardcodedConstant:Sales!E9", priority: 2, explanation: "x", suggestedFix: "y"),72 .init(findingId: "circularReference:Sales!Z1", priority: 1, explanation: "made up", suggestedFix: "n"),73 .init(findingId: " errorValue:Sales!H4 ", priority: 1, explanation: "z", suggestedFix: "w"),74 .init(findingId: "errorValue:Sales!H4", priority: 3, explanation: "duplicate", suggestedFix: nil),75 .init(findingId: "", priority: nil, explanation: nil, suggestedFix: nil),76 ])77 let review = PostChecks.review(raw, findings: Self.findings, total: 5)78 #expect(review.items.map(\.findingID) == ["errorValue:Sales!H4", "hardcodedConstant:Sales!E9"])79 #expect(review.items.map(\.priority) == [1, 2])80 #expect(review.items[0].explanation == "z")81 #expect(review.rejectedIDs == ["circularReference:Sales!Z1", ""])82 #expect(review.overview == "Two issues.")83 #expect(review.reviewedCount == 2 && review.totalFindings == 5)84 }8586 // MARK: charts8788 @Test func chartsMustUseKnownKindsAndExistingColumns() {89 let p = Fixtures.profile()90 let (accepted, rejected) = PostChecks.charts([91 ("column", "Revenue by region", "Region", ["Revenue"], "compare"),92 ("Scatter", "Units vs price", "units", ["unit price"], "relation"),93 ("sunburst", "Fancy", "Region", ["Revenue"], "x"),94 ("line", "Profit over time", "Date", ["Profit"], "x"),95 ("bar", "By customer", "Customer", ["Revenue"], "x"),96 ("line", "Text on y", "Date", ["Product"], "x"),97 ("scatter", "Text on x", "Region", ["Revenue"], "x"),98 ("column", "Revenue by region", "Region", ["Revenue"], "duplicate"),99 ("histogram", "Units", nil, ["Units"], "distribution"),100 ("pie", "Nothing", nil, [], "x"),101 ], profile: p)102 #expect(accepted.map(\.kind) == [.column, .scatter, .histogram])103 #expect(accepted[1].x == "Units" && accepted[1].y == ["Unit Price"])104 #expect(accepted.map(\.id) == [0, 1, 2])105 #expect(rejected.count == 6)106 #expect(rejected.contains { $0.contains("sunburst") } && rejected.contains { $0.contains("Profit") } && rejected.contains { $0.contains("Customer") })107 }108109 // MARK: plan110111 @Test func planStepsAreValidatedAgainstKnownColumns() {112 let p = Fixtures.profile()113 let (steps, rejected) = PostChecks.plan([114 ("filter", ["Region"], [], "Region = North"),115 ("rename", ["Unit Price"], ["Price"], ""),116 ("calculated_column", ["Units", "Price"], ["Total"], "Units * Price"),117 ("sort", ["Unit Price"], [], "descending"),118 ("groupBy", ["Region", "Segment"], ["Total sum"], "sum of Total"),119 ("teleport", ["Region"], [], ""),120 ("Remove Duplicates", [], [], "all columns"),121 ("sort", [], [], ""),122 ("rename", ["Total"], [], ""),123 ], profile: p)124 #expect(steps.map(\.operation) == [.filter, .rename, .calculatedColumn, .sort, .groupBy, .removeDuplicates, .sort, .rename])125 #expect(steps.map(\.id) == Array(1...8))126 #expect(steps[0].isValid && steps[1].isValid && steps[2].isValid)127 #expect(steps[3].issues == ["Column \"Unit Price\" does not exist at this step."])128 #expect(steps[4].issues == ["Column \"Segment\" does not exist at this step."])129 #expect(steps[5].isValid)130 #expect(steps[6].issues == ["No column is named for this step."])131 #expect(steps[7].issues == ["A new name is needed for each renamed column."])132 #expect(rejected == ["unknown operation \"teleport\""])133 let data = try! JSONEncoder().encode(steps)134 #expect(try! JSONDecoder().decode([PlanStep].self, from: data) == steps)135 }136137 // MARK: JSON fallback138139 struct Pair: Decodable, Equatable { var a: Int; var b: [String] }140141 @Test func jsonIsExtractedFromProseAndFences() throws {142 #expect(try StructuredJSON.decode(Pair.self, from: "{\"a\": 1, \"b\": [\"x\"]}") == Pair(a: 1, b: ["x"]))143 #expect(try StructuredJSON.decode(Pair.self, from: "Sure! Here it is:\n```json\n{\"a\": 2, \"b\": []}\n```\nHope it helps {not json}.") == Pair(a: 2, b: []))144 #expect(try StructuredJSON.decode(Pair.self, from: "{oops} then {\"a\": 3, \"b\": [\"} tricky { \\\" \"]}") == Pair(a: 3, b: ["} tricky { \" "]))145 #expect(try StructuredJSON.decode([Int].self, from: "values: [1, 2, 3].") == [1, 2, 3])146 }147148 @Test func malformedJsonIsRejectedWithAReason() {149 func failure(_ text: String) -> String? {150 do { _ = try StructuredJSON.decode(Pair.self, from: text); return nil } catch let e as IntelligenceError {151 if case .invalidResponse(let s) = e { return s }152 return "other"153 } catch { return "other" }154 }155 #expect(failure("no json here") == "no JSON found")156 #expect(failure("{\"a\": 1") == "no JSON found")157 #expect(failure("{\"b\": []}") == "missing \"a\"")158 #expect(failure("{\"a\": \"one\", \"b\": []}") == "wrong type at a")159 #expect(failure("{\"a\": 1, \"b\": [1]}")?.hasPrefix("wrong type at b") == true)160 #expect(failure("") == "no JSON found")161 #expect(failure("[1,2,3]") != nil)162 }163164 @Test func schemaHintDescribesTheShape() {165 let s = OutputSchema.object(name: "X", fields: [.init("kind", .choice(["a", "b"])), .init("items", .array(.integer, min: 1, max: 3), optional: true), .init("ok", .boolean, description: "flag")])166 #expect(s.hint == "{\"kind\": \"a|b\", \"items\": [0, … up to 3 items] (optional), \"ok\": true|false /* flag */}")167 }168169 // MARK: python170171 @Test func unknownOrixCallsAreListed() {172 let code = "import orix\ns = orix.active_sheet()\ndf = s.range('A1:B2').to_dataframe()\norix.show(df)\norix.save_workbook('x')\norix.http_get('u')\nfoo.orix.fake()\nmyorix.other()"173 #expect(OrixAPI.unknownCalls(in: code) == ["save_workbook", "http_get"])174 }175176 @Test func pythonIsCompiledNotExecuted() async throws {177 guard let python = PythonChecker.interpreter() else { return }178 let marker = FileManager.default.temporaryDirectory.appendingPathComponent("orix-must-not-exist-\(UUID().uuidString)")179 let code = "import pathlib\npathlib.Path(r'\(marker.path)').write_text('executed')\n"180 #expect(await PythonChecker.check(code) == .passed(interpreter: python))181 #expect(!FileManager.default.fileExists(atPath: marker.path))182 let bad = await PythonChecker.check("def f(:\n pass\n")183 guard case .failed(let message) = bad else { Issue.record("expected a syntax failure, got \(bad)"); return }184 #expect(message.contains("line 1"))185 #expect(await PythonChecker.check(" ") == .failed(message: "The answer contains no code."))186 #expect(await PythonChecker.check("x = 1", interpreter: nil) == .skipped(reason: "No python3 was found on this Mac."))187 #expect(await PythonChecker.check("x = 1", interpreter: "/nonexistent/python3") == .skipped(reason: "python3 could not be started."))188 #expect(PythonChecker.stripFence("```python\nx = 1\n```") == "x = 1")189 #expect(PythonChecker.stripFence("x = 1\n") == "x = 1")190 }191}192