SPB Git

Repository Public License

TestSupport.swift 20.9 KB 399 lines swift

1// ORIX Convert — Packages/ORIXConvertCore/Tests/ConvertCoreTests/TestSupport.swift2// Description: Shared test helpers: temporary directories, byte-level fixture builders (ZIP, OLE2, tar, gzip…),3//              mock engines (text converter, failing, slow, invalid-output…) and polling utilities.4// Author: Simon-Pierre Boucher — contact@spboucher.ai5import Compression6import Foundation7import Testing8@testable import ConvertCore910// MARK: - Temporary directories1112/// A unique temporary directory removed when the object is released.13final class TempDir: @unchecked Sendable {14    let url: URL15    init(_ label: String = "t") {16        url = FileManager.default.temporaryDirectory.appendingPathComponent("orix-core-tests-\(label)-\(UUID().uuidString)", isDirectory: true)17        try! FileManager.default.createDirectory(at: url, withIntermediateDirectories: true)18    }19    deinit { try? FileManager.default.removeItem(at: url) }2021    func file(_ name: String) -> URL { url.appendingPathComponent(name) }2223    @discardableResult24    func write(_ name: String, _ text: String) -> URL {25        let target = file(name)26        try! FileManager.default.createDirectory(at: target.deletingLastPathComponent(), withIntermediateDirectories: true)27        try! Data(text.utf8).write(to: target)28        return target29    }3031    @discardableResult32    func write(_ name: String, bytes: [UInt8]) -> URL {33        let target = file(name)34        try! FileManager.default.createDirectory(at: target.deletingLastPathComponent(), withIntermediateDirectories: true)35        try! Data(bytes).write(to: target)36        return target37    }3839    func subdirectory(_ name: String) -> URL {40        let dir = url.appendingPathComponent(name, isDirectory: true)41        try! FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)42        return dir43    }4445    func exists(_ name: String) -> Bool { FileManager.default.fileExists(atPath: file(name).path) }46    func read(_ name: String) -> String { (try? String(contentsOf: file(name), encoding: .utf8)) ?? "" }47    func list(_ sub: String = "") -> [String] {48        ((try? FileManager.default.contentsOfDirectory(atPath: sub.isEmpty ? url.path : file(sub).path)) ?? []).sorted()49    }50}5152/// Polls `condition` until it holds or `timeout` elapses.53func eventually(timeout: TimeInterval = 10, interval: TimeInterval = 0.02, _ condition: @Sendable () async -> Bool) async -> Bool {54    let deadline = Date().addingTimeInterval(timeout)55    while Date() < deadline {56        if await condition() { return true }57        try? await Task.sleep(nanoseconds: UInt64(interval * 1_000_000_000))58    }59    return await condition()60}6162// MARK: - Byte builders6364func ascii(_ s: String) -> [UInt8] { Array(s.utf8) }65func le16(_ v: Int) -> [UInt8] { [UInt8(v & 0xFF), UInt8((v >> 8) & 0xFF)] }66func le32(_ v: Int) -> [UInt8] { (0..<4).map { UInt8((v >> (8 * $0)) & 0xFF) } }67func be16(_ v: Int) -> [UInt8] { [UInt8((v >> 8) & 0xFF), UInt8(v & 0xFF)] }68func be32(_ v: Int) -> [UInt8] { (0..<4).reversed().map { UInt8((v >> (8 * $0)) & 0xFF) } }69func zeros(_ n: Int) -> [UInt8] { [UInt8](repeating: 0, count: n) }7071func compress(_ input: [UInt8], _ algorithm: compression_algorithm) -> [UInt8] {72    var output = [UInt8](repeating: 0, count: input.count * 2 + 4096)73    let n = compression_encode_buffer(&output, output.count, input, input.count, nil, algorithm)74    return Array(output[0..<n])75}7677enum Fixtures {78    /// A ZIP archive with a central directory. `deflated` entries are compressed with raw DEFLATE.79    static func zip(_ entries: [(name: String, data: [UInt8])], deflated: Set<String> = []) -> [UInt8] {80        var out: [UInt8] = []81        var central: [UInt8] = []82        for entry in entries {83            let name = ascii(entry.name)84            let isDeflated = deflated.contains(entry.name)85            let payload = isDeflated ? compress(entry.data, COMPRESSION_ZLIB) : entry.data86            let offset = out.count87            out += [0x50, 0x4B, 0x03, 0x04] + le16(20) + le16(0) + le16(isDeflated ? 8 : 0) + le16(0) + le16(0) + le32(0)88            out += le32(payload.count) + le32(entry.data.count) + le16(name.count) + le16(0) + name + payload89            central += [0x50, 0x4B, 0x01, 0x02] + le16(20) + le16(20) + le16(0) + le16(isDeflated ? 8 : 0) + le16(0) + le16(0) + le32(0)90            central += le32(payload.count) + le32(entry.data.count) + le16(name.count) + le16(0) + le16(0) + le16(0) + le16(0) + le32(0) + le32(offset) + name91        }92        let cdOffset = out.count93        out += central94        out += [0x50, 0x4B, 0x05, 0x06] + le16(0) + le16(0) + le16(entries.count) + le16(entries.count) + le32(central.count) + le32(cdOffset) + le16(0)95        return out96    }9798    /// A 512-byte ustar header for one file, with a valid checksum, followed by one data block.99    static func tar(name: String = "hello.txt", content: String = "hello\n") -> [UInt8] {100        var header = zeros(512)101        func put(_ s: String, at o: Int) { for (i, b) in ascii(s).enumerated() { header[o + i] = b } }102        put(name, at: 0)103        put("0000644\0", at: 100); put("0000000\0", at: 108); put("0000000\0", at: 116)104        put(String(format: "%011o\0", content.utf8.count), at: 124)105        put("00000000000\0", at: 136)106        put("        ", at: 148)107        header[156] = 0x30108        put("ustar\0" + "00", at: 257)109        let sum = header.reduce(0) { $0 + Int($1) }110        put(String(format: "%06o\0 ", sum), at: 148)111        var data = ascii(content)112        data += zeros(512 - data.count % 512)113        return header + data + zeros(1024)114    }115116    static func gzip(_ payload: [UInt8]) -> [UInt8] {117        [0x1F, 0x8B, 0x08, 0x00, 0, 0, 0, 0, 0x00, 0x03] + compress(payload, COMPRESSION_ZLIB) + zeros(8)118    }119120    /// A minimal OLE2 compound file whose directory holds the given stream names.121    static func ole2(streams: [String]) -> [UInt8] {122        var header = [0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1] + zeros(16) as [UInt8]123        header += le16(0x3E) + le16(3) + [0xFE, 0xFF] + le16(9) + le16(6) + zeros(6)124        header += le32(0) + le32(1) + le32(1) + le32(0) + le32(0x1000) + le32(0xFFFF_FFFE) + le32(0) + le32(0xFFFF_FFFE) + le32(0)125        header += le32(0)126        while header.count < 512 { header += le32(0xFFFF_FFFF) }127        var fat = le32(0xFFFF_FFFD) + le32(0xFFFF_FFFE)128        while fat.count < 512 { fat += le32(0xFFFF_FFFF) }129        var directory: [UInt8] = []130        func entry(_ name: String, type: UInt8) -> [UInt8] {131            var e = zeros(128)132            let units = Array(name.utf16)133            for (i, u) in units.enumerated() { e[2 * i] = UInt8(u & 0xFF); e[2 * i + 1] = UInt8(u >> 8) }134            let length = (units.count + 1) * 2135            e[64] = UInt8(length & 0xFF); e[65] = UInt8(length >> 8)136            e[66] = type137            return e138        }139        directory += entry("Root Entry", type: 5)140        for stream in streams { directory += entry(stream, type: 2) }141        while directory.count < 512 { directory += zeros(128) }142        return header + fat + directory143    }144145    static func ftyp(_ major: String, compatible: [String] = []) -> [UInt8] {146        let body = ascii(major) + be32(0) + compatible.flatMap(ascii)147        return be32(body.count + 8) + ascii("ftyp") + body + be32(8) + ascii("free")148    }149150    static let png: [UInt8] = [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A] + be32(13) + ascii("IHDR") + be32(1) + be32(1) + [8, 2, 0, 0, 0] + be32(0)151        + be32(1) + ascii("IDAT") + [0] + be32(0) + be32(0) + ascii("IEND") + be32(0)152153    static let apng: [UInt8] = [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A] + be32(13) + ascii("IHDR") + be32(1) + be32(1) + [8, 2, 0, 0, 0] + be32(0)154        + be32(8) + ascii("acTL") + be32(2) + be32(0) + be32(0) + be32(1) + ascii("IDAT") + [0] + be32(0)155156    /// Two consecutive MPEG-1 Layer III frames (128 kbit/s, 44.1 kHz → 417 bytes each).157    static let mp3Frames: [UInt8] = {158        let frame = [0xFF, 0xFB, 0x90, 0x00] + zeros(413) as [UInt8]159        return frame + frame + frame160    }()161}162163// MARK: - Mock engines164165/// Records how many conversions run at once.166final class ConcurrencyProbe: @unchecked Sendable {167    private let lock = NSLock()168    private var current = 0169    private(set) var peak = 0170    private(set) var started: [String] = []171    func enter(_ name: String) { lock.lock(); current += 1; peak = max(peak, current); started.append(name); lock.unlock() }172    func leave() { lock.lock(); current -= 1; lock.unlock() }173    var order: [String] { lock.lock(); defer { lock.unlock() }; return started }174    var maximum: Int { lock.lock(); defer { lock.unlock() }; return peak }175}176177/// A real (tiny) converter used to exercise the core end to end.178///179/// - txt → markdown: upper-cases the text (lossless for the tests' purposes, declared structural).180/// - markdown → html: wraps the text in an HTML document.181/// - txt → yaml: slow (steps of `stepDelay`), reports measured progress, honours cancellation.182/// - txt → rtf: always fails with `malformedInput`.183/// - txt → png: writes text into a `.png` (generic validation must reject it).184/// - txt → csv: writes an empty file (generic validation must reject it).185/// - txt → tsv: one output file per line.186/// - txt → markdown #text.merge: concatenates several sources.187/// - txt → folder #text.explode: a directory with one file per line.188/// - txt → folder #text.escape: a directory containing a symlink to /etc/hosts.189/// - txt → toml: valid output, but the engine's own `validate` fails.190/// - txt → latex: emits a data-loss warning and facts width/height.191struct TextEngine: ConversionEngine {192    var id = "mock-text"193    var displayName = "Mock Text"194    var probe: ConcurrencyProbe?195    var stepDelay: TimeInterval = 0.02196    var steps = 50197    var available = true198199    var paths: [ConversionPath] {200        [201            ConversionPath(.txt, .markdown, engine: id, fidelity: .structural, preserves: [.metadata], limitations: ["Text is upper-cased."]),202            ConversionPath(.txt, .yaml, engine: id, fidelity: .lossless),203            ConversionPath(.txt, .rtf, engine: id, status: .experimental, fidelity: .lossy),204            ConversionPath(.txt, .png, engine: id, fidelity: .lossy),205            ConversionPath(.txt, .csv, engine: id, fidelity: .lossy),206            ConversionPath(.txt, .tsv, engine: id, fidelity: .partial),207            ConversionPath(.txt, .toml, engine: id, fidelity: .lossless),208            ConversionPath(.txt, .latex, engine: id, fidelity: .lossless),209            ConversionPath(.txt, .markdown, engine: id, operation: "text.merge", title: "Merge Text", fidelity: .lossless, acceptsMultipleSources: true),210            ConversionPath(.txt, .folder, engine: id, operation: "text.explode", title: "Split Lines", fidelity: .lossless),211            ConversionPath(.txt, .folder, engine: id, operation: "text.escape", title: "Escape", fidelity: .lossless),212        ]213    }214215    var inspectableFormats: Set<FormatID> { [.txt, .markdown] }216217    func availability() -> EngineAvailability { available ? .available : .unavailable(reason: "mock component missing") }218219    func options(for path: ConversionPath) -> [OptionDescriptor] {220        [OptionDescriptor("suffix", label: "Suffix", kind: .text(default: "", placeholder: "text appended"))]221    }222223    func inspect(_ url: URL, format: FormatID) async throws -> FileInspection {224        let text = try String(contentsOf: url, encoding: .utf8)225        var inspection = FileInspection(format: format)226        inspection.rowCount = text.split(whereSeparator: \.isNewline).count227        inspection.pixelWidth = text.count228        inspection.pixelHeight = inspection.rowCount229        inspection.fields = [InspectionField("Text", "Characters", String(text.count))]230        return inspection231    }232233    func preflight(_ request: ConversionRequest, inspection: FileInspection?) -> [ConversionWarning] {234        request.options.bool("warn") ? [ConversionWarning(.caution, code: "preflight-test", "Preflight warning requested.")] : []235    }236237    func convert(_ request: ConversionRequest, context: ConversionContext) async throws -> ConversionOutput {238        probe?.enter(request.source.lastPathComponent)239        defer { probe?.leave() }240        guard let text = try? String(contentsOf: request.source, encoding: .utf8) else { throw ConversionError.unreadableInput("not UTF-8") }241        context.log(.info, "mock converting \(request.source.lastPathComponent)")242        let suffix = request.options.string("suffix")243        switch (request.destinationFormat, request.operation) {244        case (.markdown, nil):245            let out = context.outputURL(for: request)246            try (text.uppercased() + suffix).write(to: out, atomically: true, encoding: .utf8)247            context.progress(ProgressUpdate(fraction: 1))248            return ConversionOutput(file: out, facts: ["characters": String(text.count)])249        case (.html, nil):250            let out = context.outputURL(for: request)251            try "<!DOCTYPE html>\n<html><body><pre>\(text)</pre></body></html>\n".write(to: out, atomically: true, encoding: .utf8)252            return ConversionOutput(file: out)253        case (.yaml, nil):254            for step in 0..<steps {255                try context.checkCancellation()256                try await Task.sleep(nanoseconds: UInt64(stepDelay * 1_000_000_000))257                context.progress(ProgressUpdate(fraction: Double(step + 1) / Double(steps), message: "step \(step + 1)"))258            }259            let out = context.outputURL(for: request)260            try "text: done\ncount: \(text.count)\n".write(to: out, atomically: true, encoding: .utf8)261            return ConversionOutput(file: out)262        case (.rtf, nil):263            throw ConversionError.malformedInput("mock failure")264        case (.png, nil):265            let out = context.outputURL(for: request)266            try "this is not a png".write(to: out, atomically: true, encoding: .utf8)267            return ConversionOutput(file: out)268        case (.csv, nil):269            let out = context.outputURL(for: request)270            try Data().write(to: out)271            return ConversionOutput(file: out)272        case (.tsv, nil):273            var files: [URL] = []274            for (index, line) in text.split(whereSeparator: \.isNewline).enumerated() {275                let out = context.outputURL(for: request, suffix: "-\(index + 1)")276                try "\(index + 1)\t\(line)\n".write(to: out, atomically: true, encoding: .utf8)277                files.append(out)278            }279            return ConversionOutput(files: files)280        case (.toml, nil):281            let out = context.outputURL(for: request)282            try "[doc]\ntext = \"x\"\n".write(to: out, atomically: true, encoding: .utf8)283            return ConversionOutput(file: out)284        case (.latex, nil):285            context.warn(ConversionWarning(.dataLoss, code: "styles-lost", "Styles were dropped."))286            let out = context.outputURL(for: request)287            try "\\documentclass{article}\n\\begin{document}\n\(text)\\end{document}\n".write(to: out, atomically: true, encoding: .utf8)288            return ConversionOutput(file: out, facts: ["width": "640", "height": "480"])289        case (.markdown, "text.merge"?):290            let out = context.outputURL(for: request)291            let all = try request.allSources.map { try String(contentsOf: $0, encoding: .utf8) }.joined(separator: "\n")292            try all.write(to: out, atomically: true, encoding: .utf8)293            return ConversionOutput(file: out)294        case (.folder, "text.explode"?):295            let dir = context.outputURL(for: request, extension: "")296            try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)297            for (index, line) in text.split(whereSeparator: \.isNewline).enumerated() {298                try String(line).write(to: dir.appendingPathComponent("line-\(index + 1).txt"), atomically: true, encoding: .utf8)299            }300            return ConversionOutput(file: dir)301        case (.folder, "text.escape"?):302            let dir = context.outputURL(for: request, extension: "")303            try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)304            try "x".write(to: dir.appendingPathComponent("a.txt"), atomically: true, encoding: .utf8)305            try FileManager.default.createSymbolicLink(atPath: dir.appendingPathComponent("hosts").path, withDestinationPath: "/etc/hosts")306            return ConversionOutput(file: dir)307        default:308            throw ConversionError.unsupportedPath(source: request.sourceFormat, destination: request.destinationFormat)309        }310    }311312    func validate(_ output: ConversionOutput, request: ConversionRequest, source: FileInspection?) async throws -> [ValidationCheck] {313        if request.destinationFormat == .toml { return [ValidationCheck("mock.reopen", passed: false, "The mock refuses its TOML output.")] }314        return [ValidationCheck("mock.reopen", passed: true, "reopened")]315    }316}317318/// An engine that declares paths without doing anything, for registry and planner tests.319/// markdown → html, as a second engine (chains are only planned across engines). The work is `TextEngine`'s.320struct HTMLEngine: ConversionEngine {321    var base = TextEngine()322    var id = "mock-html"323    var displayName = "Mock HTML"324    var paths: [ConversionPath] {325        [ConversionPath(.markdown, .html, engine: id, fidelity: .structural, preserves: [.metadata, .links], limitations: ["Markdown is not rendered."])]326    }327    func availability() -> EngineAvailability { base.availability() }328    func options(for path: ConversionPath) -> [OptionDescriptor] { base.options(for: path) }329    func inspect(_ url: URL, format: FormatID) async throws -> FileInspection { try await base.inspect(url, format: format) }330    func preflight(_ request: ConversionRequest, inspection: FileInspection?) -> [ConversionWarning] { base.preflight(request, inspection: inspection) }331    func convert(_ request: ConversionRequest, context: ConversionContext) async throws -> ConversionOutput { try await base.convert(request, context: context) }332    func validate(_ output: ConversionOutput, request: ConversionRequest, source: FileInspection?) async throws -> [ValidationCheck] {333        try await base.validate(output, request: request, source: source)334    }335}336337struct StubEngine: ConversionEngine {338    var id: String339    var displayName: String { "Stub \(id)" }340    var paths: [ConversionPath]341    var unavailableReason: String?342    func availability() -> EngineAvailability { unavailableReason.map { .unavailable(reason: $0) } ?? .available }343    func inspect(_ url: URL, format: FormatID) async throws -> FileInspection { FileInspection(format: format) }344    func convert(_ request: ConversionRequest, context: ConversionContext) async throws -> ConversionOutput {345        throw ConversionError.internalError("stub")346    }347}348349struct FixedMonitor: ResourceMonitoring {350    let box: LockedBox<ResourceState>351    init(_ state: ResourceState = ResourceState()) { box = LockedBox(state) }352    func currentState() -> ResourceState { box.value }353}354355/// A queue over the mock engine with private work and history locations.356struct TestBed {357    let temp = TempDir("bed")358    let queue: ConversionQueue359    let history: ConversionHistoryStore360    let registry: FormatRegistry361    let probe = ConcurrencyProbe()362    let monitor: FixedMonitor363    let workDirectories: WorkDirectoryManager364365    init(maxConcurrent: Int = 4, maxHeavy: Int? = nil, state: ResourceState = ResourceState(), steps: Int = 50, stepDelay: TimeInterval = 0.02,366         heavyEngines: Set<String> = [], extraEngines: [any ConversionEngine] = []) {367        var engine = TextEngine()368        engine.probe = probe369        engine.steps = steps370        engine.stepDelay = stepDelay371        registry = FormatRegistry(engines: [engine, HTMLEngine(base: engine)] + extraEngines)372        monitor = FixedMonitor(state)373        workDirectories = WorkDirectoryManager(root: temp.url.appendingPathComponent("work", isDirectory: true))374        history = try! ConversionHistoryStore(url: temp.file("history.sqlite"))375        queue = ConversionQueue(registry: registry, configuration: QueueConfiguration(maxConcurrentJobs: maxConcurrent, maxHeavyJobs: maxHeavy, heavyEngineIDs: heavyEngines),376                                history: history, workDirectories: workDirectories, monitor: monitor)377    }378379    var input: URL { temp.subdirectory("in") }380    var output: URL { temp.subdirectory("out") }381382    func source(_ name: String = "note.txt", _ text: String = "hello world\nsecond line\n") -> URL {383        temp.write("in/" + name, text)384    }385386    func destination(_ policy: ConflictPolicy = .rename, template: String = "{name}") -> OutputDestination {387        OutputDestination(directory: output, nameTemplate: template, conflictPolicy: policy)388    }389390    /// Work directories still present (none should remain after a job settles).391    var leftoverWorkDirectories: [String] {392        (try? FileManager.default.contentsOfDirectory(atPath: workDirectories.root.path)) ?? []393    }394}395396extension Array where Element == ValidationCheck {397    var allPassed: Bool { allSatisfy { $0.passed } }398}399
Files main
Menu

Theme