forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBa3e9dc2Tahinli3 months ago134 commits

saveload.rs744 lines, 29.3 KiBHistoryRawDownload
1//! Saving a project and opening it again: the timeline that comes back is the
2//! one that went in, a folder of media plus its `.edith` can be moved
3//! anywhere, and every way the files on disk can have changed underneath is a
4//! refusal that says which file and why.
5//!
6//! ```text
7//! cargo test -p engine --release --test saveload -- --nocapture
8//! ```
9
10use std::path::{Path, PathBuf};
11
12use engine::PlaybackSession;
13use engine::project::{Lane, Source};
14
15fn asset(name: &str) -> PathBuf {
16 PathBuf::from(env!("CARGO_MANIFEST_DIR"))
17 .join("../../assets")
18 .join(name)
19}
20
21/// A fresh directory to litter. Canonical, because the saved paths are
22/// relative to it and `Project` canonicalises what it is handed.
23fn scratch(name: &str) -> PathBuf {
24 let dir = std::env::temp_dir().join(format!("ve_saveload_{name}_{}", std::process::id()));
25 let _ = std::fs::remove_dir_all(&dir);
26 std::fs::create_dir_all(&dir).expect("scratch dir");
27 std::fs::canonicalize(&dir).expect("canonical scratch dir")
28}
29
30fn copy_in(dir: &Path, name: &str) -> PathBuf {
31 let to = dir.join(name);
32 std::fs::copy(asset(name), &to).expect("copy the fixture");
33 to
34}
35
36/// The whole of what a picked audio stream has to survive: a file's second
37/// language goes on the timeline beside its first, the project says so on
38/// disk, and reopening it plays the same two streams. This is the file-level
39/// half of what a library row does -- `place_stream_at` is the one door that
40/// row goes through.
41#[test]
42fn a_picked_audio_stream_lands_saves_and_comes_back() {
43 let dir = scratch("streams");
44 let media = copy_in(&dir, "test_multilang.mp4");
45 let mut session = PlaybackSession::open(&media).expect("open the fixture");
46 session.set_gain(0.0); // silent like the rest of the suites
47 let frames = session.clip_at(0).expect("one clip").out_frame;
48 let end = session.timeline_duration();
49
50 // Stream 1 is the French track, same rate and layout as stream 0, so it
51 // can join this timeline; it lands as a second source of the same file.
52 assert!(
53 session
54 .place_stream_at(end, &media, 1, frames, None)
55 .expect("stream 1 matches the timeline")
56 );
57 assert_eq!(
58 session
59 .sources()
60 .iter()
61 .map(|s| s.audio_stream)
62 .collect::<Vec<_>>(),
63 [0, 1],
64 "the same file twice, on two streams"
65 );
66 assert_eq!(session.timeline_duration(), end * 2.0);
67
68 let path = dir.join("langs.edith");
69 session.save_project(&path).expect("save");
70 let text = std::fs::read_to_string(&path).expect("read back");
71 assert!(
72 text.contains("source 0 test_multilang.mp4")
73 && text.contains("source 1 test_multilang.mp4"),
74 "the streams did not reach the file:\n{text}"
75 );
76
77 let loaded = PlaybackSession::open_project(&path).expect("reopen the project");
78 assert_eq!(loaded.sources(), session.sources());
79 assert_eq!(loaded.timeline_duration(), session.timeline_duration());
80
81 // And the refusals, in the engine's own words: a stream that cannot share
82 // one output device with the timeline, and a file that is not on it.
83 let other = copy_in(&dir, "test_multiaudio.mp4");
84 let mut session = PlaybackSession::open(&other).expect("open the fixture");
85 session.set_gain(0.0);
86 let frames = session.clip_at(0).expect("one clip").out_frame;
87 let err = session
88 .place_stream_at(0.0, &other, 1, frames, None)
89 .expect_err("22.05 kHz mono cannot join a 44.1 kHz stereo timeline")
90 .to_string();
91 assert!(err.contains("22050"), "unhelpful refusal: {err}");
92 assert!(
93 session
94 .place_stream_at(0.0, &media, 1, frames, None)
95 .expect_err("a file that is not a source")
96 .to_string()
97 .contains("not on this timeline")
98 );
99 assert_eq!(session.sources().len(), 1, "a refusal added a source");
100 std::fs::remove_dir_all(&dir).expect("cleanup");
101}
102
103/// Why a trim's tail stops where the file ends: a clip reaching past the last
104/// frame of its source is a project that will not open again (`open_project`
105/// refuses it by name). Pulled in on both ends and then dragged out as far as
106/// the pointer can ask for, the save still comes back as itself.
107#[test]
108fn a_trimmed_timeline_saves_and_opens_again() {
109 use engine::project::Edge;
110
111 let dir = scratch("trim");
112 let media = copy_in(&dir, "test_av.mp4");
113 let mut session = PlaybackSession::open(&media).expect("open the fixture");
114 session.set_gain(0.0);
115 let whole = session.clip_at(0).expect("one clip");
116
117 assert!(session.trim_clip(Lane::V1, 0, Edge::End, whole.end() / 2));
118 assert!(session.trim_clip(Lane::V1, 0, Edge::Start, 5));
119 let path = dir.join("trimmed.edith");
120 session.save_project(&path).expect("save");
121 let loaded = PlaybackSession::open_project(&path).expect("a trimmed project opens");
122 assert_eq!(
123 loaded.clip_at(0).map(|c| (c.start, c.in_frame, c.out_frame)),
124 Some((5, 5, whole.end() / 2)),
125 "the trimmed range is what came back"
126 );
127
128 // A hand that keeps pulling: the tail stops at the file's own last frame,
129 // and the save of *that* opens too.
130 assert!(session.trim_clip(Lane::V1, 0, Edge::End, u32::MAX));
131 assert_eq!(
132 session.clip_at(0).expect("still one clip").out_frame,
133 whole.out_frame,
134 "as far as the file goes and not one frame further"
135 );
136 session.save_project(&path).expect("save");
137 PlaybackSession::open_project(&path).expect("and it still opens");
138 std::fs::remove_dir_all(&dir).expect("cleanup");
139}
140
141/// Two files, four clips, one of them deleted, playhead at 3 s -- a timeline
142/// with something to lose in every field.
143fn edited(dir: &Path) -> PlaybackSession {
144 let mut session = PlaybackSession::open(copy_in(dir, "test_av.mp4")).expect("open");
145 session
146 .import(&copy_in(dir, "test_av2.mp4"))
147 .expect("import the second file");
148 assert!(session.cut_at(2.0), "cut inside the first file");
149 assert!(session.cut_at(6.5), "cut inside the second file");
150 assert!(
151 session.delete_clip(Lane::V1, 1),
152 "drop the second half of the first"
153 );
154 session.seek(3.0);
155 assert_eq!(session.timeline_duration(), 6.0);
156 session
157}
158
159/// What has to survive a round trip, all of it public API.
160fn shape(session: &PlaybackSession) -> (Vec<(f64, f64, usize)>, Vec<Source>, f64) {
161 (
162 session.clip_spans_by_source(),
163 session.sources().to_vec(),
164 session.timeline_duration(),
165 )
166}
167
168/// The version-1 promise: a file written before the lanes existed still opens,
169/// as one grouped video+audio pair per clip laid end to end, and saving it
170/// again writes version 7 -- which reopens as the same timeline.
171#[test]
172fn a_version_1_project_loads_fully_grouped_and_saves_as_version_7() {
173 let dir = scratch("v1");
174 copy_in(&dir, "test_av.mp4");
175 let path = dir.join("old.edith");
176 // 60 frames of picture from two clips: [0,30) then [60,90) of the source.
177 std::fs::write(
178 &path,
179 "edith 1\nplayhead 45\nsource test_av.mp4\nclip 0 30 0\nclip 60 90 0\n",
180 )
181 .expect("write a v1 file");
182
183 let loaded = PlaybackSession::open_project(&path).expect("a v1 file still opens");
184 let fps = loaded.meta().frame_rate;
185 assert_eq!(
186 loaded.clip_spans_by_source(),
187 vec![(0.0, 30.0 / fps, 0), (30.0 / fps, 30.0 / fps, 0)],
188 "v1 clips queue up, the second starting where the first ended"
189 );
190 assert_eq!(
191 loaded.lane_spans_by_source(engine::project::Lane::A1),
192 loaded.clip_spans_by_source(),
193 "both lanes, fully grouped: a v1 timeline had no holes"
194 );
195 assert!(
196 (loaded.now() - 45.0 / fps).abs() < 1.0 / fps,
197 "playhead kept"
198 );
199
200 // Saved again it is v7, and v7 round-trips to the same timeline.
201 let v2 = dir.join("new.edith");
202 loaded.save_project(&v2).expect("save");
203 let text = std::fs::read_to_string(&v2).expect("read back");
204 assert!(text.starts_with("edith 7\n"), "{text}");
205 assert!(
206 text.contains("\nresolution 1280 720\n"),
207 "a project with no resolution of its own is saved at source 0's: {text}"
208 );
209 assert_eq!(
210 text.lines().filter(|l| l.starts_with("video ")).count(),
211 2,
212 "{text}"
213 );
214 assert_eq!(
215 text.lines().filter(|l| l.starts_with("audio ")).count(),
216 2,
217 "{text}"
218 );
219 let again = PlaybackSession::open_project(&v2).expect("open the v2 file");
220 assert_eq!(shape(&again), shape(&loaded));
221 // ...and re-saving it is byte-identical, so a round trip is stable.
222 let third = dir.join("third.edith");
223 again.save_project(&third).expect("save");
224 assert_eq!(std::fs::read(&third).expect("read"), text.as_bytes());
225
226 // And the same promise one version on: a v2 file names no audio stream and
227 // opens on stream 0, which is the only one that dialect could play.
228 let v2 = dir.join("v2.edith");
229 std::fs::write(
230 &v2,
231 "edith 2\nplayhead 0\nsource test_av.mp4\nvideo 0 0 30 0 0\naudio 0 0 30 0 0\n",
232 )
233 .expect("write a v2 file");
234 let loaded = PlaybackSession::open_project(&v2).expect("a v2 file still opens");
235 assert_eq!(loaded.sources().len(), 1);
236 assert_eq!(loaded.sources()[0].audio_stream, 0);
237 loaded.save_project(&v2).expect("save");
238 assert!(
239 std::fs::read_to_string(&v2)
240 .expect("read back")
241 .contains("source 0 test_av.mp4"),
242 "a re-saved v2 project says the stream it always meant"
243 );
244}
245
246/// What version 4 is for: a project file holds any number of lanes, in the
247/// order they are displayed in, and one holding nothing is still a lane. The
248/// timeline is built by hand because the format is what this checks -- no UI
249/// adds a lane yet -- and the file it writes back has to be the one it read.
250#[test]
251fn a_version_4_project_holds_more_than_two_lanes() {
252 use engine::project::{Lane, LaneKind};
253
254 let dir = scratch("v4_lanes");
255 copy_in(&dir, "test_av.mp4");
256 let path = dir.join("many.edith");
257 // V1, A1, V2, A2 in display order: a take grouped across V1 and A2 (link 3,
258 // one span on two lanes that are not a pair), a lone clip on V2, and an
259 // empty A1 that has only its own line to say it is there.
260 let text = "edith 4\nplayhead 0\nsource 0 test_av.mp4\n\
261 video 1 0 0 30 0 3\naudio 1\nvideo 2 40 0 20 0 -\naudio 2 0 0 30 0 3\n";
262 std::fs::write(&path, text).expect("write a v4 file");
263
264 let loaded = PlaybackSession::open_project(&path).expect("a four-lane project opens");
265 let v2 = Lane::new(LaneKind::Video, 1);
266 let a2 = Lane::new(LaneKind::Audio, 1);
267 assert_eq!(loaded.lane_clips(Lane::V1).len(), 1);
268 assert!(loaded.lane_clips(Lane::A1).is_empty(), "A1 holds nothing");
269 assert_eq!(
270 loaded
271 .lane_clips(v2)
272 .iter()
273 .map(|c| (c.start, c.end()))
274 .collect::<Vec<_>>(),
275 vec![(40, 60)],
276 "V2 is a lane of its own, gap and all"
277 );
278 assert_eq!(
279 loaded.lane_clips(a2)[0].link,
280 loaded.lane_clips(Lane::V1)[0].link,
281 "the group spans V1 and A2, which are not a pair"
282 );
283 assert_eq!(loaded.timeline_duration(), 60.0 / loaded.meta().frame_rate);
284
285 // ...and saving it writes the v7 twin of what it read: the lane list, its
286 // order and the empty lane all survive, each clip now saying it plays flat,
287 // ungraded and fitted, under a project resolution taken from the media.
288 let again = dir.join("again.edith");
289 loaded.save_project(&again).expect("save");
290 assert_eq!(
291 std::fs::read_to_string(&again).expect("read back"),
292 "edith 7\nplayhead 0\nresolution 1280 720\nsource 0 test_av.mp4\n\
293 video 1 0 0 30 0 3 - - fit\naudio 1\n\
294 video 2 40 0 20 0 - - - fit\naudio 2 0 0 30 0 3 - - fit\n",
295 "a four-lane project is written as it was read, three versions on"
296 );
297}
298
299/// What version 5 is for: a clip carries equalizer settings, the file names
300/// them once for however many clips share them, and the whole thing survives
301/// the engine's own door -- open, save, every curve and index of it intact one
302/// version on.
303#[test]
304fn a_version_5_project_carries_per_clip_equalizers() {
305 let dir = scratch("v5_eq");
306 copy_in(&dir, "test_av.mp4");
307 let path = dir.join("eq.edith");
308 // Two clips on one curve, one on another, one flat.
309 let text = "edith 5\nplayhead 0\nsource 0 test_av.mp4\n\
310 eq 80.0:-3.0:0.707:ls 1000.0:4.5:1.0:pk\n\
311 eq 12000.0:6.25:0.5:hs\n\
312 video 1 0 0 30 0 - 0\nvideo 1 30 30 60 0 - 1\n\
313 audio 1 0 0 30 0 - 0\naudio 1 30 30 60 0 - -\n";
314 std::fs::write(&path, text).expect("write a v5 file");
315
316 let loaded = PlaybackSession::open_project(&path).expect("a v5 project opens");
317 assert_eq!(loaded.lane_clips(engine::project::Lane::V1).len(), 2);
318 let again = dir.join("again.edith");
319 loaded.save_project(&again).expect("save");
320 assert_eq!(
321 std::fs::read_to_string(&again).expect("read back"),
322 "edith 7\nplayhead 0\nresolution 1280 720\nsource 0 test_av.mp4\n\
323 eq 80.0:-3.0:0.707:ls 1000.0:4.5:1.0:pk\n\
324 eq 12000.0:6.25:0.5:hs\n\
325 video 1 0 0 30 0 - 0 - fit\nvideo 1 30 30 60 0 - 1 - fit\n\
326 audio 1 0 0 30 0 - 0 - fit\naudio 1 30 30 60 0 - - - fit\n",
327 "the equalizer table and every clip's index survive a round trip"
328 );
329}
330
331/// What version 6 is for, the same claim one version on: a clip carries a
332/// colour grade beside its equalizer and the file names each once for however
333/// many clips share it. A format bump necessarily rewrites the file, so what is
334/// asserted here is the v7 twin of what was read; the byte-identity claim lives
335/// in `a_version_7_project_carries_a_resolution_and_fit_policies` below.
336#[test]
337fn a_version_6_project_carries_per_clip_colours() {
338 let dir = scratch("v6_color");
339 copy_in(&dir, "test_av.mp4");
340 let path = dir.join("color.edith");
341 // Two clips on one grade, one on another, one ungraded -- and an equalizer
342 // on a clip that is graded too, so the two tables are seen not to collide.
343 let text = "edith 6\nplayhead 0\nsource 0 test_av.mp4\n\
344 eq 80.0:-3.0:0.707:ls\n\
345 color 0.1:1.2:0.9:-0.3\n\
346 color -0.25:1.0:0.0:0.5\n\
347 video 1 0 0 30 0 - 0 0\nvideo 1 30 30 60 0 - - 1\n\
348 audio 1 0 0 30 0 - - 0\naudio 1 30 30 60 0 - - -\n";
349 std::fs::write(&path, text).expect("write a v6 file");
350
351 let loaded = PlaybackSession::open_project(&path).expect("a v6 project opens");
352 assert_eq!(loaded.lane_clips(engine::project::Lane::V1).len(), 2);
353 let again = dir.join("again.edith");
354 loaded.save_project(&again).expect("save");
355 assert_eq!(
356 std::fs::read_to_string(&again).expect("read back"),
357 "edith 7\nplayhead 0\nresolution 1280 720\nsource 0 test_av.mp4\n\
358 eq 80.0:-3.0:0.707:ls\n\
359 color 0.1:1.2:0.9:-0.3\n\
360 color -0.25:1.0:0.0:0.5\n\
361 video 1 0 0 30 0 - 0 0 fit\nvideo 1 30 30 60 0 - - 1 fit\n\
362 audio 1 0 0 30 0 - - 0 fit\naudio 1 30 30 60 0 - - - fit\n",
363 "the colour table and every clip's index survive a round trip"
364 );
365 // A dialect that could not say either one means the defaults, and those are
366 // what such a project always was: the media's own size, nothing letterboxed.
367 assert_eq!(loaded.resolution(), (1280, 720));
368 assert_eq!(
369 loaded.fit_of(engine::project::Lane::V1, 0),
370 engine::scale::FitPolicy::Fit
371 );
372}
373
374/// What version 7 is for: the project's picture size is its own, not source 0's,
375/// and each clip says how it meets it. Both survive the engine's door byte for
376/// byte -- and a project whose resolution is nobody's media size is exactly the
377/// case that could not be written down before.
378#[test]
379fn a_version_7_project_carries_a_resolution_and_fit_policies() {
380 let dir = scratch("v7_resolution");
381 copy_in(&dir, "test_av.mp4");
382 let path = dir.join("mixed.edith");
383 let text = "edith 7\nplayhead 0\nresolution 960 720\nsource 0 test_av.mp4\n\
384 video 1 0 0 30 0 - - - fill\nvideo 1 30 30 60 0 - - - center\n\
385 audio 1 0 0 60 0 - - - fit\n";
386 std::fs::write(&path, text).expect("write a v7 file");
387
388 let loaded = PlaybackSession::open_project(&path).expect("a v7 project opens");
389 assert_eq!(
390 loaded.resolution(),
391 (960, 720),
392 "the project's own size, which is not the media's 1280x720"
393 );
394 assert_eq!(
395 loaded.native_resolution(),
396 (1280, 720),
397 "...and the media's own size is still known"
398 );
399 let v1 = engine::project::Lane::V1;
400 assert_eq!(loaded.fit_of(v1, 0), engine::scale::FitPolicy::Fill);
401 assert_eq!(loaded.fit_of(v1, 1), engine::scale::FitPolicy::Center);
402
403 let again = dir.join("again.edith");
404 loaded.save_project(&again).expect("save");
405 assert_eq!(
406 std::fs::read_to_string(&again).expect("read back"),
407 text,
408 "a v7 project is written exactly as it was read"
409 );
410
411 // The refusals the new grammar adds, each naming its line.
412 for (bad, want) in [
413 (
414 "edith 7\nresolution 0 720\nsource 0 test_av.mp4\nvideo 1 0 0 30 0 - - - fit\n",
415 "0x720 is not a picture",
416 ),
417 (
418 "edith 7\nresolution 960 720\nresolution 960 720\nsource 0 test_av.mp4\n\
419 video 1 0 0 30 0 - - - fit\n",
420 "resolution belongs once",
421 ),
422 // Past 8K is refused by the same bound the keystroke has: unbounded,
423 // this line reached `open_black` and panicked on a capacity overflow.
424 (
425 "edith 7\nresolution 4294967295 4294967295\nsource 0 test_av.mp4\n\
426 video 1 0 0 30 0 - - - fit\n",
427 "4294967295x4294967295 is not a picture",
428 ),
429 (
430 "edith 7\nresolution 7681 1080\nsource 0 test_av.mp4\n\
431 video 1 0 0 30 0 - - - fit\n",
432 "7681x1080 is not a picture",
433 ),
434 (
435 "edith 7\nresolution 960 720\nsource 0 test_av.mp4\nvideo 1 0 0 30 0 - - - squish\n",
436 "not a fit policy",
437 ),
438 ] {
439 let path = dir.join("bad.edith");
440 std::fs::write(&path, bad).expect("write");
441 let err = PlaybackSession::open_project(&path)
442 .err()
443 .expect("a malformed v7 file must be refused")
444 .to_string();
445 assert!(err.contains(want), "wanted {want:?}, got {err:?}");
446 }
447}
448
449#[test]
450fn a_saved_project_reopens_as_the_same_timeline() {
451 let dir = scratch("round_trip");
452 let saved = edited(&dir);
453 let path = dir.join("edit.edith");
454 saved.save_project(&path).expect("save");
455
456 let mut loaded = PlaybackSession::open_project(&path).expect("open the project");
457 assert_eq!(shape(&loaded), shape(&saved), "clips, sources and duration");
458 assert!(
459 (loaded.now() - saved.now()).abs() < 1.0 / loaded.meta().frame_rate,
460 "playhead landed at {:.3}s, saved at {:.3}s",
461 loaded.now(),
462 saved.now()
463 );
464 assert!(!loaded.is_playing(), "a loaded project starts paused");
465 assert!(!loaded.undo(), "history is not saved");
466
467 // Saving what was loaded reproduces the file byte for byte -- the format
468 // has no state the round trip drops.
469 let again = dir.join("again.edith");
470 loaded
471 .save_project(&again)
472 .expect("save the loaded project");
473 assert_eq!(
474 std::fs::read(&path).unwrap(),
475 std::fs::read(&again).unwrap(),
476 "save -> load -> save is not a fixed point"
477 );
478 assert!(!path.with_extension("edith.part").exists(), "left a .part");
479}
480
481#[test]
482fn a_folder_of_media_and_its_project_can_be_moved() {
483 let from = scratch("relocate_from");
484 let saved = edited(&from);
485 saved.save_project(&from.join("edit.edith")).expect("save");
486 drop(saved);
487
488 let to = scratch("relocate_to");
489 for name in ["test_av.mp4", "test_av2.mp4", "edit.edith"] {
490 std::fs::copy(from.join(name), to.join(name)).expect("copy");
491 }
492 // The originals go away entirely: nothing absolute can be resolving here.
493 std::fs::remove_dir_all(&from).expect("remove the original folder");
494
495 let moved = PlaybackSession::open_project(&to.join("edit.edith")).expect("open the copy");
496 assert_eq!(moved.timeline_duration(), 6.0);
497 assert!(
498 moved.sources().iter().all(|s| s.path.starts_with(&to)),
499 "sources did not follow the folder: {:?}",
500 moved.sources()
501 );
502}
503
504#[test]
505fn orphan_sources_are_not_written() {
506 let dir = scratch("orphans");
507 let mut session = PlaybackSession::open(copy_in(&dir, "test_av.mp4")).expect("open");
508 session
509 .import(&copy_in(&dir, "test_av2.mp4"))
510 .expect("import");
511 assert!(session.undo(), "take the import back");
512 assert_eq!(
513 session.sources().len(),
514 2,
515 "the orphan entry stays in-session"
516 );
517
518 let path = dir.join("orphan.edith");
519 session.save_project(&path).expect("save");
520 let text = std::fs::read_to_string(&path).expect("read back");
521 assert_eq!(
522 text.lines().filter(|l| l.starts_with("source ")).count(),
523 1,
524 "the orphan reached the file:\n{text}"
525 );
526
527 // And the load survives the orphan's file disappearing, which is the whole
528 // point of pruning it.
529 std::fs::remove_file(dir.join("test_av2.mp4")).expect("unlink the orphan");
530 let loaded = PlaybackSession::open_project(&path).expect("open the project");
531 assert_eq!(loaded.sources().len(), 1);
532 assert_eq!(loaded.timeline_duration(), 5.0);
533}
534
535#[test]
536fn a_source_that_vanished_is_refused_by_name() {
537 let dir = scratch("missing");
538 let session = edited(&dir);
539 let path = dir.join("edit.edith");
540 session.save_project(&path).expect("save");
541 drop(session);
542
543 let gone = dir.join("test_av2.mp4");
544 std::fs::remove_file(&gone).expect("unlink");
545 let err = PlaybackSession::open_project(&path)
546 .err()
547 .expect("a project whose source is gone must not open")
548 .to_string();
549 assert!(
550 err.contains("test_av2.mp4"),
551 "the refusal must name the file: {err}"
552 );
553
554 // The first source is no different -- it is simply refused earlier.
555 std::fs::remove_file(dir.join("test_av.mp4")).expect("unlink");
556 let err = PlaybackSession::open_project(&path)
557 .err()
558 .expect("no first source, no session")
559 .to_string();
560 assert!(err.contains("test_av.mp4"), "{err}");
561}
562
563#[test]
564fn a_source_that_shrank_is_refused_by_clip() {
565 let dir = scratch("shrunk");
566 copy_in(&dir, "test_av.mp4");
567 let path = dir.join("shrunk.edith");
568 // Hand-written: a clip that ran to frame 10000 of a 150-frame file, which
569 // is what re-encoding a source shorter leaves behind.
570 std::fs::write(
571 &path,
572 "edith 1\nplayhead 0\nsource test_av.mp4\nclip 0 30 0\nclip 30 10000 0\n",
573 )
574 .expect("write");
575 let err = PlaybackSession::open_project(&path)
576 .err()
577 .expect("a clip past the end of its file must not open")
578 .to_string();
579 assert!(
580 err.contains("clip 1") && err.contains("test_av.mp4") && err.contains("150"),
581 "the refusal must name the clip, the file and its length: {err}"
582 );
583
584 // One frame short of the end is still inside the file.
585 std::fs::write(
586 &path,
587 "edith 1\nplayhead 0\nsource test_av.mp4\nclip 0 150 0\n",
588 )
589 .expect("write");
590 let whole = PlaybackSession::open_project(&path).expect("the exact length is legal");
591 assert_eq!(whole.timeline_duration(), 5.0);
592}
593
594/// A refusal must *return*. Opening a project starts decoding source 0 before
595/// it has validated anything, so by the time a later source is refused the
596/// worker is several frames in -- and with a two-frame channel that nobody is
597/// draining, it is parked in `send`. Tearing that down in the wrong order joins
598/// a thread that is waiting for a receiver the same scope still holds, and the
599/// app hangs on a bad project file instead of showing the error.
600///
601/// The many sources are what make the park a certainty rather than a race: each
602/// one is opened and probed before the clip check that refuses, which is far
603/// longer than the few frames it takes to fill the channel.
604#[test]
605fn a_refused_project_does_not_hang_on_its_own_decoder() {
606 let dir = scratch("refusal_hang");
607 copy_in(&dir, "test_av.mp4");
608 let path = dir.join("refused.edith");
609 let mut text = String::from("edith 1\nplayhead 0\n");
610 for _ in 0..1024 {
611 text.push_str("source test_av.mp4\n");
612 }
613 // Legal until the last line, which runs off the end of a 150-frame file.
614 text.push_str("clip 0 150 0\nclip 0 10000 0\n");
615 std::fs::write(&path, &text).expect("write");
616
617 let (tx, rx) = std::sync::mpsc::sync_channel(1);
618 let probe = path.clone();
619 std::thread::spawn(move || {
620 let err = PlaybackSession::open_project(&probe)
621 .err()
622 .map(|e| e.to_string());
623 let _ = tx.send(err);
624 });
625 match rx.recv_timeout(std::time::Duration::from_secs(60)) {
626 Ok(Some(err)) => assert!(err.contains("150"), "refused, but not by name: {err}"),
627 Ok(None) => panic!("a clip past the end of its file must not open"),
628 Err(_) => panic!(
629 "open_project hung on a refusal: its own decode worker is parked in \
630 send and the teardown is waiting for it"
631 ),
632 }
633}
634
635#[test]
636fn a_project_that_names_itself_is_refused() {
637 let dir = scratch("self_reference");
638 let path = dir.join("ouroboros.edith");
639 std::fs::write(
640 &path,
641 "edith 1\nplayhead 0\nsource ouroboros.edith\nclip 0 30 0\n",
642 )
643 .expect("write");
644 let err = PlaybackSession::open_project(&path)
645 .err()
646 .expect("a project is not a video")
647 .to_string();
648 assert!(err.contains("ouroboros.edith"), "{err}");
649}
650
651#[test]
652fn malformed_files_are_numbered_errors_and_never_panics() {
653 let dir = scratch("malformed");
654 copy_in(&dir, "test_av.mp4");
655 let path = dir.join("bad.edith");
656 let good = "edith 1\nplayhead 0\nsource test_av.mp4\nclip 0 30 0\n";
657
658 for (text, want) in [
659 (
660 "edith 8\nsource 0 test_av.mp4\nvideo 1 0 0 30 0 - - - fit\n",
661 "line 1",
662 ),
663 // An eq index the table does not hold, and a band shape there is none.
664 (
665 "edith 5\nsource 0 test_av.mp4\nvideo 1 0 0 30 0 - 0\n",
666 "line 3",
667 ),
668 (
669 "edith 5\nsource 0 test_av.mp4\neq 80.0:0.0:0.707:band\nvideo 1 0 0 30 0 - 0\n",
670 "line 3",
671 ),
672 // A lane number that skips one of its kind: V2 was never declared.
673 (
674 "edith 4\nsource 0 test_av.mp4\nvideo 2 0 0 30 0 -\n",
675 "line 3",
676 ),
677 // A v2 source line in a v3 file: the stream field is not optional.
678 ("edith 3\nsource test_av.mp4\nvideo 0 0 30 0 -\n", "line 2"),
679 ("not a project at all\n", "line 1"),
680 // Dialects do not mix: lane lines are v2's, `clip` is v1's.
681 ("edith 2\nsource test_av.mp4\nclip 0 30 0\n", "line 3"),
682 ("edith 1\nsource test_av.mp4\nvideo 0 0 30 0 -\n", "line 3"),
683 ("edith 2\nsource test_av.mp4\nvideo 0 0 30 0\n", "line 3"),
684 (
685 "edith 2\nsource test_av.mp4\nvideo 0 0 30 0 -\nvideo 10 0 30 0 -\n",
686 "line 4",
687 ),
688 ("edith 1\nsource test_av.mp4\nclip 0 30\n", "line 3"),
689 ("edith 1\nsource test_av.mp4\nclip 0 30 4\n", "line 3"),
690 ("edith 1\nsource test_av.mp4\nclip 30 30 0\n", "line 3"),
691 ("edith 1\nsource test_av.mp4\n\nclip 0 30 0\n", "line 3"),
692 ("edith 1\nsource test_av.mp4\nwhat 1\n", "line 3"),
693 // Crafted numbers: `start + len` used to wrap past the top of `u32`,
694 // and a wrapped end walks straight through the overlap check.
695 (
696 "edith 2\nsource test_av.mp4\nvideo 4294967290 0 30 0 -\n",
697 "line 3",
698 ),
699 ] {
700 std::fs::write(&path, text).expect("write");
701 let err = PlaybackSession::open_project(&path)
702 .err()
703 .unwrap_or_else(|| panic!("accepted {text:?}"))
704 .to_string();
705 assert!(err.starts_with(want), "{text:?} -> {err}");
706 }
707
708 // Every truncation of a good file: some are valid shorter projects, none
709 // may panic or be reported as anything but an error.
710 for cut in 0..good.len() {
711 std::fs::write(&path, &good[..cut]).expect("write");
712 let _ = PlaybackSession::open_project(&path);
713 }
714 // ...and a file that is not there at all.
715 std::fs::remove_file(&path).expect("unlink");
716 assert!(PlaybackSession::open_project(&path).is_err());
717}
718
719#[test]
720fn a_source_that_no_longer_matches_the_timeline_is_refused_in_import_words() {
721 let dir = scratch("mismatch");
722 copy_in(&dir, "test_av.mp4");
723 std::fs::copy(asset("test_mismatch.mp4"), dir.join("test_av2.mp4")).expect("substitute");
724 let path = dir.join("swapped.edith");
725 std::fs::write(
726 &path,
727 "edith 1\nplayhead 0\nsource test_av.mp4\nsource test_av2.mp4\nclip 0 30 0\nclip 0 30 1\n",
728 )
729 .expect("write");
730 let err = PlaybackSession::open_project(&path)
731 .err()
732 .expect("a source that stopped matching must not open")
733 .to_string();
734 // The suffix is `import`'s own refusal, word for word. The substitute is
735 // 640x360 *and* silent; only the second of those is a refusal now -- a
736 // resolution of its own is placed on the project canvas.
737 assert_eq!(
738 err,
739 format!(
740 "source {}: the file is silent, the timeline has audio",
741 dir.join("test_av2.mp4").display()
742 )
743 );
744}