Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
test(gpui): strengthen timeline deletion validation
Colin Kimfbc995902f70
diff --git a/apps/desktop-gpui/benches/timeline-delete.rs b/apps/desktop-gpui/benches/timeline-delete.rsindex 0263cc18a..298d1871f 100644--- a/apps/desktop-gpui/benches/timeline-delete.rs+++ b/apps/desktop-gpui/benches/timeline-delete.rs@@ -1,38 +1,57 @@-use std::{hint::black_box, time::Duration, time::Instant};+use cap_project::{ImageSegment, TextSegment, ZoomMode, ZoomSegment};+use serde::Serialize;+use std::{+ fs::File,+ hint::black_box,+ io::{BufWriter, Write},+ time::{Duration, Instant},+};#[path = "../src/editor_edits/indices.rs"]mod indices;-#[derive(Clone, PartialEq, Debug)]-struct ModeledTextSegment {- strings: [String; 4],- optional_strings: [Option<String>; 2],- geometry_and_timing: [f64; 10],- styling: [f32; 8],- track: u32,- flags: [u8; 8],+fn text_fixture(count: usize) -> Vec<TextSegment> {+ (0..count)+ .map(|index| {+ serde_json::from_value(serde_json::json!({+ "start": index as f64 * 3.,+ "end": index as f64 * 3. + 2.,+ "content": format!("Text overlay {index}"),+ }))+ .expect("valid text segment")+ })+ .collect()+}++fn image_fixture(count: usize) -> Vec<ImageSegment> {+ (0..count)+ .map(|index| ImageSegment {+ start: index as f64 * 3.,+ end: index as f64 * 3. + 2.,+ path: format!("content/images/image-{index}.png"),+ name: format!("Image {index}"),+ ..Default::default()+ })+ .collect()}-fn fixture(count: usize) -> Vec<ModeledTextSegment> {+fn zoom_fixture(count: usize) -> Vec<ZoomSegment> {(0..count)- .map(|index| ModeledTextSegment {- strings: [- format!("Text overlay {index}: {}", "recorded content ".repeat(8)),- String::from("Inter"),- String::from("#FFFFFF"),- String::from("#000000"),- ],- optional_strings: [Some(String::from("#121212")), Some(String::from("#AA88FF"))],- geometry_and_timing: [index as f64; 10],- styling: [1.; 8],- track: 0,- flags: [0; 8],+ .map(|index| ZoomSegment {+ start: index as f64 * 3.,+ end: index as f64 * 3. + 2.,+ amount: 2.,+ mode: ZoomMode::Auto,+ glide_direction: Default::default(),+ glide_speed: 0.5,+ instant_animation: false,+ edge_snap_ratio: 0.25,}).collect()}-fn remove_one_at_a_time<T>(segments: &mut Vec<T>, indices: &[usize]) -> bool {- let sorted = indices::descending_indices(indices, segments.len());+fn remove_one_at_a_time<T>(segments: &mut Vec<T>, selected: &[usize]) -> bool {+ let sorted = indices::descending_indices(selected, segments.len());if sorted.is_empty() {return false;}@@ -42,89 +61,143 @@ fn remove_one_at_a_time<T>(segments: &mut Vec<T>, indices: &[usize]) -> bool {true}-fn sample(- source: &[ModeledTextSegment],+fn sample<T: Clone>(+ source: &[T],selected: &[usize],- delete: fn(&mut Vec<ModeledTextSegment>, &[usize]) -> bool,+ delete: fn(&mut Vec<T>, &[usize]) -> bool,+ batch: usize,) -> Duration {- let mut segments = source.to_vec();+ let mut inputs: Vec<Vec<T>> = (0..batch).map(|_| source.to_vec()).collect();+ let expected_change = selected.iter().any(|index| *index < source.len());let started = Instant::now();- let changed = black_box(delete(black_box(&mut segments), black_box(selected)));+ let mut changed = false;+ for segments in &mut inputs {+ changed |= black_box(delete(black_box(segments), black_box(selected)));+ }let elapsed = started.elapsed();- assert!(changed);- black_box(&segments);+ assert_eq!(changed, expected_change);+ black_box(&inputs);elapsed}-fn run_case(name: &str, count: usize, selected: Vec<usize>, iterations: usize) {- let source = fixture(count);- let mut before = source.clone();- let mut after = source.clone();+fn run_case<T: Clone + Serialize>(+ segment_type: &str,+ name: &str,+ source: &[T],+ selected: &[usize],+ csv: &mut Option<BufWriter<File>>,+) {+ let mut before = source.to_vec();+ let mut after = source.to_vec();+ assert_eq!(+ remove_one_at_a_time(&mut before, selected),+ indices::delete_indices(&mut after, selected)+ );assert_eq!(- remove_one_at_a_time(&mut before, &selected),- indices::delete_indices(&mut after, &selected)+ serde_json::to_vec(&before).unwrap(),+ serde_json::to_vec(&after).unwrap());- assert_eq!(before, after);drop(before);drop(after);+ let batch = if selected.len() <= 2 {+ if source.len() <= 512 { 64 } else { 8 }+ } else {+ 1+ };+ let iterations = 101;for _ in 0..10 {- sample(&source, &selected, remove_one_at_a_time);- sample(&source, &selected, indices::delete_indices);+ sample(source, selected, remove_one_at_a_time, batch);+ sample(source, selected, indices::delete_indices, batch);}let mut before = Vec::with_capacity(iterations);let mut after = Vec::with_capacity(iterations);for iteration in 0..iterations {- if iteration % 2 == 0 {- before.push(sample(&source, &selected, remove_one_at_a_time));- after.push(sample(&source, &selected, indices::delete_indices));+ let (old, new) = if iteration % 2 == 0 {+ let old = sample(source, selected, remove_one_at_a_time, batch);+ (+ old,+ sample(source, selected, indices::delete_indices, batch),+ )} else {- after.push(sample(&source, &selected, indices::delete_indices));- before.push(sample(&source, &selected, remove_one_at_a_time));+ let new = sample(source, selected, indices::delete_indices, batch);+ (sample(source, selected, remove_one_at_a_time, batch), new)+ };+ if let Some(csv) = csv.as_mut() {+ writeln!(+ csv,+ "{segment_type},{},{name},{},{},{batch},{iteration},{},{}",+ std::mem::size_of::<T>(),+ source.len(),+ selected.len(),+ old.as_nanos(),+ new.as_nanos()+ )+ .unwrap();}+ before.push(old);+ after.push(new);}before.sort_unstable();after.sort_unstable();- let before = before[iterations / 2].as_secs_f64() * 1_000_000.;- let after = after[iterations / 2].as_secs_f64() * 1_000_000.;+ let micros = |duration: Duration| duration.as_secs_f64() * 1_000_000. / batch as f64;+ let old_p50 = micros(before[iterations / 2]);+ let new_p50 = micros(after[iterations / 2]);+ let old_p95 = micros(before[iterations * 95 / 100]);+ let new_p95 = micros(after[iterations * 95 / 100]);println!(- "{name:<24} segments={count:<5} deleted={:<5} before_us={before:>10.3} after_us={after:>10.3} speedup={:>7.2}x",+ "{segment_type:<5} {name:<24} segments={:<5} selected={:<5} batch={batch:<2} before_p50_us={old_p50:>10.3} after_p50_us={new_p50:>10.3} before_p95_us={old_p95:>10.3} after_p95_us={new_p95:>10.3} speedup={:>7.2}x",+ source.len(),selected.len(),- before / after+ old_p50 / new_p50);}-fn main() {+fn run_suite<T: Clone + Serialize>(+ segment_type: &str,+ fixture: fn(usize) -> Vec<T>,+ csv: &mut Option<BufWriter<File>>,+) {println!(- "Timeline deletion helper; modeled text segments: {} inline bytes, six owned strings. Setup/cloning and survivor destruction excluded; deletion and removed-value destruction included. Median of alternating runs, after equality checks.",- std::mem::size_of::<ModeledTextSegment>()- );- run_case("single-middle-small", 64, vec![32], 2000);- run_case("single-tail-small", 64, vec![63], 2000);- run_case("pair-middle-small", 64, vec![16, 32], 2000);- run_case("single-middle-large", 5000, vec![2500], 500);- run_case("single-tail-large", 5000, vec![4999], 500);- run_case("trailing-pair-large", 5000, vec![4998, 4999], 500);- run_case("sparse-near-tail-large", 5000, vec![4997, 4999], 500);- run_case("trailing-half-large", 5000, (2500..5000).collect(), 100);- run_case(- "alternating-half-small",- 512,- (0..512).step_by(2).collect(),- 500,- );- run_case(- "alternating-half-large",- 5000,- (0..5000).step_by(2).collect(),- 100,+ "{segment_type}: actual cap-project type, {} inline bytes",+ std::mem::size_of::<T>());- run_case("prefix-half-large", 5000, (0..2500).collect(), 100);- run_case(- "sparse-five-percent",- 5000,- (0..5000).step_by(20).collect(),- 100,+ for count in [64, 512, 5000] {+ let source = fixture(count);+ for (name, selected) in [+ ("empty-selection", vec![]),+ ("invalid-selection", vec![count, usize::MAX]),+ ("single-middle", vec![count / 2]),+ ("single-tail", vec![count - 1]),+ ("pair-head-tail", vec![0, count - 1]),+ ("pair-middle-tail", vec![count / 2, count - 1]),+ ("pair-near-tail", vec![count - 3, count - 1]),+ ("contiguous-middle", (count / 4..count / 2).collect()),+ ("prefix-half", (0..count / 2).collect()),+ ("trailing-half", (count / 2..count).collect()),+ ("alternating-half", (0..count).step_by(2).collect()),+ ("sparse-five-percent", (0..count).step_by(20).collect()),+ ("all", (0..count).collect()),+ ] {+ run_case(segment_type, name, &source, &selected, csv);+ }+ }+}++fn main() {+ println!(+ "Actual cap-project segment deletion helper. Setup/cloning and final survivor destruction excluded; normalization, deletion and removed-value destruction included. Serialized output equality checked for every case. Ten warmups; 101 alternating samples; batched small selections; p50/p95 of per-operation batch averages. 5,000 segments is a stress workload.");+ let mut csv = std::env::var_os("CAP_TIMELINE_BENCH_CSV")+ .map(|path| BufWriter::new(File::create(path).expect("create benchmark CSV")));+ if let Some(csv) = csv.as_mut() {+ writeln!(csv, "segment_type,inline_bytes,workload,segments,selected,batch,sample,before_total_ns,after_total_ns").unwrap();+ }+ run_suite("text", text_fixture, &mut csv);+ run_suite("image", image_fixture, &mut csv);+ run_suite("zoom", zoom_fixture, &mut csv);+ if let Some(csv) = csv.as_mut() {+ csv.flush().unwrap();+ }}diff --git a/apps/desktop-gpui/src/editor_edits.rs b/apps/desktop-gpui/src/editor_edits.rsindex 4c2767373..7932c7f0a 100644--- a/apps/desktop-gpui/src/editor_edits.rs+++ b/apps/desktop-gpui/src/editor_edits.rs@@ -36,6 +36,9 @@ use crate::editor_timeline::{self, Segment, TrackKind};mod indices;+#[cfg(test)]+mod indices_tests;+use indices::{delete_indices, descending_indices};// ---------------------------------------------------------------------------diff --git a/apps/desktop-gpui/src/editor_edits/indices_tests.rs b/apps/desktop-gpui/src/editor_edits/indices_tests.rsnew file mode 100644index 000000000..ae2169031--- /dev/null+++ b/apps/desktop-gpui/src/editor_edits/indices_tests.rs@@ -0,0 +1,122 @@+use super::indices::{delete_indices, descending_indices};+use std::{cell::RefCell, rc::Rc};++#[test]+fn selections_preserve_survivors_and_report_changes() {+ for length in 0..=8 {+ for mask in 0..(1 << length) {+ let source: Vec<usize> = (0..length).collect();+ let selected: Vec<usize> = (0..length)+ .rev()+ .filter(|index| mask & (1 << index) != 0)+ .collect();+ let expected: Vec<usize> = source+ .iter()+ .copied()+ .filter(|index| !selected.contains(index))+ .collect();+ let indices: Vec<usize> = selected+ .iter()+ .copied()+ .chain(selected.iter().copied())+ .chain([length, usize::MAX])+ .collect();+ for indices in [&selected[..], &indices[..]] {+ let mut actual = source.clone();+ assert_eq!(delete_indices(&mut actual, indices), !selected.is_empty());+ assert_eq!(actual, expected);+ }+ }+ }+}++#[test]+fn empty_and_invalid_selections_leave_values_untouched() {+ let mut values = vec![String::from("first"), String::from("last")];+ let expected = values.clone();+ assert!(!delete_indices(&mut values, &[]));+ assert!(!delete_indices(&mut values, &[2]));+ assert!(!delete_indices(&mut values, &[usize::MAX]));+ assert!(!delete_indices(&mut values, &[2, 3, usize::MAX]));+ assert_eq!(values, expected);+}++#[test]+fn deletion_preserves_vector_capacity() {+ let mut values = Vec::with_capacity(32);+ values.extend(0..8);+ let capacity = values.capacity();+ assert!(delete_indices(&mut values, &[2, 5]));+ assert_eq!(values.capacity(), capacity);+ assert!(delete_indices(&mut values, &[4, 5]));+ assert_eq!(values.capacity(), capacity);+}++#[test]+fn zero_sized_values_follow_the_same_selection_rules() {+ let mut values = vec![(); 8];+ let capacity = values.capacity();+ assert!(!delete_indices(&mut values, &[8, usize::MAX]));+ assert_eq!(values.len(), 8);+ assert!(delete_indices(&mut values, &[0, 3, 7, 7, usize::MAX]));+ assert_eq!(values.len(), 5);+ assert!(delete_indices(&mut values, &[0, 4]));+ assert_eq!(values.len(), 3);+ assert!(delete_indices(&mut values, &[1, 2]));+ assert_eq!(values.len(), 1);+ assert!(delete_indices(&mut values, &[0]));+ assert!(values.is_empty());+ assert!(!delete_indices(&mut values, &[0]));+ assert_eq!(values.capacity(), capacity);+}++#[test]+fn non_copy_values_are_dropped_exactly_once() {+ struct Tracked {+ index: usize,+ drops: Rc<RefCell<Vec<usize>>>,+ }++ impl Drop for Tracked {+ fn drop(&mut self) {+ self.drops.borrow_mut()[self.index] += 1;+ }+ }++ for indices in [+ &[4, 1, 4, usize::MAX][..],+ &[1, 3][..],+ &[2][..],+ &[2, 2][..],+ &[0, 1, 2, 3, 4][..],+ ] {+ let drops = Rc::new(RefCell::new(vec![0; 5]));+ let mut values: Vec<Tracked> = (0..5)+ .map(|index| Tracked {+ index,+ drops: drops.clone(),+ })+ .collect();+ assert!(delete_indices(&mut values, indices));+ assert_eq!(+ *drops.borrow(),+ (0..5)+ .map(|index| usize::from(indices.contains(&index)))+ .collect::<Vec<_>>()+ );+ assert_eq!(+ values.iter().map(|value| value.index).collect::<Vec<_>>(),+ (0..5)+ .filter(|index| !indices.contains(index))+ .collect::<Vec<_>>()+ );+ drop(values);+ assert_eq!(*drops.borrow(), vec![1; 5]);+ }+}++#[test]+fn clip_indices_remain_descending_unique_and_in_bounds() {+ assert_eq!(descending_indices(&[1, 4, 0, 4, 99], 5), vec![4, 1, 0]);+ assert!(descending_indices(&[0, usize::MAX], 0).is_empty());+}diff --git a/apps/desktop-gpui/tests/timeline_indices.rs b/apps/desktop-gpui/tests/timeline_indices.rsindex 62c592020..4ffa1ad4e 100644--- a/apps/desktop-gpui/tests/timeline_indices.rs+++ b/apps/desktop-gpui/tests/timeline_indices.rs@@ -1,102 +1,5 @@#[path = "../src/editor_edits/indices.rs"]mod indices;-use indices::{delete_indices, descending_indices};-use std::{cell::RefCell, rc::Rc};--#[test]-fn selections_preserve_survivors_and_report_changes() {- for length in 0..=8 {- for mask in 0..(1 << length) {- let source: Vec<usize> = (0..length).collect();- let selected: Vec<usize> = (0..length)- .rev()- .filter(|index| mask & (1 << index) != 0)- .collect();- let expected: Vec<usize> = source- .iter()- .copied()- .filter(|index| !selected.contains(index))- .collect();- let indices: Vec<usize> = selected- .iter()- .copied()- .chain(selected.iter().copied())- .chain([length, usize::MAX])- .collect();- let mut actual = source;- assert_eq!(delete_indices(&mut actual, &indices), !selected.is_empty());- assert_eq!(actual, expected);- }- }-}--#[test]-fn empty_and_invalid_selections_leave_values_untouched() {- let mut values = vec![String::from("first"), String::from("last")];- let expected = values.clone();- assert!(!delete_indices(&mut values, &[]));- assert!(!delete_indices(&mut values, &[2, 3, usize::MAX]));- assert_eq!(values, expected);-}--#[test]-fn deletion_preserves_vector_capacity() {- let mut values = Vec::with_capacity(32);- values.extend(0..8);- let capacity = values.capacity();- assert!(delete_indices(&mut values, &[2, 5]));- assert_eq!(values.capacity(), capacity);- assert!(delete_indices(&mut values, &[4, 5]));- assert_eq!(values.capacity(), capacity);-}--#[test]-fn non_copy_values_are_dropped_exactly_once() {- struct Tracked {- index: usize,- drops: Rc<RefCell<Vec<usize>>>,- }-- impl Drop for Tracked {- fn drop(&mut self) {- self.drops.borrow_mut()[self.index] += 1;- }- }-- for indices in [- &[4, 1, 4, usize::MAX][..],- &[1, 3][..],- &[2, 2][..],- &[0, 1, 2, 3, 4][..],- ] {- let drops = Rc::new(RefCell::new(vec![0; 5]));- let mut values: Vec<Tracked> = (0..5)- .map(|index| Tracked {- index,- drops: drops.clone(),- })- .collect();- assert!(delete_indices(&mut values, indices));- assert_eq!(- *drops.borrow(),- (0..5)- .map(|index| usize::from(indices.contains(&index)))- .collect::<Vec<_>>()- );- assert_eq!(- values.iter().map(|value| value.index).collect::<Vec<_>>(),- (0..5)- .filter(|index| !indices.contains(index))- .collect::<Vec<_>>()- );- drop(values);- assert_eq!(*drops.borrow(), vec![1; 5]);- }-}--#[test]-fn clip_indices_remain_descending_unique_and_in_bounds() {- assert_eq!(descending_indices(&[1, 4, 0, 4, 99], 5), vec![4, 1, 0]);- assert!(descending_indices(&[0, usize::MAX], 0).is_empty());-}+#[path = "../src/editor_edits/indices_tests.rs"]+mod indices_tests;