GitCorner

Code

ossartcraftfilmcraft🌐
f67d38b35cc5
use serde_json::{Value, json};

use crate::Session;
use crate::media_test_util::tmp_dir;
use filmcraft_project::graphic::LayerContent;
use filmcraft_project::{ClipId, ParamValue, TrackItem};
use filmcraft_render::graphic_clip::{item_layer_specs, layer_bounds};
use filmcraft_time::Tick;

fn demo(dir: &str) -> Session {
    let mut s = Session::default();
    s.execute("file.openDemoProject", json!({})).unwrap();
    s.prefs_path = Some(tmp_dir(dir).join("prefs.json"));
    // far after the demo clips, so graphics have free tracks around them
    s.execute("playhead.set", json!({"seconds": 600})).unwrap();
    s
}

fn item(s: &Session, clip: ClipId) -> TrackItem {
    s.active_sequence().unwrap().find_item(clip).unwrap().1.clone()
}

fn canvas(s: &Session) -> (u32, u32) {
    let q = s.active_sequence().unwrap();
    (q.settings.width, q.settings.height)
}

fn texts(s: &Session, clip: ClipId) -> Vec<String> {
    let it = item(s, clip);
    item_layer_specs(&it, it.source_in, canvas(s))
        .into_iter()
        .filter_map(|(_, sp)| match sp.content {
            LayerContent::Text(t) => Some(t.text),
            _ => None,
        })
        .collect()
}

fn clip_of(v: &Value) -> ClipId {
    ClipId(v["clip"].as_u64().unwrap())
}

#[test]
fn template_exports_keep_distinct_non_ascii_names() {
    let mut s = demo("gt-unicode-export");
    let c = clip_of(&s.execute("graphics.newText", json!({"text": "Headline"})).unwrap());
    let first = s.execute("graphics.template.export", json!({"clip": c.0, "name": "片头"})).unwrap();
    let first_path = first["path"].as_str().unwrap();
    let first_bytes = std::fs::read(first_path).unwrap();
    let second = s.execute("graphics.template.export", json!({"clip": c.0, "name": "片尾"})).unwrap();
    assert_ne!(first["path"], second["path"], "distinct names must not overwrite the first export");
    assert_ne!(first["id"], second["id"], "distinct templates need distinct library identities");
    assert_eq!(std::fs::read(first_path).unwrap(), first_bytes);
    let second_file: Value = serde_json::from_slice(&std::fs::read(second["path"].as_str().unwrap()).unwrap()).unwrap();
    assert_eq!(second_file["name"], "片尾");
    let installed = s.execute("graphics.template.list", json!({"source": "user"})).unwrap();
    for exported in [&first, &second] {
        assert!(installed.as_array().unwrap().iter().any(|entry| entry["id"] == exported["id"] && entry["name"] == exported["name"]));
    }
}

#[test]
fn template_export_install_apply_round_trip() {
    let mut s = demo("gt-export");
    let c = clip_of(&s.execute("graphics.newText", json!({"text": "Headline", "position": [200, 900]})).unwrap());
    s.execute("graphics.newText", json!({"text": "Subhead", "clip": c.0, "position": [200, 960], "size": 40})).unwrap();
    s.execute("graphics.newShape", json!({"shape": "rectangle", "clip": c.0, "position": [400, 920], "size": [500, 150]})).unwrap();
    s.execute("graphics.arrangeLayer", json!({"clip": c.0, "layer": 2, "to": "back"})).unwrap();
    s.execute("graphics.setRoll", json!({"clip": c.0, "mode": "crawlLeft", "startOffScreen": true})).unwrap();
    let r = s
        .execute(
            "graphics.template.export",
            json!({"clip": c.0, "name": "Show Lower Third", "category": "Lower Thirds", "controls": [
                {"layer": 1, "param": "text", "name": "Headline"},
                {"layer": "Subhead", "param": "text", "name": "Subhead"},
                {"layer": 0, "param": "fill_color", "name": "Box Color"},
                {"layer": 1, "param": "size", "name": "Headline Size", "min": 20, "max": 200},
                {"layer": 2, "param": "enabled", "name": "Show Subhead"},
            ]}),
        )
        .unwrap();
    let path = r["path"].as_str().unwrap().to_string();
    assert!(path.ends_with("show-lower-third.fcgt"), "{path}");
    assert_eq!(r["controls"].as_array().unwrap().len(), 5);
    assert_eq!(r["controls"][3]["kind"], "slider");
    assert_eq!(r["controls"][4]["kind"], "checkbox");
    // the file is our documented JSON format
    let file: Value = serde_json::from_slice(&std::fs::read(&path).unwrap()).unwrap();
    assert_eq!(file["format"], "filmcraft.graphicsTemplate");
    assert_eq!(file["version"], 1);
    assert_eq!(file["graphic"]["roll"]["mode"], "crawlLeft");

    // install into another user's library
    let mut s2 = demo("gt-install");
    let ins = s2.execute("graphics.template.install", json!({"path": path})).unwrap();
    assert_eq!(ins["id"], "user:show-lower-third");
    let list = s2.execute("graphics.template.list", json!({"query": "lower third", "source": "user"})).unwrap();
    assert_eq!(list.as_array().unwrap().len(), 1, "{list}");
    let all = s2.execute("graphics.template.list", json!({})).unwrap();
    assert!(all.as_array().unwrap().len() >= 9, "built-ins and the user template");
    let undo_before = s2.history.undo.len();
    let a = s2
        .execute(
            "graphics.template.apply",
            json!({"template": "Show Lower Third", "values": {"Headline": "Breaking News", "Box Color": "#ff0000", "headline_size": 500}}),
        )
        .unwrap();
    assert_eq!(s2.history.undo.len(), undo_before + 1, "one undo step");
    let c2 = clip_of(&a);
    assert_eq!(texts(&s2, c2), vec!["Breaking News", "Subhead"]);
    let it = item(&s2, c2);
    assert_eq!(it.graphic.as_ref().unwrap().roll.mode, filmcraft_project::RollMode::CrawlLeft);
    let ctl = s2.execute("graphics.template.controls", json!({"clip": c2.0})).unwrap();
    assert_eq!(ctl["controls"][0]["value"], "Breaking News");
    assert_eq!(ctl["controls"][2]["value"], "#ff0000");
    assert_eq!(ctl["controls"][3]["value"], 200.0, "slider clamped to its range");
    s2.execute("graphics.template.set", json!({"clip": c2.0, "control": "Show Subhead", "value": false})).unwrap();
    let ctl = s2.execute("graphics.template.controls", json!({"clip": c2.0})).unwrap();
    assert_eq!(ctl["controls"][4]["value"], false);
    s2.execute("edit.undo", json!({})).unwrap();
    s2.execute("edit.undo", json!({})).unwrap();
    assert!(s2.active_sequence().unwrap().find_item(c2).is_none(), "undo removes the applied template");
    // unknown property
    assert!(s2.execute("graphics.template.apply", json!({"template": "Show Lower Third", "values": {"Nope": 1}})).is_err());
    assert!(s2.active_sequence().unwrap().video_tracks.iter().all(|t| t.items.iter().all(|i| i.name != "Show Lower Third")));
    // remove
    s2.execute("graphics.template.remove", json!({"template": "user:show-lower-third"})).unwrap();
    assert!(s2.execute("graphics.template.list", json!({"source": "user"})).unwrap().as_array().unwrap().is_empty());
}

