74 lines
5.0 KiB
Swift
74 lines
5.0 KiB
Swift
import Foundation
|
|
|
|
@main
|
|
struct PlayerCommandChecks {
|
|
static func require(_ condition: Bool, _ message: String) {
|
|
if !condition { fatalError("FAIL: " + message) }
|
|
}
|
|
|
|
static func main() throws {
|
|
let directory = FileManager.default.temporaryDirectory.appendingPathComponent("terebi.cli-check.\(UUID().uuidString)")
|
|
try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)
|
|
defer { try? FileManager.default.removeItem(at: directory) }
|
|
let movie = directory.appendingPathComponent("video with spaces.mp4")
|
|
let optionFile = directory.appendingPathComponent("--help")
|
|
let colonFile = directory.appendingPathComponent("part:1.mp4")
|
|
let instanceFile = directory.appendingPathComponent("--new-instance")
|
|
for file in [movie, optionFile, colonFile, instanceFile] { try Data().write(to: file) }
|
|
|
|
func opened(_ arguments: [String], newInstance: Bool = false, noConfig: Bool = false) throws -> URL? {
|
|
guard case .launch(let url, let independent, let bypass) = try PlayerCommand.parse(arguments, directory: directory) else {
|
|
fatalError("FAIL: expected a launch command")
|
|
}
|
|
require(independent == newInstance, "launch mode preserves default reuse or explicit independence")
|
|
require(bypass == noConfig, "configuration loading defaults on and can be explicitly bypassed")
|
|
return url
|
|
}
|
|
require(try opened([movie.lastPathComponent]) == movie, "relative paths resolve against invocation directory, preserving spaces")
|
|
require(try opened([movie.absoluteString]) == movie, "encoded local file URLs preserve filename identity")
|
|
require(try opened(["--", "--help"]) == optionFile, "option terminator allows filenames that look like commands")
|
|
require(try opened([colonFile.path]) == colonFile, "colons in filesystem paths are not URL schemes")
|
|
let canonical = URL(string: "https://www.youtube.com/watch?v=jNQXAC9IVRw")!
|
|
require(try opened(["https://youtu.be/jNQXAC9IVRw"]) == canonical, "YouTube positional URLs canonicalize for resume identity")
|
|
require(try opened(["--new-instance"], newInstance: true) == nil, "an independent launch can omit media")
|
|
require(try opened(["--new-instance", movie.lastPathComponent], newInstance: true) == movie,
|
|
"independent local launches preserve relative paths and spaces")
|
|
require(try opened(["--new-instance", "--url", canonical.absoluteString], newInstance: true) == canonical,
|
|
"independent explicit YouTube launches retain canonical resume identity")
|
|
require(try opened(["--new-instance", "https://youtu.be/jNQXAC9IVRw"], newInstance: true) == canonical,
|
|
"independent positional YouTube launches retain canonical resume identity")
|
|
require(try opened(["--new-instance", "--", "--help"], newInstance: true) == optionFile,
|
|
"independent launch preserves the filename terminator")
|
|
require(try opened(["--", "--new-instance"]) == instanceFile,
|
|
"an escaped instance flag is a filename, not a launch option")
|
|
require(try opened(["--no-config"], noConfig: true) == nil, "configuration bypass can omit media")
|
|
require(try opened(["--no-config", movie.path], noConfig: true) == movie,
|
|
"configuration bypass retains local media delivery")
|
|
require(try opened(["--new-instance", "--no-config", "--url", canonical.absoluteString],
|
|
newInstance: true, noConfig: true) == canonical,
|
|
"independent bypass retains explicit YouTube identity")
|
|
require(try opened(["--no-config", "--new-instance", "--", "--help"],
|
|
newInstance: true, noConfig: true) == optionFile, "launch options work in either order")
|
|
guard case .help = try PlayerCommand.parse(["--no-config", "--new-instance", "--help"]) else {
|
|
fatalError("FAIL: help with launch options must not launch a player")
|
|
}
|
|
guard case .help = try PlayerCommand.parse(["--new-instance", "--help"]) else {
|
|
fatalError("FAIL: help must not launch an independent player")
|
|
}
|
|
|
|
for arguments in [["--url"], ["--url", canonical.absoluteString, movie.path], ["--"],
|
|
[movie.path, movie.path], ["--unknown"], ["missing.mp4"], [directory.path],
|
|
["file://remote-host/video.mp4"], ["https://example.com/video.mp4"],
|
|
["--new-instance", "--new-instance"], ["--new-instance", "--url"],
|
|
["--new-instance", "--"], ["--new-instance", movie.path, movie.path],
|
|
["--new-instance", "--unknown"], ["--no-config", "--no-config"],
|
|
["--no-config", "--new-instance", "--no-config"]] {
|
|
do {
|
|
_ = try PlayerCommand.parse(arguments, directory: directory)
|
|
fatalError("FAIL: accepted an invalid command")
|
|
} catch {}
|
|
}
|
|
print("Player command checks passed")
|
|
}
|
|
}
|