forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBcc667e6Tahinli3 months ago285 commits

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