Repository Public License
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : ORIX OS Automation9// File : Packages/ORIXToolRegistry/Tests/ORIXToolRegistryTests/DocumentsToolsTests.swift10// Description : Integration tests of every documents.* tool on real CSV,11// JSON, PDF, image and .xlsx files built in temp12// directories, plus unit tests of the CSV codec, JSON query13// language and Markdown block parser.14// Module : ORIXToolRegistryTests15// Repository : https://git.spboucher.ai/orix-automation16// Created : 2026-11-2117// Last Modified : 2026-11-2118//19// =============================================================================20// AUTHOR & PROFESSIONAL AFFILIATION21// =============================================================================22//23// Author : Simon-Pierre Boucher24// Position : Professor of Finance25// Institution : Université du Québec en Outaouais (UQO)26// Department : Département des sciences administratives27// Organization : SP Boucher28//29// Website : https://spboucher.ai30// Git : https://git.spboucher.ai31//32// =============================================================================33// OFFICIAL CONTACT DIRECTORY34// =============================================================================35//36// General : info@spboucher.ai37// Professional : contact@spboucher.ai38// Direct : simon@spboucher.ai39// Research : research@spboucher.ai40// Development : dev@spboucher.ai41// Support : support@spboucher.ai42// Security/Admin : admin@spboucher.ai43// Notifications : notifications@spboucher.ai44//45// Preferred Contact: support@spboucher.ai46//47// =============================================================================48// COPYRIGHT & INTELLECTUAL PROPERTY49// =============================================================================50//51// Copyright (c) 202652// Simon-Pierre Boucher53//54// All rights reserved where applicable.55//56// This file is governed by the applicable project license,57// copyright notices, institutional agreements and relevant law.58//59// Unless expressly authorized by the applicable license or60// rights holder, copying, redistribution, sublicensing,61// publication and commercial exploitation are not permitted.62//63// Existing third-party rights and notices remain unaffected.64//65// =============================================================================66// CONFIDENTIALITY & SECURITY67// =============================================================================68//69// Classification : Public70// Distribution : Public repository; use is governed by the project license71// Security Contact : admin@spboucher.ai72//73// Access to this source does not independently grant permission74// to redistribute or disclose confidential materials.75//76// Do not embed credentials, private keys, access tokens,77// certificates or confidential personal information.78//79// =============================================================================80// LICENSE & MAINTENANCE81// =============================================================================82//83// License : Proprietary, all rights reserved (see license file)84// License File : LICENSE85// Maintainer : Simon-Pierre Boucher86// Technical Contact: dev@spboucher.ai87//88// =============================================================================89// END OF SP BOUCHER SOURCE HEADER90// =============================================================================9192import Foundation93import CoreGraphics94import ImageIO95import PDFKit96import UniformTypeIdentifiers97import Testing98import ORIXCore99import ORIXTestingSupport100@testable import ORIXToolRegistry101102enum Fixtures {103 /// Writes a solid-colour image with a diagonal, in the given format.104 static func image(at url: URL, width: Int, height: Int, type: UTType = .png) throws {105 try FileManager.default.createDirectory(at: url.deletingLastPathComponent(), withIntermediateDirectories: true)106 let ctx = try #require(CGContext(data: nil, width: width, height: height, bitsPerComponent: 8, bytesPerRow: 0,107 space: CGColorSpaceCreateDeviceRGB(), bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue))108 ctx.setFillColor(red: 0.2, green: 0.4, blue: 0.9, alpha: 1); ctx.fill(CGRect(x: 0, y: 0, width: width, height: height))109 ctx.setStrokeColor(red: 1, green: 1, blue: 1, alpha: 1); ctx.setLineWidth(3)110 ctx.move(to: .zero); ctx.addLine(to: CGPoint(x: width, y: height)); ctx.strokePath()111 let image = try #require(ctx.makeImage())112 let dest = try #require(CGImageDestinationCreateWithURL(url as CFURL, type.identifier as CFString, 1, nil))113 CGImageDestinationAddImage(dest, image, nil)114 try #require(CGImageDestinationFinalize(dest))115 }116117 /// Builds a small two-sheet workbook from hand-written parts and zips it with /usr/bin/zip.118 static func workbook(in tmp: TempDirectory, name: String = "book.xlsx") async throws -> URL {119 let root = "xlsx-src-\(UUID().uuidString.prefix(6))"120 try tmp.write("\(root)/[Content_Types].xml", #"<?xml version="1.0"?><Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"/>"#)121 try tmp.write("\(root)/xl/workbook.xml", """122 <?xml version="1.0" encoding="UTF-8"?><workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">123 <sheets><sheet name="Data" sheetId="1" r:id="rId1"/><sheet name="Other & more" sheetId="2" r:id="rId2"/></sheets></workbook>124 """)125 try tmp.write("\(root)/xl/_rels/workbook.xml.rels", """126 <?xml version="1.0" encoding="UTF-8"?><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">127 <Relationship Id="rId1" Type="x" Target="worksheets/sheet1.xml"/><Relationship Id="rId2" Type="x" Target="worksheets/sheet2.xml"/></Relationships>128 """)129 try tmp.write("\(root)/xl/sharedStrings.xml", """130 <?xml version="1.0" encoding="UTF-8"?><sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="4" uniqueCount="4">131 <si><t>Name</t></si><si><t>Qty</t></si><si><t>Widget <A></t></si><si><r><rPr><b/></rPr><t>Bold</t></r><r><t xml:space="preserve"> plain</t></r><rPh><t>phonetic</t></rPh></si></sst>132 """)133 try tmp.write("\(root)/xl/worksheets/sheet1.xml", """134 <?xml version="1.0" encoding="UTF-8"?><worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><sheetData>135 <row r="1"><c r="A1" t="s"><v>0</v></c><c r="B1" t="s"><v>1</v></c></row>136 <row r="2"><c r="A2" t="s"><v>2</v></c><c r="B2"><v>3</v></c></row>137 <row r="3"><c r="A3" t="inlineStr"><is><t>Inline</t></is></c><c r="B3"><v>4.5</v></c><c r="C3"><f>B2+B3</f><v>7.5</v></c></row>138 <row r="5"><c r="A5" t="s"><v>3</v></c><c r="B5" t="b"><v>1</v></c><c r="D5" s="1"><v>45000</v></c><c r="E5" t="e"><v>#DIV/0!</v></c></row>139 </sheetData><mergeCells count="1"><mergeCell ref="A7:B7"/></mergeCells></worksheet>140 """)141 try tmp.write("\(root)/xl/worksheets/sheet2.xml", """142 <?xml version="1.0" encoding="UTF-8"?><worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><sheetData>143 <row r="1"><c r="A1" t="str"><v>second sheet</v></c></row></sheetData></worksheet>144 """)145 let out = tmp.file(name)146 try await TestProcess.run("/usr/bin/zip", ["-q", "-r", "-X", out.path, "."], cwd: tmp.file(root).path).requireSuccess(timeoutSeconds: 30)147 return out148 }149}150151@Suite("documents.* tools (integration, real files)")152struct DocumentsToolsTests {153 @Test func csvReadHandlesQuotingHeadersDelimitersAndWindows() async throws {154 let h = try await Harness(); defer { h.tmp.remove() }155 try h.tmp.write("people.csv", "name,city,note\r\n\"Tremblay, Luc\",Gatineau,\"said \"\"hi\"\"\"\r\nAda,\"Multi\nline\",\r\nBob,Hull,x\r\n")156 let r = await h.call("documents.csvRead", ["path": "people.csv"])157 #expect(r.status == .succeeded && r.output["columns"] == ["name", "city", "note"] && r.output["hasHeader"] == true && r.output["totalRows"] == 3)158 #expect(r.output["rows"]?[0] == ["Tremblay, Luc", "Gatineau", "said \"hi\""] && r.output["rows"]?[1] == ["Ada", "Multi\nline", ""])159 let window = await h.call("documents.csvRead", ["path": "people.csv", "offset": 1, "limit": 1, "asObjects": true])160 #expect(window.output["rows"] == [["name": "Ada", "city": "Multi\nline", "note": ""]] && window.truncated && window.output["rowCount"] == 1)161 try h.tmp.write("semi.csv", "1;2;3\n4;5;6\n")162 let semi = await h.call("documents.csvRead", ["path": "semi.csv"])163 #expect(semi.output["delimiter"] == ";" && semi.output["hasHeader"] == false && semi.output["columns"] == ["column1", "column2", "column3"] && semi.output["totalRows"] == 2)164 let forced = await h.call("documents.csvRead", ["path": "semi.csv", "header": "yes", "delimiter": ";"])165 #expect(forced.output["columns"] == ["1", "2", "3"] && forced.output["totalRows"] == 1)166 try h.tmp.write("tabs.tsv", "a\tb\nc\td\n")167 #expect(await h.call("documents.csvRead", ["path": "tabs.tsv", "header": "no"]).output["delimiter"] == "tab")168 #expect(await h.call("documents.csvRead", ["path": "people.csv", "delimiter": "x"]).status == .invalidInput)169 }170171 @Test func csvWriteRoundTripsAndProtectsExistingFiles() async throws {172 let h = try await Harness(); defer { h.tmp.remove() }173 let rows: JSONValue = [["a, b", 1, true], ["quote \" here", 2.5, .null], ["line\nbreak", -3, false]]174 let w = await h.call("documents.csvWrite", ["path": "out/t.csv", "columns": ["text", "n", "flag"], "rows": rows])175 #expect(w.status == .succeeded && w.output["rows"] == 3)176 let back = await h.call("documents.csvRead", ["path": "out/t.csv"])177 #expect(back.output["columns"] == ["text", "n", "flag"])178 #expect(back.output["rows"] == [["a, b", "1", "true"], ["quote \" here", "2.5", ""], ["line\nbreak", "-3", "false"]])179 #expect(await h.call("documents.csvWrite", ["path": "out/t.csv", "rows": [["x"]]]).status == .invalidInput)180 let dry = await h.call("documents.csvWrite", ["path": "out/t.csv", "rows": [["x"]], "overwrite": true], dryRun: true)181 #expect(dry.status == .simulated && h.read("out/t.csv").hasPrefix("text,n,flag"))182 let over = await h.call("documents.csvWrite", ["path": "out/t.csv", "rows": [["city": "Hull", "pop": 70000], ["city": "Aylmer"]], "overwrite": true])183 #expect(over.output["backupPath"]?.stringValue != nil && h.read("out/t.csv") == "city,pop\r\nHull,70000\r\nAylmer,\r\n")184 #expect(await h.call("documents.csvWrite", ["path": "mixed.csv", "rows": [["a"], ["k": 1]]]).status == .invalidInput)185 }186187 @Test func jsonQueryPathsWildcardsAndFilters() async throws {188 let h = try await Harness(); defer { h.tmp.remove() }189 try h.tmp.write("data.json", #"{"store":{"name":"Shop","items":[{"id":1,"name":"pen","price":2.5,"tags":["a"]},{"id":2,"name":"Notebook","price":12,"tags":[]},{"id":3,"name":"bag","price":40}]},"count":3}"#)190 func q(_ query: String) async -> ToolResult { await h.call("documents.jsonQuery", ["path": "data.json", "query": .string(query)]) }191 #expect(await q("store.name").output["result"] == "Shop")192 #expect(await q("store.items[1].name").output["result"] == "Notebook")193 #expect(await q("store.items.0.id").output["result"] == 1)194 #expect(await q("store.items[-1].name").output["result"] == "bag")195 #expect(await q("store.items[*].id").output["result"] == [1, 2, 3])196 #expect(await q("store.items[?price>10].name").output["result"] == ["Notebook", "bag"])197 #expect(await q("store.items[?name==\"pen\"].price").output["result"] == [2.5])198 #expect(await q("store.items[?name~=note].id").output["result"] == [2])199 #expect(await q("store.items[?id!=2].id").output["count"] == 2)200 let missing = await q("store.nope.deeper")201 #expect(missing.status == .succeeded && missing.output["found"] == false && missing.output["result"] == .null)202 let outline = await h.call("documents.jsonQuery", ["path": "data.json"])203 #expect(outline.output["result"]?["count"] == "3" && outline.output["result"]?["store"] == "object(2 keys)")204 #expect(await h.call("documents.jsonQuery", ["path": "data.json", "query": "store.items[*].id", "limit": 2]).truncated)205 #expect(await q("store.items[?broken]").status == .invalidInput)206 try h.tmp.write("bad.json", "{not json")207 #expect(await h.call("documents.jsonQuery", ["path": "bad.json", "query": "a"]).status == .invalidInput)208 }209210 @Test func textStatsCountsAndRanksTerms() async throws {211 let h = try await Harness(); defer { h.tmp.remove() }212 try h.tmp.write("t.txt", "The quick brown fox.\nThe lazy dog; the QUICK fox!\n\nÉté été.\n")213 let r = await h.call("documents.textStats", ["path": "t.txt", "topTerms": 3])214 #expect(r.output["lines"] == 4 && r.output["words"] == 12 && r.output["bytes"] == .int("The quick brown fox.\nThe lazy dog; the QUICK fox!\n\nÉté été.\n".utf8.count))215 #expect(r.output["topTerms"] == [["term": "fox", "count": 2], ["term": "quick", "count": 2], ["term": "été", "count": 2]])216 let inline = await h.call("documents.textStats", ["content": "one two two"])217 #expect(inline.output["words"] == 3 && inline.output["topTerms"]?[0]?["term"] == "two")218 #expect(await h.call("documents.textStats", [:]).status == .invalidInput)219 #expect(await h.call("documents.textStats", ["path": "t.txt", "content": "x"]).status == .invalidInput)220 }221222 static let report = """223 Intro paragraph with **bold**, *italic* and `code` runs that is long enough to wrap across the page width when rendered at the default size.224225 ## Findings226227 - First bullet with a longer explanation228 continued on the next line229 - Second bullet230 1. Numbered one231 2. Numbered two232233 | Item | Qty | Price |234 |------|-----|-------|235 | Pen | 3 | 2.50 |236 | Notebook | 1 | 12.00 |237238 ```239 let total = items.reduce(0, +)240 print(total)241 ```242243 ---244245 246247 Closing line ZEBRA-MARKER.248 """249250 @Test func renderPDFThenReadItBackWithPDFTextAndPDFInfo() async throws {251 let h = try await Harness(); defer { h.tmp.remove() }252 try Fixtures.image(at: h.tmp.file("square.png"), width: 240, height: 160)253 let long = (1...140).map { "Paragraph \($0) of the long section, with enough words to fill a line of text.\n" }.joined(separator: "\n")254 let r = await h.call("documents.renderPDF", ["destination": "out/report.pdf", "title": "Quarterly Report", "author": "ORIX Test",255 "content": .string(Self.report + "\n\n# Long section\n\n" + long), "pageSize": "a4"])256 #expect(r.status == .succeeded, "\(r.failure?.message ?? "")")257 let pages = try #require(r.output["pages"]?.intValue)258 #expect(pages >= 3 && r.artifacts.first?.kind == "report" && (r.output["bytes"]?.intValue ?? 0) > 2_000)259260 let info = await h.call("documents.pdfInfo", ["path": "out/report.pdf"])261 #expect(info.output["pageCount"] == .int(pages) && info.output["title"] == "Quarterly Report" && info.output["author"] == "ORIX Test")262 #expect(info.output["encrypted"] == false && info.output["pageSize"]?["widthPoints"]?.doubleValue.map { abs($0 - 595.28) < 0.5 } == true)263264 let first = await h.call("documents.pdfText", ["path": "out/report.pdf", "firstPage": 1, "lastPage": 1])265 let text = first.output["pages"]?[0]?["text"]?.stringValue ?? ""266 #expect(first.output["hasText"] == true && first.untrusted)267 for needle in ["Quarterly Report", "Findings", "First bullet", "Numbered two", "Notebook", "12.00", "let total", "Blue square", "ZEBRA-MARKER", "1 / \(pages)"] {268 #expect(text.contains(needle), "page 1 should contain \(needle)")269 }270 #expect(!text.contains("**") && !text.contains("```") && !text.contains("|---"), "markup is rendered, not printed")271 let all = await h.call("documents.pdfText", ["path": "out/report.pdf"])272 let joined = (all.output["pages"]?.arrayValue ?? []).compactMap { $0["text"]?.stringValue }.joined(separator: "\n")273 #expect(all.output["pages"]?.arrayValue?.count == pages && joined.contains("Paragraph 140 of the long section") && joined.contains("\(pages) / \(pages)"))274 let bounded = await h.call("documents.pdfText", ["path": "out/report.pdf", "maxBytes": 200])275 #expect(bounded.truncated)276 #expect(await h.call("documents.pdfText", ["path": "out/report.pdf", "firstPage": 999]).status == .invalidInput)277278 // The image really is embedded: the first page of the PDF draws an image XObject.279 let doc = try #require(PDFDocument(url: h.tmp.file("out/report.pdf")))280 #expect(doc.pageCount == pages)281 #expect(await h.call("documents.renderPDF", ["destination": "out/report.pdf", "content": "x"]).status == .invalidInput, "no silent overwrite")282 let dry = await h.call("documents.renderPDF", ["destination": "out/other.pdf", "content": "# Only a title"], dryRun: true)283 #expect(dry.status == .simulated && dry.output["pages"] == 1 && !h.exists("out/other.pdf"))284 let over = await h.call("documents.renderPDF", ["destination": "out/report.pdf", "content": "Short.", "overwrite": true])285 #expect(over.output["pages"] == 1 && over.output["backupPath"]?.stringValue != nil)286 #expect(await h.call("documents.renderPDF", ["destination": "x.pdf", "content": ""]).failure?.code == "not_found")287 #expect(await h.call("documents.renderPDF", ["destination": "x.pdf", "content": ""]).status == .unsupported)288 #expect(await h.call("documents.renderPDF", ["destination": "x.txt", "content": "x"]).status == .invalidInput)289 try h.tmp.write("not.pdf", "plain text")290 #expect(await h.call("documents.pdfInfo", ["path": "not.pdf"]).status == .unsupported)291 }292293 @Test func renderPDFDeclaresImageReadCapabilities() async throws {294 let policy = ApprovalPolicy(denyTools: ["documents.renderPDF"])295 let h = try await Harness(policy: policy); defer { h.tmp.remove() }296 _ = await h.call("documents.renderPDF", ["destination": "r.pdf", "content": "\n\ntext\n\n"])297 let capabilities = Set(await policy.requests.last?.capabilities ?? [])298 #expect(capabilities == [.fileWrite(path: h.path("r.pdf")), .fileRead(path: NSHomeDirectory() + "/Pictures/b.png"), .fileRead(path: h.path("img/a.png"))])299 }300301 @Test func imageInfoAndConvert() async throws {302 let h = try await Harness(); defer { h.tmp.remove() }303 try Fixtures.image(at: h.tmp.file("in/a.png"), width: 400, height: 200)304 try Fixtures.image(at: h.tmp.file("in/b.png"), width: 100, height: 300)305 let info = await h.call("documents.imageInfo", ["path": "in/a.png"])306 #expect(info.output["width"] == 400 && info.output["height"] == 200 && info.output["format"] == "png" && info.output["typeIdentifier"] == "public.png")307 let single = await h.call("documents.imageConvert", ["source": "in/a.png", "destination": "out/a.jpg", "maxDimension": 100, "quality": 0.6])308 #expect(single.status == .succeeded && single.output["converted"]?[0]?["width"] == 100 && single.output["converted"]?[0]?["height"] == 50, "\(single.failure?.message ?? "")")309 let jpg = await h.call("documents.imageInfo", ["path": "out/a.jpg"])310 #expect(jpg.output["format"] == "jpeg" && jpg.output["width"] == 100)311 #expect(await h.call("documents.imageConvert", ["source": "in/a.png", "destination": "out/a.jpg"]).status == .invalidInput, "existing output is kept")312 let batchDry = await h.call("documents.imageConvert", ["paths": ["in/a.png", "in/b.png"], "destination": "tiffs", "format": "tiff"], dryRun: true)313 #expect(batchDry.status == .simulated && !h.exists("tiffs"))314 let batch = await h.call("documents.imageConvert", ["paths": ["in/a.png", "in/b.png"], "destination": "tiffs", "format": "tiff"])315 #expect(batch.output["count"] == 2 && h.exists("tiffs/a.tiff") && h.exists("tiffs/b.tiff"))316 #expect(await h.call("documents.imageInfo", ["path": "tiffs/b.tiff"]).output["height"] == 300, "no upscaling or resizing without maxDimension")317 #expect(h.exists("in/a.png") && h.exists("in/b.png"), "sources are untouched")318 // A batch that would collide or overwrite does nothing at all.319 try Fixtures.image(at: h.tmp.file("other/a.png"), width: 10, height: 10)320 #expect(await h.call("documents.imageConvert", ["paths": ["in/a.png", "other/a.png"], "destination": "gifs", "format": "gif"]).status == .invalidInput)321 try h.tmp.write("pngs/b.png", "placeholder")322 let blocked = await h.call("documents.imageConvert", ["paths": ["in/a.png", "in/b.png"], "destination": "pngs", "format": "png"])323 #expect(blocked.status == .invalidInput && !h.exists("pngs/a.png") && h.read("pngs/b.png") == "placeholder")324 #expect(await h.call("documents.imageConvert", ["source": "in/a.png", "destination": "in/a.png", "format": "png"]).status == .invalidInput)325 #expect(await h.call("documents.imageConvert", ["source": "in/a.png", "paths": ["in/b.png"], "destination": "x.png"]).status == .invalidInput)326 try h.tmp.write("fake.png", "not an image")327 #expect(await h.call("documents.imageInfo", ["path": "fake.png"]).status == .unsupported)328 #expect(await h.call("documents.imageConvert", ["source": "fake.png", "destination": "f.jpg"]).status == .unsupported)329 }330331 @Test func xlsxReadSheetsValuesAndHonestLimits() async throws {332 let h = try await Harness(); defer { h.tmp.remove() }333 _ = try await Fixtures.workbook(in: h.tmp)334 let r = await h.call("documents.xlsxRead", ["path": "book.xlsx"])335 #expect(r.status == .succeeded, "\(r.failure?.message ?? "")")336 #expect(r.output["sheet"] == "Data" && r.output["sheets"] == ["Data", "Other & more"] && r.output["rowNumbers"] == [1, 2, 3, 5])337 let rows = r.output["rows"]?.arrayValue ?? []338 #expect(rows.count == 4 && rows[0] == ["Name", "Qty", .null, .null, .null])339 #expect(rows[1] == ["Widget <A>", 3, .null, .null, .null])340 #expect(rows[2] == ["Inline", 4.5, 7.5, .null, .null])341 #expect(rows[3] == ["Bold plain", true, .null, 45000, "#DIV/0!"])342 let notes = (r.output["unsupported"]?.arrayValue ?? []).compactMap(\.stringValue).joined(separator: " | ")343 #expect(notes.contains("serial numbers") && notes.contains("1 formula") && notes.contains("1 merged") && notes.contains("Excel errors"))344 let second = await h.call("documents.xlsxRead", ["path": "book.xlsx", "sheet": "Other & more"])345 #expect(second.output["rows"] == [["second sheet"]] && second.output["unsupported"] == [])346 #expect(await h.call("documents.xlsxRead", ["path": "book.xlsx", "sheet": "2"]).output["sheet"] == "Other & more")347 let limited = await h.call("documents.xlsxRead", ["path": "book.xlsx", "limit": 2])348 #expect(limited.output["rowCount"] == 2 && limited.truncated)349 #expect(await h.call("documents.xlsxRead", ["path": "book.xlsx", "sheet": "Nope"]).failure?.code == "not_found")350 try h.tmp.write("legacy.xls", "x"); try h.tmp.write("fake.xlsx", "this is not a zip archive")351 #expect(await h.call("documents.xlsxRead", ["path": "legacy.xls"]).status == .unsupported)352 #expect(await h.call("documents.xlsxRead", ["path": "fake.xlsx"]).status == .unsupported)353 #expect(await h.call("filesystem.archive", ["source": "in-none", "destination": "x.zip"]).failure?.code == "not_found")354 }355}356357@Suite("Document codecs (unit)")358struct DocumentCodecTests {359 @Test func csvCodecEdgeCases() {360 #expect(CSVCodec.parse("a,b\n1,2") == [["a", "b"], ["1", "2"]])361 #expect(CSVCodec.parse("a,b\r\n1,2\r\n") == [["a", "b"], ["1", "2"]])362 #expect(CSVCodec.parse("\u{FEFF}x,\"\",z\n") == [["x", "", "z"]])363 #expect(CSVCodec.parse("\"a\"\"b\",\"c,d\",\"e\r\nf\"") == [["a\"b", "c,d", "e\r\nf"]])364 #expect(CSVCodec.parse("a,,\n,,") == [["a", "", ""], ["", "", ""]])365 #expect(CSVCodec.parse("").isEmpty)366 #expect(CSVCodec.parse("x;y", delimiter: ";") == [["x", "y"]])367 let rows = [["plain", "with,comma", "with \"quote\"", "multi\r\nline", " padded "], ["", "é", "", "", ""]]368 #expect(CSVCodec.parse(CSVCodec.encode(rows)) == rows)369 #expect(CSVCodec.detectDelimiter("a;b;c\n1;2;3") == ";" && CSVCodec.detectDelimiter("a\tb\n1\t2") == "\t" && CSVCodec.detectDelimiter("single") == ",")370 #expect(CSVCodec.looksLikeHeader([["name", "age"], ["Ann", "3"]]) && !CSVCodec.looksLikeHeader([["1", "2"]]) && !CSVCodec.looksLikeHeader([["a", "a"]]))371 }372373 @Test func jsonQueryEvaluation() throws {374 let doc: JSONValue = ["a": ["b": [10, 20, 30]], "list": [["k": "x", "n": 1], ["k": "y", "n": 2]], "0": "zero"]375 #expect(try JSONQuery.evaluate(doc, query: "$.a.b[2]").values == [30])376 #expect(try JSONQuery.evaluate(doc, query: "a.b.*").values == [10, 20, 30])377 #expect(try JSONQuery.evaluate(doc, query: "list[?n>=2].k").values == ["y"])378 #expect(try JSONQuery.evaluate(doc, query: "list[?@.k=='x'].n").values == [1])379 #expect(try JSONQuery.evaluate(doc, query: "list[?missing==1]").values.isEmpty)380 #expect(try JSONQuery.evaluate(doc, query: "a.b[9]").values.isEmpty)381 #expect(try JSONQuery.evaluate(doc, query: "a.b").multiple == false && JSONQuery.evaluate(doc, query: "list[*]").multiple)382 #expect(throws: ToolError.self) { try JSONQuery.parse("a[0") }383 }384385 @Test func markdownBlocks() {386 let blocks = PDFBlock.parse("# Title\n\npara one\nstill para\n\n- a\n- b\n3. c\n\n```\ncode | not table\n# not heading\n```\n| h1 | h2 |\n|:--|--:|\n| 1 | 2 |\n\n\n***\ntail")387 #expect(blocks == [388 .heading(level: 1, text: "Title"), .paragraph("para one still para"), .bullet(marker: "•", text: "a"), .bullet(marker: "•", text: "b"),389 .bullet(marker: "3.", text: "c"), .code("code | not table\n# not heading"), .table(rows: [["h1", "h2"], ["1", "2"]], hasHeader: true),390 .image(alt: "cap", path: "p/i.png"), .rule, .paragraph("tail"),391 ])392 #expect(PDFBlock.imagePaths("\n\ntext \n\n") == ["one.png", "two.png"])393 }394395 @Test func xlsxColumnReferences() {396 #expect(XLSXSheetParser.column("A1") == 0 && XLSXSheetParser.column("Z9") == 25 && XLSXSheetParser.column("AA10") == 26 && XLSXSheetParser.column("12") == nil)397 }398}399