#[test]
fn mogrt_files_are_never_read() {
    let mut s = demo("gt-mogrt");
    let d = tmp_dir("gt-mogrt-files");
    let m = d.join("fancy.mogrt");
    std::fs::write(&m, b"PK\x03\x04whatever").unwrap();
    let e = s.execute("graphics.template.install", json!({"path": m.to_string_lossy()})).unwrap_err().to_string();
    assert!(e.contains(".mogrt"), "{e}");
    // a ZIP renamed to .fcgt is refused too
    let z = d.join("renamed.fcgt");
    std::fs::write(&z, b"PK\x03\x04whatever").unwrap();
    let e = s.execute("graphics.template.install", json!({"path": z.to_string_lossy()})).unwrap_err().to_string();
    assert!(e.contains("does not read"), "{e}");
}

#[test]
fn builtin_templates_apply_and_thumbnail() {
    let mut s = demo("gt-builtin");
    let list = s.execute("graphics.template.list", json!({"category": "Callouts"})).unwrap();
    assert_eq!(list.as_array().unwrap().len(), 2);
    for e in s.execute("graphics.template.list", json!({"source": "builtin"})).unwrap().as_array().unwrap().clone() {
        let id = e["id"].as_str().unwrap();
        let r = s.execute("graphics.template.apply", json!({"template": id})).unwrap();
        assert!(!texts(&s, clip_of(&r)).is_empty(), "{id}");
        let th = s.execute("graphics.template.thumbnail", json!({"template": id, "width": 160})).unwrap();
        assert_eq!((th["width"].as_u64(), th["height"].as_u64()), (Some(160), Some(90)));
        let png = filmcraft_project::gtemplate::base64_decode(th["pngBase64"].as_str().unwrap()).unwrap();
        assert!(png.starts_with(b"\x89PNG"));
        let (_, _, rgba) = super::thumbnail(&super::find_template(&s, id).unwrap().template, 160);
        assert!(rgba.chunks(4).filter(|p| p[3] > 128).count() > 50, "{id}: the thumbnail shows something");
        s.execute("playhead.set", json!({"seconds": 600 + 20 * s.history.undo.len()})).unwrap();
    }
}

#[test]
fn template_thumbnail_dimensions_fit_a_bounded_rectangle() {
    assert_eq!(super::thumbnail_dimensions([1920, 1080], 160), (160, 90));
    assert_eq!(super::thumbnail_dimensions([1, 16_384], 1920), (1, 1920));
    assert_eq!(super::thumbnail_dimensions([16_384, 1], 1920), (1920, 1));
}

#[test]
fn responsive_time_protects_intro_and_outro_when_trimmed() {
    let mut s = demo("gt-time");
    let c = clip_of(&s.execute("graphics.newText", json!({"text": "Fade", "seconds": 5})).unwrap());
    let sec = |x: f64| Tick::from_seconds_f64(x);
    // opacity: 0 → 100 over the first second, 100 → 0 over the last
    s.edit_sequence("kf", |q, _, _| {
        let (_, it) = q.find_item_mut(c).unwrap();
        let (si, d) = (it.source_in, it.duration);
        let e = it.effects.iter_mut().find(|e| e.effect == "graphic_text").unwrap();
        let p = e.params.get_mut("opacity").unwrap();
        p.toggle_animation(si);
        p.set_at(si, ParamValue::Float(0.0));
        for (t, v) in [(si + sec(1.0), 100.0), (si + d - sec(1.0), 100.0), (si + d, 0.0)] {
            p.set_at(t, ParamValue::Float(v));
        }
        Ok(())
    })
    .unwrap();
    s.execute("graphics.setResponsiveTime", json!({"clip": c.0, "introSeconds": 1, "outroSeconds": 1})).unwrap();
    // opacity (0..100) at clip-relative `u` seconds, or `u` seconds before the clip end
    let op = |s: &Session, u: f64| {
        let it = item(s, c);
        let sp = item_layer_specs(&it, it.source_in + sec(u), canvas(s));
        (sp[0].1.transform.opacity * 100.0) as f64
    };
    let op_end = |s: &Session, before: f64| {
        let it = item(s, c);
        let d = it.duration;
        let sp = item_layer_specs(&it, it.source_in + d - sec(before), canvas(s));
        (sp[0].1.transform.opacity * 100.0) as f64
    };
    let near = |a: f64, b: f64| (a - b).abs() < 1e-3;
    let d0 = item(&s, c).duration;
    assert!(near(op(&s, 0.5), 50.0));
    assert!(near(op_end(&s, 0.5), 50.0));
    // extend by 150 frames: the intro and outro keep their timing, the middle holds
    let rate = s.sequence_rate();
    s.execute("timeline.trim", json!({"clip": c.0, "edge": "out", "deltaFrames": 150})).unwrap();
    assert_eq!(item(&s, c).duration, d0 + rate.tick_of(150));
    assert!(near(op(&s, 0.5), 50.0));
    assert!(near(op(&s, 5.0), 100.0));
    assert!(near(op_end(&s, 0.5), 50.0));
    assert!(near(op_end(&s, 0.25), 25.0));
    // trim the head by 60 frames: the intro plays from the new start
    s.execute("timeline.trim", json!({"clip": c.0, "edge": "in", "deltaFrames": 60})).unwrap();
    assert!(near(op(&s, 0.0), 0.0));
    assert!(near(op(&s, 0.5), 50.0));
    assert!(near(op_end(&s, 0.5), 50.0), "the outro still ends at the clip end");
    // without responsive time the head trim would have cut the fade-in
    assert!(s.execute("graphics.setResponsiveTime", json!({"clip": c.0, "introSeconds": 30})).is_err());
}

