| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | use std::path::{Path, PathBuf}; |
| 11 | |
| 12 | use engine::PlaybackSession; |
| 13 | use engine::project::{Lane, Source}; |
| 14 | use engine::scratch::Scratch; |
| 15 | |
| 16 | fn asset(name: &str) -> PathBuf { |
| 17 | PathBuf::from(env!("CARGO_MANIFEST_DIR")) |
| 18 | .join("../../assets") |
| 19 | .join(name) |
| 20 | } |
| 21 | |
| 22 | |
| 23 | fn scratch(name: &str) -> Scratch { |
| 24 | Scratch::dir(&format!("ve_saveload_{name}")) |
| 25 | } |
| 26 | |
| 27 | fn 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 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | #[test] |
| 39 | fn 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); |
| 44 | let end = session.timeline_duration(); |
| 45 | |
| 46 | |
| 47 | |
| 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 | |
| 78 | |
| 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 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | #[test] |
| 103 | fn 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 | |
| 126 | |
| 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 | |
| 139 | |
| 140 | fn edited(dir: &Path) -> PlaybackSession { |
| 141 | let mut session = PlaybackSession::open(copy_in(dir, "test_av.mp4")).expect("open"); |
| 142 | |
| 143 | |
| 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 | |
| 165 | fn 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 | |
| 174 | |
| 175 | |
| 176 | #[test] |
| 177 | fn 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 | |
| 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 | |
| 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 | |
| 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 | |
| 232 | |
| 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 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | #[test] |
| 256 | fn 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 | |
| 263 | |
| 264 | |
| 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 | |
| 291 | |
| 292 | |
| 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 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | #[test] |
| 309 | fn 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 | |
| 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 | |
| 337 | |
| 338 | |
| 339 | |
| 340 | |
| 341 | #[test] |
| 342 | fn 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 | |
| 347 | |
| 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 | |
| 371 | |
| 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 | |
| 380 | |
| 381 | |
| 382 | |
| 383 | #[test] |
| 384 | fn 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 | |
| 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 | |
| 431 | |
| 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] |
| 458 | fn 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 | |
| 476 | |
| 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] |
| 490 | fn 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 | |
| 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] |
| 513 | fn 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 | |
| 517 | |
| 518 | |
| 519 | session |
| 520 | .import(©_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 | |
| 538 | |
| 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] |
| 546 | fn 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 | |
| 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] |
| 574 | fn 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 | |
| 579 | |
| 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 | |
| 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 | |
| 605 | |
| 606 | |
| 607 | |
| 608 | |
| 609 | |
| 610 | |
| 611 | |
| 612 | |
| 613 | |
| 614 | #[test] |
| 615 | fn 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 | |
| 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] |
| 646 | fn 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] |
| 662 | fn 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 | |
| 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 | |
| 683 | ( |
| 684 | "edith 4\nsource 0 test_av.mp4\nvideo 2 0 0 30 0 -\n", |
| 685 | "line 3", |
| 686 | ), |
| 687 | |
| 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 | |
| 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 | |
| 704 | |
| 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 | |
| 719 | |
| 720 | for cut in 0..good.len() { |
| 721 | std::fs::write(&path, &good[..cut]).expect("write"); |
| 722 | let _ = PlaybackSession::open_project(&path); |
| 723 | } |
| 724 | |
| 725 | std::fs::remove_file(&path).expect("unlink"); |
| 726 | assert!(PlaybackSession::open_project(&path).is_err()); |
| 727 | } |
| 728 | |
| 729 | #[test] |
| 730 | fn 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 | |
| 745 | |
| 746 | |
| 747 | |
| 748 | |
| 749 | |
| 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 | |
| 760 | |
| 761 | |
| 762 | #[test] |
| 763 | fn 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 | |
| 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 | |
| 797 | |
| 798 | |
| 799 | |
| 800 | |
| 801 | |
| 802 | #[test] |
| 803 | fn 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 | |
| 818 | |
| 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 | } |