SPB Git

Repository Public License

MatrixTests.swift 6.4 KB 105 lines swift

1// ORIX Convert — Packages/EngineSubtitle/Tests/EngineSubtitleTests/MatrixTests.swift2// Description: One conversion per declared path of the subtitle engine, validated by the core's generic checks, the3//              engine's own validation and an independent comparison with the fixture's known cues.4// Author: Simon-Pierre Boucher — contact@spboucher.ai5import ConvertCore6@testable import EngineSubtitle7import Foundation8import Testing910@Suite struct MatrixTests {11    static let engine = SubtitleEngine()12    static let allPaths = engine.paths1314    @Test func declaresTwentyConversionsAndTenOperations() {15        let conversions = Self.allPaths.filter { $0.operation == nil }16        #expect(conversions.count == 20)17        #expect(Set(conversions.map { "\($0.source)>\($0.destination)" }).count == 20)18        #expect(Self.allPaths.filter { $0.operation == SubtitleEngine.retimeOperation }.count == 5)19        #expect(Self.allPaths.filter { $0.operation == SubtitleEngine.cleanOperation }.count == 5)20        #expect(Set(Self.allPaths.map(\.id)).count == 30)21        #expect(Self.allPaths.allSatisfy { !$0.limitations.isEmpty && $0.engine == "subtitle" })22    }2324    @Test(arguments: allPaths)25    func everyDeclaredPathConvertsAndValidates(path: ConversionPath) async throws {26        let scratch = try Scratch(); defer { scratch.cleanUp() }27        let source = try scratch.file("fixture." + Fixtures.ext(path.source), Fixtures.text(path.source))28        var options: [String: OptionValue] = [:]29        if path.operation == SubtitleEngine.retimeOperation { options["shift.ms"] = .int(2000) }30        if path.operation == SubtitleEngine.cleanOperation { options["clean.whitespace"] = .bool(true) }31        // The fixture's times fall on 25 fps frame boundaries; the header lets the re-read know the rate.32        if path.destination == .sub, path.source != .sub { options["fps.output"] = .string("25"); options["fps.header"] = .bool(true) }33        let result = try await convert(source, path.source, path.destination, operation: path.operation, options: options, in: scratch)34        for check in result.checks { #expect(check.passed, "\(path.id): \(check.name) — \(check.detail)") }35        #expect(result.checks.count >= 5)36        #expect(FileTypeDetector.detect(result.url).format == path.destination)3738        // Independent of the engine's validation: compare with the cues the fixture is known to contain.39        let doc = try result.document(path.destination)40        #expect(doc.cues.count == Fixtures.times.count)41        let shift = path.operation == SubtitleEngine.retimeOperation ? 2000 : 042        for (n, cue) in doc.cues.enumerated() {43            #expect(cue.startMs == Fixtures.times[n].0 + shift, "\(path.id) cue \(n + 1) start")44            #expect(cue.endMs == Fixtures.times[n].1 + shift, "\(path.id) cue \(n + 1) end")45            #expect(cue.plainText == Fixtures.plain[n], "\(path.id) cue \(n + 1) text")46        }47        // Options offered for the path are all understood by the settings decoder.48        let descriptors = Self.engine.options(for: path)49        #expect(Set(descriptors.map(\.id)).count == descriptors.count)50        _ = try SubtitleEngine.settings(from: ConversionOptions(Dictionary(uniqueKeysWithValues: descriptors.map { ($0.id, $0.defaultValue) })))51    }5253    @Test(arguments: SubtitleEngine.formats)54    func inspectReportsWhatWasRead(format: FormatID) async throws {55        let scratch = try Scratch(); defer { scratch.cleanUp() }56        let source = try scratch.file("fixture." + Fixtures.ext(format), Fixtures.text(format))57        let i = try await Self.engine.inspect(source, format: format)58        #expect(i.entryCount == 6)59        #expect(i.durationSeconds == 3602)60        #expect(i.fields.contains { $0.key == "Cues" && $0.value == "6" })61        #expect(i.fields.contains { $0.key == "First cue starts" && $0.value == "00:00:01.000" })62        #expect(i.fields.contains { $0.key == "Text encoding" && $0.value.hasPrefix("UTF-8") })63        if format == .sub { #expect(i.frameRate == 25) } else { #expect(i.frameRate == nil) }64        if format == .ass || format == .ssa {65            #expect(i.fields.contains { $0.key == "Styles" && $0.value == "2" })66            #expect(i.pixelWidth == 1280 && i.pixelHeight == 720)67        }68    }6970    @Test func stylesSurviveBetweenFormatsThatCanHoldThem() async throws {71        let scratch = try Scratch(); defer { scratch.cleanUp() }72        let source = try scratch.file("f.ass", Fixtures.ass(ssa: false))73        let srt = try await convert(source, .ass, .srt, in: scratch)74        #expect(srt.text.contains("<b>Bold</b> and <i>italic</i>"))75        #expect(srt.text.contains("<font color=\"#FF8000\">"))76        let vtt = try await convert(source, .ass, .vtt, in: scratch)77        #expect(vtt.text.contains("<c.color_FF8000>"))78        #expect(vtt.text.contains("::cue(.color_FF8000) { color: #FF8000; }"))79        #expect(vtt.text.contains("00:00:13.000 --> 00:00:15.000 line:5%"))80        let back = try await convert(vtt.url, .vtt, .ass, in: scratch)81        let doc = try back.document(.ass)82        #expect(doc.cues[2].spans.first?.attributes.bold == true)83        #expect(doc.cues[3].spans.first?.attributes.color == 0xFF8000)84        #expect(doc.cues[4].alignment == 8)85        #expect(back.text.contains("PlayResX: 1920") && back.text.contains("Style: Default,Arial,48,"))86    }8788    @Test func assAndSsaTranslateDialectDetails() async throws {89        let scratch = try Scratch(); defer { scratch.cleanUp() }90        let source = try scratch.file("f.ass", Fixtures.ass(ssa: false))91        let ssa = try await convert(source, .ass, .ssa, in: scratch)92        #expect(ssa.text.contains("[V4 Styles]") && ssa.text.contains("ScriptType: v4.00\n"))93        #expect(ssa.text.contains("{\\a6}日本語の字幕"))                       // \an8 → \a694        #expect(ssa.text.contains("Style: Sign,Georgia,36,65535,"))           // &H0000FFFF → decimal95        #expect(ssa.text.contains("Marked=0,0:00:10.00"))96        #expect(ssa.text.contains("Comment: Marked=0,0:00:00.00,0:00:00.50"))97        #expect(ssa.warnings.has("ssa-style-fields-dropped"))                 // underline, ScaleX, Spacing of "Sign"98        let ass = try await convert(ssa.url, .ssa, .ass, in: scratch)99        #expect(ass.text.contains("{\\an8}日本語の字幕"))100        #expect(ass.text.contains("Style: Sign,Georgia,36,&H0000FFFF,"))101        let doc = try ass.document(.ass)102        #expect(doc.styleCount == 2 && doc.cues[3].features.contains(.namedStyle))103    }104}105
Files main
Menu

Theme