SPB Git

Repository Public License

CommandTests.swift 13.4 KB 222 lines swift

1// ORIX Convert — Packages/ConvertKit/Tests/ConvertKitTests/CommandTests.swift2// Description: Exit codes and JSON output schemas of the read-only commands (formats, paths, detect, inspect, plan,3//              options, presets, doctor, matrix, workflow validate).4// Author: Simon-Pierre Boucher — contact@spboucher.ai5import Foundation6import Testing7import ConvertCore8import ConvertKit9@testable import ConvertCLI1011@Suite struct CommandTests {12    @Test func usageErrorsExitWithTwo() async {13        #expect(await run([]).code == 2)14        #expect(await run(["frobnicate"]).code == 2)15        #expect(await run(["paths", "--bogus"]).code == 2)16        #expect(await run(["plan", "file.png"]).code == 2)17        #expect(await run(["convert", "a.png"]).code == 2)18        #expect(await run(["convert", "a.png", "--to", "notaformat"]).code == 2)19        #expect(await run(["convert", "a.png", "--to", "jpeg", "--on-conflict", "overwrite"]).code == 2)20        #expect(await run(["convert", "a.png", "--to", "jpeg", "--jobs", "0"]).code == 2)21        #expect(await run(["convert", "a.png", "--to", "jpeg", "--output", "x.jpg", "--output-dir", "d"]).code == 2)22        #expect(await run(["formats", "--category", "sandwich"]).code == 2)23        let unknown = await run(["frobnicate"])24        #expect(unknown.capture.err.contains("unknown command"))25        #expect(unknown.capture.out.isEmpty)26    }2728    @Test func helpAndVersion() async {29        let help = await run(["help"])30        #expect(help.code == 0)31        for name in ["formats", "paths", "detect", "inspect", "plan", "convert", "options", "workflow", "presets", "doctor", "matrix"] {32            #expect(help.capture.out.contains("  \(name)"))33        }34        #expect(await run(["convert", "--help"]).capture.out.contains("--on-conflict"))35        #expect(await run(["--version"]).capture.out == "orix-convert \(CLI.version)")36    }3738    @Test func formatsJSON() async throws {39        let result = await run(["formats", "--json"])40        #expect(result.code == 0)41        let output = try result.capture.decode(FormatsCommand.Output.self)42        #expect(output.count == output.formats.count)43        #expect(output.count == Shared.registry().counts.formatsWithPath)44        let png = try #require(output.formats.first { $0.id == "png" })45        #expect(png.readable && png.writable && png.destinations > 0 && png.category == "image")46        let images = try (await run(["formats", "--category", "image", "--json"])).capture.decode(FormatsCommand.Output.self)47        #expect(!images.formats.isEmpty && images.formats.allSatisfy { $0.category == "image" })48    }4950    @Test func pathsJSONMatchesTheRegistry() async throws {51        let registry = Shared.registry()52        let all = try (await run(["paths", "--json"])).capture.decode(PathsCommand.Output.self)53        #expect(all.count == registry.paths.count)54        let filtered = try (await run(["paths", "--from", "png", "--to", "jpg", "--json"])).capture.decode(PathsCommand.Output.self)55        #expect(filtered.count == registry.paths(from: .png, to: .jpeg).count)56        #expect(filtered.paths.allSatisfy { $0.source == "png" && $0.destination == "jpeg" })57        let engine = try (await run(["paths", "--engine", "pdf", "--json"])).capture.decode(PathsCommand.Output.self)58        #expect(engine.paths.allSatisfy { $0.engine == "pdf" } && engine.count == registry.paths(engine: "pdf").count)59        #expect(await run(["paths", "--engine", "nope"]).code == 2)60    }6162    @Test func detectReportsMismatchAndMissingFiles() async throws {63        let scratch = Scratch()64        let png = try scratch.png("picture.png")65        let lying = scratch.file("lying.jpg")66        try FileManager.default.copyItem(at: png, to: lying)67        let garbage = scratch.file("noise.bin")68        try Data([0, 255, 0, 17, 3, 9, 200, 1]).write(to: garbage)69        let rows = try (await run(["detect", png.path, lying.path, "--json"])).capture.decode([DetectCommand.Row].self)70        #expect(rows.map { $0.format } == ["png", "png"])71        #expect(rows[0].mismatch == false && rows[0].confidence == "certain")72        #expect(rows[1].mismatch == true)73        #expect(await run(["detect", png.path]).code == 0)74        #expect(await run(["detect", garbage.path]).code == 3)75        let missing = await run(["detect", scratch.file("absent.png").path])76        #expect(missing.code == 3 && missing.capture.err.contains("does not exist"))77    }7879    @Test func inspectReadsRealMetadata() async throws {80        let scratch = Scratch()81        let png = try scratch.png("picture.png", width: 96, height: 64)82        let result = await run(["inspect", png.path, "--json"])83        #expect(result.code == 0)84        let output = try result.capture.decode(InspectCommand.Output.self)85        #expect(output.engine == "image")86        #expect(output.inspection.pixelWidth == 96 && output.inspection.pixelHeight == 64)87        #expect(output.inspection.fields.contains { $0.group == "File" && $0.key == "Size" })88        #expect(await run(["inspect", scratch.file("absent.png").path]).code == 3)89    }9091    @Test func planDirectChainedAndUnsupported() async throws {92        let scratch = Scratch()93        let png = try scratch.png("picture.png")94        let direct = try (await run(["plan", png.path, "--to", "jpeg", "--json"])).capture.decode(PlanCommand.Output.self)95        #expect(direct.chained == false && direct.steps.count == 1 && direct.fidelity == "lossy" && direct.sourceFormat == "png")96        #expect(!direct.limitations.isEmpty)97        let text = await run(["plan", png.path, "--to", "jpeg"])98        #expect(text.code == 0 && text.capture.out.contains("Plan: png → jpeg") && text.capture.out.contains("Limitations"))99        let wav = scratch.file("tone.wav")100        try Seeds.writeWAV(to: wav, seconds: 0.2)101        let unsupported = await run(["plan", wav.path, "--to", "docx"])102        #expect(unsupported.code == 4)103        #expect(unsupported.capture.err.contains("not supported"))104        #expect(await run(["plan", scratch.file("absent.png").path, "--to", "jpeg"]).code == 3)105106        // A pair with no direct path that the planner bridges.107        let svg = try scratch.text("drawing.svg", Seeds.svg)108        let chained = try (await run(["plan", svg.path, "--to", "heic", "--json"])).capture.decode(PlanCommand.Output.self)109        #expect(chained.chained && chained.steps.count == 2 && chained.steps.map { $0.engine } == ["vector", "image"])110    }111112    @Test func optionsDescribeThePath() async throws {113        let result = await run(["options", "--from", "png", "--to", "jpeg", "--json"])114        #expect(result.code == 0)115        let output = try result.capture.decode(OptionsCommand.Output.self)116        #expect(output.engine == "image" && !output.options.isEmpty)117        let quality = try #require(output.options.first { $0.key == "quality" })118        #expect(quality.type == "integer" || quality.type == "number")119        #expect(quality.minimum != nil && quality.maximum != nil && !quality.defaultValue.isEmpty)120        #expect(await run(["options", "--from", "wav", "--to", "docx"]).code == 4)121        #expect(await run(["options", "--from", "png"]).code == 2)122        let text = await run(["options", "--from", "png", "--to", "jpeg"])123        #expect(text.capture.out.contains("--option quality="))124    }125126    @Test func doctorFindsEveryToolInsideTheRepository() async throws {127        let result = await run(["doctor", "--json"])128        let output = try result.capture.decode(DoctorCommand.Output.self)129        #expect(output.problems.isEmpty, "\(output.problems)")130        #expect(result.code == 0)131        #expect(output.engines.count == ORIXEngines.all().count)132        #expect(output.engines.allSatisfy { $0.available })133        let repository = try #require(Repository.root()).path134        for tool in output.tools where tool.path != nil {135            let path = tool.resolvedPath ?? tool.path ?? ""136            #expect(path.hasPrefix(repository + "/"), "\(tool.name) resolves to \(path)")137            #expect(!DoctorCommand.foreignPrefixes.contains { path.hasPrefix($0 + "/") })138        }139        #expect(output.tools.first { $0.name == "ffmpeg" }?.version != nil)140        #expect(output.loadedImagesOutsideSystem.allSatisfy { $0.inside })141        #expect(output.searchDirectories.allSatisfy { dir in !DoctorCommand.foreignPrefixes.contains { dir.path.hasPrefix($0) } })142    }143144    @Test func matrixIsComputedFromTheRegistry() async throws {145        let registry = Shared.registry()146        let document = try (await run(["matrix", "--json"])).capture.decode(MatrixDocument.self)147        #expect(document.rows.count == registry.paths.count + registry.counts.unavailablePaths)148        #expect(document.totals.paths == registry.counts.availablePaths)149        #expect(document.totals.distinctPairs == registry.counts.distinctPairs)150        #expect(document.totals.formatsWithPath == registry.counts.formatsWithPath)151        #expect(document.totals.byEngine.values.reduce(0, +) == document.totals.paths)152        #expect(document.totals.plainConversionPaths + document.totals.operationPaths == document.totals.paths)153        #expect(document.totals.testsFromNotes + document.totals.testsFromSuite + document.totals.testsUnknown == document.totals.paths)154        #expect(document.identity.registryFingerprint.count == 64)155        // The image notes name a test for PNG → JPEG.156        let row = try #require(document.rows.first { $0.engine == "image" && $0.source == "png" && $0.destination == "jpeg" && $0.operation == nil })157        #expect(row.testSource == "notes" && row.test?.contains("MatrixTests") == true)158        let markdown = await run(["matrix", "--markdown"])159        #expect(markdown.capture.out.contains("| Source | Destination | Operation | Engine | Status | Fidelity | Preserves | Limitations | Tests | Performance |"))160        #expect(markdown.capture.out.contains("Registry fingerprint (SHA-256): `\(document.identity.registryFingerprint)`"))161        #expect(await run(["matrix"]).code == 2)162    }163164    @Test func engineNotesParsing() {165        var notes = EngineNotes()166        let text = """167        ## Declared paths168        | Source | Destination | Operation | Status | Test |169        |---|---|---|---|---|170        | png | jpeg | — | supported | `MatrixTests.everyPath` |171        | mp4 | mov | convert | supported | `VideoPaths.conversion` |172        | png | svg | `image.vectorize` | supported | `traces("png")` |173        | not-a-format | jpeg | — | supported | `X` |174        ## Measured performance175        | Scenario | Concurrency | Wall time |176        |---|---|---|177        | PNG → JPEG (q 85) | 18 | 9.76 s |178        | CSV (2.36 GB) → Parquet, zstd | 1 | 6.90 s |179        | 4K H.264 → HEVC, MP4 | 1 | 4.64 |180        """181        notes.parse(text, package: "EngineX", engines: ["x"])182        func path(_ s: FormatID, _ d: FormatID, _ op: String? = nil) -> ConversionPath { ConversionPath(s, d, engine: "x", operation: op, fidelity: .lossy) }183        #expect(notes.test(package: "EngineX", path: path(.png, .jpeg)) == "MatrixTests.everyPath")184        #expect(notes.test(package: "EngineX", path: path(.mp4, .mov)) == "VideoPaths.conversion")185        #expect(notes.test(package: "EngineX", path: path(.png, .svg, "image.vectorize")) == "traces(\"png\")")186        #expect(notes.test(package: "EngineX", path: path(.png, .svg)) == nil)187        #expect(notes.performance(package: "EngineX", path: path(.png, .jpeg))?.hasPrefix("9.76 s") == true)188        #expect(notes.performance(package: "EngineX", path: path(.csv, .parquet))?.hasPrefix("6.90 s") == true)189        #expect(notes.performance(package: "EngineX", path: path(.mp4, .mov)) == nil)190        #expect(EngineNotes.package(forDocument: "model3d.md", packages: ["EngineModel3D", "EnginePDF"]) == "EngineModel3D")191        #expect(EngineNotes.package(forDocument: "media.md", packages: ["EngineMedia"]) == "EngineMedia")192    }193194    @Test func presetsListReadsTheUserFile() async throws {195        let result = await run(["presets", "list", "--json"])196        #expect(result.code == 0 || result.code == 6)197        let object = try result.capture.object()198        #expect(object["presets"] is [Any] && object["file"] is String)199        #expect(await run(["presets", "delete"]).code == 2)200    }201202    @Test func workflowValidateAndExample() async throws {203        let scratch = Scratch()204        let example = await run(["workflow", "example"])205        #expect(example.code == 0)206        let file = try scratch.text("flow.json", example.capture.out)207        let valid = await run(["workflow", "validate", file.path, "--json"])208        #expect(valid.code == 0)209        #expect(try valid.capture.decode(WorkflowCommand.ValidationOutput.self).valid)210        // Optional members may be left out; a convert node nobody can satisfy is an error.211        let minimal = try scratch.text("minimal.json", #"{"name": "Bad", "nodes": [{"convert": {"destination": "docx", "options": {"values": {}}}}]}"#)212        let invalid = await run(["workflow", "validate", minimal.path, "--for", "wav", "--json"])213        #expect(invalid.code == 6)214        let output = try invalid.capture.decode(WorkflowCommand.ValidationOutput.self)215        #expect(!output.valid && output.issues.contains { $0.severity == "error" })216        let broken = try scratch.text("broken.json", #"{"name": "x", "nodes": [{"teleport": {}}]}"#)217        #expect(await run(["workflow", "validate", broken.path]).code == 6)218        #expect(await run(["workflow", "validate", scratch.file("absent.json").path]).code == 3)219        #expect(await run(["workflow", "dance"]).code == 2)220    }221}222
Files main
Menu

Theme