#[test]
fn rolls_at_exact_times() {
    let mut s = demo("gt-roll");
    let c = clip_of(&s.execute("graphics.newShape", json!({"shape": "rectangle", "position": [960, 540], "size": [200, 100], "seconds": 4})).unwrap());
    s.execute("graphics.setRoll", json!({"clip": c.0, "mode": "roll", "startOffScreen": true, "endOffScreen": true, "prerollFrames": 0})).unwrap();
    let (w, h) = canvas(&s);
    let top = |s: &Session, u: Tick| {
        let it = item(s, c);
        layer_bounds(&item_layer_specs(&it, it.source_in + u, (w, h))[0].1)[1]
    };
    let d = item(&s, c).duration;
    // starts with its top on the frame bottom, ends with its bottom on the frame top: travel = h + 100
    assert!((top(&s, Tick::ZERO) - h as f64).abs() < 1e-6);
    assert!((top(&s, d) - (-100.0)).abs() < 1e-6);
    assert!((top(&s, Tick(d.0 / 4)) - (h as f64 - (h as f64 + 100.0) / 4.0)).abs() < 1e-6);
    // ease in / out
    s.execute("graphics.setRoll", json!({"clip": c.0, "easeInSeconds": 1, "easeOutSeconds": 1})).unwrap();
    let u = Tick::from_seconds_f64(0.5);
    let v = 1.0 / (d.seconds() - 1.0);
    let want = h as f64 - (h as f64 + 100.0) * v * 0.25 / 2.0;
    assert!((top(&s, u) - want).abs() < 1e-6, "{} vs {want}", top(&s, u));
    s.execute("graphics.setRoll", json!({"clip": c.0, "mode": "off"})).unwrap();
    assert!((top(&s, u) - 490.0).abs() < 1e-6);
    assert!(s.execute("graphics.setRoll", json!({"clip": c.0, "mode": "sideways"})).is_err());
}

#[test]
fn pins_follow_text_growth() {
    let mut s = demo("gt-pins");
    let c = clip_of(&s.execute("graphics.newText", json!({"text": "Hi", "position": [300, 500], "size": 80})).unwrap());
    s.execute("graphics.newShape", json!({"shape": "rectangle", "clip": c.0, "position": [330, 470], "size": [200, 120]})).unwrap();
    s.execute("graphics.arrangeLayer", json!({"clip": c.0, "layer": 1, "to": "back"})).unwrap();
    // box (layer 0) pinned to the text (layer 1) on all edges
    s.execute("graphics.pin", json!({"clip": c.0, "layer": 0, "to": 1})).unwrap();
    let boxes = |s: &mut Session| {
        let l = s.execute("graphics.list", json!({"clip": c.0})).unwrap();
        let q = |i: usize| {
            let v: Vec<f64> = l["layers"][i]["quad"].as_array().unwrap().iter().flat_map(|p| [p[0].as_f64().unwrap(), p[1].as_f64().unwrap()]).collect();
            [v[0], v[1], v[4], v[5]]
        };
        (q(0), q(1))
    };
    let (b0, t0) = boxes(&mut s);
    let gap = [b0[0] - t0[0], b0[1] - t0[1], b0[2] - t0[2], b0[3] - t0[3]];
    s.execute("graphics.setText", json!({"clip": c.0, "layer": 1, "text": "Hello, responsive world"})).unwrap();
    let (b1, t1) = boxes(&mut s);
    assert!(t1[2] > t0[2] + 300.0, "the text grew");
    for k in 0..4 {
        assert!((b1[k] - t1[k] - gap[k]).abs() < 1e-3, "edge {k}: {b1:?} {t1:?} {gap:?}");
    }
    // move the text: the box follows; unpin
    s.execute("graphics.set", json!({"clip": c.0, "layer": 1, "props": {"position": [100, 300]}})).unwrap();
    let (b2, t2) = boxes(&mut s);
    assert!((b2[0] - t2[0] - gap[0]).abs() < 1e-3);
    s.execute("graphics.pin", json!({"clip": c.0, "layer": 0, "to": "none"})).unwrap();
    let list = s.execute("graphics.list", json!({"clip": c.0})).unwrap();
    assert!(list["layers"][0]["pin"].is_null());
    assert!(s.execute("graphics.pin", json!({"clip": c.0, "layer": 0, "to": 0})).is_err(), "not to itself");
}

