forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBfbc29d1Tahinli2 months ago972 commits

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