forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiB8c7bf80Tahinli3 months ago114 commits

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