Skip to content

EffectCraft: An open-source motion graphics and compositing tool written by scratch in pure Rust

Merge pull request #845 from Ni-zav/fix/814-batch-remove-keys-s20261011T141200Z-fd2e

Brandon Thomas61f5b937dc05
diff --cc crates/script/src/tests.rs
index 34ac592fa,92519491e..2dd6a1497
--- a/crates/script/src/tests.rs
+++ b/crates/script/src/tests.rs
@@@ -731,87 -731,22 +731,107 @@@ fn import_options_sequence_picks_one_st
let _ = std::fs::remove_dir_all(&dir);
}
+
+#[test]
+fn binary_file_io_preserves_all_byte_values() {
+ let dir = std::env::temp_dir().join(format!("ec-script-binary-{}", std::process::id()));
+ std::fs::create_dir_all(&dir).unwrap();
+ let src = dir.join("sig.bin");
+ let dst = dir.join("written.bin");
+ let copied = dir.join("copied.bin");
+ let original = [0x89u8, 0x50, 0x4e, 0x47, 0x00, 0x7f, 0x80, 0xff, 0x0a];
+ std::fs::write(&src, original).unwrap();
+ let mut s = session();
+ s.path = Some(dir.join("project.ecproj").to_string_lossy().to_string());
+ s.prefs.scripting.allow_scripts_write_files = true;
+ let q = |p: &std::path::Path| serde_json::to_string(&p.to_string_lossy()).unwrap();
+
+ let read = ok(&mut s, &format!(
+ "var f = new File({}); f.encoding = 'BINARY'; f.open('r'); var data = f.read(); f.close(); [data.length, data.charCodeAt(0), data.charCodeAt(6), data.charCodeAt(7)]",
+ q(&src)
+ ));
+ assert_eq!(read.result, json!([9, 0x89, 0x80, 0xff]));
+
+ ok(&mut s, &format!(
+ "var f = new File({}); f.encoding = 'BINARY'; f.open('w'); f.write(String.fromCharCode(0x89, 0x50, 0x4e, 0x47, 0, 127, 128, 255, 10)); f.close();",
+ q(&dst)
+ ));
+ assert_eq!(std::fs::read(&dst).unwrap(), original);
+
+ let copy = ok(&mut s, &format!("new File({}).copy({})", q(&src), q(&copied)));
+ assert_eq!(copy.result, json!(true));
+ assert_eq!(std::fs::read(&copied).unwrap(), original);
+
+ ok(&mut s, &format!(
+ "var f = new File({}); f.encoding = 'BINARY'; f.open('a'); f.write(String.fromCharCode(0xfe)); f.close();",
+ q(&dst)
+ ));
+ assert_eq!(std::fs::read(&dst).unwrap(), [original.as_slice(), &[0xfe]].concat());
+
+ let invalid = run_code(
+ &mut s,
+ &format!("var f = new File({}); f.encoding = 'BINARY'; f.open('w'); f.write(String.fromCharCode(0x100)); f.close();", q(&dst)),
+ "invalid-binary.jsx",
+ );
+ assert!(invalid.error.is_some(), "code points above 255 must not be truncated");
+ let _ = std::fs::remove_dir_all(&dir);
+}
+
+#[test]
+fn script_undo_uses_stable_after_effects_command_id() {
+ let mut s = session();
+ let lookup = ok(&mut s, "app.findMenuCommandId('Undo')");
+ assert_eq!(lookup.result, json!(16));
+ // ID 16 used to index an unrelated command as the registry grew. It must
+ // undo the preceding layer creation rather than dispatching by position.
+ let r = ok(
+ &mut s,
+ "var c = app.project.items.addComp('Undo ID', 100, 100, 1, 1, 24); c.layers.addNull(); app.executeCommand(16); c.numLayers",
+ );
+ assert_eq!(r.result, json!(0));
+ assert_eq!(s.project.comp(ItemId(1)).unwrap().layers.len(), 0);
+ // Lookup of other command names still uses the existing registry.
+ assert!(ok(&mut s, "app.findMenuCommandId('New Project')").result.as_u64().unwrap_or(0) > 0);
+}
+
+#[test]
+fn long_script_undo_group_counts_as_one_level() {
+ let mut s = session();
+ s.prefs.general.undo_levels = 3;
+ ok(&mut s, "var c = app.project.items.addComp('Long Undo', 100, 100, 1, 1, 24); c.layers.addNull();");
+ assert_eq!(s.history.undo.len(), 2);
+ // More edits than the history cap must still be one undoable group.
+ ok(
+ &mut s,
+ "var c = app.project.item(1); app.beginUndoGroup('Forty layers'); for (var i = 0; i < 40; i++) c.layers.addNull(); app.endUndoGroup();",
+ );
+ assert_eq!(s.project.comp(ItemId(1)).unwrap().layers.len(), 41);
+ assert_eq!(s.history.undo.len(), 3);
+ assert_eq!(s.history.undo[2].0, "Forty layers");
+ s.undo();
+ assert_eq!(s.project.comp(ItemId(1)).unwrap().layers.len(), 1);
+ // Entries preceding the group remain available instead of being trimmed by
+ // the forty intermediate operations.
+ s.undo();
+ assert_eq!(s.project.comp(ItemId(1)).unwrap().layers.len(), 0);
+}
++
+ #[test]
+ fn script_remove_keys_batches_a_range_as_one_edit() {
+ let mut s = session();
+ let o = ok(
+ &mut s,
+ "var c = app.project.items.addComp('Batch Keys', 100, 100, 1, 10, 25); var p = c.layers.addNull().transform.rotation; for (var i = 0; i < 5; i++) p.setValueAtTime(i, i * 10); p.removeKeys(2, 4); [p.numKeys, p.keyTime(1), p.keyTime(2)]",
+ );
+ assert_eq!(o.result, json!([2, 0, 4]));
+ // The entire deletion is one undo step; keys outside the range survive.
+ s.undo();
+ let restored = ok(&mut s, "app.project.item(1).layer(1).transform.rotation.numKeys");
+ assert_eq!(restored.result, json!(5));
+ let invalid = run_code(
+ &mut s,
+ "app.project.item(1).layer(1).transform.rotation.removeKeys(0, 2)",
+ "remove-keys-range.jsx",
+ );
+ assert!(invalid.error.is_some());
+ }