Repository Public License
1// ORIX Convert — Packages/EngineImage/Tests/EngineImageTests/MatrixTests.swift2// Description: Converts a generated fixture through every declared path of the image engine and validates the3// result. A path that is not exercised here must not be declared.4// Author: Simon-Pierre Boucher — contact@spboucher.ai5import ConvertCore6import CoreGraphics7import Foundation8import Testing9@testable import EngineImage1011extension ConversionPath: @retroactive CustomTestStringConvertible {12 public var testDescription: String { id }13}1415@Suite("Declared path matrix")16struct MatrixTests {17 static let plainPaths = ImageEngine.declaredPaths.filter { $0.operation == nil }18 static let operationPaths = ImageEngine.declaredPaths.filter { $0.operation != nil }1920 @Test("Formats are taken from ImageIO at run time and the matrix is complete")21 func matrixShape() {22 let sources = Set(Self.plainPaths.map(\.source)), destinations = Set(Self.plainPaths.map(\.destination))23 #expect(Self.plainPaths.count == sources.count * destinations.count)24 #expect(Set(ImageEngine.declaredPaths.map(\.id)).count == ImageEngine.declaredPaths.count)25 // Never svg or pdf, never an unreadable or unwritable format.26 #expect(!sources.contains(.svg) && !destinations.contains(.svg) && !sources.contains(.pdf) && !destinations.contains(.pdf))27 #expect(!sources.contains(.jxl) && !destinations.contains(.jxl))28 #expect(!destinations.contains(.dng) && !destinations.contains(.raw))29 for path in ImageEngine.declaredPaths where path.source == .raw || path.source == .dng { #expect(path.status == .experimental) }30 for required in [FormatID.png, .jpeg, .heic, .webp, .avif, .tiff, .gif, .bmp] { #expect(sources.contains(required) && destinations.contains(required)) }31 }3233 @Test("Every declared path converts a generated fixture and validates", arguments: plainPaths)34 func everyPath(_ path: ConversionPath) async throws {35 let source = try await Fixtures.url(for: path.source)36 let run = try await convert(source, from: path.source, to: path.destination)37 #expect(run.failedChecks.isEmpty, "\(run.failedChecks)")38 #expect(run.checks.contains { $0.name == "decodes" && $0.passed })39 #expect(run.checks.contains { $0.name == "dimensions" && $0.passed })40 #expect(run.url.path.hasPrefix(Fixtures.directory.path))41 let size = (try FileManager.default.attributesOfItem(atPath: run.url.path)[.size] as? Int) ?? 042 #expect(size > 0)43 #expect(run.output.facts["bytes"] == String(size))44 let state = try #require(run.output.facts["verify.pixels"])4546 if path.fidelity == .lossless {47 // The promise of a lossless path: decoding the output gives back the pixels of the source.48 #expect(state == "identical")49 let handle = try ImageSourceHandle.open(source)50 let original = try handle.image(at: handle.info.selectedIndex, maxPixels: 1 << 30, checkCancellation: {})51 let difference = try PixelCompare.compare(original, try run.image())52 #expect(difference.identical, "max difference \(difference.maxDifference), \(difference.differingPixels) pixels")53 } else if state != "identical" {54 let psnr = try #require(run.output.facts["psnr.db"].flatMap(Double.init))55 #expect(psnr.isFinite && psnr > 0)56 // Quality encoders on a smooth fixture must stay well above noise level.57 if [FormatID.jpeg, .heic, .heif, .avif, .webp, .jp2].contains(path.destination) { #expect(psnr > 24, "PSNR \(psnr) dB") }58 }5960 // Declared preservation must be observable on the fixture. These fixtures have really transparent pixels:61 let transparentFixtures: Set<FormatID> = [.png, .apng, .heic, .heif, .avif, .webp, .tiff, .ico, .icns, .tga, .jp2, .exr, .psd]62 let destination = try #require(ImageFormats.spec(path.destination))63 if transparentFixtures.contains(path.source) {64 #expect(run.output.facts["alpha"] == (destination.alpha ? "yes" : "no"))65 #expect(path.preserves.contains(.transparency) == destination.alpha)66 #expect(run.codes.contains("alpha-flattened") == !destination.alpha)67 } else {68 #expect(run.output.facts["alpha"] == "no" || destination.id == .ico || destination.id == .icns)69 }70 // Fixtures of these formats carry EXIF and GPS: a path that declares metadata preservation must deliver it.71 if path.preserves.contains(.metadata), [FormatID.png, .jpeg, .heic, .heif, .tiff].contains(path.source) {72 #expect(!run.codes.contains("metadata-dropped"))73 #expect(run.output.facts["metadata.exif"] != "none" && run.output.facts["metadata.gps"] != "none", "\(run.output.facts.filter { $0.key.hasPrefix("metadata") })")74 }75 if path.preserves.contains(.colorProfile) { #expect(run.output.facts["metadata.icc"] != nil || destination.id == .webp || destination.id == .apng, "\(run.output.facts)") }76 if path.preserves.contains(.animation) {77 #expect(run.output.facts["frames"] == "4")78 #expect(run.checks.contains { $0.name == "animation timing" && $0.passed })79 }80 if run.inspection.isAnimated == true, !destination.animated { #expect(run.codes.contains("animation-first-frame")) }81 if destination.id == .ico { #expect(run.output.facts["frames"] == String(IconPlan.sizes(for: .ico, longEdge: max(run.inspection.pixelWidth ?? 0, run.inspection.pixelHeight ?? 0), requested: nil).count)) }82 }8384 @Test("Named operations run on generated fixtures", arguments: operationPaths)85 func operations(_ path: ConversionPath) async throws {86 let source = try await Fixtures.url(for: path.source)87 let run = try await convert(source, from: path.source, to: path.destination, operation: path.operation)88 #expect(run.failedChecks.isEmpty, "\(run.failedChecks)")89 if path.fidelity == .lossless {90 #expect(run.output.facts["verify.pixels"] == "identical")91 #expect(try PixelCompare.compare(try Synth.decode(source), try run.image()).identical)92 }93 }94}95