#[test]
fn character_stroke_sizes_and_switches_do_not_change_the_layer() {
    let mut s = demo("gt-char-stroke-size");
    let c = clip_of(&s.execute("graphics.newText", json!({"text": "甲12乙"})).unwrap());
    let before = item(&s, c);
    let style = json!({"stroke": true, "strokeWidth": 24, "strokeType": "inner", "stroke2": false, "stroke2Width": 36, "stroke2Type": "center"});
    let result = s.execute("graphics.setCharStyle", json!({"clip": c.0, "start": 1, "end": 3, "style": style})).unwrap();
    assert_eq!(
        result["styles"],
        json!([{"start": 1, "end": 3, "stroke": true, "strokeWidth": 24.0, "strokeType": 2,
        "stroke2": false, "stroke2Width": 36.0, "stroke2Type": 1}])
    );
    let after = item(&s, c);
    for (a, b) in after.effects.iter().zip(&before.effects) {
        assert_eq!(a.params, b.params, "layer parameters stay unchanged");
    }
    assert_eq!(serde_json::from_str::<TrackItem>(&serde_json::to_string(&after).unwrap()).unwrap(), after);
    s.execute("edit.undo", json!({})).unwrap();
    assert_eq!(item(&s, c), before);
    s.execute("edit.redo", json!({})).unwrap();
    assert_eq!(item(&s, c), after);
    let path = tmp_dir("gt-char-stroke-size-save").join("stroke-sizes.fcproj");
    s.execute("file.saveAs", json!({"path": path})).unwrap();
    let mut reopened = Session::default();
    reopened.execute("file.open", json!({"path": path})).unwrap();
    assert_eq!(item(&reopened, c).effects, after.effects);
}

#[test]
fn character_stroke_sizes_and_types_reject_invalid_values_atomically() {
    let mut s = demo("gt-char-stroke-size-invalid");
    let c = clip_of(&s.execute("graphics.newText", json!({"text": "1234"})).unwrap());
    let before = item(&s, c);
    let history = s.execute("history.list", json!({})).unwrap();
    for key in ["strokeWidth", "stroke_width", "stroke2Width", "stroke2_width"] {
        for value in [json!(null), json!("wide"), json!(-1), json!(1001), json!(1e300)] {
            assert!(s.execute("graphics.setCharStyle", json!({"clip": c.0, "style": {key: value}})).is_err());
            assert_eq!(item(&s, c), before);
        }
    }
    for key in ["stroke", "stroke2", "strokeType", "stroke2Type"] {
        for value in [json!(null), json!("invalid"), json!(-1), json!(3)] {
            assert!(s.execute("graphics.setCharStyle", json!({"clip": c.0, "style": {key: value}})).is_err());
            assert_eq!(item(&s, c), before);
        }
    }
    assert_eq!(s.execute("history.list", json!({})).unwrap(), history);
}

#[test]
fn character_stroke_colours_are_local_undoable_and_persistent() {
    let mut s = demo("gt-char-strokes");
    let c = clip_of(&s.execute("graphics.newText", json!({"text": "甲12乙"})).unwrap());
    s.execute("graphics.setCharStyle", json!({"clip": c.0, "start": 1, "end": 3, "style": {"color": "#ffff00"}})).unwrap();
    let before = item(&s, c);
    let styled = s
        .execute(
            "graphics.setCharStyle",
            json!({
                "clip": c.0, "start": 1, "end": 3,
                "style": {"strokeColor": "#ff0000", "stroke2Color": "#0000ff"}
            }),
        )
        .unwrap()["styles"]
        .clone();
    assert_eq!(
        styled,
        json!([{
            "start": 1, "end": 3, "fill": [1.0, 1.0, 0.0, 1.0], "strokeColor": [1.0, 0.0, 0.0, 1.0], "stroke2Color": [0.0, 0.0, 1.0, 1.0]
        }])
    );
    let after = item(&s, c);
    assert_eq!(after.effects[0].params, before.effects[0].params, "layer appearance is unchanged");
    let saved = serde_json::to_string(&after).unwrap();
    assert_eq!(serde_json::from_str::<TrackItem>(&saved).unwrap(), after);
    s.execute("edit.undo", json!({})).unwrap();
    assert_eq!(item(&s, c), before);
    s.execute("edit.redo", json!({})).unwrap();
    assert_eq!(item(&s, c), after);
    let path = tmp_dir("gt-char-strokes-save").join("strokes.fcproj");
    s.execute("file.saveAs", json!({"path": path})).unwrap();
    let mut reopened = Session::default();
    reopened.execute("file.open", json!({"path": path})).unwrap();
    assert_eq!(item(&reopened, c).effects, after.effects, "project files preserve both stroke colours");
    s.execute("graphics.setText", json!({"clip": c.0, "text": "新甲12乙"})).unwrap();
    let runs = s.execute("graphics.list", json!({"clip": c.0})).unwrap()["layers"][0]["styles"].clone();
    assert_eq!((runs[0]["start"].as_u64(), runs[0]["end"].as_u64()), (Some(2), Some(4)));
    s.execute("graphics.setCharStyle", json!({"clip": c.0, "clear": true})).unwrap();
    assert_eq!(s.execute("graphics.list", json!({"clip": c.0})).unwrap()["layers"][0]["styles"], json!([]));
}

#[test]
fn character_stroke_colours_reject_invalid_input_without_edits() {
    let mut s = demo("gt-char-strokes-invalid");
    let c = clip_of(&s.execute("graphics.newText", json!({"text": "1234"})).unwrap());
    let before = item(&s, c);
    let history = s.execute("history.list", json!({})).unwrap();
    for key in ["strokeColor", "stroke_color", "stroke2Color", "stroke2_color"] {
        for colour in [json!(null), json!(true), json!("invalid"), json!([1, 2]), json!([1e300, 0, 0, 1])] {
            assert!(s.execute("graphics.setCharStyle", json!({"clip": c.0, "style": {key: colour}})).is_err(), "{key}: {colour}");
            assert_eq!(item(&s, c), before);
            assert_eq!(s.execute("history.list", json!({})).unwrap(), history);
        }
    }
}

