SPB Git

Repository Public License

IntegrationFixesTests.swift 33.6 KB 436 lines swift

1// ORIX Convert — Packages/ORIXConvertCore/Tests/ConvertCoreTests/IntegrationFixesTests.swift2// Description: Cross-component fixes found when every engine was wired together: container / parent formats, detector3//              order, KMZ, folder outputs, multi-file outputs, same-format conversions, generic (any-file) operations,4//              chain rules, inspector choice, tool lookup, secrets in watch rules.5// Author: Simon-Pierre Boucher — contact@spboucher.ai6import Foundation7import Testing8@testable import ConvertCore910/// A small engine with the shapes the fixes are about.11/// - folder → zip #archive.create (several sources): writes a real stored ZIP of the sources' names and contents.12/// - folder → gz #archive.compress (any file): writes a gzip of the file.13/// - txt → txt: appends the `suffix` option (same-format conversion with a setting).14/// - woff → ttf: writes an OpenType/CFF font named `.otf` (the flavour decides the extension).15/// - zip → folder #archive.extract: `layout` option picks what the folder holds.16private struct ShapesEngine: ConversionEngine {17    let id = "shapes"18    let displayName = "Shapes"19    var paths: [ConversionPath] {20        [21            ConversionPath(.folder, .zip, engine: id, operation: "archive.create", title: "Create ZIP archive", fidelity: .lossless, acceptsMultipleSources: true),22            ConversionPath(.folder, .gz, engine: id, operation: "archive.compress", title: "Compress to gzip", fidelity: .lossless, appliesToAnyFile: true),23            ConversionPath(.txt, .txt, engine: id, fidelity: .lossless),24            ConversionPath(.woff, .ttf, engine: id, fidelity: .lossless),25            ConversionPath(.zip, .folder, engine: id, operation: "archive.extract", title: "Extract", fidelity: .lossless),26        ]27    }28    func options(for path: ConversionPath) -> [OptionDescriptor] {29        path.source == .txt ? [OptionDescriptor("suffix", label: "Suffix", kind: .text(default: "", placeholder: "")),30                               OptionDescriptor("password", label: "Password", kind: .text(default: "", placeholder: ""))] : []31    }32    func inspect(_ url: URL, format: FormatID) async throws -> FileInspection { FileInspection(format: format) }33    func convert(_ request: ConversionRequest, context: ConversionContext) async throws -> ConversionOutput {34        let fm = FileManager.default35        switch (request.destinationFormat, request.operation) {36        case (.zip, "archive.create"?):37            let entries = try request.allSources.map { (name: $0.lastPathComponent, data: [UInt8](try Data(contentsOf: $0))) }38            let out = context.outputURL(for: request)39            try Data(Fixtures.zip(entries)).write(to: out)40            return ConversionOutput(file: out, facts: ["entries": String(entries.count)])41        case (.gz, "archive.compress"?):42            let out = context.outputURL(for: request)43            try Data(Fixtures.gzip([UInt8](try Data(contentsOf: request.source)))).write(to: out)44            return ConversionOutput(file: out)45        case (.txt, nil):46            let out = context.outputURL(for: request)47            try ((try String(contentsOf: request.source, encoding: .utf8)) + request.options.string("suffix")).write(to: out, atomically: true, encoding: .utf8)48            return ConversionOutput(file: out)49        case (.ttf, nil):50            let out = context.outputURL(for: request, extension: "otf")51            try Data(ascii("OTTO") + be16(1) + zeros(64)).write(to: out)52            context.warn(ConversionWarning(.info, code: "font-flavour", "This font has CFF outlines, saved as .otf."))53            return ConversionOutput(file: out)54        case (.folder, "archive.extract"?):55            let dir = context.outputURL(for: request, extension: "")56            try fm.createDirectory(at: dir.appendingPathComponent("empty/inner"), withIntermediateDirectories: true)57            if request.options.string("layout") == "dangling" {58                try fm.createSymbolicLink(atPath: dir.appendingPathComponent("later").path, withDestinationPath: "empty/not-there-yet")59            }60            return ConversionOutput(file: dir)61        default:62            throw ConversionError.unsupportedPath(source: request.sourceFormat, destination: request.destinationFormat)63        }64    }65}6667@Suite struct ContainerFormatTests {68    let temp = TempDir("containers")69    private let docxParts: [(name: String, data: [UInt8])] = [("[Content_Types].xml", ascii("<Types>wordprocessingml.document</Types>")), ("word/document.xml", ascii("<w:document/>"))]7071    @Test func parentFormatsAcceptTheirChildrenAndNotTheReverse() {72        for child in [FormatID.docx, .xlsx, .pptx, .odt, .ods, .odp, .epub, .threeMF, .usdz, .kmz] {73            #expect(FormatEquivalence.accepts(detected: child, as: .zip), "\(child.rawValue) content is a valid zip")74            #expect(!FormatEquivalence.accepts(detected: .zip, as: child), "a plain zip is not a \(child.rawValue)")75            #expect(FormatEquivalence.parents(of: child) == [.zip])76        }77        for child in [FormatID.svg, .kml, .gpx, .dae, .plist] { #expect(FormatEquivalence.accepts(detected: child, as: .xml)) }78        #expect(FormatEquivalence.accepts(detected: .geojson, as: .json) && FormatEquivalence.accepts(detected: .gltf, as: .json))79        #expect(FormatEquivalence.accepts(detected: .geopackage, as: .sqlite) && FormatEquivalence.accepts(detected: .tgz, as: .gz))80        #expect(FormatEquivalence.accepts(detected: .svg, as: .txt), "any textual format where plain text is expected")81        #expect(FormatEquivalence.isParent(.zip, of: .docx) && !FormatEquivalence.isParent(.zip, of: .zip) && !FormatEquivalence.isParent(.docx, of: .zip))82        #expect(!FormatEquivalence.accepts(detected: .docx, as: .xlsx) && !FormatEquivalence.accepts(detected: .png, as: .zip))83    }8485    @Test func zipOutputsThatLookLikeDocumentsAreValidZips() {86        // What an archive engine writes when asked to zip a USD layer, an unzipped Word document or an EPUB tree.87        let cases: [(String, [(name: String, data: [UInt8])])] = [88            ("usd", [("scene.usda", ascii("#usda 1.0\n")), ("readme.txt", ascii("x"))]),89            ("docx", docxParts),90            ("epub", [("mimetype", ascii("application/epub+zip")), ("OEBPS/a.xhtml", ascii("<html/>"))]),91            ("kml", [("doc.kml", ascii("<kml/>"))]),92        ]93        for (name, entries) in cases {94            let work = temp.subdirectory("work-\(name)")95            let url = work.appendingPathComponent("archive.zip")96            try? Data(Fixtures.zip(entries)).write(to: url)97            let checks = OutputValidator.validate(ConversionOutput(file: url), destinationFormat: .zip, workDirectory: work)98            #expect(checks.allSatisfy { $0.passed }, "\(name): \(checks.filter { !$0.passed }.map(\.detail))")99            // Under another name the content is still seen for what it looks like, and is still an acceptable zip.100            let other = work.appendingPathComponent("archive.bin")101            try? Data(Fixtures.zip(entries)).write(to: other)102            #expect(OutputValidator.formatCheck(other, expected: .zip).passed)103        }104        // And a zip is still not accepted where a document was announced.105        let work = temp.subdirectory("work-plain")106        let plain = work.appendingPathComponent("report.docx")107        try? Data(Fixtures.zip([("a.txt", ascii("x"))])).write(to: plain)108        #expect(!OutputValidator.validate(ConversionOutput(file: plain), destinationFormat: .docx, workDirectory: work).allSatisfy { $0.passed })109    }110111    @Test func aFileNamedZipIsAZipWhateverItsEntriesLookLike() {112        let usd = FileTypeDetector.detect(data: Data(Fixtures.zip([("scene.usdc", ascii("PXR-USDC")), ("b.txt", ascii("x"))])), fileName: "models.zip")113        #expect(usd.format == .zip && !usd.mismatch && usd.confidence == .certain && usd.evidence.contains("USD layer"))114        let docx = FileTypeDetector.detect(data: Data(Fixtures.zip(docxParts)), fileName: "unzipped-report.zip")115        #expect(docx.format == .zip && !docx.mismatch)116        // The same bytes under their own name, or under no telling name, keep the precise format.117        #expect(FileTypeDetector.detect(data: Data(Fixtures.zip(docxParts)), fileName: "report.docx").format == .docx)118        #expect(FileTypeDetector.detect(data: Data(Fixtures.zip(docxParts)), fileName: "report").format == .docx)119        #expect(FileTypeDetector.detect(data: Data(Fixtures.zip([("scene.usdc", ascii("PXR-USDC"))])), fileName: "model.usdz").format == .usdz)120        // Other parents keep the child: an SVG named .xml stays an SVG (nothing is lost by being precise there).121        #expect(FileTypeDetector.detect(data: Data(ascii("<?xml version=\"1.0\"?><svg xmlns=\"http://www.w3.org/2000/svg\"/>")), fileName: "drawing.xml").format == .svg)122    }123124    @Test func kmzIsDetectedFromItsContent() {125        let doc = FileTypeDetector.detect(data: Data(Fixtures.zip([("doc.kml", ascii("<kml/>")), ("files/icon.png", [0x89])])), fileName: "places.kmz")126        #expect(doc.format == .kmz && doc.confidence == .certain && !doc.mismatch)127        let top = FileTypeDetector.detect(data: Data(Fixtures.zip([("Route.KML", ascii("<kml/>"))])), fileName: "route.kmz")128        #expect(top.format == .kmz)129        #expect(FileTypeDetector.detect(data: Data(Fixtures.zip([("doc.kml", ascii("<kml/>"))])), fileName: "noname").format == .kmz)130        // A KML only in a sub-folder is an ordinary archive; a .kmz without any KML is flagged.131        #expect(FileTypeDetector.detect(data: Data(Fixtures.zip([("maps/a.kml", ascii("<kml/>"))])), fileName: "bundle.zip").format == .zip)132        let fake = FileTypeDetector.detect(data: Data(Fixtures.zip([("a.txt", ascii("x"))])), fileName: "fake.kmz")133        #expect(fake.format == .zip && fake.mismatch)134        let entry = FormatCatalog.format(.kmz)135        #expect(entry?.category == .geospatial && entry?.preferredExtension == "kmz" && entry?.mime == "application/vnd.google-earth.kmz")136        #expect(FormatCatalog.formats(forExtension: "kmz").map(\.id) == [.kmz])137    }138139    @Test func containersHoldingASmallPDFAreNotPDFs() {140        let pdf = "%PDF-1.4\n1 0 obj<<>>endobj\ntrailer<<>>\n%%EOF\n"141        // TAR: the member's bytes start at offset 512.142        let tar = FileTypeDetector.detect(data: Data(Fixtures.tar(name: "paper.pdf", content: pdf)), fileName: "paper.tar")143        #expect(tar.format == .tar && tar.confidence == .certain && !tar.mismatch)144        #expect(FileTypeDetector.detect(data: Data(Fixtures.tar(name: "paper.pdf", content: pdf)), fileName: "paper").format == .tar)145        // Zstandard frame with a raw (uncompressed) block: magic, frame header, block header, then the PDF bytes.146        let raw = ascii(pdf)147        let zst: [UInt8] = [0x28, 0xB5, 0x2F, 0xFD, 0x20, UInt8(raw.count)] + [UInt8((raw.count << 3) & 0xFF | 1), UInt8((raw.count >> 5) & 0xFF), UInt8((raw.count >> 13) & 0xFF)] + raw148        #expect(FileTypeDetector.detect(data: Data(zst), fileName: "paper.pdf.zst").format == .zst)149        #expect(FileTypeDetector.detect(data: Data(zst), fileName: "paper").format == .zst)150        // gzip with a stored deflate block, and a ZIP with a stored entry.151        let stored: [UInt8] = [0x1F, 0x8B, 0x08, 0, 0, 0, 0, 0, 0, 3, 0x01] + le16(raw.count) + le16(~raw.count & 0xFFFF) + raw + zeros(8)152        #expect(FileTypeDetector.detect(data: Data(stored), fileName: "paper.pdf.gz").format == .gz)153        #expect(FileTypeDetector.detect(data: Data(Fixtures.zip([("paper.pdf", raw)])), fileName: "paper.zip").format == .zip)154        // The lenient rule itself still works: junk before the header within the first kilobyte.155        let junk = FileTypeDetector.detect(data: Data([0x00, 0x01, 0x02] + ascii("garbage\n") + raw), fileName: "scan.pdf")156        #expect(junk.format == .pdf && junk.confidence == .likely)157        #expect(FileTypeDetector.detect(data: Data(raw), fileName: "x.pdf").confidence == .certain)158    }159160    @Test func trueTypeAndOpenTypeStandForEachOther() {161        #expect(FormatEquivalence.accepts(detected: .otf, as: .ttf) && FormatEquivalence.accepts(detected: .ttf, as: .otf))162        #expect(!FormatEquivalence.accepts(detected: .woff, as: .ttf))163    }164}165166@Suite struct FolderOutputTests {167    let temp = TempDir("folder-outputs")168    private func validate(_ name: String) -> [ValidationCheck] {169        OutputValidator.validate(ConversionOutput(file: temp.file(name)), destinationFormat: .folder, workDirectory: temp.url)170    }171172    @Test func aFolderOfEmptyFoldersIsAnOutputAndAnEmptyFolderIsNot() {173        _ = temp.subdirectory("tree/a/b")174        _ = temp.subdirectory("tree/c")175        let checks = validate("tree")176        #expect(checks.allSatisfy { $0.passed } && checks.contains { $0.name == "output.not-empty" && $0.detail.contains("no regular file") })177        _ = temp.subdirectory("bare")178        #expect(validate("bare").contains { $0.name == "output.not-empty" && !$0.passed })179    }180181    @Test func danglingLinksInsideAreInsideAndEscapesAreNot() throws {182        let fm = FileManager.default183        temp.write("tree/real/file.txt", "x")184        // Dangling, lexically inside: to a missing name, through a missing folder, and through a regular file.185        try fm.createSymbolicLink(atPath: temp.file("tree/missing").path, withDestinationPath: "nowhere.txt")186        try fm.createSymbolicLink(atPath: temp.file("tree/real/up").path, withDestinationPath: "../ghost/deep/file")187        try fm.createSymbolicLink(atPath: temp.file("tree/through-file").path, withDestinationPath: "real/file.txt/below")188        try fm.createSymbolicLink(atPath: temp.file("tree/absolute-inside").path, withDestinationPath: temp.file("tree/not-yet").path)189        #expect(validate("tree").allSatisfy { $0.passed }, "\(validate("tree").filter { !$0.passed }.map(\.detail))")190        // Escapes stay refused, whether or not the target exists.191        for (name, target) in [("up-out", "../outside.txt"), ("up-out-missing", "../../no/such/place"), ("abs", "/etc/hosts"), ("abs-missing", "/no/such/file"),192                               ("sneaky", "real/../../outside.txt")] {193            try fm.createSymbolicLink(atPath: temp.file("tree/\(name)").path, withDestinationPath: target)194            let checks = validate("tree")195            #expect(checks.contains { $0.name == "output.no-symlink-escape" && !$0.passed && $0.detail.contains(name) }, "\(name) → \(target)")196            try fm.removeItem(at: temp.file("tree/\(name)"))197        }198        // An existing link inside that leads outside makes a path through it an escape too.199        temp.write("outside/secret.txt", "s")200        try fm.createSymbolicLink(atPath: temp.file("tree/door").path, withDestinationPath: temp.file("outside").path)201        try fm.createSymbolicLink(atPath: temp.file("tree/via-door").path, withDestinationPath: "door/secret.txt")202        let viaDoor = validate("tree")203        #expect(viaDoor.contains { $0.name == "output.no-symlink-escape" && !$0.passed && $0.detail.contains("door") && $0.detail.contains("via-door") })204    }205206    @Test func theQueueDeliversAFolderOfEmptyFoldersWithADanglingLink() async throws {207        let bed = TestBed(extraEngines: [ShapesEngine()])208        let zip = bed.temp.write("in/empty.zip", bytes: Fixtures.zip([("a.txt", ascii("x"))]))209        var request = ConversionJobRequest(sources: [zip], sourceFormat: .zip, destinationFormat: .folder, operation: "archive.extract",210                                           options: ConversionOptions(["layout": .string("dangling")]), destination: bed.destination())211        request.engineID = "shapes"212        let job = try #require(await bed.queue.waitForJob(await bed.queue.enqueue(request)))213        #expect(job.state == .finished, "\(job.error ?? "")")214        #expect(bed.temp.exists("out/empty/empty/inner") && (try? FileManager.default.destinationOfSymbolicLink(atPath: bed.temp.file("out/empty/later").path)) == "empty/not-there-yet")215    }216}217218@Suite struct GenericOperationTests {219    @Test func folderSourcedOperationsAreOfferedForAnySelection() async throws {220        let bed = TestBed(extraEngines: [ShapesEngine()])221        let resolver = ConversionCapabilityResolver(registry: bed.registry)222        let generic = resolver.genericOperations()223        #expect(generic.forSelection.map(\.id) == ["zip#archive.create"] && generic.forSelection.first?.title == "Create ZIP archive")224        #expect(generic.forEachFile.map(\.id) == ["gz#archive.compress"])225        // The analysis carries them for every drop, including files nothing recognises; a folder is not offered226        // the per-file operation.227        let unknown = bed.temp.write("in/blob.xyz", bytes: [0, 1, 2, 3, 250, 251])228        let note = bed.source("note.txt")229        _ = bed.temp.write("in/album/one.txt", "1")230        let analysis = await SmartDropAnalyzer(registry: bed.registry).analyze([unknown, note, bed.temp.file("in/album")])231        #expect(analysis.selectionOperations.map(\.id) == ["zip#archive.create"] && analysis.fileOperations.map(\.id) == ["gz#archive.compress"])232        #expect(analysis.items.first { $0.url == unknown }?.format == nil)233        #expect(analysis.folders.first?.targets.map(\.id) == ["zip#archive.create"])234        // The registry holds ONE path per destination for them, not one per format.235        #expect(bed.registry.paths(to: .zip).count == 1 && bed.registry.paths(to: .gz).count == 1)236    }237238    @Test func aMixedSelectionBecomesOneArchiveAndEachFileItsOwnStream() async throws {239        let bed = TestBed(extraEngines: [ShapesEngine()])240        let manager = ConversionJobManager(engines: [TextEngine(), ShapesEngine()], workDirectories: bed.workDirectories)241        let generic = manager.resolver.genericOperations()242        let files = [bed.temp.write("in/paper.pdf", "%PDF-1.4\n%%EOF\n"), bed.temp.write("in/picture.png", bytes: [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]), bed.temp.write("in/blob", bytes: [9, 9, 9])]243        let create = try #require(generic.forSelection.first)244        let ids = await manager.enqueueGeneric(files, target: create, destination: bed.destination())245        #expect(ids.count == 1)246        let job = try #require(await manager.queue.waitForJob(ids[0]))247        #expect(job.state == .finished && job.sourceFormat == .folder && job.request.sources.count == 3, "\(job.error ?? "")")248        let archive = try #require(job.outputs.first?.url)249        #expect(archive.lastPathComponent == "paper.zip" && FileTypeDetector.detect(archive).format == .zip && job.facts["entries"] == "3")250        // Per-file operation: one job per file, each compressed whatever it is.251        let compress = try #require(generic.forEachFile.first)252        let streams = await manager.enqueueGeneric(Array(files.prefix(2)), target: compress, destination: bed.destination())253        #expect(streams.count == 2)254        for id in streams { #expect(await manager.queue.waitForJob(id)?.state == .finished) }255        #expect(bed.temp.list("out").sorted() == ["paper.gz", "paper.zip", "picture.gz"])256    }257}258259@Suite struct SameFormatTests {260    @Test func theRuleRunsOnlyWhenAPathExistsAndASettingDiffers() {261        let registry = FormatRegistry(engines: [TextEngine(), ShapesEngine()])262        #expect(SameFormatRule.decide(format: .txt, options: .init(), registry: registry) == .passThrough)263        #expect(SameFormatRule.decide(format: .txt, options: ConversionOptions(["suffix": .string("")]), registry: registry) == .passThrough, "the default value changes nothing")264        #expect(SameFormatRule.decide(format: .txt, options: ConversionOptions(["unknown.key": .int(3)]), registry: registry) == .passThrough, "not an option of the path")265        #expect(SameFormatRule.decide(format: .txt, options: ConversionOptions(["suffix": .string("!")]), registry: registry) == .run)266        // No markdown → markdown path: nothing can run; settings given for it are reported as ignored.267        #expect(SameFormatRule.decide(format: .markdown, options: .init(), registry: registry) == .passThrough)268        #expect(SameFormatRule.decide(format: .markdown, options: ConversionOptions(["suffix": .string("!")]), registry: registry) == .settingsIgnored)269    }270271    @Test func aWorkflowAppliesItsSettingsToFilesAlreadyInTheDestinationFormat() async throws {272        let bed = TestBed(extraEngines: [ShapesEngine()])273        let runner = WorkflowRunner(queue: bed.queue, history: bed.history, workDirectories: bed.workDirectories)274        let out = bed.temp.subdirectory("out")275        let input = bed.temp.write("in/already.txt", "body")276        // With a setting: the txt → txt path runs.277        let withSetting = Workflow(name: "Suffix", nodes: [.convert(destination: .txt, operation: nil, options: ConversionOptions(["suffix": .string(" [done]")])),278                                                           .export(folder: out, conflictPolicy: .replace)])279        let first = try await runner.run(withSetting, inputs: [input])280        #expect(first.items[0].status == .succeeded && first.items[0].jobIDs.count == 1 && bed.temp.read("out/already.txt") == "body [done]")281        // Without one: the file passes through untouched, no job.282        let plain = Workflow(name: "Plain", nodes: [.convert(destination: .txt, operation: nil, options: .init()), .export(folder: out, conflictPolicy: .replace)])283        let second = try await runner.run(plain, inputs: [input])284        #expect(second.items[0].jobIDs.isEmpty && bed.temp.read("out/already.txt") == "body")285        // Settings for a format that has no same-format path: passed through, and said.286        let markdown = Workflow(name: "MD", nodes: [.convert(destination: .markdown, operation: nil, options: ConversionOptions(["suffix": .string("!")])),287                                                    .export(folder: out, conflictPolicy: .replace)])288        let third = try await runner.run(markdown, inputs: [bed.temp.write("in/ready.md", "# T\n")])289        #expect(third.items[0].jobIDs.isEmpty && third.items[0].warnings.map(\.code) == ["same-format-settings-ignored"] && bed.temp.read("out/ready.md") == "# T\n")290    }291}292293@Suite struct OutputNamingTests {294    @Test func anEquivalentExtensionChosenByTheEngineIsKept() async throws {295        let bed = TestBed(extraEngines: [ShapesEngine()])296        let woff = bed.temp.write("in/face.woff", bytes: ascii("wOFF") + zeros(40))297        let job = try #require(await bed.queue.waitForJob(await bed.queue.enqueue(298            ConversionJobRequest(sources: [woff], sourceFormat: .woff, destinationFormat: .ttf, destination: bed.destination(template: "{name}-unpacked")))))299        #expect(job.state == .finished, "\(job.error ?? "")")300        #expect(job.outputs.map(\.url.lastPathComponent) == ["face-unpacked.otf"] && bed.temp.list("out") == ["face-unpacked.otf"])301        #expect(job.warnings.map(\.code) == ["font-flavour"])302        #expect(job.checks.contains { $0.name == "output.format" && $0.passed && $0.detail.contains("otf") })303    }304}305306@Suite struct ChainRuleTests {307    private func path(_ s: FormatID, _ d: FormatID, _ engine: String, multiple: Bool = false) -> ConversionPath {308        ConversionPath(s, d, engine: engine, fidelity: .lossless, producesMultipleOutputs: multiple)309    }310311    @Test func neverChainsAfterAStepThatCanYieldSeveralFiles() {312        // By the flag.313        let flagged = FormatRegistry(engines: [StubEngine(id: "a", paths: [path(.txt, .csv, "a", multiple: true)]), StubEngine(id: "b", paths: [path(.csv, .parquet, "b")])])314        #expect(ConversionPlanner(registry: flagged).chains(from: .txt, to: .parquet).isEmpty)315        #expect(throws: ConversionError.unsupportedPath(source: .txt, destination: .parquet)) { try ConversionPlanner(registry: flagged).plan(from: .txt, to: .parquet) }316        // Without the flag: a paged source to a still image (one PNG per slide / page).317        let paged = FormatRegistry(engines: [StubEngine(id: "office", paths: [path(.pptx, .png, "office"), path(.pdf, .jpeg, "office"), path(.pptx, .pdf, "office")]),318                                             StubEngine(id: "image", paths: [path(.png, .apng, "image"), path(.jpeg, .webp, "image"), path(.pdf, .png, "image")])])319        let planner = ConversionPlanner(registry: paged)320        #expect(planner.chains(from: .pptx, to: .apng).isEmpty && planner.chains(from: .pdf, to: .webp).isEmpty)321        #expect(ConversionPlanner.mayProduceSeveralFiles(path(.pptx, .png, "x")) && ConversionPlanner.mayProduceSeveralFiles(path(.xlsx, .svg, "x")))322        #expect(!ConversionPlanner.mayProduceSeveralFiles(path(.pptx, .pdf, "x")) && !ConversionPlanner.mayProduceSeveralFiles(path(.png, .jpeg, "x")))323        #expect(!ConversionPlanner.mayProduceSeveralFiles(path(.pdf, .tiff, "x")), "TIFF holds several pages")324        // The capabilities do not advertise what the planner refuses.325        #expect(!ConversionCapabilityResolver(registry: paged).capabilities(for: .pptx).chainedDestinations.contains(.apng))326        // pptx → pdf (one file) → png is still a chain.327        #expect(planner.chains(from: .pptx, to: .png).map(\.intermediates) == [[.pdf]])328    }329330    @Test func neverChainsTwoStepsOfTheSameEngine() throws {331        // The font engine reads OTF and writes TTF but declares no OTF → TTF: it does not convert outlines.332        let font = StubEngine(id: "font", paths: [path(.otf, .woff, "font"), path(.woff, .ttf, "font"), path(.otf, .woff2, "font"), path(.woff2, .ttf, "font")])333        let alone = FormatRegistry(engines: [font])334        #expect(ConversionPlanner(registry: alone).chains(from: .otf, to: .ttf).isEmpty)335        #expect(ConversionCapabilityResolver(registry: alone).capabilities(for: .otf).chainedDestinations.isEmpty)336        #expect(throws: ConversionError.unsupportedPath(source: .otf, destination: .ttf)) { try ConversionPlanner(registry: alone).plan(from: .otf, to: .ttf) }337        // Another engine providing the second step makes a real chain.338        let other = StubEngine(id: "other", paths: [path(.woff, .ttf, "other")])339        let plan = try ConversionPlanner(registry: FormatRegistry(engines: [font, other])).plan(from: .otf, to: .ttf)340        #expect(plan.engines == ["font", "other"] && plan.intermediates == [.woff])341    }342343    @Test func pathsStoredBeforeTheNewFlagsStillDecode() throws {344        let old = #"{"source":"png","destination":"jpeg","engine":"image","status":"supported","fidelity":"lossy","preserves":["metadata"],"limitations":[],"acceptsMultipleSources":false}"#345        let decoded = try JSONDecoder().decode(ConversionPath.self, from: Data(old.utf8))346        #expect(decoded == ConversionPath(.png, .jpeg, engine: "image", fidelity: .lossy, preserves: [.metadata]) && !decoded.producesMultipleOutputs && !decoded.appliesToAnyFile)347        let flagged = ConversionPath(.pdf, .png, engine: "pdf", fidelity: .lossy, producesMultipleOutputs: true)348        #expect(try JSONDecoder().decode(ConversionPath.self, from: JSONEncoder().encode(flagged)).producesMultipleOutputs)349    }350}351352@Suite struct InspectorChoiceTests {353    private struct Inspecting: ConversionEngine {354        let id: String355        let displayName = "Inspecting"356        let paths: [ConversionPath]357        let inspectableFormats: Set<FormatID>358        func inspect(_ url: URL, format: FormatID) async throws -> FileInspection { FileInspection(format: format) }359        func convert(_ request: ConversionRequest, context: ConversionContext) async throws -> ConversionOutput { throw ConversionError.internalError("stub") }360    }361362    @Test func theEngineOfTheFormatsOwnCategoryInspectsWhateverTheRegistrationOrder() {363        // The PDF engine accepts images (to build PDFs) and says it can inspect them; the image engine converts them.364        let pdf = Inspecting(id: "pdf", paths: [ConversionPath(.png, .pdf, engine: "pdf", operation: "pdf.from-images", fidelity: .lossless, acceptsMultipleSources: true),365                                                ConversionPath(.pdf, .png, engine: "pdf", fidelity: .lossy), ConversionPath(.pdf, .pdf, engine: "pdf", operation: "pdf.compress", fidelity: .lossy)],366                             inspectableFormats: [.pdf, .png, .jpeg, .bmp])367        let image = Inspecting(id: "image", paths: [ConversionPath(.png, .jpeg, engine: "image", fidelity: .lossy), ConversionPath(.jpeg, .jpeg, engine: "image", operation: "image.optimize", fidelity: .lossy)],368                               inspectableFormats: [.png, .jpeg])369        for order in [[pdf, image], [image, pdf]] {370            let registry = FormatRegistry(engines: order)371            #expect(registry.inspector(for: .png)?.id == "image", "plain same-category conversion")372            #expect(registry.inspector(for: .jpeg)?.id == "image", "same-category operation beats a cross-category consumer")373            #expect(registry.inspector(for: .pdf)?.id == "pdf")374            #expect(registry.inspector(for: .bmp)?.id == "pdf", "the only engine that inspects it")375            #expect(registry.inspector(for: .gif) == nil)376        }377        // Equal claims: registration order decides.378        let twin = Inspecting(id: "twin", paths: [ConversionPath(.png, .tiff, engine: "twin", fidelity: .lossless)], inspectableFormats: [.png])379        #expect(FormatRegistry(engines: [twin, image]).inspector(for: .png)?.id == "twin" && FormatRegistry(engines: [image, twin]).inspector(for: .png)?.id == "image")380    }381}382383@Suite struct ToolLookupTests {384    @Test func aRealApplicationNeverBorrowsToolsFromTheRepository() {385        let source = #filePath        // this file sits in the repository, like Tools.swift386        let id = BundledTools.applicationBundleIdentifier387        func dirs(executable: String, bundle: String, identifier: String? = nil, env: [String: String] = [:]) -> [String] {388            BundledTools.searchDirectories(executable: URL(fileURLWithPath: executable), mainBundle: URL(fileURLWithPath: bundle), bundleIdentifier: identifier, environment: env, sourceFile: source).map(\.path)389        }390        func usesRepository(_ list: [String]) -> Bool { list.contains { $0.hasSuffix("EmbeddedEngines/tools") } }391        // The app: its own Helpers, nothing else — even though the repository is right there.392        #expect(dirs(executable: "/Applications/ORIX Convert.app/Contents/MacOS/ORIX Convert", bundle: "/Applications/ORIX Convert.app", identifier: id) == ["/Applications/ORIX Convert.app/Contents/Helpers"])393        // The CLI inside the app's Helpers: its own directory (its main bundle is that directory, not an .app).394        #expect(dirs(executable: "/tmp/v/ORIX Convert.app/Contents/Helpers/orix-convert", bundle: "/tmp/v/ORIX Convert.app/Contents/Helpers") == ["/tmp/v/ORIX Convert.app/Contents/Helpers"])395        // The override comes first and does not open the repository fallback inside the app.396        #expect(dirs(executable: "/A.app/Contents/MacOS/A", bundle: "/A.app", identifier: id, env: ["ORIX_CONVERT_TOOLS": "/opt/tools"]) == ["/opt/tools", "/A.app/Contents/Helpers"])397        // Package tests and tools run from .build: the repository's EmbeddedEngines/tools.398        #expect(usesRepository(dirs(executable: "/repo/Packages/X/.build/debug/orix-convert", bundle: "/repo/Packages/X/.build/debug")))399        // `swift test` runs from inside Xcode.app (swiftpm-testing-helper, xctest): an `.app` in the path of the400        // executable is not "the shipped application".401        let xcode = "/Applications/Xcode.app/Contents/Developer/usr/libexec/swift/pm"402        #expect(usesRepository(dirs(executable: xcode + "/swiftpm-testing-helper", bundle: xcode)))403        #expect(usesRepository(dirs(executable: "/Applications/Xcode.app/Contents/Developer/usr/bin/xctest", bundle: "/Applications/Xcode.app/Contents/Developer/usr/bin", identifier: "com.apple.xctest")))404        // And it holds for the process running this very test.405        #expect(usesRepository(BundledTools.searchDirectories().map(\.path)), "\(BundledTools.searchDirectories())")406        // Another application hosting the code without a Helpers folder (a test host) keeps the development fallback.407        #expect(usesRepository(dirs(executable: "/Build/Host.app/Contents/MacOS/Host", bundle: "/Build/Host.app", identifier: "com.example.host")))408    }409}410411@Suite struct WatchRuleSecretTests {412    @Test func watchRulesNeverKeepOrStoreSecretOptionValues() async throws {413        let bed = TestBed()414        let store = bed.temp.file("support/watch.json")415        let runner = WorkflowRunner(queue: bed.queue, history: bed.history, workDirectories: bed.workDirectories)416        func watcher() -> FolderWatcher {417            FolderWatcher(runner: runner, presets: PresetStore(fileURL: bed.temp.file("support/presets.json")), workflows: WorkflowLibrary(fileURL: bed.temp.file("support/workflows.json")), storeURL: store)418        }419        let options = ConversionOptions(["suffix": .string("kept"), "password": .string("hunter2-watch"), "output.password.owner": .string("owner-secret-9")])420        let rule = WatchRule(name: "Protected", folder: bed.temp.subdirectory("inbox"), match: .formats([.txt]),421                             action: .convert(destination: .markdown, operation: nil, options: options), destinationFolder: bed.temp.subdirectory("outbox"))422        #expect(rule.redactingSecrets().action == .convert(destination: .markdown, operation: nil, options: ConversionOptions(["suffix": .string("kept")])))423        let first = watcher()424        let added = try await first.addRule(rule)425        guard case .convert(_, _, let kept) = added.action else { Issue.record("action changed"); return }426        #expect(kept.values.keys.sorted() == ["suffix"])427        let text = try String(contentsOf: store, encoding: .utf8)428        #expect(!text.contains("hunter2-watch") && !text.contains("owner-secret-9") && !text.contains("password") && text.contains("kept"))429        // A file written by an older build with a secret in it is cleaned when loaded and at the next save.430        try text.replacingOccurrences(of: "\"suffix\"", with: "\"password\":{\"string\":{\"_0\":\"legacy-secret\"}},\"suffix\"").write(to: store, atomically: true, encoding: .utf8)431        let second = watcher()432        let loaded = await second.rules433        if case .convert(_, _, let reloaded)? = loaded.first?.action { #expect(reloaded.values.keys.sorted() == ["suffix"] || loaded.isEmpty) }434    }435}436
Files main
Menu

Theme