Skip to content

AI media harness for planning, generating, and editing locally

TypeScript
13.2 KB
Raw Blame
use super::batch::execute_commands;
use super::*;

#[test]
fn clip_speed_round_trips_and_validates() {
    let mut config = fixture();
    config.timeline.tracks[0].clips[0].playback_rate = Some(0.5);
    let json = serde_json::to_value(&config).unwrap();
    assert_eq!(json["timeline"]["tracks"][0]["clips"][0]["playbackRate"], 0.5);
    let restored: ProjectConfig = serde_json::from_value(json).unwrap();
    assert_eq!(validate_and_normalize_config(restored).unwrap().timeline.tracks[0].clips[0].playback_rate, Some(0.5));
    config.timeline.tracks[0].clips[0].playback_rate = Some(0.0);
    assert!(validate_and_normalize_config(config).unwrap_err().contains("playback rate"));
}

#[test]
fn scene_creation_uses_injected_model_defaults() {
    let current = fixture();
    let batch = parse_jsonl_commands(
        r#"{"op":"scene.add","id":"future-scene","title":"Future model","seconds":7}
{"op":"shot.add","scene":"future-scene","id":"future-shot","at":0,"action":"A bird lands."}
{"op":"commit","summary":"Create a scene"}"#,
    )
    .unwrap();
    let next = super::batch::execute_with_defaults(
        &current,
        &batch.commands,
        &current.updated_at,
        crate::generation::models::SceneDefaults {
            provider_id: "future-model",
            duration_seconds: 7.0,
        },
    )
    .unwrap();
    let scene = next
        .generation_jobs
        .iter()
        .find(|scene| scene.id == "future-scene")
        .unwrap();
    assert_eq!(scene.provider_id.as_deref(), Some("future-model"));
    assert_eq!(scene.prompt, "A bird lands.");
    assert_eq!(current, fixture());
}
use super::clips::find_clip_location;
use crate::project::validation::validate_and_normalize_config;
use crate::project::*;

fn fixture() -> ProjectConfig {
    serde_json::from_str(include_str!(
        "../../../../fixtures/project-v1-complete.json"
    ))
    .unwrap()
}

#[test]
fn joined_video_clip_commands_use_scene_relative_lengths_and_trims() {
    let mut config = fixture();
    let mut asset = config.assets[0].clone();
    asset.id = "joined-video".into();
    asset.kind = "generated".into();
    asset.duration_ms = Some(10125);
    asset.scene_segments = Some(vec![SceneMediaSegment { scene_id: "continued".into(),
        latent_relative_path: "latents/continued.safetensors".into(), start_frame: 124, frame_count: 119 }]);
    config.assets.push(asset);
    let command: ProjectCommand = serde_json::from_value(serde_json::json!({ "op": "clip.add", "id": "joined-clip", "asset": "joined-video" })).unwrap();
    let next = execute_commands_at(&config, &[command], "2026-10-03T00:00:00.000Z").unwrap();
    let clip = next.timeline.tracks.iter().flat_map(|track| &track.clips).find(|clip| clip.id == "joined-clip").unwrap();
    assert_eq!(clip.source_start_ms, 0);
    assert_eq!(clip.duration_ms, 4958);
    let invalid: ProjectCommand = serde_json::from_value(serde_json::json!({ "op": "clip.set", "id": "joined-clip", "seconds": 6 })).unwrap();
    assert!(execute_commands_at(&next, &[invalid], "2026-10-03T00:00:00.000Z").is_err());
}