#[test]
fn character_styles_follow_edits() {
    let mut s = demo("gt-chars");
    let c = clip_of(&s.execute("graphics.newText", json!({"text": "Hello World"})).unwrap());
    s.execute("graphics.setCharStyle", json!({"clip": c.0, "start": 6, "end": 11, "style": {"bold": true, "color": "#ff0000", "size": 140}})).unwrap();
    let st = s.execute("graphics.list", json!({"clip": c.0})).unwrap()["layers"][0]["styles"].clone();
    assert_eq!(st, json!([{"start": 6, "end": 11, "fauxBold": true, "fill": [1.0, 0.0, 0.0, 1.0], "size": 140.0}]));
    s.execute("graphics.setText", json!({"clip": c.0, "text": "Oh, Hello World"})).unwrap();
    let st = s.execute("graphics.list", json!({"clip": c.0})).unwrap()["layers"][0]["styles"].clone();
    assert_eq!((st[0]["start"].as_u64(), st[0]["end"].as_u64()), (Some(10), Some(15)));
    // the layout uses the run: the styled word is taller
    let it = item(&s, c);
    let sp = &item_layer_specs(&it, it.source_in, canvas(&s))[0].1;
    let LayerContent::Text(t) = &sp.content else { panic!() };
    let l = filmcraft_render::graphic_clip::text_layout(t);
    assert!(l.glyphs.iter().filter(|g| g.run == 1).all(|g| g.size == 140.0 && g.synth_bold));
    s.execute("graphics.setCharStyle", json!({"clip": c.0, "clear": true})).unwrap();
    assert_eq!(s.execute("graphics.list", json!({"clip": c.0})).unwrap()["layers"][0]["styles"], json!([]));
    assert!(s.execute("graphics.setCharStyle", json!({"clip": c.0, "start": 3, "end": 3, "style": {"bold": true}})).is_err());
    assert!(s.execute("graphics.setCharStyle", json!({"clip": c.0, "style": {"sparkle": true}})).is_err());
}

#[test]
fn strikethrough_per_character_and_per_layer() {
    let mut s = demo("gt-strike");
    let c = clip_of(&s.execute("graphics.newText", json!({"text": "old new"})).unwrap());
    s.execute("graphics.setCharStyle", json!({"clip": c.0, "start": 0, "end": 3, "style": {"strikethrough": true}})).unwrap();
    let st = s.execute("graphics.list", json!({"clip": c.0})).unwrap()["layers"][0]["styles"].clone();
    assert_eq!(st, json!([{"start": 0, "end": 3, "strikethrough": true}]));
    let layout = |s: &Session| {
        let it = item(s, c);
        let sp = &item_layer_specs(&it, it.source_in, canvas(s))[0].1;
        let LayerContent::Text(t) = &sp.content else { panic!() };
        filmcraft_render::graphic_clip::text_layout(t)
    };
    let l = layout(&s);
    assert_eq!(l.underline_runs, vec![1], "one stroke, over the styled word");
    assert!(l.underlines[0][2] <= l.caret(3).0 + 0.01);
    s.execute("graphics.setCharStyle", json!({"clip": c.0, "clear": true})).unwrap();
    s.execute("graphics.set", json!({"clip": c.0, "props": {"strikethrough": true}})).unwrap();
    let l = layout(&s);
    assert_eq!(l.underlines.len(), 1);
    assert!((l.underlines[0][2] - l.underlines[0][0] - l.lines[0].width).abs() < 0.01, "the whole line");
}

#[test]
fn upgrade_caption_to_graphic() {
    let mut s = demo("gt-caption");
    s.execute("captions.add", json!({"text": "Hello <i>there</i>", "seconds": 600, "durationSeconds": 2})).unwrap();
    let caps = s.execute("captions.list", json!({})).unwrap()["tracks"][0]["captions"].clone();
    let before = caps.as_array().unwrap().len();
    let cap = caps.as_array().unwrap().iter().find(|c| c["text"] == "Hello <i>there</i>").unwrap().clone();
    let r = s.execute("graphics.upgradeCaption", json!({})).unwrap();
    assert_eq!(r["upgraded"], 1);
    let c = ClipId(r["clips"][0].as_u64().unwrap());
    assert_eq!(texts(&s, c), vec!["Hello there"]);
    let it = item(&s, c);
    assert_eq!(it.start.0, cap["start"].as_i64().unwrap());
    assert_eq!(it.end().0, cap["end"].as_i64().unwrap());
    let ex = it.effects.iter().find(|e| e.effect == "graphic_text").unwrap().layer.as_ref().unwrap();
    assert_eq!((ex.runs[0].start, ex.runs[0].end, ex.runs[0].style.faux_italic), (6, 11, Some(true)));
    let after = s.execute("captions.list", json!({})).unwrap()["tracks"][0]["captions"].as_array().unwrap().len();
    assert_eq!(after, before - 1, "the caption became a graphic");
    s.execute("edit.undo", json!({})).unwrap();
    assert!(s.active_sequence().unwrap().find_item(c).is_none());
    assert_eq!(s.execute("captions.list", json!({})).unwrap()["tracks"][0]["captions"].as_array().unwrap().len(), before);
}

#[test]
fn select_all_then_upgrade_converts_every_caption() {
    let mut s = demo("gt-caption-all");
    for (i, t) in ["One", "Two", "Three"].iter().enumerate() {
        s.execute("captions.add", json!({"text": t, "seconds": 600 + 3 * i, "durationSeconds": 2})).unwrap();
    }
    let n = s.execute("captions.list", json!({})).unwrap()["tracks"][0]["captions"].as_array().unwrap().len();
    s.execute("edit.deselectAll", json!({})).unwrap();
    s.execute("edit.selectAll", json!({})).unwrap();
    assert_eq!(s.state.caption_selection.len(), n, "Select All takes the captions too");
    let r = s.execute("graphics.upgradeCaption", json!({})).unwrap();
    assert_eq!(r["upgraded"], n);
    assert!(s.execute("captions.list", json!({})).unwrap()["tracks"][0]["captions"].as_array().unwrap().is_empty());
}

