Repository Public License
1// ORIX Convert — Packages/EngineArchive/Tests/EngineArchiveTests/MatrixTests.swift2// Description: One real conversion per declared path of the archive engine (extract, create, compress, convert,3// recompress, verify), each checked by the core's validation, the engine's own, and an independent4// comparison with the source data.5// Author: Simon-Pierre Boucher — contact@spboucher.ai6import ConvertCore7@testable import EngineArchive8import Foundation9import Testing10import zlib1112/// Fixtures shared by the matrix: one tree, and one archive of it per format, made once.13actor MatrixFixtures {14 static let shared = MatrixFixtures()15 private var scratch: Scratch?16 private var archives: [FormatID: URL] = [:]17 private var treeURL: URL?18 /// Formats whose fixture had to be written by the engine itself because no other tool on this Mac writes them.19 private(set) var selfMade = Set<FormatID>()2021 static let rarFiles: [(name: String, data: String)] = [("readme.txt", "Stored in a RAR archive.\n"), ("docs/notes with spaces.txt", "Second file, in a folder.\n"), ("empty.txt", "")]2223 func base() throws -> Scratch {24 if let scratch { return scratch }25 let s = try Scratch()26 scratch = s27 return s28 }2930 func tree() throws -> URL {31 if let treeURL { return treeURL }32 let url = try Tree.make(at: try base().url("matrix/tree"), bigMegabytes: 1)33 treeURL = url34 return url35 }3637 func streamPayload() throws -> URL {38 let url = try base().url("matrix/payload.log")39 if !FileManager.default.fileExists(atPath: url.path) { try Tree.bigLog(megabytes: 1).write(to: url) }40 return url41 }4243 func archive(_ format: FormatID) async throws -> URL {44 if let url = archives[format] { return url }45 let scratch = try base()46 var url: URL?47 if format == .rar {48 url = try scratch.file("matrix/fixture.rar", Craft.rar5(Self.rarFiles))49 } else if ArchiveKind.isStream(format) {50 url = try Fixture.systemStream(format, of: try streamPayload(), in: scratch)51 if url == nil {52 selfMade.insert(format)53 url = try await runEngine([try streamPayload()], .txt, format, operation: ArchiveEngine.compressOperation, name: "payload.log", in: scratch).url54 }55 } else {56 url = try Fixture.system(format, of: try tree(), in: scratch)57 if url == nil {58 selfMade.insert(format)59 url = try await runEngine([try tree()], .folder, format, operation: ArchiveEngine.createOperation, name: "fixture", in: scratch).url60 }61 }62 let result = try #require(url)63 archives[format] = result64 return result65 }66}6768@Suite struct MatrixTests {69 static let engine = ArchiveEngine()70 static let allPaths = engine.paths71 static let files = ArchiveEngine.fileFormats7273 @Test func pathCountsAreWhatTheEngineDocuments() {74 let p = Self.allPaths75 let n = Self.files.count76 #expect(Set(p.map(\.id)).count == p.count)77 #expect(p.filter { $0.operation == ArchiveEngine.extractOperation }.count == 12)78 #expect(p.filter { $0.operation == ArchiveEngine.createOperation }.count == (n + 1) * 7)79 #expect(p.filter { $0.operation == ArchiveEngine.compressOperation }.count == (n - 8) * 4 + 4) // + the generic “any file” declaration (.folder source)80 #expect(p.filter { $0.operation == nil }.count == 8 * 7 - 7 + 12)81 #expect(p.filter { $0.operation == ArchiveEngine.recompressOperation }.count == 7 + 4)82 #expect(p.filter { $0.operation == ArchiveEngine.verifyOperation }.count == 24)83 // RAR is never a destination, and everything that reads RAR is marked experimental.84 #expect(!p.contains { $0.destination == .rar })85 #expect(p.filter { $0.source == .rar && $0.operation != ArchiveEngine.createOperation && $0.operation != ArchiveEngine.compressOperation }.allSatisfy { $0.status == .experimental })86 #expect(p.filter { $0.status == .experimental }.allSatisfy { $0.source == .rar } && p.filter { $0.status == .experimental }.count == 1 + 7 + 2)87 #expect(p.allSatisfy { !$0.limitations.isEmpty })88 // Without file sources only the folder paths remain for creation, and nothing for single-file compression.89 let lean = ArchiveEngine(declaresFileSources: false).paths90 #expect(lean.filter { $0.operation == ArchiveEngine.createOperation }.count == 7)91 #expect(lean.filter { $0.operation == ArchiveEngine.compressOperation }.allSatisfy { $0.source == .folder } && lean.filter { $0.operation == ArchiveEngine.compressOperation }.count == 4)92 #expect(lean.count == 12 + 7 + 4 + 61 + 11 + 24)93 #expect((makeEngines().first as? ArchiveEngine)?.declaresFileSources == false)94 print("ARCHIVE-PATHS total=\(p.count) fileFormats=\(n) lean=\(lean.count)")95 }9697 static func crc(_ data: Data) -> String { data.withUnsafeBytes { String(format: "%08x", UInt32(truncatingIfNeeded: crc32(0, $0.bindMemory(to: Bytef.self).baseAddress, uInt(data.count)))) } }9899 /// The expected content of the RAR fixture, as a tree on disk.100 static func rarTree(in scratch: Scratch) throws -> URL {101 let root = try scratch.directory("rar-expected-\(UUID().uuidString.prefix(6))")102 for file in MatrixFixtures.rarFiles {103 let url = root.appendingPathComponent(file.name)104 try FileManager.default.createDirectory(at: url.deletingLastPathComponent(), withIntermediateDirectories: true)105 try Data(file.data.utf8).write(to: url)106 }107 return root108 }109110 @Test(arguments: allPaths)111 func everyDeclaredPathConvertsAndValidates(path: ConversionPath) async throws {112 let scratch = try Scratch(); defer { scratch.cleanUp() }113 let fixtures = MatrixFixtures.shared114 let label = path.id115 switch path.operation {116 case ArchiveEngine.createOperation where path.source != .folder, ArchiveEngine.compressOperation:117 // The engine does not look inside the file: any catalog format is a file to store. One file per format,118 // named with that format's extension.119 let ext = FormatCatalog.format(path.source)?.preferredExtension ?? path.source.rawValue120 let content = Tree.random(1500, seed: UInt64(abs(ext.hashValue % 9973)) + 3) + Data("plain text that compresses, plain text that compresses\n".utf8)121 let source = try scratch.file("sample.\(ext)", content)122 let out = try await runEngine([source], path.source, path.destination, operation: path.operation, in: scratch)123 expectAllPass(out.checks, label)124 let verification = try await ArchiveVerifier.verify(out.url, format: path.destination)125 #expect(verification.passed && verification.entries.count == 1 && verification.entries[0].crc32 == Self.crc(content), "\(label)")126 if path.operation == ArchiveEngine.createOperation { #expect(verification.entries[0].path == "sample.\(ext)", "\(label)") }127 case ArchiveEngine.createOperation:128 let tree = try await fixtures.tree()129 let out = try await runEngine([tree], .folder, path.destination, operation: path.operation, in: scratch)130 expectAllPass(out.checks, label)131 let back = try await runEngine([out.url], path.destination, .folder, operation: ArchiveEngine.extractOperation, name: "back", in: scratch)132 try Tree.expectEqual(tree, back.url.appendingPathComponent("tree"), label)133 case ArchiveEngine.extractOperation:134 let archive = try await fixtures.archive(path.source)135 let out = try await runEngine([archive], path.source, .folder, operation: path.operation, name: "out", in: scratch)136 expectAllPass(out.checks, label)137 #expect(out.checks.contains { $0.name == "Extracted files identical to the archive's content" })138 if path.source == .rar {139 try Tree.expectEqual(try Self.rarTree(in: scratch), out.url, times: false, modes: false, label)140 // RAR 4 as well, through the same path.141 let rar4 = try scratch.file("four.rar", Craft.rar4(MatrixFixtures.rarFiles))142 let four = try await runEngine([rar4], .rar, .folder, operation: path.operation, name: "four", in: scratch)143 expectAllPass(four.checks, label + " (RAR 4)")144 try Tree.expectEqual(try Self.rarTree(in: scratch), four.url, times: false, modes: false, label + " (RAR 4)")145 } else if ArchiveKind.isStream(path.source) {146 let names = try FileManager.default.contentsOfDirectory(atPath: out.url.path)147 #expect(names == ["payload.log"], "\(label): \(names)")148 #expect(try Data(contentsOf: out.url.appendingPathComponent("payload.log")) == Data(contentsOf: try await fixtures.streamPayload()), "\(label)")149 } else {150 try Tree.expectEqual(try await fixtures.tree(), out.url.appendingPathComponent("tree"), directoryTimes: path.source != .sevenZip, label)151 }152 case ArchiveEngine.verifyOperation:153 let archive = try await fixtures.archive(path.source)154 let out = try await runEngine([archive], path.source, path.destination, operation: path.operation, in: scratch)155 expectAllPass(out.checks, label)156 let expectedFiles = path.source == .rar ? "3" : ArchiveKind.isStream(path.source) ? "1" : "8"157 #expect(out.output.facts["passed"] == "yes" && out.output.facts["failed"] == "0" && out.output.facts["checked"] == expectedFiles, "\(label): \(out.output.facts)")158 let text = try String(contentsOf: out.url, encoding: .utf8)159 if path.destination == .json {160 let decoded = try JSONDecoder().decode(ArchiveEngine.VerificationFile.self, from: Data(text.utf8))161 #expect(decoded.passed && decoded.report.entries.allSatisfy(\.ok) && decoded.report.entries.count == Int(expectedFiles), "\(label)")162 } else {163 #expect(text.contains("Result: PASSED") && text.contains("Checked: \(expectedFiles) file(s)"), "\(label)")164 }165 #expect(!out.collector.has("verification-failed"))166 default:167 // Archive → archive and stream → stream (conversion or recompression).168 let archive = try await fixtures.archive(path.source)169 let out = try await runEngine([archive], path.source, path.destination, operation: path.operation, options: ["compression.level": .string("fast")], in: scratch)170 expectAllPass(out.checks, label)171 if ArchiveKind.isStream(path.destination) {172 let payload = try Data(contentsOf: try await fixtures.streamPayload())173 if let decoded = try Fixture.systemDecompress(out.url, path.destination) { #expect(decoded == payload, "\(label) (outside decoder)") }174 let back = try await runEngine([out.url], path.destination, .folder, operation: ArchiveEngine.extractOperation, name: "payload.log", in: scratch)175 #expect(try Data(contentsOf: back.url.appendingPathComponent("payload.log")) == payload, "\(label)")176 } else {177 // Read the result with a tool that is not this engine whenever one exists.178 let extracted: URL179 if Fixture.systemReads(path.destination) { extracted = try Fixture.systemExtract(out.url, path.destination, in: scratch) }180 else { extracted = try await runEngine([out.url], path.destination, .folder, operation: ArchiveEngine.extractOperation, name: "back", in: scratch).url }181 if path.source == .rar { try Tree.expectEqual(try Self.rarTree(in: scratch), extracted, times: false, modes: false, label) }182 else { try Tree.expectEqual(try await fixtures.tree(), extracted.appendingPathComponent("tree"), directoryTimes: path.source != .sevenZip && path.destination != .zip, label) }183 }184 }185 // Every option offered for the path is understood by the settings decoder at its default value.186 let descriptors = Self.engine.options(for: path)187 #expect(Set(descriptors.map(\.id)).count == descriptors.count, "\(label)")188 _ = try ArchiveSettings(options: ConversionOptions(Dictionary(uniqueKeysWithValues: descriptors.map { ($0.id, $0.defaultValue) })))189 }190191 @Test func fixturesComeFromOtherToolsWhereverOneExists() async throws {192 for format in ArchiveEngine.archiveFormats { _ = try await MatrixFixtures.shared.archive(format) }193 let selfMade = await MatrixFixtures.shared.selfMade194 // Only xz and zstd can lack an outside encoder (macOS ships neither command); RAR is assembled by hand.195 #expect(selfMade.isSubset(of: [.xz, .zst, .tzst]))196 print("ARCHIVE-FIXTURES selfMade=\(selfMade.map(\.rawValue).sorted()) homebrewXZ=\(Fixture.homebrew("xz") != nil) homebrewZSTD=\(Fixture.homebrew("zstd") != nil)")197 }198199 @Test func anArchiveOfEmptyFoldersIsCreatedAndExtracted() async throws {200 let scratch = try Scratch(); defer { scratch.cleanUp() }201 let tree = try scratch.directory("hollow")202 try FileManager.default.createDirectory(at: tree.appendingPathComponent("a/b"), withIntermediateDirectories: true)203 try FileManager.default.createDirectory(at: tree.appendingPathComponent("c"), withIntermediateDirectories: true)204 for format in [FormatID.zip, .tar, .sevenZip] {205 let archive = try await runEngine([tree], .folder, format, operation: ArchiveEngine.createOperation, name: "hollow-\(format.rawValue)", in: scratch)206 expectAllPass(archive.checks, "create \(format.rawValue)")207 let back = try await runEngine([archive.url], format, .folder, operation: ArchiveEngine.extractOperation, name: "back-\(format.rawValue)", in: scratch)208 expectAllPass(back.checks, "extract \(format.rawValue)") // includes the core's folder check: entries of any kind209 var isDirectory: ObjCBool = false210 #expect(FileManager.default.fileExists(atPath: back.url.appendingPathComponent("hollow/a/b").path, isDirectory: &isDirectory) && isDirectory.boolValue, "\(format.rawValue)")211 #expect(FileManager.default.fileExists(atPath: back.url.appendingPathComponent("hollow/c").path))212 }213 }214215 @Test func looseFilesCanBeArchivedThroughTheFolderPathWhateverTheEngineDeclares() async throws {216 let scratch = try Scratch(); defer { scratch.cleanUp() }217 let a = try scratch.file("a.unknownext", Data("alpha".utf8)), b = try scratch.file("b", Data("beta".utf8))218 let lean = ArchiveEngine(declaresFileSources: false)219 let out = try await runEngine([a, b], .folder, .zip, operation: ArchiveEngine.createOperation, engine: lean, in: scratch)220 expectAllPass(out.checks, "loose files")221 #expect(try await ArchiveBrowser.list(out.url, format: .zip).entries.map(\.path) == ["a.unknownext", "b"])222 let gz = try await runEngine([a], .txt, .gz, operation: ArchiveEngine.compressOperation, engine: lean, in: scratch)223 expectAllPass(gz.checks, "loose file gz")224 // A folder cannot become a single stream, and says what to do instead.225 let folder = try scratch.directory("dir")226 do {227 _ = try await runEngine([folder], .txt, .gz, operation: ArchiveEngine.compressOperation, in: scratch)228 Issue.record("compressed a folder to a single stream")229 } catch ConversionError.invalidOption(let message) { #expect(message.contains(".tar.gz")) }230 await #expect(throws: ConversionError.unsupportedPath(source: .zip, destination: .rar)) { _ = try await runEngine([a], .zip, .rar, operation: nil, in: scratch) }231 await #expect(throws: ConversionError.unsupportedPath(source: .folder, destination: .rar)) { _ = try await runEngine([folder], .folder, .rar, operation: ArchiveEngine.createOperation, in: scratch) }232 }233}234