Files
terebi/Checks/resolved-playback.swift
T

95 lines
5.3 KiB
Swift

import AppKit
@main
@MainActor
struct ResolvedPlaybackCheck {
static func require(_ condition: @autoclosure () -> Bool, _ message: String) {
if !condition() { fatalError("FAIL: " + message) }
}
static func until(_ message: String, model: PlayerModel, _ predicate: () -> Bool) async throws {
let deadline = ContinuousClock.now.advanced(by: .seconds(45))
while !predicate(), ContinuousClock.now < deadline {
if model.state.status == PLAYER_ERROR || model.commandError != nil {
fatalError("FAIL: " + (model.errorText ?? "Playback failed"))
}
try await Task.sleep(for: .milliseconds(10))
}
require(predicate(), message)
}
static func main() async {
do { try await run() }
catch { fatalError("FAIL: " + error.localizedDescription) }
}
static func run() async throws {
PlayerModel.metadataLifecycleCheck()
if CommandLine.arguments[1] == "--metadata-lifecycle" {
print("PASS: metadata lifecycle replacement, A-B-A stale results, and actual-duration changes")
return
}
_ = NSApplication.shared
NSApp.setActivationPolicy(.prohibited)
let input = CommandLine.arguments[1]
let file = URL(fileURLWithPath: input)
let source: ResolvedSource
if input.hasPrefix("https://") {
guard case .miruro(let mediaID, let episode) = try StreamURL.parse(input) else {
fatalError("FAIL: URL smoke requires a Miruro episode")
}
source = try await MiruroResolver.resolve(mediaID: mediaID, episode: episode)
} else {
source = try ResolvedSource.load(from: file)
}
let suite = "terebi.resolved-check." + UUID().uuidString
let defaults = UserDefaults(suiteName: suite)!
defer { defaults.removePersistentDomain(forName: suite) }
let model = PlayerModel(defaults: defaults, progressFile: CommandLine.arguments.count > 2 ? CommandLine.arguments[2] : nil)
defer { model.shutdown() }
if input.hasPrefix("https://") { model.openStream(input) }
else { model.openResolvedSource(file) }
try await until("video/audio decode and advancing playback", model: model) {
model.state.status == PLAYER_PLAYING && model.state.has_video && model.state.has_audio && model.state.position > 0.2
}
print("PASS: resolved HLS decodes video/audio and advances")
fflush(stdout)
if input.hasPrefix("https://") {
try await until("descriptive title and measured-duration metadata bands", model: model) {
model.fileName != source.title && !model.visibleIntervals.isEmpty
}
let intervals = model.visibleIntervals
require(intervals.allSatisfy {
$0.start.isFinite && $0.end.isFinite && $0.start >= 0 &&
$0.end > $0.start && $0.end <= model.state.duration
}, "bands use finite in-bounds actual media duration")
require(intervals.filter { $0.kind == .intro }.count <= 1 &&
intervals.filter { $0.kind == .outro }.count <= 1, "at most one band of each kind")
print("PASS: Miruro title '\(model.fileName!)' and \(intervals.count) measured-duration bands")
}
require(model.setPlayback(playing: false), "pause accepted")
try await until("pause acknowledged", model: model) { model.state.status == PLAYER_PAUSED }
require(model.state.can_seek && model.state.duration > 0, "seekable measured duration")
guard let index = source.subtitles.firstIndex(where: \.isDefault) else {
fatalError("FAIL: smoke requires a default subtitle")
}
let track = source.subtitles[index]
let document = try await WebVTTDocument.load(url: track.url, headers: track.headers)
let playableCues = document.cues.filter { !$0.text.isEmpty && $0.start + 0.1 < model.state.duration }
require(playableCues.count >= 2, "smoke requires two visible subtitle cues")
let target = playableCues[1].start + min(0.1, (playableCues[1].end - playableCues[1].start) / 2)
require(!document.text(at: target).isEmpty, "forward seek targets a visible English cue")
require(model.seekRelative(target - model.state.position), "seek accepted")
try await until("seek acknowledged and subtitle synchronized", model: model) {
!model.state.seek_pending && abs(model.state.position - target) < 0.2 &&
model.subtitleText == document.text(at: model.state.position) && model.subtitleError == nil
}
require(model.selectedSubtitle == index && model.subtitleTracks[index].label == track.label, "default selection and label retained")
let backward = playableCues[0].start + min(0.1, (playableCues[0].end - playableCues[0].start) / 2)
require(model.seekRelative(backward - model.state.position), "backward seek accepted")
try await until("backward seek subtitle synchronized", model: model) {
!model.state.seek_pending && abs(model.state.position - backward) < 0.2 &&
model.subtitleText == document.text(at: model.state.position)
}
print("PASS: default VTT selected, forward/backward seek and acknowledged cue timing")
}
}