#[test]
fn select_all_then_delete_removes_clips_and_captions_in_one_undo() {
    for cmd in ["edit.clear", "edit.rippleDelete"] {
        let mut s = demo("gt-caption-all-delete");
        s.execute("captions.add", json!({"text": "One", "seconds": 600, "durationSeconds": 2})).unwrap();
        let count = |s: &Session| {
            let q = s.active_sequence().unwrap();
            let clips: usize = q.all_tracks().map(|t| t.items.len()).sum();
            let caps: usize = q.caption_tracks.iter().map(|t| t.captions.len()).sum();
            (clips, caps)
        };
        let before = count(&s);
        assert!(before.0 > 0 && before.1 > 0);
        s.execute("edit.selectAll", json!({})).unwrap();
        s.execute(cmd, json!({})).unwrap();
        assert_eq!(count(&s), (0, 0), "{cmd}: one Delete takes clips and captions");
        s.execute("edit.undo", json!({})).unwrap();
        assert_eq!(count(&s), before, "{cmd}: one undo restores both");
    }
}

#[test]
fn select_all_then_delete_with_a_caption_inside_a_deleted_clip_and_a_locked_track() {
    for cmd in ["edit.clear", "edit.rippleDelete"] {
        // a caption lying inside the clips: the clip ripple takes it before the caption delete runs
        let mut s = demo("gt-caption-inside-delete");
        s.execute("captions.add", json!({"text": "Inside", "seconds": 1, "durationSeconds": 2})).unwrap();
        s.execute("edit.selectAll", json!({})).unwrap();
        assert!(!s.state.caption_selection.is_empty());
        s.execute(cmd, json!({})).unwrap();
        let q = s.active_sequence().unwrap();
        assert_eq!(q.all_tracks().map(|t| t.items.len()).sum::<usize>(), 0, "{cmd}: clips gone");
        assert_eq!(q.caption_tracks.iter().map(|t| t.captions.len()).sum::<usize>(), 0, "{cmd}: caption gone");
        assert!(s.state.caption_selection.is_empty());
        s.execute("edit.undo", json!({})).unwrap();
        assert!(s.active_sequence().unwrap().all_tracks().any(|t| !t.items.is_empty()), "{cmd}: one undo restores");
        // a caption on a locked track stays, and stays selected; the clips still go
        let mut s = demo("gt-caption-locked-delete");
        s.execute("captions.add", json!({"text": "Locked", "seconds": 600, "durationSeconds": 2})).unwrap();
        s.execute("captions.setTrack", json!({"track": "C1", "locked": true})).unwrap();
        s.execute("edit.selectAll", json!({})).unwrap();
        let caps = s.state.caption_selection.clone();
        s.execute(cmd, json!({})).unwrap();
        let q = s.active_sequence().unwrap();
        assert_eq!(q.all_tracks().map(|t| t.items.len()).sum::<usize>(), 0, "{cmd}: the clips go");
        assert_eq!(q.caption_tracks.iter().map(|t| t.captions.len()).sum::<usize>(), caps.len(), "{cmd}: the locked caption stays");
        assert_eq!(s.state.caption_selection, caps);
    }
}

#[test]
fn explicit_clip_deletion_keeps_selected_captions_and_rejects_invalid_targets() {
    for cmd in ["edit.clear", "edit.rippleDelete"] {
        let mut s = demo("gt-caption-explicit-delete");
        s.execute("captions.add", json!({"text": "Keep me", "seconds": 600, "durationSeconds": 2})).unwrap();
        s.execute("edit.selectAll", json!({})).unwrap();
        let captions = s.state.caption_selection.clone();
        let clips = s.state.selection.clone();
        let count = s.active_sequence().unwrap().caption_tracks.iter().map(|t| t.captions.len()).sum::<usize>();
        for target in [json!({"clip": null}), json!({"clips": []}), json!({"clips": [u64::MAX]})] {
            let before = s.project.clone();
            assert!(s.execute(cmd, target).is_err());
            assert!(std::sync::Arc::ptr_eq(&before, &s.project));
            assert_eq!(s.state.caption_selection, captions);
            assert_eq!(s.state.selection, clips);
        }
        s.execute(cmd, json!({"clips": clips.iter().map(|c| c.0).collect::<Vec<_>>()})).unwrap();
        assert_eq!(s.active_sequence().unwrap().caption_tracks.iter().map(|t| t.captions.len()).sum::<usize>(), count);
        assert_eq!(s.state.caption_selection, captions, "explicit clip targets leave captions selected");
    }
}

#[test]
fn caption_font_style_is_upgraded_listed_and_replaced() {
    let mut s = demo("gt-caption-font");
    s.execute("captions.add", json!({"text": "Styled", "seconds": 600, "durationSeconds": 2})).unwrap();
    s.execute("captions.setStyle", json!({"font": "Inter", "fontStyle": "Medium"})).unwrap();
    let used = s.execute("graphics.fonts.used", json!({})).unwrap().to_string();
    assert!(used.contains("Medium"), "{used}");
    // a style-specific replacement skips other styles and moves this one
    s.execute("file.replaceFonts", json!({"from": {"family": "Inter", "style": "Bold"}, "to": "Noto Serif"})).unwrap();
    assert_eq!(s.active_sequence().unwrap().caption_tracks[0].style.font, "Inter");
    // upgrading carries the style onto the graphic's text
    let r = s.execute("graphics.upgradeCaption", json!({})).unwrap();
    let it = item(&s, ClipId(r["clips"][0].as_u64().unwrap()));
    let e = it.effects.iter().find(|e| e.effect == "graphic_text").unwrap();
    assert_eq!(e.param("font_style").unwrap().value, ParamValue::Text("Medium".into()));
    s.execute("edit.undo", json!({})).unwrap();
    s.execute("file.replaceFonts", json!({"from": {"family": "Inter", "style": "Medium"}, "to": {"family": "Noto Serif", "style": "Bold"}})).unwrap();
    let st = &s.active_sequence().unwrap().caption_tracks[0].style;
    assert_eq!((st.font.as_str(), st.font_style.as_str()), ("Noto Serif", "Bold"));
}