#[test]
fn parses_jsonl_and_preserves_explicit_null_patches() {
    let batch = parse_jsonl_commands(
        r#"```jsonl
{"op":"scene.set","id":"job-01","sound":null}
{"op":"commit","summary":"Removed the sound design."}
```"#,
    )
    .unwrap();
    assert_eq!(batch.summary, "Removed the sound design.");
    assert!(matches!(
        &batch.commands[0],
        ProjectCommand::SceneSet {
            sound: super::patch::Patch::Clear,
            ..
        }
    ));
}

#[test]
fn scene_frame_commands_accept_continuity_and_clear_the_last_frame() {
    let config = fixture();
    let id = &config.generation_jobs[0].id;
    let image = &config
        .references
        .iter()
        .find(|reference| reference.kind == "image")
        .unwrap()
        .id;
    let command: ProjectCommand = serde_json::from_value(serde_json::json!({
        "op": "scene.set", "id": id, "endFrame": image, "usePreviousSceneLastFrame": true
    }))
    .unwrap();
    let updated = execute_commands_at(&config, &[command], "2026-09-12T00:00:00.000Z").unwrap();
    assert_eq!(
        updated.generation_jobs[0].end_frame_reference_id.as_deref(),
        Some(image.as_str())
    );
    assert_eq!(
        updated.generation_jobs[0].scene_type.as_deref(),
        Some("continue")
    );
    let clear: ProjectCommand = serde_json::from_value(serde_json::json!({
        "op": "scene.set", "id": id, "endFrame": null, "startFrame": image
    }))
    .unwrap();
    let updated = execute_commands_at(&updated, &[clear], "2026-09-12T00:00:00.000Z").unwrap();
    assert!(updated.generation_jobs[0].end_frame_reference_id.is_none());
    assert_ne!(
        updated.generation_jobs[0].use_previous_scene_last_frame,
        Some(true)
    );
    assert_eq!(
        updated.generation_jobs[0]
            .start_frame_reference_id
            .as_deref(),
        Some(image.as_str())
    );
}

#[test]
fn requires_a_final_non_empty_commit() {
    assert!(
        parse_jsonl_commands(r#"{"op":"scene.remove","id":"job-01"}"#)
            .unwrap_err()
            .contains("must end")
    );
    assert!(parse_jsonl_commands(r#"{"op":"commit","summary":""}"#)
        .unwrap_err()
        .contains("summary"));
}

#[test]
fn executes_reference_scene_and_shot_commands_as_one_batch() {
    let commands = parse_jsonl_commands(
        r#"{"op":"ref.add","id":"ref-mara","name":"Mara","text":"A woman in her thirties with cropped black hair, a rust wool jacket, charcoal trousers, and black leather boots.","use":["character"]}
{"op":"scene.add","id":"scene-opening","title":"Workshop arrival","seconds":8}
{"op":"shot.add","scene":"scene-opening","id":"shot-wide","at":0,"action":"@[ref:ref-mara] opens the workshop door and stops beside the workbench."}
{"op":"clip.add","id":"clip-workshop","scene":"scene-opening"}
{"op":"commit","summary":"Added Mara and the opening scene to the timeline."}"#,
    )
    .unwrap()
    .commands;
    let next = execute_commands(&fixture(), &commands).unwrap();
    let scene = next
        .generation_jobs
        .iter()
        .find(|job| job.id == "scene-opening")
        .unwrap();
    assert_eq!(scene.prompt, scene.creative_brief);
    assert_eq!(scene.reference_ids, ["ref-mara"]);
    assert_eq!(scene.shots.as_ref().unwrap()[0].id, "shot-wide");
    assert_eq!(scene.status, "draft");
    assert!(scene.output_relative_path.is_none());
    let clip = next
        .timeline
        .tracks
        .iter()
        .flat_map(|track| &track.clips)
        .find(|clip| clip.id == "clip-workshop")
        .unwrap();
    assert_eq!(
        clip.start_ms, 8_000,
        "default placement appends after the existing clip"
    );
    assert_eq!(clip.duration_ms, 8_000);
    assert_eq!(clip.asset_id, "asset-scene-opening");
    assert_eq!(clip.status, "draft");
    assert!(next
        .assets
        .iter()
        .any(|asset| asset.id == "asset-scene-opening"));
}

#[test]
fn timeline_commands_support_explicit_arrangements_and_removal() {
    let current = validate_and_normalize_config(fixture()).unwrap();
    let commands = parse_jsonl_commands(
        r#"{"op":"clip.add","id":"clip-macro-second-layer","asset":"asset-imported","track":"track-v2","at":2,"seconds":3,"sourceAt":1,"label":"Macro insert"}
{"op":"clip.set","id":"clip-macro-second-layer","track":"track-v3","at":4}
{"op":"commit","summary":"Placed and moved the macro insert."}"#,
    )
    .unwrap()
    .commands;
    let arranged = execute_commands(&current, &commands).unwrap();
    let clip = arranged.timeline.tracks[2]
        .clips
        .iter()
        .find(|clip| clip.id == "clip-macro-second-layer")
        .unwrap();
    assert_eq!(clip.start_ms, 4_000);
    assert_eq!(clip.duration_ms, 3_000);
    assert_eq!(clip.source_start_ms, 1_000);
    assert_eq!(clip.label, "Macro insert");

    let clip_id = clip.id.clone();
    let removed =
        execute_commands(&arranged, &[ProjectCommand::ClipRemove { id: clip_id }]).unwrap();
    assert!(find_clip_location(&removed, "clip-macro-second-layer").is_none());
}

#[test]
fn a_failed_command_does_not_mutate_the_input() {
    let current = fixture();
    let original = current.clone();
    let commands = vec![
        ProjectCommand::ProjectSet {
            name: Some("Changed".into()),
            prompt: None,
            target_seconds: None,
            aspect_ratio: None,
            resolution: None,
            frame_rate: None,
            background_color: None,
        },
        ProjectCommand::SceneRemove {
            id: "missing-scene".into(),
        },
    ];
    let error = execute_commands(&current, &commands).unwrap_err();
    assert!(error.contains("Command 2 (scene.remove)"));
    assert_eq!(current, original);
}

#[test]
fn project_video_settings_update_both_mirrors_without_retiming_media() {
    let current = validate_and_normalize_config(fixture()).unwrap();
    let commands = parse_jsonl_commands(
        r#"{"op":"project.set","aspectRatio":"9:16","resolution":"544p","targetSeconds":900}
{"op":"commit","summary":"Changed the project video settings."}"#,
    )
    .unwrap();
    let next = execute_commands(&current, &commands.commands).unwrap();
    assert_eq!(next.settings.aspect_ratio, "9:16");
    assert_eq!(next.settings.resolution, "544p");
    assert_eq!(next.brief.aspect_ratio, "9:16");
    assert_eq!(next.brief.resolution, "544p");
    assert_eq!(next.brief.target_duration_seconds, 900);
    assert_eq!(next.timeline, current.timeline);
    assert_eq!(next.assets, current.assets);
    for patch in [
        r#""resolution":"8k""#,
        r#""aspectRatio":"21:9""#,
    ] {
        let command: ProjectCommand =
            serde_json::from_str(&format!(r#"{{"op":"project.set",{patch}}}"#)).unwrap();
        assert!(execute_commands(&current, &[command]).is_err());
    }
}

#[test]
fn a_small_scene_patch_preserves_render_owned_fields() {
    let current = fixture();
    let before = current
        .generation_jobs
        .iter()
        .find(|job| job.id == "job-opener")
        .unwrap()
        .clone();
    let commands = parse_jsonl_commands(
        r#"{"op":"scene.set","id":"job-opener","title":"Warmer opener"}
{"op":"commit","summary":"Renamed the opener."}"#,
    )
    .unwrap()
    .commands;
    let next = execute_commands_at(&current, &commands, "2026-09-02T20:00:00.000Z").unwrap();
    let after = next
        .generation_jobs
        .iter()
        .find(|job| job.id == "job-opener")
        .unwrap();
    assert_eq!(after.title, "Warmer opener");
    assert_eq!(after.status, before.status);
    assert_eq!(after.output_relative_path, before.output_relative_path);
    assert_eq!(after.generation_snapshot, before.generation_snapshot);
    assert_eq!(after.updated_at, "2026-09-02T20:00:00.000Z");
}

#[test]
fn commands_cannot_deserialize_protected_scene_fields() {
    let error = serde_json::from_str::<ProjectCommand>(
        r#"{"op":"scene.set","id":"job-01","outputRelativePath":"outside.mp4"}"#,
    )
    .unwrap_err();
    assert!(error.to_string().contains("unknown field"), "{error}");
}

#[test]
fn motions_are_agent_editable_inline_assets_with_same_batch_clip_metadata() {
    let current = fixture();
    let motion = serde_json::json!({ "version":1, "space":"2d", "width":320, "height":180, "duration":3,
        "source":"export function update({time,nodes}) {}", "nodes":[{"id":"title","name":"Title","kind":"text","text":"Hello"}],
        "parameters":[], "overrides":{"title":{"offset":[12,24,0]}} });
    let commands: Vec<ProjectCommand> = serde_json::from_value(serde_json::json!([
        {"op":"motion.add", "id":"motion-title", "name":"Title", "motion":motion},
        {"op":"clip.add", "id":"motion-clip", "asset":"motion-title"}
    ])).unwrap();
    let next = execute_commands_at(&current, &commands, "2026-10-25T00:00:00.000Z").unwrap();
    let asset = next.assets.iter().find(|a| a.id == "motion-title").unwrap();
    assert_eq!(asset.motion.as_ref(), Some(&motion));
    assert_eq!(asset.duration_ms, Some(3000));
    assert_eq!(asset.width, Some(320));
    let clip = next.timeline.tracks.iter().flat_map(|t| &t.clips).find(|c| c.id == "motion-clip").unwrap();
    assert_eq!(clip.duration_ms, 3000);
    let restored: ProjectConfig = serde_json::from_value(serde_json::to_value(&next).unwrap()).unwrap();
    let restored = validate_and_normalize_config(restored).unwrap();
    assert_eq!(restored.assets, next.assets);
    assert_eq!(restored.timeline, next.timeline);
    let mut longer = motion.clone(); longer["duration"] = serde_json::json!(7);
    let commands: Vec<ProjectCommand> = serde_json::from_value(serde_json::json!([
        {"op":"motion.set", "id":"motion-title", "motion":longer},
        {"op":"clip.add", "id":"motion-longer", "asset":"motion-title"}
    ])).unwrap();
    let edited = execute_commands_at(&next, &commands, "2026-10-25T00:00:00.000Z").unwrap();
    assert_eq!(edited.timeline.tracks.iter().flat_map(|t| &t.clips).find(|c| c.id == "motion-longer").unwrap().duration_ms, 7000);
    let mut invalid = motion.clone(); invalid["overrides"]["missing"] = serde_json::json!({});
    let bad: ProjectCommand = serde_json::from_value(serde_json::json!({"op":"motion.set","id":"motion-title","motion":invalid})).unwrap();
    assert!(execute_commands_at(&next, &[bad], "2026-10-25T00:00:00.000Z").is_err());
    assert_eq!(current, fixture());
}