775 lines
27 KiB
Rust
775 lines
27 KiB
Rust
use pptx_reporter::{archive, engine, model::*, playback, reports};
|
||
use std::{fs, fs::File, io::Write, path::Path, sync::atomic::AtomicBool};
|
||
use zip::write::SimpleFileOptions;
|
||
|
||
fn picture(id: &str, trim: &str) -> String {
|
||
format!(
|
||
r#"<p:pic><p:nvPicPr><p:cNvPr id="{id}"/><p:nvPr><a:videoFile r:link="legacy"/><p14:media r:embed="media">{trim}</p14:media></p:nvPr></p:nvPicPr></p:pic>"#
|
||
)
|
||
}
|
||
fn timing(id: &str, attrs: &str) -> String {
|
||
format!(
|
||
r#"<p:video><p:cMediaNode><p:cTn id="{id}" {attrs}/><p:tgtEl><p:spTgt spid="{id}"/></p:tgtEl></p:cMediaNode></p:video>"#
|
||
)
|
||
}
|
||
fn slide(shapes: &str, animation: &str) -> String {
|
||
format!(
|
||
r#"<p:sld xmlns:p="{}" xmlns:r="{}" xmlns:p14="{}" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"><p:cSld><p:spTree>{shapes}</p:spTree></p:cSld><p:timing>{animation}</p:timing></p:sld>"#,
|
||
archive::P,
|
||
archive::R,
|
||
archive::P14
|
||
)
|
||
}
|
||
fn deck(path: &Path, slides: &[(String, String)], media: bool) {
|
||
let mut zip = zip::ZipWriter::new(File::create(path).unwrap());
|
||
let mut write = |name: &str, bytes: &[u8]| {
|
||
zip.start_file(name, SimpleFileOptions::default()).unwrap();
|
||
zip.write_all(bytes).unwrap();
|
||
};
|
||
if media {
|
||
write("ppt/media/shared.mp4", b"video");
|
||
}
|
||
for (i, (shapes, animation)) in slides.iter().enumerate() {
|
||
write(
|
||
&format!("ppt/slides/slide{}.xml", i + 1),
|
||
slide(shapes, animation).as_bytes(),
|
||
);
|
||
write(&format!("ppt/slides/_rels/slide{}.xml.rels",i+1),br#"<Relationships><Relationship Id="legacy" Target="../media/shared.mp4" Type="example/video"/><Relationship Id="media" Target="../media/shared.mp4" Type="example/media"/><Relationship Id="external" Target="https://example.invalid" TargetMode="External" Type="example/video"/></Relationships>"#);
|
||
}
|
||
zip.finish().unwrap();
|
||
}
|
||
fn sample() -> Report {
|
||
let dir = tempfile::tempdir().unwrap();
|
||
let path = dir.path().join("Review & plans.pptx");
|
||
deck(
|
||
&path,
|
||
&[
|
||
(
|
||
picture("2", r#"<p14:trim st="1s" end="2s"/>"#),
|
||
timing("2", r#"dur="indefinite""#),
|
||
),
|
||
(picture("2", ""), timing("2", r#"repeatCount="indefinite""#)),
|
||
(picture("2", ""), timing("2", r#"spd="200000""#)),
|
||
],
|
||
true,
|
||
);
|
||
let mut zip = zip::ZipArchive::new(File::open(&path).unwrap()).unwrap();
|
||
let d = archive::discover(&mut zip);
|
||
let video = Video {
|
||
slides: vec![1, 2, 3],
|
||
duration_seconds: Some(10.0),
|
||
name: "shared <clip> #1.mp4".into(),
|
||
size_bytes: 1234,
|
||
resolution: "32×24".into(),
|
||
codec: "mpeg4".into(),
|
||
archive_path: "ppt/media/shared.mp4".into(),
|
||
extracted_file: Some("../videos/shared <clip> #1.mp4".into()),
|
||
};
|
||
engine::assemble(&path, vec![video], &d)
|
||
}
|
||
#[test]
|
||
fn trims_and_loops_have_distinct_occurrences() {
|
||
let r = sample();
|
||
assert_eq!(r.embedded_videos, 1);
|
||
assert_eq!(r.playbacks.len(), 3);
|
||
assert_eq!(r.total_duration_seconds, 7.0);
|
||
assert_eq!(r.total_media_duration_seconds, 10.0);
|
||
assert_eq!(r.playbacks[0].settings.playback_status, "trimmed");
|
||
assert_eq!(r.playbacks[1].settings.playback_status, "looping");
|
||
assert_eq!(r.playbacks[2].settings.playback_status, "custom-timing");
|
||
let ontime = reports::ontime(&r);
|
||
let events = ontime["rundowns"]["default"]["entries"]
|
||
.as_object()
|
||
.unwrap();
|
||
assert_eq!(events.len(), 1);
|
||
assert_eq!(events.values().next().unwrap()["duration"], 7000);
|
||
}
|
||
#[test]
|
||
fn units_and_unsafe_trim_values() {
|
||
for (s, expected) in [
|
||
("1500", 1.5),
|
||
("1.25s", 1.25),
|
||
("1min", 60.0),
|
||
("0.5h", 1800.0),
|
||
("250ms", 0.25),
|
||
("1000µs", 0.001),
|
||
("1000μs", 0.001),
|
||
("1000000ns", 0.001),
|
||
] {
|
||
assert_eq!(playback::time_offset(s), Some(expected));
|
||
}
|
||
for s in [
|
||
"-1", "NaN", "Infinity", "1:30", "2seconds", "", ".2", "2.", "1.2.3",
|
||
] {
|
||
assert_eq!(playback::time_offset(s), None);
|
||
}
|
||
let video = Video {
|
||
duration_seconds: Some(10.0),
|
||
..Default::default()
|
||
};
|
||
let settings = Settings {
|
||
trim_start_seconds: 10.0,
|
||
..Default::default()
|
||
};
|
||
let p = playback::effective(&video, settings);
|
||
assert_eq!(p.settings.playback_status, "invalid-trim");
|
||
assert!(!p.ontime_eligible);
|
||
let p = playback::effective(&Video::default(), Settings::default());
|
||
assert_eq!(p.settings.playback_status, "unknown");
|
||
assert!(p.ontime_eligible);
|
||
}
|
||
#[test]
|
||
fn invalid_custom_inherited_and_unrelated_timing() {
|
||
let relationships = [
|
||
(
|
||
"media".into(),
|
||
(Some("part".into()), "example/media".into()),
|
||
),
|
||
(
|
||
"legacy".into(),
|
||
(Some("part".into()), "example/video".into()),
|
||
),
|
||
]
|
||
.into();
|
||
for (trim, animation, status) in [
|
||
(
|
||
r#"<p14:trim st="-1"/>"#.to_string(),
|
||
String::new(),
|
||
"invalid-trim",
|
||
),
|
||
(
|
||
r#"<p14:trim st="1s"/><p14:trim st="2s"/>"#.into(),
|
||
String::new(),
|
||
"invalid-trim",
|
||
),
|
||
(
|
||
String::new(),
|
||
timing("2", r#"autoRev="1""#),
|
||
"custom-timing",
|
||
),
|
||
(String::new(), timing("2", r#"dur="3000""#), "custom-timing"),
|
||
(
|
||
String::new(),
|
||
timing("2", r#"repeatCount="2000""#),
|
||
"looping",
|
||
),
|
||
(
|
||
String::new(),
|
||
format!(
|
||
r#"<p:par><p:cTn repeatCount="indefinite"><p:childTnLst>{}</p:childTnLst></p:cTn></p:par>"#,
|
||
timing("2", "")
|
||
),
|
||
"looping",
|
||
),
|
||
(
|
||
String::new(),
|
||
timing("99", r#"repeatCount="indefinite""#),
|
||
"normal",
|
||
),
|
||
] {
|
||
let xml = slide(&picture("2", &trim), &animation);
|
||
let doc = roxmltree::Document::parse(&xml).unwrap();
|
||
let settings = playback::shape_settings(&doc, &relationships, &["part".into()], 1);
|
||
assert_eq!(settings.len(), 1);
|
||
assert_eq!(settings[0].1.playback_status, status);
|
||
}
|
||
}
|
||
#[test]
|
||
fn all_reports_escape_content_and_keep_readable_trim_loop_annotations() {
|
||
let r = sample();
|
||
for format in FORMATS {
|
||
assert!(!reports::render(&r, format).unwrap().is_empty());
|
||
}
|
||
let html = reports::html(&r);
|
||
assert!(html.contains("shared <clip> #1.mp4"));
|
||
assert!(html.contains("../videos/shared%20%3Cclip%3E%20%231.mp4"));
|
||
assert!(html.contains("Trim removed"));
|
||
assert!(html.contains(">Looping</span>"));
|
||
let dense = reports::typst(&r, true);
|
||
assert!(dense.contains("flipped: false"));
|
||
assert!(!dense.contains("Embedded videos"));
|
||
assert!(dense.contains("Looping"));
|
||
assert!(dense.contains("Trim removed"));
|
||
for markdown in [true, false] {
|
||
let text = reports::text(&r, markdown);
|
||
let widths: Vec<_> = text
|
||
.lines()
|
||
.filter(|l| l.starts_with('|'))
|
||
.map(|s| s.chars().count())
|
||
.collect();
|
||
assert!(widths.iter().all(|w| *w == widths[0]));
|
||
assert!(text.contains("00:00:07.000"));
|
||
assert!(text.contains("Trim removed"));
|
||
}
|
||
assert_eq!(reports::time(None), "Unknown");
|
||
assert_eq!(reports::time(Some(3661.125)), "01:01:01.125");
|
||
assert_eq!(reports::size(1024), "1.00 KiB");
|
||
assert_eq!(reports::size(4), "4 B");
|
||
assert_eq!(reports::escape("<&>\"'"), "<&>"'");
|
||
assert_eq!(reports::typst_string("a\n\"\\#"), "\"a\\u{a}\\\"\\\\#\"");
|
||
assert!(reports::render(&r, "bad").is_err());
|
||
}
|
||
#[test]
|
||
fn format_options_and_probe_errors() {
|
||
assert_eq!(formats(&["all,md".into()]).unwrap().len(), 8);
|
||
assert_eq!(formats(&[" JSON,md ".into()]).unwrap(), vec!["json", "md"]);
|
||
assert!(formats(&["html,".into()]).is_err());
|
||
assert!(Options::default().validate().is_err());
|
||
assert!(engine::find_ffprobe(Some(Path::new("/missing/probe"))).is_err());
|
||
assert_eq!(engine::probe_json(b"broken").0, None);
|
||
assert_eq!(
|
||
engine::probe_json(br#"{"format":{"duration":"NaN"}}"#).0,
|
||
None
|
||
);
|
||
let p = engine::probe_json(br#"{"format":{"duration":"1.25"},"streams":[{"codec_type":"audio"},{"codec_type":"video","width":32,"height":24,"codec_name":"mpeg4"}]}"#);
|
||
assert_eq!(p, (Some(1.25), "32×24".into(), "mpeg4".into()));
|
||
assert_eq!(
|
||
archive::normalize("ppt/slides/slide1.xml", "../media/foo.mp4#thing"),
|
||
"ppt/media/foo.mp4"
|
||
);
|
||
assert_eq!(
|
||
archive::normalize("ppt/slides/slide1.xml", "/ppt/media/foo.mp4?x"),
|
||
"ppt/media/foo.mp4"
|
||
);
|
||
assert!(!archive::is_video("foo.mp3"));
|
||
assert!(archive::is_video("foo.MP4"));
|
||
}
|
||
#[test]
|
||
fn empty_extract_only_and_cancelled_runs() {
|
||
let dir = tempfile::tempdir().unwrap();
|
||
let source = dir.path().join("test.pptx");
|
||
deck(&source, &[], false);
|
||
let mut options = Options {
|
||
source: source.clone(),
|
||
..Default::default()
|
||
};
|
||
let outcome = engine::run(&options, &AtomicBool::new(false), |_| {}).unwrap();
|
||
assert!(outcome.paths.is_empty());
|
||
assert!(!options.output_dir().exists());
|
||
deck(&source, &[], true);
|
||
options.extract_only = true;
|
||
let outcome = engine::run(&options, &AtomicBool::new(false), |_| {}).unwrap();
|
||
let media = outcome.output.join("videos/shared.mp4");
|
||
assert_eq!(fs::read(&media).unwrap(), b"video");
|
||
fs::write(&media, b"preserved").unwrap();
|
||
engine::run(&options, &AtomicBool::new(false), |_| {}).unwrap();
|
||
assert_eq!(fs::read(&media).unwrap(), b"preserved");
|
||
options.overwrite = true;
|
||
engine::run(&options, &AtomicBool::new(false), |_| {}).unwrap();
|
||
assert_eq!(fs::read(&media).unwrap(), b"video");
|
||
assert!(engine::run(&options, &AtomicBool::new(true), |_| {}).is_err());
|
||
options.timings_only = true;
|
||
assert!(options.validate().is_err());
|
||
}
|
||
#[test]
|
||
fn fallback_orphans_unknown_durations_and_external_links() {
|
||
let mut discovery = archive::Discovery::default();
|
||
discovery.external.insert(3);
|
||
let r = engine::assemble(
|
||
Path::new("orphan.pptx"),
|
||
vec![Video {
|
||
name: "orphan.mp4".into(),
|
||
..Default::default()
|
||
}],
|
||
&discovery,
|
||
);
|
||
assert_eq!(r.playbacks.len(), 1);
|
||
let project = reports::ontime(&r);
|
||
let event = project["rundowns"]["default"]["entries"]
|
||
.as_object()
|
||
.unwrap()
|
||
.values()
|
||
.next()
|
||
.unwrap();
|
||
assert_eq!(event["skip"], true);
|
||
assert_eq!(event["cue"], "Unreferenced");
|
||
assert!(
|
||
project["project"]["description"]
|
||
.as_str()
|
||
.unwrap()
|
||
.contains("slides: 3")
|
||
);
|
||
assert!(reports::html(&r).contains("external media links: 3"));
|
||
assert!(reports::typst(&r, false).contains("Unknown"));
|
||
}
|
||
#[cfg(feature = "pdf")]
|
||
#[test]
|
||
fn both_pdf_layouts_compile_offline() {
|
||
for dense in [true, false] {
|
||
let bytes = pptx_reporter::pdf::compile(&reports::typst(&sample(), dense)).unwrap();
|
||
assert!(bytes.starts_with(b"%PDF-"));
|
||
}
|
||
assert!(pptx_reporter::pdf::compile("#read(\"/etc/passwd\")").is_err());
|
||
}
|
||
#[cfg(feature = "desktop")]
|
||
#[test]
|
||
fn gui_drop_and_recent_files() {
|
||
let dir = tempfile::tempdir().unwrap();
|
||
let path = dir.path().join("test.PPTX");
|
||
fs::write(&path, b"").unwrap();
|
||
assert_eq!(
|
||
pptx_reporter::gui::accept_drop([path.clone()]).unwrap(),
|
||
vec![path.clone()]
|
||
);
|
||
assert!(pptx_reporter::gui::accept_drop([]).is_err());
|
||
assert!(pptx_reporter::gui::accept_drop([path.clone(), path.clone()]).is_ok());
|
||
assert!(pptx_reporter::gui::accept_drop([dir.path().to_path_buf()]).is_ok());
|
||
assert!(pptx_reporter::gui::accept_drop([dir.path().join("test.pdf")]).is_err());
|
||
let mut prefs = pptx_reporter::gui::Preferences::default();
|
||
for i in 0..10 {
|
||
prefs.remember(format!("{i}.pptx").into());
|
||
}
|
||
prefs.remember("5.pptx".into());
|
||
assert_eq!(prefs.recent.len(), 8);
|
||
assert_eq!(prefs.recent[0], Path::new("5.pptx"));
|
||
}
|
||
|
||
#[test]
|
||
fn presentation_order_overrides_slide_filenames_and_tracks_external_links() {
|
||
let dir = tempfile::tempdir().unwrap();
|
||
let path = dir.path().join("ordered.pptx");
|
||
deck(&path, &[(picture("2", ""), String::new())], true);
|
||
let file = fs::OpenOptions::new()
|
||
.read(true)
|
||
.write(true)
|
||
.open(&path)
|
||
.unwrap();
|
||
let mut zip = zip::ZipWriter::new_append(file).unwrap();
|
||
let mut add = |name: &str, body: &str| {
|
||
zip.start_file(name, SimpleFileOptions::default()).unwrap();
|
||
zip.write_all(body.as_bytes()).unwrap();
|
||
};
|
||
add(
|
||
"ppt/presentation.xml",
|
||
&format!(
|
||
r#"<p:presentation xmlns:p="{}" xmlns:r="{}"><p:sldIdLst><p:sldId r:id="first"/><p:sldId r:id="second"/></p:sldIdLst></p:presentation>"#,
|
||
archive::P,
|
||
archive::R
|
||
),
|
||
);
|
||
add(
|
||
"ppt/_rels/presentation.xml.rels",
|
||
r#"<Relationships><Relationship Id="first" Target="slides/slide12.xml"/><Relationship Id="second" Target="slides/slide1.xml"/></Relationships>"#,
|
||
);
|
||
add(
|
||
"ppt/slides/slide12.xml",
|
||
&slide(r#"<a:videoFile r:link="remote"/>"#, ""),
|
||
);
|
||
add(
|
||
"ppt/slides/_rels/slide12.xml.rels",
|
||
r#"<Relationships><Relationship Id="remote" Target="https://example.invalid/clip.mp4" TargetMode="External" Type="example/video"/></Relationships>"#,
|
||
);
|
||
zip.finish().unwrap();
|
||
let mut zip = zip::ZipArchive::new(File::open(path).unwrap()).unwrap();
|
||
assert_eq!(
|
||
archive::slides(&mut zip),
|
||
["ppt/slides/slide12.xml", "ppt/slides/slide1.xml"]
|
||
);
|
||
let found = archive::discover(&mut zip);
|
||
assert_eq!(
|
||
found.uses["ppt/media/shared.mp4"]
|
||
.iter()
|
||
.copied()
|
||
.collect::<Vec<_>>(),
|
||
[2]
|
||
);
|
||
assert_eq!(found.external.iter().copied().collect::<Vec<_>>(), [1]);
|
||
}
|
||
|
||
#[test]
|
||
fn cli_help_invalid_options_and_empty_deck() {
|
||
use std::process::Command;
|
||
let exe = env!("CARGO_BIN_EXE_pptx-reporter");
|
||
let help = Command::new(exe).arg("--help").output().unwrap();
|
||
assert!(help.status.success());
|
||
assert!(String::from_utf8_lossy(&help.stdout).contains("--timings-only"));
|
||
assert!(
|
||
!Command::new(exe)
|
||
.args(["missing.pptx", "--report", "wrong"])
|
||
.output()
|
||
.unwrap()
|
||
.status
|
||
.success()
|
||
);
|
||
assert!(
|
||
!Command::new(exe)
|
||
.args(["missing.pptx", "--timings-only", "--extract-only"])
|
||
.output()
|
||
.unwrap()
|
||
.status
|
||
.success()
|
||
);
|
||
let dir = tempfile::tempdir().unwrap();
|
||
let path = dir.path().join("empty.pptx");
|
||
deck(&path, &[], false);
|
||
let output = Command::new(exe).arg(&path).output().unwrap();
|
||
assert!(output.status.success());
|
||
assert!(String::from_utf8_lossy(&output.stdout).contains("No embedded videos"));
|
||
assert!(!String::from_utf8_lossy(&output.stdout).contains('\u{1b}'));
|
||
assert!(!dir.path().join("empty_videos").exists());
|
||
}
|
||
#[cfg(unix)]
|
||
#[test]
|
||
fn engine_probes_temporaries_generates_reports_and_handles_failure() {
|
||
use std::os::unix::fs::PermissionsExt;
|
||
let dir = tempfile::tempdir().unwrap();
|
||
let source = dir.path().join("test.pptx");
|
||
deck(
|
||
&source,
|
||
&[(picture("2", r#"<p14:trim st="1s"/>"#), String::new())],
|
||
true,
|
||
);
|
||
let probe = dir.path().join("ffprobe");
|
||
fs::write(
|
||
&probe,
|
||
"#!/bin/sh\nprintf '%s' '{\"format\":{\"duration\":\"10\"},\"streams\":[]}'\n",
|
||
)
|
||
.unwrap();
|
||
fs::set_permissions(&probe, fs::Permissions::from_mode(0o755)).unwrap();
|
||
let mut options = Options {
|
||
source,
|
||
timings_only: true,
|
||
ffprobe: Some(probe.clone()),
|
||
formats: formats(&["all".into()]).unwrap(),
|
||
..Default::default()
|
||
};
|
||
#[cfg(not(feature = "pdf"))]
|
||
options.formats.retain(|f| f != "pdf" && f != "pdf-dense");
|
||
let outcome = engine::run(&options, &AtomicBool::new(false), |_| {}).unwrap();
|
||
assert!(!outcome.output.join("videos").exists());
|
||
assert_eq!(outcome.report.unwrap().total_duration_seconds, 9.0);
|
||
assert!(outcome.output.join("reports/timings.ontime.json").is_file());
|
||
options.timings_only = false;
|
||
engine::run(&options, &AtomicBool::new(false), |_| {}).unwrap();
|
||
assert_eq!(
|
||
fs::read(outcome.output.join("videos/shared.mp4")).unwrap(),
|
||
b"video"
|
||
);
|
||
fs::write(&probe, "#!/bin/sh\nexit 1\n").unwrap();
|
||
assert_eq!(
|
||
engine::probe(&probe, Path::new("any"), &AtomicBool::new(false))
|
||
.unwrap()
|
||
.0,
|
||
None
|
||
);
|
||
assert!(engine::probe(&probe, Path::new("any"), &AtomicBool::new(true)).is_err());
|
||
}
|
||
|
||
#[cfg(unix)]
|
||
fn fake_probe(dir: &Path) -> std::path::PathBuf {
|
||
use std::os::unix::fs::PermissionsExt;
|
||
let path = dir.join("ffprobe");
|
||
fs::write(
|
||
&path,
|
||
"#!/bin/sh\nprintf '%s' '{\"format\":{\"duration\":\"10\"},\"streams\":[]}'\n",
|
||
)
|
||
.unwrap();
|
||
fs::set_permissions(&path, fs::Permissions::from_mode(0o755)).unwrap();
|
||
path
|
||
}
|
||
|
||
#[cfg(unix)]
|
||
#[test]
|
||
fn batch_pptx_groups_preserve_order_trims_and_unique_ids() {
|
||
let dir = tempfile::tempdir().unwrap();
|
||
let a = dir.path().join("A.pptx");
|
||
let b = dir.path().join("B.pptx");
|
||
deck(
|
||
&a,
|
||
&[
|
||
(
|
||
picture("2", r#"<p14:trim st="1s" end="2s"/>"#),
|
||
String::new(),
|
||
),
|
||
(picture("3", ""), timing("3", r#"repeatCount="indefinite""#)),
|
||
],
|
||
true,
|
||
);
|
||
deck(&b, &[(picture("2", ""), String::new())], true);
|
||
let options = Options {
|
||
source: b,
|
||
additional_sources: vec![a],
|
||
output: Some(dir.path().join("out")),
|
||
ffprobe: Some(fake_probe(dir.path())),
|
||
formats: vec!["json".into(), "ontime".into()],
|
||
..Default::default()
|
||
};
|
||
let result = engine::run(&options, &AtomicBool::new(false), |_| {}).unwrap();
|
||
let report = result.report.unwrap();
|
||
assert_eq!(report.total_duration_seconds, 17.0);
|
||
assert_eq!(report.groups.len(), 2);
|
||
assert_eq!(report.groups[0].title, "A.pptx");
|
||
assert_eq!(report.playbacks[2].settings.slide, Some(1));
|
||
assert_eq!(
|
||
report.playbacks[0].video.extracted_file.as_deref(),
|
||
Some("../001/videos/shared.mp4")
|
||
);
|
||
let json = reports::ontime(&report);
|
||
let rundown = &json["rundowns"]["default"];
|
||
let order = rundown["order"].as_array().unwrap();
|
||
let flat = rundown["flatOrder"].as_array().unwrap();
|
||
assert_eq!(order.len(), 2);
|
||
assert_eq!(flat.len(), 4); // Groups precede their supported cues; loop omitted.
|
||
let entries = &rundown["entries"];
|
||
assert_eq!(entries.as_object().unwrap().len(), 4);
|
||
assert_eq!(entries[order[0].as_str().unwrap()]["title"], "A.pptx");
|
||
assert_eq!(entries[flat[3].as_str().unwrap()]["timeStart"], 7000);
|
||
assert_eq!(entries[flat[3].as_str().unwrap()]["linkStart"], true);
|
||
assert_eq!(entries[flat[1].as_str().unwrap()]["parent"], order[0]);
|
||
assert_eq!(entries[order[0].as_str().unwrap()]["entries"][0], flat[1]);
|
||
assert_eq!(
|
||
fs::read(result.output.join("001/videos/shared.mp4")).unwrap(),
|
||
b"video"
|
||
);
|
||
assert_eq!(
|
||
fs::read(result.output.join("002/videos/shared.mp4")).unwrap(),
|
||
b"video"
|
||
);
|
||
}
|
||
|
||
#[cfg(unix)]
|
||
#[test]
|
||
fn video_folder_and_parent_folder_use_filename_order() {
|
||
let dir = tempfile::tempdir().unwrap();
|
||
let root = dir.path().join("show");
|
||
let a = root.join("01 Intro");
|
||
let b = root.join("02 Talk");
|
||
fs::create_dir_all(&a).unwrap();
|
||
fs::create_dir_all(&b).unwrap();
|
||
fs::write(a.join("02.MP4"), b"second").unwrap();
|
||
fs::write(a.join("01.mov"), b"first").unwrap();
|
||
fs::write(a.join("notes.txt"), b"ignored").unwrap();
|
||
fs::write(b.join("01.mov"), b"other").unwrap();
|
||
let mut options = Options {
|
||
source: a.clone(),
|
||
timings_only: true,
|
||
ffprobe: Some(fake_probe(dir.path())),
|
||
formats: vec!["json".into(), "ontime".into()],
|
||
..Default::default()
|
||
};
|
||
let report = engine::run(&options, &AtomicBool::new(false), |_| {})
|
||
.unwrap()
|
||
.report
|
||
.unwrap();
|
||
assert_eq!(report.playbacks[0].video.name, "01.mov");
|
||
assert_eq!(report.playbacks[1].settings.slide, Some(2));
|
||
assert_eq!(report.total_duration_seconds, 20.0);
|
||
assert!(report.groups.is_empty());
|
||
assert!(!options.output_dir().join("videos").exists());
|
||
options.source = root.clone();
|
||
let result = engine::run(&options, &AtomicBool::new(false), |_| {}).unwrap();
|
||
let report = result.report.unwrap();
|
||
assert_eq!(report.groups.len(), 2);
|
||
assert_eq!(report.groups[1].title, "02 Talk");
|
||
assert_eq!(report.total_duration_seconds, 30.0);
|
||
assert_eq!(report.playbacks[2].settings.slide, Some(1));
|
||
fs::write(root.join("mixed.mp4"), b"ambiguous").unwrap();
|
||
assert!(
|
||
engine::run(&options, &AtomicBool::new(false), |_| {})
|
||
.unwrap_err()
|
||
.to_string()
|
||
.contains("mixes")
|
||
);
|
||
options.source = b;
|
||
options.timings_only = false;
|
||
options.extract_only = true;
|
||
engine::run(&options, &AtomicBool::new(false), |_| {}).unwrap();
|
||
let copied = options.output_dir().join("videos/01.mov");
|
||
fs::write(&copied, b"preserved").unwrap();
|
||
engine::run(&options, &AtomicBool::new(false), |_| {}).unwrap();
|
||
assert_eq!(fs::read(&copied).unwrap(), b"preserved");
|
||
options.overwrite = true;
|
||
engine::run(&options, &AtomicBool::new(false), |_| {}).unwrap();
|
||
assert_eq!(fs::read(&copied).unwrap(), b"other");
|
||
assert!(engine::run(&options, &AtomicBool::new(true), |_| {}).is_err());
|
||
}
|
||
|
||
#[test]
|
||
fn batch_discovery_empty_duplicates_and_pptx_directory() {
|
||
let dir = tempfile::tempdir().unwrap();
|
||
let mut options = Options {
|
||
source: dir.path().to_owned(),
|
||
..Default::default()
|
||
};
|
||
assert!(
|
||
engine::run(&options, &AtomicBool::new(false), |_| {})
|
||
.unwrap()
|
||
.paths
|
||
.is_empty()
|
||
);
|
||
let a = dir.path().join("a.pptx");
|
||
let b = dir.path().join("b.PPTX");
|
||
deck(&a, &[], false);
|
||
deck(&b, &[], false);
|
||
assert_eq!(
|
||
pptx_reporter::inputs::discover(&options).unwrap(),
|
||
vec![a.clone(), b.clone()]
|
||
);
|
||
options.additional_sources = vec![a.clone(), b.clone()];
|
||
assert_eq!(
|
||
pptx_reporter::inputs::discover(&options).unwrap(),
|
||
vec![a, b]
|
||
);
|
||
assert!(
|
||
engine::run(&options, &AtomicBool::new(false), |_| {})
|
||
.unwrap()
|
||
.paths
|
||
.is_empty()
|
||
);
|
||
}
|
||
|
||
#[cfg(unix)]
|
||
#[test]
|
||
fn batch_failure_preserves_reports_and_cli_accepts_multiple_inputs() {
|
||
let dir = tempfile::tempdir().unwrap();
|
||
let a = dir.path().join("A.pptx");
|
||
let b = dir.path().join("B.pptx");
|
||
deck(&a, &[(picture("2", ""), String::new())], true);
|
||
fs::write(&b, b"invalid archive").unwrap();
|
||
let output = dir.path().join("out");
|
||
fs::create_dir_all(output.join("reports")).unwrap();
|
||
let report = output.join("reports/timings.json");
|
||
fs::write(&report, b"previous report").unwrap();
|
||
let probe = fake_probe(dir.path());
|
||
let cli = || {
|
||
std::process::Command::new(env!("CARGO_BIN_EXE_pptx-reporter"))
|
||
.arg(&b)
|
||
.arg(&a)
|
||
.args(["--timings-only", "--report", "json", "--ffprobe"])
|
||
.arg(&probe)
|
||
.arg("--output")
|
||
.arg(&output)
|
||
.output()
|
||
.unwrap()
|
||
};
|
||
assert!(!cli().status.success());
|
||
assert_eq!(fs::read(&report).unwrap(), b"previous report");
|
||
deck(&b, &[(picture("2", ""), String::new())], true);
|
||
assert!(cli().status.success());
|
||
let result: Report = serde_json::from_slice(&fs::read(&report).unwrap()).unwrap();
|
||
assert_eq!(result.groups[0].title, "A.pptx");
|
||
assert_eq!(result.total_duration_seconds, 20.0);
|
||
}
|
||
|
||
#[cfg(unix)]
|
||
#[test]
|
||
fn folder_refuses_media_symlinks_and_source_overwrite() {
|
||
use std::os::unix::fs::symlink;
|
||
let dir = tempfile::tempdir().unwrap();
|
||
let source = dir.path().join("videos");
|
||
fs::create_dir(&source).unwrap();
|
||
let video = source.join("clip.mp4");
|
||
fs::write(&video, b"source").unwrap();
|
||
let mut options = Options {
|
||
source,
|
||
output: Some(dir.path().to_owned()),
|
||
extract_only: true,
|
||
overwrite: true,
|
||
..Default::default()
|
||
};
|
||
assert!(engine::run(&options, &AtomicBool::new(false), |_| {}).is_err());
|
||
let output = dir.path().join("out");
|
||
fs::create_dir_all(output.join("videos")).unwrap();
|
||
symlink(&video, output.join("videos/clip.mp4")).unwrap();
|
||
options.output = Some(output);
|
||
assert!(engine::run(&options, &AtomicBool::new(false), |_| {}).is_err());
|
||
assert_eq!(fs::read(video).unwrap(), b"source");
|
||
}
|
||
|
||
#[cfg(unix)]
|
||
#[test]
|
||
fn natural_order_applies_to_batch_inputs_folder_videos_and_ontime() {
|
||
let dir = tempfile::tempdir().unwrap();
|
||
let root = dir.path().join("show");
|
||
fs::create_dir(&root).unwrap();
|
||
let mut inputs = vec![];
|
||
for name in ["talk10.pptx", "talk2.pptx", "talk 10", "talk 2"] {
|
||
let path = root.join(name);
|
||
if name.ends_with("pptx") {
|
||
deck(&path, &[(picture("2", ""), String::new())], true);
|
||
} else {
|
||
fs::create_dir(&path).unwrap();
|
||
for video in ["media10.mp4", "media2.mp4", "media 10.mp4", "media 2.mp4"] {
|
||
fs::write(path.join(video), b"video").unwrap();
|
||
}
|
||
}
|
||
inputs.push(path);
|
||
}
|
||
let mut options = Options {
|
||
source: root,
|
||
timings_only: true,
|
||
ffprobe: Some(fake_probe(dir.path())),
|
||
formats: vec!["ontime".into()],
|
||
..Default::default()
|
||
};
|
||
let expected = vec![
|
||
inputs[3].clone(),
|
||
inputs[2].clone(),
|
||
inputs[1].clone(),
|
||
inputs[0].clone(),
|
||
];
|
||
assert_eq!(pptx_reporter::inputs::discover(&options).unwrap(), expected);
|
||
let report = engine::run(&options, &AtomicBool::new(false), |_| {})
|
||
.unwrap()
|
||
.report
|
||
.unwrap();
|
||
assert_eq!(
|
||
report.playbacks[..4]
|
||
.iter()
|
||
.map(|p| p.video.name.as_str())
|
||
.collect::<Vec<_>>(),
|
||
vec![
|
||
"talk 2 / media 2.mp4",
|
||
"talk 2 / media 10.mp4",
|
||
"talk 2 / media2.mp4",
|
||
"talk 2 / media10.mp4"
|
||
]
|
||
);
|
||
let json = reports::ontime(&report);
|
||
let rundown = &json["rundowns"]["default"];
|
||
let groups = rundown["order"].as_array().unwrap();
|
||
assert_eq!(
|
||
rundown["entries"][groups[2].as_str().unwrap()]["title"],
|
||
"talk2.pptx"
|
||
);
|
||
options.source = inputs.remove(0);
|
||
options.additional_sources = inputs;
|
||
assert_eq!(pptx_reporter::inputs::discover(&options).unwrap(), expected);
|
||
}
|
||
|
||
#[test]
|
||
fn natural_media_names_do_not_override_powerpoint_slide_order() {
|
||
let videos = [(2, "media1.mp4"), (1, "media10.mp4"), (1, "media2.mp4")]
|
||
.into_iter()
|
||
.map(|(slide, name)| Video {
|
||
name: name.into(),
|
||
archive_path: name.into(),
|
||
slides: vec![slide],
|
||
duration_seconds: Some(1.0),
|
||
..Default::default()
|
||
})
|
||
.collect();
|
||
let report = engine::assemble(
|
||
Path::new("test.pptx"),
|
||
videos,
|
||
&archive::Discovery::default(),
|
||
);
|
||
let expected = vec!["media2.mp4", "media10.mp4", "media1.mp4"];
|
||
assert_eq!(
|
||
report
|
||
.videos
|
||
.iter()
|
||
.map(|v| v.name.as_str())
|
||
.collect::<Vec<_>>(),
|
||
expected
|
||
);
|
||
assert_eq!(
|
||
report
|
||
.playbacks
|
||
.iter()
|
||
.map(|p| p.video.name.as_str())
|
||
.collect::<Vec<_>>(),
|
||
expected
|
||
);
|
||
}
|