#[test]
fn source_graphics_share_edits() {
    let mut s = demo("gt-source");
    let c = clip_of(&s.execute("graphics.newText", json!({"text": "Shared"})).unwrap());
    let r = s.execute("graphics.upgradeToSourceGraphic", json!({"clip": c.0})).unwrap();
    let item_id = r["item"].as_u64().unwrap();
    assert!(s.execute("graphics.upgradeToSourceGraphic", json!({"clip": c.0})).is_err());
    // a second instance from the project item
    let v1 = s.active_sequence().unwrap().find_item(c).unwrap().0;
    s.execute("timeline.place", json!({"item": item_id, "track": v1.0, "time": Tick::from_seconds_f64(700.0).0})).unwrap();
    let c2 = s.active_sequence().unwrap().video_tracks.iter().flat_map(|t| &t.items).find(|i| i.item.0 == item_id && i.id != c).unwrap().id;
    assert_eq!(texts(&s, c2), vec!["Shared"]);
    s.execute("graphics.setText", json!({"clip": c2.0, "text": "Edited once"})).unwrap();
    assert_eq!(texts(&s, c), vec!["Edited once"], "the other instance follows");
    s.execute("graphics.set", json!({"clip": c.0, "props": {"size": 60}})).unwrap();
    let size = |s: &Session, cl: ClipId| item(s, cl).effects.iter().find(|e| e.effect == "graphic_text").unwrap().params["size"].value.clone();
    assert_eq!(size(&s, c2), ParamValue::Float(60.0));
    s.execute("edit.undo", json!({})).unwrap();
    assert_eq!(size(&s, c2), size(&s, c), "undo restores both");
    assert_ne!(size(&s, c2), ParamValue::Float(60.0));
    // new plain graphics do not reuse the source graphic's item
    let c3 = clip_of(&s.execute("graphics.newText", json!({"text": "Plain", "seconds": 1, "time": Tick::from_seconds_f64(800.0).0})).unwrap());
    assert_ne!(item(&s, c3).item.0, item_id);
}

#[test]
fn replace_fonts_in_project() {
    let mut s = demo("gt-fonts");
    let c = clip_of(&s.execute("graphics.newText", json!({"text": "Serif", "font": "Noto Serif"})).unwrap());
    s.execute("graphics.setCharStyle", json!({"clip": c.0, "start": 0, "end": 2, "style": {"font": "Noto Serif"}})).unwrap();
    let used = s.execute("graphics.fonts.used", json!({})).unwrap();
    assert!(used.as_array().unwrap().iter().any(|f| f["family"] == "Noto Serif" && f["missing"] == false), "{used}");
    let r = s.execute("file.replaceFonts", json!({"from": "Noto Serif", "to": "JetBrains Mono"})).unwrap();
    assert_eq!(r["replaced"], 2);
    let used = s.execute("graphics.fonts.used", json!({})).unwrap();
    assert!(!used.as_array().unwrap().iter().any(|f| f["family"] == "Noto Serif"));
    s.execute("edit.undo", json!({})).unwrap();
    let used = s.execute("graphics.fonts.used", json!({})).unwrap();
    assert!(used.as_array().unwrap().iter().any(|f| f["family"] == "Noto Serif"));
}

#[test]
fn caption_formats_through_the_engine() {
    let mut s = demo("gt-capfmt");
    s.execute("captions.add", json!({"text": "One", "seconds": 1, "durationSeconds": 2})).unwrap();
    s.execute("captions.add", json!({"text": "Two\nlines", "seconds": 4, "durationSeconds": 1})).unwrap();
    let d = tmp_dir("gt-capfmt-files");
    let want = s.execute("captions.list", json!({})).unwrap()["tracks"][0]["captions"].clone();
    for ext in ["mcc", "stl", "ttml", "dfxp"] {
        let path = d.join(format!("caps.{ext}")).to_string_lossy().to_string();
        let r = s.execute("captions.export", json!({"path": path})).unwrap();
        assert_eq!(r["captions"], 2, "{ext}");
        let imp = s.execute("captions.import", json!({"path": path})).unwrap();
        assert_eq!(imp["captions"], 2, "{ext}: {imp}");
        let got = s.execute("captions.list", json!({"track": "C1"})).unwrap()["tracks"][0]["captions"].clone();
        for i in 0..2 {
            assert_eq!(got[i]["text"], want[i]["text"], "{ext}");
            assert_eq!(got[i]["in"], want[i]["in"], "{ext}");
            assert_eq!(got[i]["out"], want[i]["out"], "{ext}");
        }
    }
}

/// #188: a copied Graphic clip pastes at the playhead as a second Graphic clip.
#[test]
fn copy_paste_duplicates_a_graphic_clip() {
    let mut s = demo("gt-paste");
    let c = clip_of(&s.execute("graphics.newText", json!({"text": "Lyric", "seconds": 2})).unwrap());
    s.execute("timeline.select", json!({"clips": [c.0]})).unwrap();
    let v = s.execute("edit.copy", json!({})).unwrap();
    assert_eq!(v["copied"], 1, "{v}");
    s.execute("playhead.set", json!({"seconds": 620})).unwrap();
    s.execute("edit.paste", json!({})).unwrap();
    let q = s.active_sequence().unwrap();
    let pasted: Vec<&TrackItem> = q.all_tracks().flat_map(|t| t.items.iter()).filter(|i| i.id != c && i.item == item(&s, c).item).collect();
    assert_eq!(pasted.len(), 1, "one copy");
    assert!((pasted[0].start.seconds() - 620.0).abs() < 0.05, "at the playhead: {:?}", pasted[0].start);
    assert_eq!(texts(&s, pasted[0].id), vec!["Lyric".to_string()]);
}

