SPB Git

Repository Public License

Fixtures.swift 53.3 KB 839 lines swift

1// ORIX Convert — Packages/ConvertKit/Sources/ConvertCLI/Fixtures.swift2// Description: Test material for the end-to-end sweep and the benchmarks. A small set of seeds is generated in code3//              (Core Graphics, hand-written containers, the bundled ffmpeg); every other format is obtained by4//              converting an existing fixture through the engines themselves (bootstrap chain).5// Author: Simon-Pierre Boucher — contact@spboucher.ai6import Foundation7import CoreGraphics8import ImageIO9import UniformTypeIdentifiers10import zlib11import ConvertCore12import ConvertKit1314enum Seeds {15    enum SeedError: Error, LocalizedError {16        case failed(String)17        var errorDescription: String? { if case .failed(let m) = self { return m }; return nil }18    }1920    // MARK: Images2122    /// A picture with smooth gradients, hard edges and fine detail, drawn with Core Graphics. `seed` varies it.23    static func image(width: Int, height: Int, seed: Int = 0, alpha: Bool = false) throws -> CGImage {24        let space = CGColorSpace(name: CGColorSpace.sRGB) ?? CGColorSpaceCreateDeviceRGB()25        let info = alpha ? CGImageAlphaInfo.premultipliedLast.rawValue : CGImageAlphaInfo.noneSkipLast.rawValue26        guard let context = CGContext(data: nil, width: width, height: height, bitsPerComponent: 8, bytesPerRow: 0, space: space, bitmapInfo: info) else {27            throw SeedError.failed("Core Graphics could not create a \(width)×\(height) bitmap.")28        }29        let w = CGFloat(width), h = CGFloat(height)30        let hue = CGFloat((seed * 37) % 360) / 36031        if !alpha {32            let colors = [CGColor(red: 0.05 + 0.4 * hue, green: 0.15, blue: 0.45, alpha: 1), CGColor(red: 0.95, green: 0.75 - 0.3 * hue, blue: 0.35, alpha: 1)] as CFArray33            if let gradient = CGGradient(colorsSpace: space, colors: colors, locations: [0, 1]) {34                context.drawLinearGradient(gradient, start: .zero, end: CGPoint(x: w, y: h), options: [])35            }36        }37        var state = UInt64(truncatingIfNeeded: seed &* 2654435761 &+ 12345)38        func next() -> CGFloat {39            state = state &* 6364136223846793005 &+ 144269504088896340740            return CGFloat((state >> 33) % 10_000) / 10_00041        }42        for _ in 0..<24 {43            context.setFillColor(CGColor(red: next(), green: next(), blue: next(), alpha: 0.85))44            let rect = CGRect(x: next() * w * 0.9, y: next() * h * 0.9, width: next() * w * 0.3 + 4, height: next() * h * 0.3 + 4)45            if next() > 0.5 { context.fillEllipse(in: rect) } else { context.fill(rect) }46        }47        context.setStrokeColor(CGColor(red: 1, green: 1, blue: 1, alpha: 0.9))48        context.setLineWidth(max(1, w / 400))49        for index in 0..<40 {50            context.move(to: CGPoint(x: CGFloat(index) * w / 40, y: 0))51            context.addLine(to: CGPoint(x: w - CGFloat(index) * w / 40, y: h))52        }53        context.strokePath()54        guard let image = context.makeImage() else { throw SeedError.failed("Core Graphics could not make the image.") }55        return image56    }5758    static func writeImage(_ image: CGImage, to url: URL, type: UTType = .png) throws {59        guard let destination = CGImageDestinationCreateWithURL(url as CFURL, type.identifier as CFString, 1, nil) else {60            throw SeedError.failed("ImageIO cannot write \(type.identifier).")61        }62        CGImageDestinationAddImage(destination, image, nil)63        guard CGImageDestinationFinalize(destination) else { throw SeedError.failed("ImageIO failed to write \(url.lastPathComponent).") }64    }6566    // MARK: Audio6768    /// 16-bit PCM WAV of mixed sines, written in pieces so that long files do not need their size in memory.69    static func writeWAV(to url: URL, seconds: Double, sampleRate: Int = 44_100, channels: Int = 2) throws {70        let frames = Int(seconds * Double(sampleRate))71        let dataBytes = frames * channels * 272        var header = Data()73        func u32(_ v: Int) { var x = UInt32(v).littleEndian; header.append(Data(bytes: &x, count: 4)) }74        func u16(_ v: Int) { var x = UInt16(v).littleEndian; header.append(Data(bytes: &x, count: 2)) }75        header.append(Data("RIFF".utf8)); u32(36 + dataBytes); header.append(Data("WAVEfmt ".utf8))76        u32(16); u16(1); u16(channels); u32(sampleRate); u32(sampleRate * channels * 2); u16(channels * 2); u16(16)77        header.append(Data("data".utf8)); u32(dataBytes)78        FileManager.default.createFile(atPath: url.path, contents: header)79        guard let handle = try? FileHandle(forWritingTo: url) else { throw SeedError.failed("\(url.lastPathComponent) could not be written.") }80        defer { try? handle.close() }81        try handle.seekToEnd()82        let chunkFrames = sampleRate * 583        var frame = 084        while frame < frames {85            let count = min(chunkFrames, frames - frame)86            var samples = [Int16](repeating: 0, count: count * channels)87            for i in 0..<count {88                let t = Double(frame + i) / Double(sampleRate)89                let envelope = 0.6 + 0.4 * sin(2 * .pi * 0.25 * t)90                for channel in 0..<channels {91                    let f = channel == 0 ? 440.0 : 554.3792                    let value = envelope * (0.5 * sin(2 * .pi * f * t) + 0.2 * sin(2 * .pi * f * 2.01 * t) + 0.1 * sin(2 * .pi * 97 * t))93                    samples[i * channels + channel] = Int16(max(-1, min(1, value)) * 30_000).littleEndian94                }95            }96            try samples.withUnsafeBytes { try handle.write(contentsOf: Data($0)) }97            frame += count98        }99    }100101    /// A one-track Standard MIDI File: a C-major arpeggio.102    static func midi() -> Data {103        var track: [UInt8] = [0x00, 0xFF, 0x51, 0x03, 0x07, 0xA1, 0x20, 0x00, 0xC0, 0x00]104        for note: UInt8 in [60, 64, 67, 72, 67, 64, 60] {105            track += [0x00, 0x90, note, 0x60, 0x60, 0x80, note, 0x40]106        }107        track += [0x00, 0xFF, 0x2F, 0x00]108        var data = Data([0x4D, 0x54, 0x68, 0x64, 0, 0, 0, 6, 0, 0, 0, 1, 0, 0x60, 0x4D, 0x54, 0x72, 0x6B])109        var length = UInt32(track.count).bigEndian110        data.append(Data(bytes: &length, count: 4))111        data.append(contentsOf: track)112        return data113    }114115    // MARK: Text116117    static let text = """118    ORIX Convert end-to-end fixture119120    This plain text file is a seed of the cross-engine sweep. It has several paragraphs,121    accented letters (é, à, ü, ñ), a number (1 234,56) and a URL: https://example.invalid/page.122123    The quick brown fox jumps over the lazy dog. Portez ce vieux whisky au juge blond qui fume.124    """125126    static let markdown = """127    # ORIX Convert fixture128129    A **Markdown** seed with *emphasis*, `inline code` and a [link](https://example.invalid/).130131    ## A list132133    - First item134    - Second item with accents: é à ü135    - Third item136137    ## A table138139    | City | Country | Population |140    |---|---|---:|141    | Gatineau | Canada | 291041 |142    | Québec | Canada | 549459 |143    | Lyon | France | 522250 |144145    ## Code146147    ```swift148    let answer = 6 * 7149    ```150151    > A quotation closes the document.152    """153154    /// The HTML seed with a small PNG embedded as a data URI, so that documents derived from it carry a picture.155    static func htmlWithPicture() throws -> String {156        let scratch = FileManager.default.temporaryDirectory.appendingPathComponent("orix-seed-\(UUID().uuidString).png")157        defer { try? FileManager.default.removeItem(at: scratch) }158        try writeImage(try image(width: 96, height: 72, seed: 11), to: scratch)159        let encoded = try Data(contentsOf: scratch).base64EncodedString()160        return html.replacingOccurrences(of: "</body>", with: "<p><img alt=\"Generated picture\" width=\"96\" height=\"72\" src=\"data:image/png;base64,\(encoded)\"></p>\n</body>")161    }162163    /// A minimal EPUB 3 written by hand: two chapters, a navigation document, a picture and a declared cover.164    static func epub() throws -> Data {165        let scratch = FileManager.default.temporaryDirectory.appendingPathComponent("orix-seed-\(UUID().uuidString).png")166        defer { try? FileManager.default.removeItem(at: scratch) }167        try writeImage(try image(width: 300, height: 400, seed: 21), to: scratch)168        let cover = try Data(contentsOf: scratch)169        func page(_ title: String, _ body: String) -> String {170            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE html>\n<html xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:epub=\"http://www.idpf.org/2007/ops\" lang=\"en\"><head><meta charset=\"utf-8\"/><title>\(title)</title></head><body>\(body)</body></html>\n"171        }172        let opf = """173        <?xml version="1.0" encoding="UTF-8"?>174        <package xmlns="http://www.idpf.org/2007/opf" version="3.0" unique-identifier="id">175          <metadata xmlns:dc="http://purl.org/dc/elements/1.1/">176            <dc:identifier id="id">urn:uuid:6f1d2c3a-0000-4000-8000-0000000000e1</dc:identifier>177            <dc:title>ORIX Convert fixture book</dc:title>178            <dc:language>en</dc:language>179            <dc:creator>ORIX Convert</dc:creator>180            <meta property="dcterms:modified">2026-01-01T00:00:00Z</meta>181            <meta name="cover" content="cover-image"/>182          </metadata>183          <manifest>184            <item id="nav" href="nav.xhtml" media-type="application/xhtml+xml" properties="nav"/>185            <item id="c1" href="chapter1.xhtml" media-type="application/xhtml+xml"/>186            <item id="c2" href="chapter2.xhtml" media-type="application/xhtml+xml"/>187            <item id="cover-image" href="cover.png" media-type="image/png" properties="cover-image"/>188          </manifest>189          <spine><itemref idref="c1"/><itemref idref="c2"/></spine>190        </package>191192        """193        let container = "<?xml version=\"1.0\"?>\n<container version=\"1.0\" xmlns=\"urn:oasis:names:tc:opendocument:xmlns:container\"><rootfiles><rootfile full-path=\"OEBPS/content.opf\" media-type=\"application/oebps-package+xml\"/></rootfiles></container>\n"194        let nav = page("Contents", "<nav epub:type=\"toc\"><h1>Contents</h1><ol><li><a href=\"chapter1.xhtml\">Chapter one</a></li><li><a href=\"chapter2.xhtml\">Chapter two</a></li></ol></nav>")195        let one = page("Chapter one", "<h1>Chapter one</h1><p>The first chapter of the fixture book, with <em>emphasis</em> and accents: é à ü.</p><p><img src=\"cover.png\" alt=\"Picture\"/></p>")196        let two = page("Chapter two", "<h1>Chapter two</h1><p>The quick brown fox jumps over the lazy dog.</p><ul><li>First item</li><li>Second item</li></ul>")197        // The mimetype entry must come first and be stored uncompressed: this writer stores everything.198        return zip(entries: [("mimetype", Data("application/epub+zip".utf8)), ("META-INF/container.xml", Data(container.utf8)), ("OEBPS/content.opf", Data(opf.utf8)),199                             ("OEBPS/nav.xhtml", Data(nav.utf8)), ("OEBPS/chapter1.xhtml", Data(one.utf8)), ("OEBPS/chapter2.xhtml", Data(two.utf8)), ("OEBPS/cover.png", cover)])200    }201202    static let html = """203    <!DOCTYPE html>204    <html lang="en">205    <head><meta charset="utf-8"><title>ORIX Convert fixture</title></head>206    <body>207    <h1>ORIX Convert fixture</h1>208    <p>An <strong>HTML</strong> seed with <em>emphasis</em> and a <a href="https://example.invalid/">link</a>.</p>209    <h2>A list</h2>210    <ul><li>First item</li><li>Second item: é à ü</li></ul>211    <table><tr><th>City</th><th>Population</th></tr><tr><td>Gatineau</td><td>291041</td></tr><tr><td>Lyon</td><td>522250</td></tr></table>212    </body>213    </html>214    """215216    static func csv(rows: Int = 24) -> String {217        var out = "id,name,city,amount,active,joined\n"218        let cities = ["Gatineau", "Québec", "Lyon", "Zürich", "São Paulo", "Tokyo"]219        for i in 1...rows {220            out += "\(i),Person \(i),\(cities[i % cities.count]),\(String(format: "%.2f", locale: Locale(identifier: "en_US_POSIX"), Double(i) * 12.5)),\(i % 2 == 0 ? "true" : "false"),2026-\(String(format: "%02d", i % 12 + 1))-\(String(format: "%02d", i % 27 + 1))\n"221        }222        return out223    }224225    static func json(rows: Int = 12) -> String {226        let cities = ["Gatineau", "Québec", "Lyon", "Zürich"]227        let items = (1...rows).map { i in228            "  {\"id\": \(i), \"name\": \"Person \(i)\", \"city\": \"\(cities[i % cities.count])\", \"amount\": \(Double(i) * 12.5), \"active\": \(i % 2 == 0)}"229        }230        return "[\n" + items.joined(separator: ",\n") + "\n]\n"231    }232233    /// A table whose rows are places (longitude / latitude columns), for the geospatial readers.234    static let placesCSV = """235    name,longitude,latitude,population236    Gatineau,-75.7013,45.4765,291041237    Ottawa,-75.6972,45.4215,1017449238    Montreal,-73.5673,45.5017,1762949239    Quebec,-71.2080,46.8139,549459240241    """242243    static let pointsGeoJSON = """244    {245      "type": "FeatureCollection",246      "features": [247        {"type": "Feature", "properties": {"name": "Gatineau", "population": 291041}, "geometry": {"type": "Point", "coordinates": [-75.7013, 45.4765]}},248        {"type": "Feature", "properties": {"name": "Ottawa", "population": 1017449}, "geometry": {"type": "Point", "coordinates": [-75.6972, 45.4215]}},249        {"type": "Feature", "properties": {"name": "Track", "population": 0}, "geometry": {"type": "LineString", "coordinates": [[-75.72, 45.43], [-75.70, 45.44], [-75.68, 45.46]]}}250      ]251    }252    """253254    static let srt = """255    1256    00:00:00,200 --> 00:00:01,200257    First subtitle line.258259    2260    00:00:01,400 --> 00:00:02,300261    Deuxième ligne, avec des accents : é à ü.262263    3264    00:00:02,400 --> 00:00:02,900265    <i>Third</i> and last.266267    """268269    static let geojson = """270    {271      "type": "FeatureCollection",272      "features": [273        {"type": "Feature", "properties": {"name": "Gatineau", "population": 291041}, "geometry": {"type": "Point", "coordinates": [-75.7013, 45.4765]}},274        {"type": "Feature", "properties": {"name": "Ottawa River segment", "population": null}, "geometry": {"type": "LineString", "coordinates": [[-75.72, 45.43], [-75.70, 45.44], [-75.68, 45.46]]}},275        {"type": "Feature", "properties": {"name": "Parc", "population": 0}, "geometry": {"type": "Polygon", "coordinates": [[[-75.75, 45.48], [-75.73, 45.48], [-75.73, 45.50], [-75.75, 45.50], [-75.75, 45.48]]]}}276      ]277    }278    """279280    static let svg = """281    <?xml version="1.0" encoding="UTF-8"?>282    <svg xmlns="http://www.w3.org/2000/svg" width="320" height="240" viewBox="0 0 320 240">283      <rect width="320" height="240" fill="#f4f1ea"/>284      <circle cx="100" cy="120" r="70" fill="#d1495b"/>285      <rect x="170" y="60" width="110" height="120" rx="12" fill="#00798c"/>286      <path d="M20 220 C 80 160, 240 260, 300 200" fill="none" stroke="#30638e" stroke-width="6"/>287      <polygon points="160,20 185,70 135,70" fill="#edae49"/>288    </svg>289    """290291    /// The same drawing with real `<text>` elements: what a chain such as SVG → PDF → DOCX needs to have text to292    /// rebuild.293    static let svgWithText = """294    <?xml version="1.0" encoding="UTF-8"?>295    <svg xmlns="http://www.w3.org/2000/svg" width="480" height="320" viewBox="0 0 480 320">296      <rect width="480" height="320" fill="#f4f1ea"/>297      <circle cx="400" cy="250" r="40" fill="#d1495b"/>298      <text x="24" y="60" font-family="Helvetica" font-size="28" fill="#1d2d44">ORIX Convert fixture</text>299      <text x="24" y="110" font-family="Helvetica" font-size="18" fill="#1d2d44">This drawing carries real text elements.</text>300      <text x="24" y="145" font-family="Helvetica" font-size="18" fill="#1d2d44">Second line of text for the chained plans.</text>301    </svg>302    """303304    /// A unit cube with normals and texture coordinates.305    static let obj = """306    # ORIX Convert fixture: cube307    o Cube308    v -0.5 -0.5 -0.5309    v 0.5 -0.5 -0.5310    v 0.5 0.5 -0.5311    v -0.5 0.5 -0.5312    v -0.5 -0.5 0.5313    v 0.5 -0.5 0.5314    v 0.5 0.5 0.5315    v -0.5 0.5 0.5316    vt 0 0317    vt 1 0318    vt 1 1319    vt 0 1320    vn 0 0 -1321    vn 0 0 1322    vn -1 0 0323    vn 1 0 0324    vn 0 -1 0325    vn 0 1 0326    f 1/1/1 4/4/1 3/3/1327    f 1/1/1 3/3/1 2/2/1328    f 5/1/2 6/2/2 7/3/2329    f 5/1/2 7/3/2 8/4/2330    f 1/1/3 5/2/3 8/3/3331    f 1/1/3 8/3/3 4/4/3332    f 2/1/4 3/4/4 7/3/4333    f 2/1/4 7/3/4 6/2/4334    f 1/1/5 2/2/5 6/3/5335    f 1/1/5 6/3/5 5/4/5336    f 4/1/6 8/4/6 7/3/6337    f 4/1/6 7/3/6 3/2/6338339    """340341    // MARK: Containers342343    /// A ZIP archive with stored (uncompressed) entries, written by hand.344    static func zip(entries: [(name: String, data: Data)]) -> Data {345        var out = Data(), central = Data()346        func le16(_ v: Int) -> Data { var x = UInt16(truncatingIfNeeded: v).littleEndian; return Data(bytes: &x, count: 2) }347        func le32(_ v: Int) -> Data { var x = UInt32(truncatingIfNeeded: v).littleEndian; return Data(bytes: &x, count: 4) }348        for entry in entries {349            let name = Data(entry.name.utf8)350            let crc = entry.data.withUnsafeBytes { Int(crc32(0, $0.bindMemory(to: Bytef.self).baseAddress, uInt($0.count))) }351            let offset = out.count352            let common = le16(20) + le16(0x0800) + le16(0) + le16(0) + le16(0x5A21) + le32(crc) + le32(entry.data.count) + le32(entry.data.count) + le16(name.count) + le16(0)353            out += le32(0x04034B50) + common + name + entry.data354            central += le32(0x02014B50) + le16(0x031E) + common + le16(0) + le16(0) + le16(0) + le32(Int(0o100644) << 16) + le32(offset) + name355        }356        let centralOffset = out.count357        out += central358        out += le32(0x06054B50) + le16(0) + le16(0) + le16(entries.count) + le16(entries.count) + le32(central.count) + le32(centralOffset) + le16(0)359        return out360    }361362    /// A minimal PowerPoint presentation (Office Open XML written by hand): one title and one line of text per slide.363    static func pptx(slides titles: [String], picture: Data? = nil) -> Data {364        let head = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"365        let ns = "xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" xmlns:p=\"http://schemas.openxmlformats.org/presentationml/2006/main\""366        let rel = "http://schemas.openxmlformats.org/officeDocument/2006/relationships"367        let pkg = "application/vnd.openxmlformats-officedocument.presentationml"368        func escape(_ t: String) -> String { t.replacingOccurrences(of: "&", with: "&amp;").replacingOccurrences(of: "<", with: "&lt;") }369        let emptyTree = "<p:spTree><p:nvGrpSpPr><p:cNvPr id=\"1\" name=\"\"/><p:cNvGrpSpPr/><p:nvPr/></p:nvGrpSpPr><p:grpSpPr/>"370        func shape(_ id: Int, _ name: String, _ type: String?, y: Int, height: Int, size: Int, _ text: String) -> String {371            "<p:sp><p:nvSpPr><p:cNvPr id=\"\(id)\" name=\"\(name)\"/><p:cNvSpPr/><p:nvPr>\(type.map { "<p:ph type=\"\($0)\"/>" } ?? "")</p:nvPr></p:nvSpPr>"372            + "<p:spPr><a:xfrm><a:off x=\"685800\" y=\"\(y)\"/><a:ext cx=\"7772400\" cy=\"\(height)\"/></a:xfrm><a:prstGeom prst=\"rect\"><a:avLst/></a:prstGeom></p:spPr>"373            + "<p:txBody><a:bodyPr/><a:lstStyle/><a:p><a:r><a:rPr lang=\"en-US\" sz=\"\(size)\"/><a:t>\(escape(text))</a:t></a:r></a:p></p:txBody></p:sp>"374        }375        var entries: [(String, String)] = []376        var types = head + "<Types xmlns=\"http://schemas.openxmlformats.org/package/2006/content-types\"><Default Extension=\"rels\" ContentType=\"application/vnd.openxmlformats-package.relationships+xml\"/><Default Extension=\"xml\" ContentType=\"application/xml\"/><Default Extension=\"png\" ContentType=\"image/png\"/>"377        types += "<Override PartName=\"/ppt/presentation.xml\" ContentType=\"\(pkg).presentation.main+xml\"/><Override PartName=\"/ppt/slideMasters/slideMaster1.xml\" ContentType=\"\(pkg).slideMaster+xml\"/>"378        types += "<Override PartName=\"/ppt/slideLayouts/slideLayout1.xml\" ContentType=\"\(pkg).slideLayout+xml\"/><Override PartName=\"/ppt/theme/theme1.xml\" ContentType=\"application/vnd.openxmlformats-officedocument.theme+xml\"/>"379        for i in titles.indices { types += "<Override PartName=\"/ppt/slides/slide\(i + 1).xml\" ContentType=\"\(pkg).slide+xml\"/>" }380        entries.append(("[Content_Types].xml", types + "</Types>"))381        entries.append(("_rels/.rels", head + "<Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\"><Relationship Id=\"rId1\" Type=\"\(rel)/officeDocument\" Target=\"ppt/presentation.xml\"/></Relationships>"))382        var presentation = head + "<p:presentation \(ns)><p:sldMasterIdLst><p:sldMasterId id=\"2147483648\" r:id=\"rId1\"/></p:sldMasterIdLst><p:sldIdLst>"383        var presentationRels = head + "<Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\"><Relationship Id=\"rId1\" Type=\"\(rel)/slideMaster\" Target=\"slideMasters/slideMaster1.xml\"/>"384        for i in titles.indices {385            presentation += "<p:sldId id=\"\(256 + i)\" r:id=\"rId\(i + 2)\"/>"386            presentationRels += "<Relationship Id=\"rId\(i + 2)\" Type=\"\(rel)/slide\" Target=\"slides/slide\(i + 1).xml\"/>"387        }388        presentationRels += "<Relationship Id=\"rId\(titles.count + 2)\" Type=\"\(rel)/theme\" Target=\"theme/theme1.xml\"/></Relationships>"389        entries.append(("ppt/presentation.xml", presentation + "</p:sldIdLst><p:sldSz cx=\"9144000\" cy=\"6858000\" type=\"screen4x3\"/><p:notesSz cx=\"6858000\" cy=\"9144000\"/></p:presentation>"))390        entries.append(("ppt/_rels/presentation.xml.rels", presentationRels))391        entries.append(("ppt/slideMasters/slideMaster1.xml", head + "<p:sldMaster \(ns)><p:cSld><p:bg><p:bgRef idx=\"1001\"><a:schemeClr val=\"bg1\"/></p:bgRef></p:bg>\(emptyTree)</p:spTree></p:cSld>"392                        + "<p:clrMap bg1=\"lt1\" tx1=\"dk1\" bg2=\"lt2\" tx2=\"dk2\" accent1=\"accent1\" accent2=\"accent2\" accent3=\"accent3\" accent4=\"accent4\" accent5=\"accent5\" accent6=\"accent6\" hlink=\"hlink\" folHlink=\"folHlink\"/>"393                        + "<p:sldLayoutIdLst><p:sldLayoutId id=\"2147483649\" r:id=\"rId1\"/></p:sldLayoutIdLst></p:sldMaster>"))394        entries.append(("ppt/slideMasters/_rels/slideMaster1.xml.rels", head + "<Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\"><Relationship Id=\"rId1\" Type=\"\(rel)/slideLayout\" Target=\"../slideLayouts/slideLayout1.xml\"/><Relationship Id=\"rId2\" Type=\"\(rel)/theme\" Target=\"../theme/theme1.xml\"/></Relationships>"))395        entries.append(("ppt/slideLayouts/slideLayout1.xml", head + "<p:sldLayout \(ns) type=\"title\"><p:cSld name=\"Title\">\(emptyTree)</p:spTree></p:cSld><p:clrMapOvr><a:masterClrMapping/></p:clrMapOvr></p:sldLayout>"))396        entries.append(("ppt/slideLayouts/_rels/slideLayout1.xml.rels", head + "<Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\"><Relationship Id=\"rId1\" Type=\"\(rel)/slideMaster\" Target=\"../slideMasters/slideMaster1.xml\"/></Relationships>"))397        func color(_ name: String, _ rgb: String) -> String { "<a:\(name)><a:srgbClr val=\"\(rgb)\"/></a:\(name)>" }398        let fill = "<a:solidFill><a:schemeClr val=\"phClr\"/></a:solidFill>"399        let line = "<a:ln w=\"9525\"><a:solidFill><a:schemeClr val=\"phClr\"/></a:solidFill></a:ln>"400        let effect = "<a:effectStyle><a:effectLst/></a:effectStyle>"401        var theme = head + "<a:theme xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\" name=\"ORIX\"><a:themeElements><a:clrScheme name=\"ORIX\">"402        theme += color("dk1", "000000") + color("lt1", "FFFFFF") + color("dk2", "1F3A5F") + color("lt2", "EEECE1") + color("accent1", "D1495B") + color("accent2", "00798C")403        theme += color("accent3", "EDAE49") + color("accent4", "30638E") + color("accent5", "003D5B") + color("accent6", "8D99AE") + color("hlink", "0000FF") + color("folHlink", "800080")404        theme += "</a:clrScheme><a:fontScheme name=\"ORIX\"><a:majorFont><a:latin typeface=\"Helvetica\"/><a:ea typeface=\"\"/><a:cs typeface=\"\"/></a:majorFont><a:minorFont><a:latin typeface=\"Helvetica\"/><a:ea typeface=\"\"/><a:cs typeface=\"\"/></a:minorFont></a:fontScheme>"405        theme += "<a:fmtScheme name=\"ORIX\"><a:fillStyleLst>\(fill)\(fill)\(fill)</a:fillStyleLst><a:lnStyleLst>\(line)\(line)\(line)</a:lnStyleLst><a:effectStyleLst>\(effect)\(effect)\(effect)</a:effectStyleLst><a:bgFillStyleLst>\(fill)\(fill)\(fill)</a:bgFillStyleLst></a:fmtScheme></a:themeElements></a:theme>"406        entries.append(("ppt/theme/theme1.xml", theme))407        for (i, title) in titles.enumerated() {408            let body = shape(2, "Title", "title", y: 685800, height: 1143000, size: 4000, title) + shape(3, "Text", nil, y: 2286000, height: 3200400, size: 2400, "Slide \(i + 1) of \(titles.count): the quick brown fox jumps over the lazy dog.")409            // The first slide carries the picture, when one is given.410            let withPicture = i == 0 && picture != nil411            let pic = withPicture ? "<p:pic><p:nvPicPr><p:cNvPr id=\"4\" name=\"Picture\"/><p:cNvPicPr/><p:nvPr/></p:nvPicPr><p:blipFill><a:blip r:embed=\"rId2\"/><a:stretch><a:fillRect/></a:stretch></p:blipFill>"412                + "<p:spPr><a:xfrm><a:off x=\"5486400\" y=\"3657600\"/><a:ext cx=\"2926080\" cy=\"2194560\"/></a:xfrm><a:prstGeom prst=\"rect\"><a:avLst/></a:prstGeom></p:spPr></p:pic>" : ""413            entries.append(("ppt/slides/slide\(i + 1).xml", head + "<p:sld \(ns)><p:cSld>\(emptyTree)\(body)\(pic)</p:spTree></p:cSld><p:clrMapOvr><a:masterClrMapping/></p:clrMapOvr></p:sld>"))414            entries.append(("ppt/slides/_rels/slide\(i + 1).xml.rels", head + "<Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\"><Relationship Id=\"rId1\" Type=\"\(rel)/slideLayout\" Target=\"../slideLayouts/slideLayout1.xml\"/>"415                            + (withPicture ? "<Relationship Id=\"rId2\" Type=\"\(rel)/image\" Target=\"../media/image1.png\"/>" : "") + "</Relationships>"))416        }417        var files = entries.map { ($0.0, Data($0.1.utf8)) }418        if let picture { files.append(("ppt/media/image1.png", picture)) }419        return zip(entries: files)420    }421422    /// A NetCDF classic (CDF-1) file written by hand: dimensions `time` (4) and `x` (3), a coordinate variable423    /// `time` (double), a variable `temperature(time, x)` (float) with a `units` attribute, and a global `title`.424    /// With `grid`, the dimensions are `lat` (4) and `lon` (3), each with its coordinate variable (degrees): the425    /// flattened table then has `lat` / `lon` columns, which a chain to a geospatial format needs.426    static func netcdfClassic(grid: Bool = false) -> Data {427        var out = Data()428        func int(_ v: Int) { var x = Int32(v).bigEndian; out.append(Data(bytes: &x, count: 4)) }429        func pad() { while out.count % 4 != 0 { out.append(0) } }430        func name(_ text: String) { int(text.utf8.count); out.append(Data(text.utf8)); pad() }431        func textAttribute(_ key: String, _ value: String) { name(key); int(2); int(value.utf8.count); out.append(Data(value.utf8)); pad() }432        let first: [Double] = grid ? [45.40, 45.45, 45.50, 45.55] : [0, 6, 12, 18]433        let second: [Double] = [-75.80, -75.75, -75.70]434        let temperatures: [Float] = [271.5, 272.25, 273.0, 274.5, 275.25, 276.0, 277.5, 278.25, 279.0, 276.5, 275.25, 274.0]435        let firstName = grid ? "lat" : "time", secondName = grid ? "lon" : "x"436        // The header is written twice: once to learn its size, once with the real data offsets.437        func header(firstOffset: Int, secondOffset: Int, temperatureOffset: Int) {438            out = Data([0x43, 0x44, 0x46, 0x01])439            int(0)                                            // no record dimension440            int(0x0A); int(2); name(firstName); int(4); name(secondName); int(3)441            int(0x0C); int(1); textAttribute("title", "ORIX Convert fixture")442            int(0x0B); int(grid ? 3 : 2)443            name(firstName); int(1); int(0); int(0x0C); int(1); textAttribute("units", grid ? "degrees_north" : "hours"); int(6); int(first.count * 8); int(firstOffset)444            if grid {445                name(secondName); int(1); int(1); int(0x0C); int(1); textAttribute("units", "degrees_east"); int(6); int(second.count * 8); int(secondOffset)446            }447            name("temperature"); int(2); int(0); int(1); int(0x0C); int(1); textAttribute("units", "K"); int(5); int(temperatures.count * 4); int(temperatureOffset)448        }449        header(firstOffset: 0, secondOffset: 0, temperatureOffset: 0)450        let start = out.count451        let secondBytes = grid ? second.count * 8 : 0452        header(firstOffset: start, secondOffset: start + first.count * 8, temperatureOffset: start + first.count * 8 + secondBytes)453        for value in first { var bits = value.bitPattern.bigEndian; out.append(Data(bytes: &bits, count: 8)) }454        if grid { for value in second { var bits = value.bitPattern.bigEndian; out.append(Data(bytes: &bits, count: 8)) } }455        for value in temperatures { var bits = value.bitPattern.bigEndian; out.append(Data(bytes: &bits, count: 4)) }456        return out457    }458459    static func writeFolder(at url: URL) throws {460        let fm = FileManager.default461        try fm.createDirectory(at: url.appendingPathComponent("docs/notes", isDirectory: true), withIntermediateDirectories: true)462        try Data(text.utf8).write(to: url.appendingPathComponent("readme.txt"))463        try Data(csv().utf8).write(to: url.appendingPathComponent("docs/table.csv"))464        try Data(markdown.utf8).write(to: url.appendingPathComponent("docs/notes/note.md"))465        try writeImage(try image(width: 64, height: 48, seed: 7), to: url.appendingPathComponent("picture.png"))466    }467468    /// A TrueType font copied from the system at run time (nothing is redistributed).469    static func smallestSystemCollection() -> URL? {470        let directory = "/System/Library/Fonts"471        let names = ((try? FileManager.default.contentsOfDirectory(atPath: directory)) ?? []).filter { $0.lowercased().hasSuffix(".ttc") }472        func size(_ name: String) -> Int { ((try? FileManager.default.attributesOfItem(atPath: directory + "/" + name)[.size]) as? NSNumber)?.intValue ?? Int.max }473        return names.min { (size($0), $0) < (size($1), $1) }.map { URL(fileURLWithPath: directory + "/" + $0) }474    }475476    static func systemFont(extension ext: String) -> URL? {477        let fm = FileManager.default478        let preferred = ["Arial.ttf", "Andale Mono.ttf", "Courier New.ttf", "Georgia.ttf"]479        let directories = ["/System/Library/Fonts/Supplemental", "/System/Library/Fonts"]480        if ext == "ttf" {481            for directory in directories { for name in preferred where fm.fileExists(atPath: directory + "/" + name) { return URL(fileURLWithPath: directory + "/" + name) } }482        }483        for directory in directories {484            let names = ((try? fm.contentsOfDirectory(atPath: directory)) ?? []).sorted()485            if let name = names.first(where: { $0.lowercased().hasSuffix("." + ext) && !$0.hasPrefix("LastResort") }) { return URL(fileURLWithPath: directory + "/" + name) }486        }487        return nil488    }489490    // MARK: Video491492    /// A test video assembled by the bundled ffmpeg from material generated here: PNG frames drawn with Core Graphics493    /// (`distinctFrames` of them, repeated `loops` times), a WAV tone and, when `subtitles` is given, a text subtitle494    /// track. (The bundled ffmpeg has no `lavfi` device, so its synthetic sources cannot be used.)495    static func writeVideo(to url: URL, width: Int, height: Int, rate: Int = 25, distinctFrames: Int = 75, loops: Int = 1, codec: String = "mpeg4",496                           subtitles: URL? = nil, extra: [String] = []) async throws {497        let ffmpeg = try BundledTools.require("ffmpeg")498        let fm = FileManager.default499        let scratch = url.deletingLastPathComponent().appendingPathComponent(".frames-\(UUID().uuidString)", isDirectory: true)500        try fm.createDirectory(at: scratch, withIntermediateDirectories: true)501        defer { try? fm.removeItem(at: scratch) }502        let failed = DevCommand.LockedCounter()503        DispatchQueue.concurrentPerform(iterations: distinctFrames) { index in504            do {505                let space = CGColorSpace(name: CGColorSpace.sRGB) ?? CGColorSpaceCreateDeviceRGB()506                guard let context = CGContext(data: nil, width: width, height: height, bitsPerComponent: 8, bytesPerRow: 0, space: space,507                                              bitmapInfo: CGImageAlphaInfo.noneSkipLast.rawValue) else { failed.increment(); return }508                let background = try image(width: width, height: height, seed: 3)509                context.draw(background, in: CGRect(x: 0, y: 0, width: width, height: height))510                // A square that moves across the frame, so consecutive frames differ.511                let t = CGFloat(index) / CGFloat(max(1, distinctFrames - 1))512                context.setFillColor(CGColor(red: 1, green: 0.9, blue: 0.1, alpha: 1))513                let side = CGFloat(height) / 5514                context.fill(CGRect(x: t * (CGFloat(width) - side), y: (CGFloat(height) - side) * (0.5 + 0.4 * sin(t * 2 * .pi)), width: side, height: side))515                guard let frame = context.makeImage() else { failed.increment(); return }516                try writeImage(frame, to: scratch.appendingPathComponent(String(format: "frame-%05d.png", index + 1)))517            } catch { failed.increment() }518        }519        guard failed.value == 0 else { throw SeedError.failed("\(failed.value) video frame(s) could not be drawn.") }520        let seconds = Double(distinctFrames * loops) / Double(rate)521        let tone = scratch.appendingPathComponent("tone.wav")522        try writeWAV(to: tone, seconds: seconds)523        var arguments = ["-hide_banner", "-loglevel", "error", "-nostdin", "-y"]524        if loops > 1 { arguments += ["-stream_loop", String(loops - 1)] }525        arguments += ["-framerate", String(rate), "-i", scratch.appendingPathComponent("frame-%05d.png").path, "-i", tone.path]526        if let subtitles { arguments += ["-i", subtitles.path] }527        arguments += ["-map", "0:v", "-map", "1:a"]528        if subtitles != nil { arguments += ["-map", "2:s", "-c:s", url.pathExtension == "mkv" ? "srt" : "mov_text"] }529        arguments += ["-c:v", codec, "-pix_fmt", "yuv420p", "-r", String(rate), "-c:a", "aac", "-b:a", "128k"] + extra + ["-t", String(seconds), url.path]530        _ = try await ProcessRunner.runChecked(ffmpeg, arguments: arguments, timeout: 1200)531    }532}533534/// A fixture: a real file (or folder) of a format, and how it was obtained.535struct Fixture: Codable, Sendable {536    let format: String537    let path: String538    /// `seed`, `seed (…)` for a seed variant, or the identifier of the conversion path that produced it (followed by539    /// `@<origin of its source>` when the source was not the primary fixture).540    let origin: String541    var url: URL { URL(fileURLWithPath: path) }542    var isSeed: Bool { origin.hasPrefix("seed") }543}544545/// One or more real files per format. The first fixture of a format is its primary one; the others are variants546/// with different content (a font collection, a points-only GeoJSON, the same format produced by another engine…),547/// made on demand when a path cannot work on the primary.548actor FixtureStore {549    static let seedFormats: [FormatID] = [.png, .wav, .midi, .txt, .markdown, .html, .csv, .json, .srt, .geojson, .svg, .obj, .zip, .folder, .ttf, .otf, .pptx, .pdf, .epub, .netcdf, .mp4]550    static let maximumVariants = 8551552    let directory: URL553    let service: ConversionService554    let log: @Sendable (String) -> Void555    private(set) var fixtures: [FormatID: [Fixture]] = [:]556    private(set) var missing: [FormatID: String] = [:]557    private var triedPrimary = Set<String>()558    private var triedVariant: [FormatID: Set<String>] = [:]559    private var exhausted = Set<FormatID>()560    private var inProgress: [FormatID: Task<Fixture?, Never>] = [:]561562    init(directory: URL, service: ConversionService, log: @escaping @Sendable (String) -> Void) {563        self.directory = directory; self.service = service; self.log = log564    }565566    private var manifestURL: URL { directory.appendingPathComponent("manifest.json") }567568    func loadManifest() {569        guard let data = try? Data(contentsOf: manifestURL), let list = try? JSONDecoder().decode([Fixture].self, from: data) else { return }570        for fixture in list where FileManager.default.fileExists(atPath: fixture.path) { fixtures[FormatID(fixture.format), default: []].append(fixture) }571    }572573    private func saveManifest() {574        let list = fixtures.keys.sorted().flatMap { fixtures[$0] ?? [] }575        if let data = try? Shared.json(list).data(using: .utf8) { try? data.write(to: manifestURL, options: .atomic) }576    }577578    var all: [Fixture] { fixtures.keys.sorted().flatMap { fixtures[$0] ?? [] } }579    func primary(_ format: FormatID) -> Fixture? { fixtures[format]?.first }580581    private func folder(_ format: FormatID, _ index: Int) throws -> URL {582        let folder = directory.appendingPathComponent(format.rawValue, isDirectory: true).appendingPathComponent(index == 0 ? "primary" : "variant-\(index)", isDirectory: true)583        try? FileManager.default.removeItem(at: folder)584        try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)585        return folder586    }587588    /// Writes the seeds of `format` (the primary one first, then its variants).589    private func seeds(_ format: FormatID) async throws -> [Fixture] {590        let fm = FileManager.default591        let ext = FormatCatalog.format(format)?.preferredExtension ?? format.rawValue592        var result: [Fixture] = []593        func target() throws -> URL {594            let folder = try folder(format, result.count)595            return format == .folder ? folder.appendingPathComponent("fixture", isDirectory: true) : folder.appendingPathComponent("fixture." + ext)596        }597        func add(_ label: String, _ write: (URL) throws -> Void) throws {598            let url = try target()599            try write(url)600            result.append(Fixture(format: format.rawValue, path: url.path, origin: label))601        }602        switch format {603        case .png: try add("seed") { try Seeds.writeImage(try Seeds.image(width: 320, height: 240, seed: 1), to: $0) }604        case .wav: try add("seed") { try Seeds.writeWAV(to: $0, seconds: 2) }605        case .midi: try add("seed") { try Seeds.midi().write(to: $0) }606        case .txt: try add("seed") { try Data(Seeds.text.utf8).write(to: $0) }607        case .markdown: try add("seed") { try Data(Seeds.markdown.utf8).write(to: $0) }608        case .html: try add("seed") { try Data(try Seeds.htmlWithPicture().utf8).write(to: $0) }609        case .pdf:610            try add("seed (three text pages drawn with Core Graphics)") { try DevCommand.writePDF(to: $0, pages: 3, scanned: false) }611            try add("seed (two page images without a text layer)") { try DevCommand.writePDF(to: $0, pages: 2, scanned: true) }612        case .epub: try add("seed") { try Seeds.epub().write(to: $0) }613        case .netcdf:614            try add("seed (NetCDF classic written by hand)") { try Seeds.netcdfClassic().write(to: $0) }615            try add("seed (NetCDF classic with lat / lon coordinate variables)") { try Seeds.netcdfClassic(grid: true).write(to: $0) }616        case .csv:617            try add("seed") { try Data(Seeds.csv().utf8).write(to: $0) }618            try add("seed (longitude / latitude columns)") { try Data(Seeds.placesCSV.utf8).write(to: $0) }619        case .json: try add("seed") { try Data(Seeds.json().utf8).write(to: $0) }620        case .srt: try add("seed") { try Data(Seeds.srt.utf8).write(to: $0) }621        case .geojson:622            try add("seed") { try Data(Seeds.geojson.utf8).write(to: $0) }623            try add("seed (points and lines only)") { try Data(Seeds.pointsGeoJSON.utf8).write(to: $0) }624        case .svg:625            try add("seed") { try Data(Seeds.svg.utf8).write(to: $0) }626            try add("seed (with text elements)") { try Data(Seeds.svgWithText.utf8).write(to: $0) }627        case .obj: try add("seed") { try Data(Seeds.obj.utf8).write(to: $0) }628        case .zip:629            try add("seed") {630                try Seeds.zip(entries: [("readme.txt", Data(Seeds.text.utf8)), ("docs/table.csv", Data(Seeds.csv().utf8)), ("docs/note.md", Data(Seeds.markdown.utf8))]).write(to: $0)631            }632        case .folder:633            try add("seed") { try Seeds.writeFolder(at: $0) }634            // `.folder` is also the pseudo-source of the generic operations. The ones marked `appliesToAnyFile`635            // (compress one file to a gzip / bzip2 / xz / zstd stream) take ONE regular file of any kind: this is it.636            try add("seed (one loose file, for the any-file operations)") { try Data((Seeds.text + Seeds.csv()).utf8).write(to: $0) }637        case .pptx:638            try add("seed") { url in639                let scratch = url.deletingLastPathComponent().appendingPathComponent("picture.png")640                try Seeds.writeImage(try Seeds.image(width: 320, height: 240, seed: 5), to: scratch)641                let picture = try Data(contentsOf: scratch)642                try? fm.removeItem(at: scratch)643                try Seeds.pptx(slides: ["ORIX Convert fixture", "Second slide", "Troisième diapositive"], picture: picture).write(to: url)644            }645        case .ttf:646            guard let font = Seeds.systemFont(extension: "ttf") else { throw Seeds.SeedError.failed("No .ttf font was found in /System/Library/Fonts.") }647            try add("seed (\(font.lastPathComponent) copied from the system at run time)") { try fm.copyItem(at: font, to: $0) }648            if let collection = Seeds.smallestSystemCollection() {649                try add("seed (font collection \(collection.lastPathComponent) copied from the system at run time)") { try fm.copyItem(at: collection, to: $0) }650            }651        case .otf:652            guard let font = Seeds.systemFont(extension: "otf") else { throw Seeds.SeedError.failed("No .otf font was found in /System/Library/Fonts.") }653            try add("seed (\(font.lastPathComponent) copied from the system at run time)") { try fm.copyItem(at: font, to: $0) }654        case .mp4:655            let url = try target()656            let subtitles = url.deletingLastPathComponent().appendingPathComponent("subtitles.srt")657            try Data(Seeds.srt.utf8).write(to: subtitles)658            // H.264 (VideoToolbox) + AAC + a text subtitle track: what most MP4 files hold.659            do {660                try await Seeds.writeVideo(to: url, width: 320, height: 240, codec: "h264_videotoolbox", subtitles: subtitles, extra: ["-b:v", "600k"])661            } catch {662                try await Seeds.writeVideo(to: url, width: 320, height: 240, subtitles: subtitles)663            }664            try? fm.removeItem(at: subtitles)665            result.append(Fixture(format: format.rawValue, path: url.path, origin: "seed (frames drawn in code, encoded by the bundled ffmpeg, with a subtitle track)"))666        default: throw Seeds.SeedError.failed("No seed generator for \(format.rawValue).")667        }668        return result669    }670671    private func waitWithTimeout(_ id: UUID, seconds: Double) async -> ConversionJob? {672        let queue = service.queue673        let flag = DevCommand.LockedCounter()674        let watchdog = Task {675            try await Task.sleep(nanoseconds: UInt64(seconds * 1_000_000_000))676            flag.increment()677            await queue.cancel(id)678        }679        let job = await queue.waitForJob(id)680        watchdog.cancel()681        return flag.value > 0 ? nil : job682    }683684    private func convert(_ path: ConversionPath, source: Fixture, into folder: URL, options: [String: OptionValue] = [:]) async -> (fixture: URL?, error: String?) {685        let request = ConversionJobRequest(sources: [source.url], sourceFormat: path.source, destinationFormat: path.destination, operation: path.operation,686                                           options: ConversionOptions(options), engineID: path.engine,687                                           destination: OutputDestination(directory: folder, nameTemplate: "fixture", conflictPolicy: .replace),688                                           allowChaining: false, recordInHistory: false)689        let id = await service.queue.enqueue(request)690        guard let job = await waitWithTimeout(id, seconds: 600) else { return (nil, "timed out") }691        _ = await service.queue.remove(id)692        if job.state == .finished, let output = job.outputs.first {693            // Formats made of several files (a shapefile with its .dbf/.shx, a glTF with its .bin) are delivered as a694            // folder: the fixture is the file a user would pick inside it.695            var isDirectory: ObjCBool = false696            let isFolderFormat = FormatCatalog.format(path.destination)?.traits.contains(.directory) ?? false697            if !isFolderFormat, FileManager.default.fileExists(atPath: output.url.path, isDirectory: &isDirectory), isDirectory.boolValue {698                let extensions = FormatCatalog.format(path.destination)?.extensions ?? [path.destination.rawValue]699                let inside = ((try? FileManager.default.contentsOfDirectory(atPath: output.url.path)) ?? []).sorted()700                guard let name = inside.first(where: { extensions.contains(($0 as NSString).pathExtension.lowercased()) }) else {701                    return (nil, "the output folder holds no .\(extensions.first ?? "") file")702                }703                return (output.url.appendingPathComponent(name), nil)704            }705            return (output.url, nil)706        }707        return (nil, job.error ?? job.state.rawValue)708    }709710    /// One primary fixture per requested format where possible: seeds first, then conversions from formats already711    /// in hand, round after round, through the real queue.712    func build(for wanted: Set<FormatID>) async {713        try? FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)714        for format in Self.seedFormats where fixtures[format] == nil {715            do {716                fixtures[format] = try await seeds(format)717            } catch {718                missing[format] = "seed generation failed: \(CLI.describe(error))"719                log("fixture \(format.rawValue): \(missing[format] ?? "")")720            }721        }722        saveManifest()723        let registry = service.registry724        var lastErrors: [FormatID: String] = [:]725        while !InterruptCenter.shared.isInterrupted {726            var attempts: [(format: FormatID, path: ConversionPath, source: Fixture, folder: URL)] = []727            for format in wanted.subtracting(fixtures.keys).sorted() {728                let candidates = registry.paths(to: format).filter { path in729                    !path.acceptsMultipleSources && path.source != format && path.source != .folder && fixtures[path.source] != nil && !triedPrimary.contains(path.id)730                }.sorted { a, b in731                    // Plain conversions before named operations (single-file compression is only offered as one).732                    if (a.operation == nil) != (b.operation == nil) { return a.operation == nil }733                    // Seeds first (shortest chain), then the core's own preference.734                    let aSeed = primary(a.source)?.isSeed ?? false, bSeed = primary(b.source)?.isSeed ?? false735                    if aSeed != bSeed { return aSeed }736                    // Among seeds, the order of `seedFormats` (a WAV before a MIDI file, a PNG before an SVG).737                    let aRank = Self.seedFormats.firstIndex(of: a.source) ?? Int.max, bRank = Self.seedFormats.firstIndex(of: b.source) ?? Int.max738                    if aSeed, aRank != bRank { return aRank < bRank }739                    return ConversionCapabilityResolver.prefers(a, over: b)740                }741                if let path = candidates.first, let source = primary(path.source), let folder = try? folder(format, 0) { attempts.append((format, path, source, folder)) }742            }743            guard !attempts.isEmpty else { break }744            for attempt in attempts { triedPrimary.insert(attempt.path.id) }745            let done = await withTaskGroup(of: (Int, URL?, String?).self) { group -> [(Int, URL?, String?)] in746                for (index, attempt) in attempts.enumerated() {747                    group.addTask { let r = await self.convert(attempt.path, source: attempt.source, into: attempt.folder); return (index, r.fixture, r.error) }748                }749                var list: [(Int, URL?, String?)] = []750                for await item in group { list.append(item) }751                return list752            }753            for (index, url, error) in done {754                let attempt = attempts[index]755                if let url {756                    fixtures[attempt.format] = [Fixture(format: attempt.format.rawValue, path: url.path, origin: attempt.path.id)]757                    saveManifest()758                } else {759                    lastErrors[attempt.format] = "\(attempt.path.id): \(error ?? "failed")"760                    log("fixture \(attempt.format.rawValue): \(attempt.path.id) did not produce it — \(error ?? "failed")")761                }762            }763            saveManifest()764        }765        for format in wanted.subtracting(fixtures.keys) where missing[format] == nil {766            let writers = registry.paths(to: format).filter { !$0.acceptsMultipleSources && $0.source != format && $0.source != .folder }767            if writers.isEmpty {768                missing[format] = "no engine writes \(format.rawValue) and no seed can be generated in code"769            } else if let error = lastErrors[format] {770                missing[format] = "every conversion that could produce it failed (last: \(error))"771            } else {772                missing[format] = "its only producers start from formats that have no fixture either (\(Set(writers.map(\.source.rawValue)).sorted().joined(separator: ", ")))"773            }774        }775    }776777    /// The fixture number `index` of a format: an existing one, or a new variant made now (another producing path or778    /// another source variant). `hint` is the path that could not work on the fixtures tried so far: variants written779    /// by its reverse path (destination → format, same operation) are tried first. `nil` when no further variant can780    /// be made.781    func fixture(_ format: FormatID, index: Int, hint: (destination: FormatID, operation: String?)? = nil) async -> Fixture? {782        while true {783            if let list = fixtures[format], index < list.count { return list[index] }784            guard fixtures[format] != nil, index < Self.maximumVariants, !exhausted.contains(format) else { return nil }785            if let running = inProgress[format] { _ = await running.value; continue }786            let task = Task { await self.makeVariant(of: format, hint: hint) }787            inProgress[format] = task788            let made = await task.value789            inProgress[format] = nil790            if made == nil { return nil }791        }792    }793794    private func makeVariant(of format: FormatID, hint: (destination: FormatID, operation: String?)?) async -> Fixture? {795        let registry = service.registry796        // MPEG program streams: the default MP4 → MPEG conversion writes MP2 sound, which MP4 cannot hold without797        // re-encoding, so `mpeg → mp4 #video.remux` has nothing to copy. A real MPEG-PS with AC-3 sound can be remuxed.798        let ac3Key = "video:mp4>mpeg[audio.codec=ac3]"799        if format == .mpeg, !triedVariant[format, default: []].contains(ac3Key), let source = fixtures[.mp4]?.first,800           let path = registry.paths(from: .mp4, to: .mpeg, operation: .some(nil)).first, let folder = try? folder(format, fixtures[format]?.count ?? 1) {801            triedVariant[format, default: []].insert(ac3Key)802            if let url = await convert(path, source: source, into: folder, options: ["audio.codec": .string("ac3")]).fixture {803                let fixture = Fixture(format: format.rawValue, path: url.path, origin: ac3Key)804                fixtures[format, default: []].append(fixture)805                saveManifest()806                return fixture807            }808        }809        let producers = registry.paths(to: format).filter { !$0.acceptsMultipleSources && $0.source != format && $0.source != .folder }810        var candidates: [(path: ConversionPath, source: Fixture, key: String, rank: [Int])] = []811        for path in producers {812            for (position, source) in (fixtures[path.source] ?? []).enumerated() {813                let key = path.id + "@" + source.origin814                let isPrimary = position == 0 && fixtures[format]?.first?.origin == path.id815                if isPrimary || triedVariant[format, default: []].contains(key) { continue }816                // Reverse of the failing path first, then seed variants (different content), then seeds, then the rest;817                // plain conversions before named operations.818                let rank = [path.source == hint?.destination ? 0 : 1, hint != nil && path.operation == hint?.operation ? 0 : 1,819                            source.isSeed && source.origin != "seed" ? 0 : 1, source.isSeed ? 0 : 1, path.operation == nil ? 0 : 1, position]820                candidates.append((path, source, key, rank))821            }822        }823        guard !candidates.isEmpty else { exhausted.insert(format); return nil }824        candidates.sort { a, b in a.rank != b.rank ? a.rank.lexicographicallyPrecedes(b.rank) : a.key < b.key }825        for candidate in candidates.prefix(3) {826            triedVariant[format, default: []].insert(candidate.key)827            guard let folder = try? folder(format, fixtures[format]?.count ?? 1) else { return nil }828            let result = await convert(candidate.path, source: candidate.source, into: folder)829            if let url = result.fixture {830                let fixture = Fixture(format: format.rawValue, path: url.path, origin: candidate.key)831                fixtures[format, default: []].append(fixture)832                saveManifest()833                return fixture834            }835        }836        return nil837    }838}839
Files main
Menu

Theme