This commit is contained in:
Skyler Lehmkuhl
2025-11-29 13:39:31 -05:00
parent f9761b8af3
commit 8f830b7799
19 changed files with 2291 additions and 69 deletions
@@ -0,0 +1,338 @@
//! Integration tests for clip workflow operations
//!
//! Tests end-to-end clip operations including creation, transformation,
//! timeline placement, and undo/redo.
use lightningbeam_core::action::Action;
use lightningbeam_core::actions::{
MoveClipInstancesAction, TransformClipInstancesAction, TrimClipInstancesAction,
TrimData, TrimType,
};
use lightningbeam_core::clip::{ClipInstance, VectorClip};
use lightningbeam_core::document::Document;
use lightningbeam_core::layer::{AnyLayer, VectorLayer};
use lightningbeam_core::object::Transform;
use std::collections::HashMap;
use uuid::Uuid;
/// Create a test document with a vector layer containing a clip instance
fn setup_test_document() -> (Document, Uuid, Uuid, Uuid) {
let mut document = Document::new("Test Project");
// Create a vector clip
let vector_clip = VectorClip::new("Test Clip", 10.0, 1920.0, 1080.0);
let clip_id = vector_clip.id;
document.vector_clips.insert(clip_id, vector_clip);
// Create a vector layer with a clip instance
let mut layer = VectorLayer::new("Layer 1");
let mut clip_instance = ClipInstance::new(clip_id);
clip_instance.timeline_start = 0.0;
clip_instance.transform = Transform::with_position(100.0, 100.0);
let instance_id = clip_instance.id;
layer.clip_instances.push(clip_instance);
let layer_id = document.root.add_child(AnyLayer::Vector(layer));
(document, layer_id, clip_id, instance_id)
}
#[test]
fn test_clip_instance_creation_workflow() {
let (document, layer_id, clip_id, instance_id) = setup_test_document();
// Verify clip is in document
assert!(document.vector_clips.contains_key(&clip_id));
// Verify clip instance is on layer
if let Some(AnyLayer::Vector(layer)) = document.get_layer(&layer_id) {
let instance = layer
.clip_instances
.iter()
.find(|ci| ci.id == instance_id);
assert!(instance.is_some());
let instance = instance.unwrap();
assert_eq!(instance.clip_id, clip_id);
assert_eq!(instance.timeline_start, 0.0);
assert_eq!(instance.transform.x, 100.0);
assert_eq!(instance.transform.y, 100.0);
} else {
panic!("Layer not found");
}
}
#[test]
fn test_move_clip_instance_workflow() {
let (mut document, layer_id, _clip_id, instance_id) = setup_test_document();
// Create move action: move from 0.0 to 5.0 seconds
let mut layer_moves = HashMap::new();
layer_moves.insert(layer_id, vec![(instance_id, 0.0, 5.0)]);
let mut action = MoveClipInstancesAction::new(layer_moves);
// Execute
action.execute(&mut document);
// Verify position changed
if let Some(AnyLayer::Vector(layer)) = document.get_layer(&layer_id) {
let instance = layer
.clip_instances
.iter()
.find(|ci| ci.id == instance_id)
.unwrap();
assert_eq!(instance.timeline_start, 5.0);
}
// Rollback (undo)
action.rollback(&mut document);
// Verify position restored
if let Some(AnyLayer::Vector(layer)) = document.get_layer(&layer_id) {
let instance = layer
.clip_instances
.iter()
.find(|ci| ci.id == instance_id)
.unwrap();
assert_eq!(instance.timeline_start, 0.0);
}
// Re-execute (redo)
action.execute(&mut document);
// Verify position changed again
if let Some(AnyLayer::Vector(layer)) = document.get_layer(&layer_id) {
let instance = layer
.clip_instances
.iter()
.find(|ci| ci.id == instance_id)
.unwrap();
assert_eq!(instance.timeline_start, 5.0);
}
}
#[test]
fn test_transform_clip_instance_workflow() {
let (mut document, layer_id, _clip_id, instance_id) = setup_test_document();
// Create transform action: move, rotate, scale
let old_transform = Transform::with_position(100.0, 100.0);
let mut new_transform = Transform::with_position(200.0, 150.0);
new_transform.rotation = 45.0;
new_transform.scale_x = 1.5;
new_transform.scale_y = 1.5;
let mut transforms = HashMap::new();
transforms.insert(instance_id, (old_transform, new_transform));
let mut action = TransformClipInstancesAction::new(layer_id, transforms);
// Execute
action.execute(&mut document);
// Verify transform changed
if let Some(AnyLayer::Vector(layer)) = document.get_layer_mut(&layer_id) {
let instance = layer
.clip_instances
.iter()
.find(|ci| ci.id == instance_id)
.unwrap();
assert_eq!(instance.transform.x, 200.0);
assert_eq!(instance.transform.y, 150.0);
assert_eq!(instance.transform.rotation, 45.0);
assert_eq!(instance.transform.scale_x, 1.5);
assert_eq!(instance.transform.scale_y, 1.5);
}
// Rollback
action.rollback(&mut document);
// Verify transform restored
if let Some(AnyLayer::Vector(layer)) = document.get_layer_mut(&layer_id) {
let instance = layer
.clip_instances
.iter()
.find(|ci| ci.id == instance_id)
.unwrap();
assert_eq!(instance.transform.x, 100.0);
assert_eq!(instance.transform.y, 100.0);
assert_eq!(instance.transform.rotation, 0.0);
assert_eq!(instance.transform.scale_x, 1.0);
}
}
#[test]
fn test_trim_clip_instance_workflow() {
let (mut document, layer_id, _clip_id, instance_id) = setup_test_document();
// Create trim action: trim 2 seconds from left
let mut layer_trims = HashMap::new();
layer_trims.insert(
layer_id,
vec![(
instance_id,
TrimType::TrimLeft,
TrimData::left(0.0, 0.0),
TrimData::left(2.0, 2.0),
)],
);
let mut action = TrimClipInstancesAction::new(layer_trims);
// Execute
action.execute(&mut document);
// Verify trim applied
if let Some(AnyLayer::Vector(layer)) = document.get_layer(&layer_id) {
let instance = layer
.clip_instances
.iter()
.find(|ci| ci.id == instance_id)
.unwrap();
assert_eq!(instance.trim_start, 2.0);
assert_eq!(instance.timeline_start, 2.0);
}
// Rollback
action.rollback(&mut document);
// Verify trim restored
if let Some(AnyLayer::Vector(layer)) = document.get_layer(&layer_id) {
let instance = layer
.clip_instances
.iter()
.find(|ci| ci.id == instance_id)
.unwrap();
assert_eq!(instance.trim_start, 0.0);
assert_eq!(instance.timeline_start, 0.0);
}
}
#[test]
fn test_multiple_clip_instances_workflow() {
let mut document = Document::new("Test Project");
// Create a vector clip
let vector_clip = VectorClip::new("Test Clip", 10.0, 1920.0, 1080.0);
let clip_id = vector_clip.id;
document.vector_clips.insert(clip_id, vector_clip);
// Create layer with multiple clip instances
let mut layer = VectorLayer::new("Layer 1");
let mut instance1 = ClipInstance::new(clip_id);
instance1.timeline_start = 0.0;
let id1 = instance1.id;
let mut instance2 = ClipInstance::new(clip_id);
instance2.timeline_start = 5.0;
let id2 = instance2.id;
let mut instance3 = ClipInstance::new(clip_id);
instance3.timeline_start = 10.0;
let id3 = instance3.id;
layer.clip_instances.push(instance1);
layer.clip_instances.push(instance2);
layer.clip_instances.push(instance3);
let layer_id = document.root.add_child(AnyLayer::Vector(layer));
// Move all three instances
let mut layer_moves = HashMap::new();
layer_moves.insert(
layer_id,
vec![
(id1, 0.0, 1.0),
(id2, 5.0, 6.0),
(id3, 10.0, 11.0),
],
);
let mut action = MoveClipInstancesAction::new(layer_moves);
action.execute(&mut document);
// Verify all moved
if let Some(AnyLayer::Vector(layer)) = document.get_layer(&layer_id) {
assert_eq!(
layer.clip_instances.iter().find(|ci| ci.id == id1).unwrap().timeline_start,
1.0
);
assert_eq!(
layer.clip_instances.iter().find(|ci| ci.id == id2).unwrap().timeline_start,
6.0
);
assert_eq!(
layer.clip_instances.iter().find(|ci| ci.id == id3).unwrap().timeline_start,
11.0
);
}
// Rollback all
action.rollback(&mut document);
// Verify all restored
if let Some(AnyLayer::Vector(layer)) = document.get_layer(&layer_id) {
assert_eq!(
layer.clip_instances.iter().find(|ci| ci.id == id1).unwrap().timeline_start,
0.0
);
assert_eq!(
layer.clip_instances.iter().find(|ci| ci.id == id2).unwrap().timeline_start,
5.0
);
assert_eq!(
layer.clip_instances.iter().find(|ci| ci.id == id3).unwrap().timeline_start,
10.0
);
}
}
#[test]
fn test_clip_time_remapping() {
let mut document = Document::new("Test Project");
// Create a 10 second clip
let vector_clip = VectorClip::new("Test Clip", 10.0, 1920.0, 1080.0);
let clip_id = vector_clip.id;
let clip_duration = vector_clip.duration;
document.vector_clips.insert(clip_id, vector_clip);
// Create instance at timeline 5.0 with trim_start of 2.0
let mut layer = VectorLayer::new("Layer 1");
let mut instance = ClipInstance::new(clip_id);
instance.timeline_start = 5.0;
instance.trim_start = 2.0;
instance.trim_end = Some(8.0); // Clip plays from 2.0 to 8.0 internal time
layer.clip_instances.push(instance.clone());
document.root.add_child(AnyLayer::Vector(layer));
// Test time remapping
// At timeline time 5.0, clip internal time should be 2.0 (trim_start)
let clip_time = instance.remap_time(5.0, clip_duration);
assert_eq!(clip_time, Some(2.0));
// At timeline time 6.0, clip internal time should be 3.0
let clip_time = instance.remap_time(6.0, clip_duration);
assert_eq!(clip_time, Some(3.0));
// At timeline time 10.999, clip internal time should be just under 8.0
// The clip plays from timeline 5.0 to 11.0 (exclusive end)
// At timeline 10.999: relative_time = 5.999, content_time = 5.999
// Since content_window = 6.0, we get: trim_start + 5.999 = 7.999
let clip_time = instance.remap_time(10.999, clip_duration);
assert!(clip_time.is_some());
let time = clip_time.unwrap();
assert!(time > 7.9 && time < 8.0, "Expected ~7.999, got {}", time);
// At timeline time 11.0 (exact end), clip should be past its end (None)
// because the range is [timeline_start, timeline_start + effective_duration)
let clip_time = instance.remap_time(11.0, clip_duration);
assert_eq!(clip_time, None);
// At timeline time 4.0, clip hasn't started yet (None)
let clip_time = instance.remap_time(4.0, clip_duration);
assert_eq!(clip_time, None);
}
@@ -0,0 +1,241 @@
//! Integration tests for layer property operations
//!
//! Tests solo, mute, lock, opacity, and visibility interactions.
use lightningbeam_core::action::Action;
use lightningbeam_core::actions::{LayerProperty, SetLayerPropertiesAction};
use lightningbeam_core::document::Document;
use lightningbeam_core::layer::{AnyLayer, LayerTrait, VectorLayer};
/// Create a test document with multiple layers
fn setup_multi_layer_document() -> (Document, Vec<uuid::Uuid>) {
let mut document = Document::new("Test Project");
let layer1 = VectorLayer::new("Layer 1");
let layer2 = VectorLayer::new("Layer 2");
let layer3 = VectorLayer::new("Layer 3");
let id1 = document.root.add_child(AnyLayer::Vector(layer1));
let id2 = document.root.add_child(AnyLayer::Vector(layer2));
let id3 = document.root.add_child(AnyLayer::Vector(layer3));
(document, vec![id1, id2, id3])
}
#[test]
fn test_solo_interaction_single_layer() {
let (mut document, ids) = setup_multi_layer_document();
let id1 = ids[0];
// Solo layer 1
let mut action = SetLayerPropertiesAction::new(id1, LayerProperty::Soloed(true));
action.execute(&mut document);
// Verify layer 1 is soloed, others are not
assert_eq!(document.root.get_child(&ids[0]).unwrap().soloed(), true);
assert_eq!(document.root.get_child(&ids[1]).unwrap().soloed(), false);
assert_eq!(document.root.get_child(&ids[2]).unwrap().soloed(), false);
// Only layer 1 should be "effectively visible" for rendering
let any_soloed = document.visible_layers().any(|l| l.soloed());
assert!(any_soloed);
// Unsolo
action.rollback(&mut document);
assert_eq!(document.root.get_child(&ids[0]).unwrap().soloed(), false);
}
#[test]
fn test_solo_interaction_multiple_layers() {
let (mut document, ids) = setup_multi_layer_document();
// Solo layers 1 and 2
let mut action = SetLayerPropertiesAction::new_batch(
vec![ids[0], ids[1]],
LayerProperty::Soloed(true),
);
action.execute(&mut document);
// Verify layers 1 and 2 are soloed
assert_eq!(document.root.get_child(&ids[0]).unwrap().soloed(), true);
assert_eq!(document.root.get_child(&ids[1]).unwrap().soloed(), true);
assert_eq!(document.root.get_child(&ids[2]).unwrap().soloed(), false);
// Unsolo both
action.rollback(&mut document);
assert_eq!(document.root.get_child(&ids[0]).unwrap().soloed(), false);
assert_eq!(document.root.get_child(&ids[1]).unwrap().soloed(), false);
}
#[test]
fn test_mute_and_volume_interaction() {
let (mut document, ids) = setup_multi_layer_document();
let id1 = ids[0];
// Set volume to 0.5
let mut vol_action = SetLayerPropertiesAction::new(id1, LayerProperty::Volume(0.5));
vol_action.execute(&mut document);
assert_eq!(document.root.get_child(&id1).unwrap().volume(), 0.5);
// Mute the layer
let mut mute_action = SetLayerPropertiesAction::new(id1, LayerProperty::Muted(true));
mute_action.execute(&mut document);
// Layer is muted but volume is still 0.5
assert_eq!(document.root.get_child(&id1).unwrap().muted(), true);
assert_eq!(document.root.get_child(&id1).unwrap().volume(), 0.5);
// Unmute
mute_action.rollback(&mut document);
// Volume should still be 0.5
assert_eq!(document.root.get_child(&id1).unwrap().muted(), false);
assert_eq!(document.root.get_child(&id1).unwrap().volume(), 0.5);
}
#[test]
fn test_lock_prevents_conceptual_editing() {
let (mut document, ids) = setup_multi_layer_document();
let id1 = ids[0];
// Lock layer 1
let mut action = SetLayerPropertiesAction::new(id1, LayerProperty::Locked(true));
action.execute(&mut document);
assert_eq!(document.root.get_child(&id1).unwrap().locked(), true);
// Note: The lock state is a flag that UI should check before allowing edits
// The core library doesn't enforce this - it's the UI's responsibility
// Unlock
action.rollback(&mut document);
assert_eq!(document.root.get_child(&id1).unwrap().locked(), false);
}
#[test]
fn test_opacity_cascading() {
let (mut document, ids) = setup_multi_layer_document();
let id1 = ids[0];
// Set opacity to 0.5
let mut action = SetLayerPropertiesAction::new(id1, LayerProperty::Opacity(0.5));
action.execute(&mut document);
assert_eq!(document.root.get_child(&id1).unwrap().opacity(), 0.5);
// Set to 0.0 (fully transparent)
let mut action2 = SetLayerPropertiesAction::new(id1, LayerProperty::Opacity(0.0));
action2.execute(&mut document);
assert_eq!(document.root.get_child(&id1).unwrap().opacity(), 0.0);
// Rollback to 0.5
action2.rollback(&mut document);
assert_eq!(document.root.get_child(&id1).unwrap().opacity(), 0.5);
// Rollback to 1.0
action.rollback(&mut document);
assert_eq!(document.root.get_child(&id1).unwrap().opacity(), 1.0);
}
#[test]
fn test_visibility_and_solo_interaction() {
let (mut document, ids) = setup_multi_layer_document();
// Hide layer 1
let mut hide_action = SetLayerPropertiesAction::new(ids[0], LayerProperty::Visible(false));
hide_action.execute(&mut document);
// Solo layer 1 (while hidden)
let mut solo_action = SetLayerPropertiesAction::new(ids[0], LayerProperty::Soloed(true));
solo_action.execute(&mut document);
// Layer 1 is hidden and soloed
assert_eq!(document.root.get_child(&ids[0]).unwrap().visible(), false);
assert_eq!(document.root.get_child(&ids[0]).unwrap().soloed(), true);
// visible_layers() should NOT include hidden layers
let visible_count = document.visible_layers().count();
assert_eq!(visible_count, 2); // Only layers 2 and 3
// Check if any visible layer is soloed (should be false since layer 1 is hidden)
let any_visible_soloed = document.visible_layers().any(|l| l.soloed());
assert_eq!(any_visible_soloed, false);
}
#[test]
fn test_batch_property_changes() {
let (mut document, ids) = setup_multi_layer_document();
// Lock all layers
let mut lock_action = SetLayerPropertiesAction::new_batch(
ids.clone(),
LayerProperty::Locked(true),
);
lock_action.execute(&mut document);
for id in &ids {
assert_eq!(document.root.get_child(id).unwrap().locked(), true);
}
// Set opacity on all layers
let mut opacity_action = SetLayerPropertiesAction::new_batch(
ids.clone(),
LayerProperty::Opacity(0.75),
);
opacity_action.execute(&mut document);
for id in &ids {
assert_eq!(document.root.get_child(id).unwrap().opacity(), 0.75);
}
// Rollback opacity
opacity_action.rollback(&mut document);
for id in &ids {
assert_eq!(document.root.get_child(id).unwrap().opacity(), 1.0);
}
// Layers should still be locked
for id in &ids {
assert_eq!(document.root.get_child(id).unwrap().locked(), true);
}
}
#[test]
fn test_property_undo_redo_sequence() {
let (mut document, ids) = setup_multi_layer_document();
let id1 = ids[0];
// Sequence of changes
let mut actions: Vec<SetLayerPropertiesAction> = vec![
SetLayerPropertiesAction::new(id1, LayerProperty::Opacity(0.8)),
SetLayerPropertiesAction::new(id1, LayerProperty::Locked(true)),
SetLayerPropertiesAction::new(id1, LayerProperty::Muted(true)),
];
// Execute all
for action in &mut actions {
action.execute(&mut document);
}
// Verify final state
let layer = document.root.get_child(&id1).unwrap();
assert_eq!(layer.opacity(), 0.8);
assert_eq!(layer.locked(), true);
assert_eq!(layer.muted(), true);
// Undo in reverse order
for action in actions.iter_mut().rev() {
action.rollback(&mut document);
}
// Verify initial state
let layer = document.root.get_child(&id1).unwrap();
assert_eq!(layer.opacity(), 1.0);
assert_eq!(layer.locked(), false);
assert_eq!(layer.muted(), false);
}
@@ -0,0 +1,294 @@
//! Integration tests for rendering scenarios
//!
//! Tests complex rendering scenarios including solo, mute, opacity cascading,
//! and clip instance rendering.
use lightningbeam_core::clip::{ClipInstance, VectorClip};
use lightningbeam_core::document::Document;
use lightningbeam_core::layer::{AnyLayer, LayerTrait, VectorLayer};
use lightningbeam_core::object::ShapeInstance;
use lightningbeam_core::renderer::{render_document, render_document_with_transform};
use lightningbeam_core::shape::{Shape, ShapeColor};
use vello::kurbo::{Affine, Circle, Shape as KurboShape};
use vello::Scene;
/// Create a test document with multiple layers containing shapes
fn setup_rendering_document() -> (Document, Vec<uuid::Uuid>) {
let mut document = Document::new("Test Project");
document.width = 800.0;
document.height = 600.0;
// Layer 1 with a red circle
let mut layer1 = VectorLayer::new("Red Layer");
let circle1 = Circle::new((100.0, 100.0), 50.0);
let shape1 = Shape::new(circle1.to_path(0.1)).with_fill(ShapeColor::rgb(255, 0, 0));
let instance1 = ShapeInstance::new(shape1.id);
layer1.add_shape(shape1);
layer1.add_object(instance1);
// Layer 2 with a green circle
let mut layer2 = VectorLayer::new("Green Layer");
let circle2 = Circle::new((200.0, 200.0), 50.0);
let shape2 = Shape::new(circle2.to_path(0.1)).with_fill(ShapeColor::rgb(0, 255, 0));
let instance2 = ShapeInstance::new(shape2.id);
layer2.add_shape(shape2);
layer2.add_object(instance2);
// Layer 3 with a blue circle
let mut layer3 = VectorLayer::new("Blue Layer");
let circle3 = Circle::new((300.0, 300.0), 50.0);
let shape3 = Shape::new(circle3.to_path(0.1)).with_fill(ShapeColor::rgb(0, 0, 255));
let instance3 = ShapeInstance::new(shape3.id);
layer3.add_shape(shape3);
layer3.add_object(instance3);
let id1 = document.root.add_child(AnyLayer::Vector(layer1));
let id2 = document.root.add_child(AnyLayer::Vector(layer2));
let id3 = document.root.add_child(AnyLayer::Vector(layer3));
(document, vec![id1, id2, id3])
}
#[test]
fn test_render_empty_document() {
let document = Document::new("Empty");
let mut scene = Scene::new();
// Should not panic
render_document(&document, &mut scene);
}
#[test]
fn test_render_document_with_shapes() {
let (document, _ids) = setup_rendering_document();
let mut scene = Scene::new();
// Should render all 3 layers without error
render_document(&document, &mut scene);
}
#[test]
fn test_render_with_transform() {
let (document, _ids) = setup_rendering_document();
let mut scene = Scene::new();
// Render with zoom and pan
let transform = Affine::translate((100.0, 50.0)) * Affine::scale(2.0);
render_document_with_transform(&document, &mut scene, transform);
}
#[test]
fn test_render_solo_single_layer() {
let (mut document, ids) = setup_rendering_document();
// Solo layer 2 (green)
if let Some(layer) = document.root.get_child_mut(&ids[1]) {
layer.set_soloed(true);
}
// Count visible layers for rendering
let any_soloed = document.visible_layers().any(|l| l.soloed());
assert!(any_soloed);
let layers_to_render: Vec<_> = document
.visible_layers()
.filter(|l| l.soloed())
.collect();
assert_eq!(layers_to_render.len(), 1);
// Render should work
let mut scene = Scene::new();
render_document(&document, &mut scene);
}
#[test]
fn test_render_solo_multiple_layers() {
let (mut document, ids) = setup_rendering_document();
// Solo layers 1 and 3
if let Some(layer) = document.root.get_child_mut(&ids[0]) {
layer.set_soloed(true);
}
if let Some(layer) = document.root.get_child_mut(&ids[2]) {
layer.set_soloed(true);
}
// Two layers should render
let layers_to_render: Vec<_> = document
.visible_layers()
.filter(|l| l.soloed())
.collect();
assert_eq!(layers_to_render.len(), 2);
let mut scene = Scene::new();
render_document(&document, &mut scene);
}
#[test]
fn test_render_hidden_layer_not_rendered() {
let (mut document, ids) = setup_rendering_document();
// Hide layer 2
if let Some(layer) = document.root.get_child_mut(&ids[1]) {
layer.set_visible(false);
}
// Only 2 visible layers
assert_eq!(document.visible_layers().count(), 2);
let mut scene = Scene::new();
render_document(&document, &mut scene);
}
#[test]
fn test_render_with_layer_opacity() {
let (mut document, ids) = setup_rendering_document();
// Set different opacities
if let Some(layer) = document.root.get_child_mut(&ids[0]) {
layer.set_opacity(0.5);
}
if let Some(layer) = document.root.get_child_mut(&ids[1]) {
layer.set_opacity(0.25);
}
if let Some(layer) = document.root.get_child_mut(&ids[2]) {
layer.set_opacity(1.0);
}
// Verify opacities
assert_eq!(document.root.get_child(&ids[0]).unwrap().opacity(), 0.5);
assert_eq!(document.root.get_child(&ids[1]).unwrap().opacity(), 0.25);
assert_eq!(document.root.get_child(&ids[2]).unwrap().opacity(), 1.0);
let mut scene = Scene::new();
render_document(&document, &mut scene);
}
#[test]
fn test_render_with_clip_instances() {
let mut document = Document::new("Test");
// Create a vector clip
let mut clip_layer = VectorLayer::new("Clip Content");
let circle = Circle::new((50.0, 50.0), 25.0);
let shape = Shape::new(circle.to_path(0.1)).with_fill(ShapeColor::rgb(255, 255, 0));
let instance = ShapeInstance::new(shape.id);
clip_layer.add_shape(shape);
clip_layer.add_object(instance);
let mut vector_clip = VectorClip::new("Yellow Circle Clip", 5.0, 100.0, 100.0);
vector_clip.layers.roots.push(lightningbeam_core::layer_tree::LayerNode::new(
AnyLayer::Vector(clip_layer),
));
let clip_id = vector_clip.id;
document.vector_clips.insert(clip_id, vector_clip);
// Create a layer with a clip instance
let mut layer = VectorLayer::new("Main Layer");
let mut clip_instance = ClipInstance::new(clip_id);
clip_instance.timeline_start = 0.0;
clip_instance.transform.x = 100.0;
clip_instance.transform.y = 100.0;
layer.clip_instances.push(clip_instance);
document.root.add_child(AnyLayer::Vector(layer));
// Set time within clip range
document.set_time(2.0);
let mut scene = Scene::new();
render_document(&document, &mut scene);
}
#[test]
fn test_render_clip_instance_outside_time_range() {
let mut document = Document::new("Test");
// Create a vector clip
let vector_clip = VectorClip::new("Test Clip", 5.0, 100.0, 100.0);
let clip_id = vector_clip.id;
document.vector_clips.insert(clip_id, vector_clip);
// Create clip instance starting at time 10.0
let mut layer = VectorLayer::new("Main Layer");
let mut clip_instance = ClipInstance::new(clip_id);
clip_instance.timeline_start = 10.0;
layer.clip_instances.push(clip_instance);
document.root.add_child(AnyLayer::Vector(layer));
// Set time before clip starts
document.set_time(5.0);
// Clip shouldn't render (it hasn't started yet)
let mut scene = Scene::new();
render_document(&document, &mut scene);
}
#[test]
fn test_render_all_layers_hidden() {
let (mut document, ids) = setup_rendering_document();
// Hide all layers
for id in &ids {
if let Some(layer) = document.root.get_child_mut(id) {
layer.set_visible(false);
}
}
// No visible layers
assert_eq!(document.visible_layers().count(), 0);
// Should still render (just background)
let mut scene = Scene::new();
render_document(&document, &mut scene);
}
#[test]
fn test_render_solo_hidden_layer_interaction() {
let (mut document, ids) = setup_rendering_document();
// Hide and solo layer 1
if let Some(layer) = document.root.get_child_mut(&ids[0]) {
layer.set_visible(false);
layer.set_soloed(true);
}
// Layer 1 is hidden, so not in visible_layers()
// The solo flag on a hidden layer doesn't affect rendering
let visible_soloed: Vec<_> = document
.visible_layers()
.filter(|l| l.soloed())
.collect();
// No visible layer is soloed
assert_eq!(visible_soloed.len(), 0);
// All 2 visible layers should render (layers 2 and 3)
assert_eq!(document.visible_layers().count(), 2);
let mut scene = Scene::new();
render_document(&document, &mut scene);
}
#[test]
fn test_render_background_color() {
let mut document = Document::new("Test");
document.background_color = ShapeColor::rgb(128, 128, 128);
let mut scene = Scene::new();
render_document(&document, &mut scene);
}
#[test]
fn test_render_at_different_times() {
let (mut document, _ids) = setup_rendering_document();
// Render at different times
for time in [0.0, 0.5, 1.0, 2.5, 5.0, 10.0] {
document.set_time(time);
let mut scene = Scene::new();
render_document(&document, &mut scene);
}
}
@@ -0,0 +1,277 @@
//! Integration tests for selection operations
//!
//! Tests mixed selections of shape instances and clip instances,
//! selection state management, and interaction with transforms.
use lightningbeam_core::action::Action;
use lightningbeam_core::actions::TransformClipInstancesAction;
use lightningbeam_core::clip::ClipInstance;
use lightningbeam_core::document::Document;
use lightningbeam_core::layer::{AnyLayer, VectorLayer};
use lightningbeam_core::object::{ShapeInstance, Transform};
use lightningbeam_core::selection::Selection;
use lightningbeam_core::shape::Shape;
use std::collections::HashMap;
use uuid::Uuid;
use vello::kurbo::{Circle, Rect, Shape as KurboShape};
/// Create a test document with shapes and clips
fn setup_mixed_content_document() -> (Document, Uuid, Vec<Uuid>, Vec<Uuid>) {
let mut document = Document::new("Test Project");
let mut layer = VectorLayer::new("Layer 1");
// Add shapes
let circle = Circle::new((50.0, 50.0), 25.0);
let shape1 = Shape::new(circle.to_path(0.1));
let _shape1_id = shape1.id;
let instance1 = ShapeInstance::new(shape1.id);
let instance1_id = instance1.id;
layer.add_shape(shape1);
layer.add_object(instance1);
let rect = Rect::new(100.0, 100.0, 150.0, 150.0);
let shape2 = Shape::new(rect.to_path(0.1));
let instance2 = ShapeInstance::new(shape2.id);
let instance2_id = instance2.id;
layer.add_shape(shape2);
layer.add_object(instance2);
// Add clip instances
let clip_id = Uuid::new_v4();
let clip_instance1 = ClipInstance::new(clip_id);
let clip_instance1_id = clip_instance1.id;
layer.clip_instances.push(clip_instance1);
let clip_instance2 = ClipInstance::new(clip_id);
let clip_instance2_id = clip_instance2.id;
layer.clip_instances.push(clip_instance2);
let layer_id = document.root.add_child(AnyLayer::Vector(layer));
let shape_ids = vec![instance1_id, instance2_id];
let clip_ids = vec![clip_instance1_id, clip_instance2_id];
(document, layer_id, shape_ids, clip_ids)
}
#[test]
fn test_selection_of_shape_instances() {
let (_document, _layer_id, shape_ids, _clip_ids) = setup_mixed_content_document();
let mut selection = Selection::new();
// Select first shape instance
selection.add_shape_instance(shape_ids[0]);
assert!(selection.contains_shape_instance(&shape_ids[0]));
assert!(!selection.contains_shape_instance(&shape_ids[1]));
assert_eq!(selection.shape_instances().len(), 1);
// Add second shape instance
selection.add_shape_instance(shape_ids[1]);
assert!(selection.contains_shape_instance(&shape_ids[0]));
assert!(selection.contains_shape_instance(&shape_ids[1]));
assert_eq!(selection.shape_instances().len(), 2);
// Toggle first shape instance (deselect)
selection.toggle_shape_instance(shape_ids[0]);
assert!(!selection.contains_shape_instance(&shape_ids[0]));
assert!(selection.contains_shape_instance(&shape_ids[1]));
assert_eq!(selection.shape_instances().len(), 1);
}
#[test]
fn test_selection_of_clip_instances() {
let (_document, _layer_id, _shape_ids, clip_ids) = setup_mixed_content_document();
let mut selection = Selection::new();
// Select clip instances
selection.add_clip_instance(clip_ids[0]);
assert!(selection.contains_clip_instance(&clip_ids[0]));
assert_eq!(selection.clip_instances().len(), 1);
selection.add_clip_instance(clip_ids[1]);
assert!(selection.contains_clip_instance(&clip_ids[0]));
assert!(selection.contains_clip_instance(&clip_ids[1]));
assert_eq!(selection.clip_instances().len(), 2);
// Toggle
selection.toggle_clip_instance(clip_ids[0]);
assert!(!selection.contains_clip_instance(&clip_ids[0]));
assert!(selection.contains_clip_instance(&clip_ids[1]));
}
#[test]
fn test_mixed_selection() {
let (_document, _layer_id, shape_ids, clip_ids) = setup_mixed_content_document();
let mut selection = Selection::new();
// Select both shapes and clips
selection.add_shape_instance(shape_ids[0]);
selection.add_shape_instance(shape_ids[1]);
selection.add_clip_instance(clip_ids[0]);
selection.add_clip_instance(clip_ids[1]);
assert_eq!(selection.shape_instances().len(), 2);
assert_eq!(selection.clip_instances().len(), 2);
// Clear only clip instances
selection.clear_clip_instances();
assert_eq!(selection.shape_instances().len(), 2);
assert_eq!(selection.clip_instances().len(), 0);
// Re-add clip
selection.add_clip_instance(clip_ids[0]);
// Full clear
selection.clear();
assert_eq!(selection.shape_instances().len(), 0);
assert_eq!(selection.clip_instances().len(), 0);
}
#[test]
fn test_select_only_shape_instance() {
let (_document, _layer_id, shape_ids, clip_ids) = setup_mixed_content_document();
let mut selection = Selection::new();
// Select multiple items
selection.add_shape_instance(shape_ids[0]);
selection.add_shape_instance(shape_ids[1]);
selection.add_clip_instance(clip_ids[0]);
// Select only shape_ids[0] - this clears ALL selections first
selection.select_only_shape_instance(shape_ids[0]);
assert!(selection.contains_shape_instance(&shape_ids[0]));
assert!(!selection.contains_shape_instance(&shape_ids[1]));
// select_only_shape_instance calls clear() so clip instances are also cleared
assert!(!selection.contains_clip_instance(&clip_ids[0]));
}
#[test]
fn test_select_only_clip_instance() {
let (_document, _layer_id, shape_ids, clip_ids) = setup_mixed_content_document();
let mut selection = Selection::new();
// Select multiple items
selection.add_shape_instance(shape_ids[0]);
selection.add_clip_instance(clip_ids[0]);
selection.add_clip_instance(clip_ids[1]);
// Select only clip_ids[0] - this clears ALL selections first
selection.select_only_clip_instance(clip_ids[0]);
assert!(selection.contains_clip_instance(&clip_ids[0]));
assert!(!selection.contains_clip_instance(&clip_ids[1]));
// select_only_clip_instance calls clear() so shape instances are also cleared
assert!(!selection.contains_shape_instance(&shape_ids[0]));
}
#[test]
fn test_selection_with_transform_action() {
let (mut document, layer_id, _shape_ids, clip_ids) = setup_mixed_content_document();
let mut selection = Selection::new();
selection.add_clip_instance(clip_ids[0]);
// Transform selected clip instance
let old_transform = Transform::new();
let new_transform = Transform::with_position(50.0, 50.0);
let mut transforms = HashMap::new();
for &id in selection.clip_instances() {
transforms.insert(id, (old_transform.clone(), new_transform.clone()));
}
let mut action = TransformClipInstancesAction::new(layer_id, transforms);
action.execute(&mut document);
// Verify transform applied
if let Some(AnyLayer::Vector(layer)) = document.get_layer_mut(&layer_id) {
let instance = layer
.clip_instances
.iter()
.find(|ci| ci.id == clip_ids[0])
.unwrap();
assert_eq!(instance.transform.x, 50.0);
assert_eq!(instance.transform.y, 50.0);
}
// Rollback
action.rollback(&mut document);
if let Some(AnyLayer::Vector(layer)) = document.get_layer_mut(&layer_id) {
let instance = layer
.clip_instances
.iter()
.find(|ci| ci.id == clip_ids[0])
.unwrap();
assert_eq!(instance.transform.x, 0.0);
assert_eq!(instance.transform.y, 0.0);
}
}
#[test]
fn test_selection_is_empty() {
let selection = Selection::new();
assert!(selection.is_empty());
let mut selection2 = Selection::new();
selection2.add_shape_instance(Uuid::new_v4());
assert!(!selection2.is_empty());
let mut selection3 = Selection::new();
selection3.add_clip_instance(Uuid::new_v4());
assert!(!selection3.is_empty());
}
#[test]
fn test_selection_count() {
let mut selection = Selection::new();
let id1 = Uuid::new_v4();
let id2 = Uuid::new_v4();
let clip_id = Uuid::new_v4();
selection.add_shape_instance(id1);
selection.add_shape_instance(id2);
selection.add_clip_instance(clip_id);
assert_eq!(selection.shape_instances().len(), 2);
assert_eq!(selection.clip_instances().len(), 1);
// Remove one
selection.remove_shape_instance(&id1);
assert_eq!(selection.shape_instances().len(), 1);
// Remove clip
selection.remove_clip_instance(&clip_id);
assert_eq!(selection.clip_instances().len(), 0);
}
#[test]
fn test_duplicate_selection_handling() {
let mut selection = Selection::new();
let id = Uuid::new_v4();
// Add same ID multiple times
selection.add_shape_instance(id);
selection.add_shape_instance(id);
selection.add_shape_instance(id);
// Should only contain one instance (dedup behavior)
assert_eq!(selection.shape_instances().len(), 1);
// Same for clip instances
let clip_id = Uuid::new_v4();
selection.add_clip_instance(clip_id);
selection.add_clip_instance(clip_id);
assert_eq!(selection.clip_instances().len(), 1);
}