#[test]
fn character_style_drag_merges_only_its_target_and_range() {
    let mut s = demo("gt-char-drag");
    let c = clip_of(&s.execute("graphics.newText", json!({"text":"Alpha Bravo"})).unwrap());
    let before = s.project.clone();
    let n = s.history.undo.len();
    for i in 0..8 {
        s.execute("graphics.setCharStyle", json!({"clip":c.0,"start":0,"end":5,"style":{"size":105+i*5},"merge":true,"begin":i==0})).unwrap();
    }
    assert_eq!(s.history.undo.len(), n + 1, "one drag, one undo step");
    let after = s.project.clone();
    s.execute("edit.undo", json!({})).unwrap();
    assert_eq!(*s.project, *before);
    s.execute("edit.redo", json!({})).unwrap();
    assert_eq!(*s.project, *after);
    s.execute("graphics.setCharStyle", json!({"clip":c.0,"start":0,"end":5,"style":{"size":150},"merge":true,"begin":true})).unwrap();
    assert_eq!(s.history.undo.len(), n + 2, "a new gesture on the same range starts a new step");
    s.execute("graphics.setCharStyle", json!({"clip":c.0,"start":6,"end":11,"style":{"size":160},"merge":true})).unwrap();
    assert_eq!(s.history.undo.len(), n + 3, "another character range does not merge");
    let before = s.project.clone();
    assert!(s.execute("graphics.setCharStyle", json!({"clip":c.0,"start":3,"end":3,"style":{"size":170},"merge":true})).is_err());
    assert_eq!(*s.project, *before);
    assert_eq!(s.history.undo.len(), n + 3, "an invalid edit adds no undo step");
}

#[test]
fn character_style_merge_preserves_default_calls_and_separate_clips() {
    let mut s = demo("gt-char-merge-targets");
    let a = clip_of(&s.execute("graphics.newText", json!({"text":"First", "newClip":true})).unwrap());
    let b = clip_of(&s.execute("graphics.newText", json!({"text":"Other", "newClip":true})).unwrap());
    assert_ne!(a, b);
    let n = s.history.undo.len();
    for c in [a, b] {
        s.execute("graphics.setCharStyle", json!({"clip":c.0,"start":0,"end":5,"style":{"size":120},"merge":true})).unwrap();
    }
    assert_eq!(s.history.undo.len(), n + 2, "distinct clips do not merge");
    for size in [130, 140] {
        s.execute("graphics.setCharStyle", json!({"clip":b.0,"style":{"size":size}})).unwrap();
    }
    assert_eq!(s.history.undo.len(), n + 4, "default calls retain separate undo steps");
    let added = s.execute("graphics.newText", json!({"clip":b.0,"text":"Layer"})).unwrap();
    assert_eq!(clip_of(&added), b);
    let n = s.history.undo.len();
    for layer in [0, 1] {
        s.execute("graphics.setCharStyle", json!({"clip":b.0,"layer":layer,"start":0,"end":5,"style":{"size":150},"merge":true})).unwrap();
    }
    assert_eq!(s.history.undo.len(), n + 2, "distinct layers do not merge");
}

/// Issue #3094: a gradient stop set as `#rrggbbaa` keeps its opacity.
#[test]
fn gradient_stop_keeps_its_alpha() {
    let mut s = demo("gt-gradient-alpha");
    let c = clip_of(&s.execute("graphics.newText", json!({"text": "G"})).unwrap());
    s.execute("graphics.set", json!({"clip": c.0, "props": {"gradient_start": "#ff000080", "gradient_end": "#0000ff"}})).unwrap();
    let it = item(&s, c);
    let col = |id: &str| it.effects.iter().find_map(|e| e.params.get(id)).and_then(|p| p.value.as_color());
    let start = col("gradient_start").unwrap();
    assert!((start[3] - 128.0 / 255.0).abs() < 1e-3, "{start:?}");
    assert_eq!(col("gradient_end").unwrap()[3], 1.0);
}

/// Remove works on library entries only, and a key that names two of them removes neither.
#[test]
fn remove_picks_one_library_file_and_never_a_path_outside_it() {
    let mut s = demo("gt-remove");
    let c = clip_of(&s.execute("graphics.newText", json!({"text": "Headline", "position": [200, 900]})).unwrap());
    let r =
        s.execute("graphics.template.export", json!({"clip": c.0, "name": "Dup", "controls": [{"layer": 0, "param": "text", "name": "Headline"}]})).unwrap();
    let path = std::path::PathBuf::from(r["path"].as_str().unwrap());
    let twin = path.with_file_name("twin.fcgt");
    std::fs::copy(&path, &twin).unwrap();
    // two files claim the id: the id removes neither
    let e = s.execute("graphics.template.remove", json!({"template": "user:dup"})).unwrap_err().to_string();
    assert!(e.contains("more than one"), "{e}");
    assert!(path.exists() && twin.exists());
    // a path outside the library is not a library template
    let outside = tmp_dir("gt-remove-outside").join("elsewhere.fcgt");
    std::fs::copy(&path, &outside).unwrap();
    assert!(s.execute("graphics.template.remove", json!({"template": outside.to_string_lossy()})).is_err());
    assert!(outside.exists());
    // a library file is removed by its own path
    s.execute("graphics.template.remove", json!({"template": twin.to_string_lossy()})).unwrap();
    assert!(!twin.exists() && path.exists());
    s.execute("graphics.template.remove", json!({"template": "user:dup"})).unwrap();
    assert!(!path.exists());
}