FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust
T5: headless scripted UI tests (egui_kittest + control channel)
Brandon Thomas7dcdef697498
diff --git a/Cargo.lock b/Cargo.lockindex a2d7c1539..bd2aecbe8 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -1103,6 +1103,20 @@ dependencies = ["winit",]+[[package]]+name = "egui_kittest"+version = "0.36.2"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "5eb3f3effb2726fee23449293e67acbf6cc6ce35525b1722bd5743bb348114d5"+dependencies = [+ "eframe",+ "egui",+ "kittest",+ "log",+ "serde",+ "toml 1.1.6+spec-1.1.0",+]+[[package]]name = "either"version = "1.18.0"@@ -1629,6 +1643,7 @@ version = "0.1.0"dependencies = ["eframe","egui",+ "egui_kittest","filmcraft-audio-dsp","filmcraft-color","filmcraft-edit",@@ -2459,6 +2474,16 @@ version = "3.1.0"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "e2db585e1d738fc771bf08a151420d3ed193d9d895a36df7f6f8a9456b911ddc"+[[package]]+name = "kittest"+version = "0.4.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "90ceaa75eb0036a32b6b9833962eb18137449e9817e2e586006471925b727fd5"+dependencies = [+ "accesskit",+ "accesskit_consumer 0.35.0",+]+[[package]]name = "kurbo"version = "0.13.1"@@ -3956,6 +3981,15 @@ dependencies = ["serde",]+[[package]]+name = "serde_spanned"+version = "1.1.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "6662b5879511e06e8999a8a235d848113e942c9124f211511b16466ee2995f26"+dependencies = [+ "serde_core",+]+[[package]]name = "shlex"version = "2.0.1"@@ -4333,7 +4367,7 @@ dependencies = ["cfg-expr","heck 0.5.0","pkg-config",- "toml",+ "toml 0.8.2","version-compare",]@@ -4466,11 +4500,24 @@ source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "185d8ab0dfbb35cf1399a6344d8484209c088f75f8f68230da55d48d95d43e3d"dependencies = ["serde",- "serde_spanned",+ "serde_spanned 0.6.9","toml_datetime 0.6.3","toml_edit 0.20.2",]+[[package]]+name = "toml"+version = "1.1.6+spec-1.1.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "920602543f0911ab71da12c50d59701da54c196d1a2bf5cb4b75667f137a406a"+dependencies = [+ "serde_core",+ "serde_spanned 1.1.1",+ "toml_datetime 1.1.1+spec-1.1.0",+ "toml_parser",+ "winnow 1.0.4",+]+[[package]]name = "toml_datetime"version = "0.6.3"@@ -4508,7 +4555,7 @@ checksum = "396e4d48bbb2b7554c944bde63101b5ae446cff6ec4a24227428f15eb72ef338"dependencies = ["indexmap","serde",- "serde_spanned",+ "serde_spanned 0.6.9","toml_datetime 0.6.3","winnow 0.5.40",]diff --git a/crates/ui-egui/Cargo.toml b/crates/ui-egui/Cargo.tomlindex 540fc7bbb..a42a66f5f 100644--- a/crates/ui-egui/Cargo.toml+++ b/crates/ui-egui/Cargo.toml@@ -27,3 +27,6 @@ serde = { workspace = true }serde_json = { workspace = true }log = { workspace = true }rayon = { workspace = true }++[dev-dependencies]+egui_kittest = { workspace = true, features = ["eframe"] }diff --git a/crates/ui-egui/tests/scripted.rs b/crates/ui-egui/tests/scripted.rsnew file mode 100644index 000000000..2053f2363--- /dev/null+++ b/crates/ui-egui/tests/scripted.rs@@ -0,0 +1,212 @@+//! Scripted UI tests: the real `FilmcraftApp` runs headless under `egui_kittest` (no window, no+//! GPU) and is driven through the same JSON control channel that agents use over TCP/MCP+//! (`docs/control-protocol.md`). Each test opens the demo project, sends control requests+//! (`engine.execute`, `ui.menu.invoke`, `ui.click`, `ui.inspect`, …) between frames and asserts on+//! the replies and on `sequence.inspect` JSON.+//!+//! The harness steps egui frames itself: synthetic input queued by `ui.click`/`ui.key` is moved+//! into the next frame's `RawInput` through the app's own `raw_input_hook`, exactly as eframe does.+//! Screenshots (`ui.screenshot`) need a real viewport and are not covered here.++use std::sync::mpsc::{Sender, channel};++use egui_kittest::Harness;+use filmcraft_engine::Session;+use filmcraft_ui_egui::FilmcraftApp;+use filmcraft_ui_egui::control::ControlRequest;+use serde_json::{Value, json};++struct Driver {+ harness: Harness<'static, FilmcraftApp>,+ tx: Sender<ControlRequest>,+}++impl Driver {+ /// The app with the demo project open, after its first frames (theme and fonts installed).+ fn demo() -> Self {+ let mut session = Session::default();+ session.execute("file.openDemoProject", json!({})).expect("demo project");+ let (tx, rx) = channel();+ let app = FilmcraftApp::new(session).with_control(rx);+ let harness = Harness::builder().with_size(egui::vec2(1600.0, 980.0)).with_max_steps(10_000).build_eframe(move |_cc| app);+ let mut d = Driver { harness, tx };+ d.frames(4);+ d+ }++ /// Run `n` frames, feeding queued synthetic input like eframe's `raw_input_hook`.+ fn frames(&mut self, n: usize) {+ for _ in 0..n {+ let ctx = self.harness.ctx.clone();+ let mut raw = std::mem::take(self.harness.input_mut());+ eframe::App::raw_input_hook(self.harness.state_mut(), &ctx, &mut raw);+ *self.harness.input_mut() = raw;+ self.harness.step();+ }+ }++ /// Send a control request and run frames until it is answered.+ fn call(&mut self, method: &str, params: Value) -> Value {+ let (req, reply) = ControlRequest::new(method, params.clone());+ self.tx.send(req).unwrap();+ for _ in 0..600 {+ self.frames(1);+ if let Ok(v) = reply.try_recv() {+ return v;+ }+ }+ panic!("no reply to {method} {params}");+ }++ /// `call` that must succeed; returns `result`.+ fn ok(&mut self, method: &str, params: Value) -> Value {+ let v = self.call(method, params.clone());+ assert_eq!(v["ok"], json!(true), "{method} {params} failed: {v}");+ v["result"].clone()+ }++ fn exec(&mut self, command: &str, params: Value) -> Value {+ self.ok("engine.execute", json!({"command": command, "params": params}))+ }++ fn sequence(&mut self) -> Value {+ self.exec("sequence.inspect", json!({}))+ }++ fn inspect(&mut self) -> Value {+ self.ok("ui.inspect", json!({}))+ }++ fn element_ids(&mut self, prefix: &str) -> Vec<String> {+ let v = self.ok("ui.elements", json!({"prefix": prefix}));+ v.as_array().unwrap().iter().filter_map(|e| e["id"].as_str().map(str::to_string)).collect()+ }+}++/// Clips on a video track in `sequence.inspect` JSON.+fn track_clips(seq: &Value, track: usize) -> Vec<Value> {+ seq["video"][track]["items"].as_array().cloned().unwrap_or_default()+}++#[test]+fn demo_project_opens_headless_and_registers_widgets() {+ let mut d = Driver::demo();+ let ui = d.inspect();+ assert!(ui["activeSequence"].is_u64(), "demo sequence is active: {ui}");+ assert!(ui["elements"].as_u64().unwrap() > 50, "widgets registered: {}", ui["elements"]);+ let panels = d.element_ids("panel.");+ for p in ["panel.Timeline", "panel.Program", "panel.Project", "panel.tab.Timeline"] {+ assert!(panels.iter().any(|x| x == p), "{p} missing from {panels:?}");+ }+ let tools = d.element_ids("tools.");+ assert!(tools.len() >= 5, "tool buttons: {tools:?}");+ let seq = d.sequence();+ assert_eq!(track_clips(&seq, 0).len(), 6, "demo V1 clips: {seq}");+}++#[test]+fn razor_then_undo_through_the_control_channel() {+ let mut d = Driver::demo();+ let before = track_clips(&d.sequence(), 0).len();+ d.exec("playhead.set", json!({"seconds": 2.0}));+ let r = d.exec("timeline.razor", json!({"track": "V1"}));+ assert_eq!(r["cuts"], json!(1), "{r}");+ let after = track_clips(&d.sequence(), 0);+ assert_eq!(after.len(), before + 1);+ // the timeline shows the new clip on the next frame+ d.frames(2);+ let new_clip = after[1]["clip"].as_u64().unwrap();+ let at = d.ok("ui.timeline.locate", json!({"clip": new_clip}));+ assert!(at["x"].as_f64().is_some(), "{at}");+ // Undo from the menu path (UI command dispatch), then Redo by shortcut-free command id.+ d.ok("ui.menu.invoke", json!({"id": "edit.undo"}));+ assert_eq!(track_clips(&d.sequence(), 0).len(), before);+ d.exec("edit.redo", json!({}));+ assert_eq!(track_clips(&d.sequence(), 0).len(), before + 1);+}++#[test]+fn insert_from_source_ripples_the_sequence() {+ let mut d = Driver::demo();+ let project = d.exec("project.inspect", json!({}));+ let item = first_media_item(&project).unwrap_or_else(|| panic!("no media item in {project}"));+ let dur0 = d.sequence()["duration"].as_i64().unwrap();+ d.exec("source.open", json!({"item": item}));+ let rate = &d.sequence()["settings"]["frame_rate"];+ let (num, den) = (rate["num"].as_i64().unwrap(), rate["den"].as_i64().unwrap());+ let tick = |f: i64| f * 254_016_000_000 * den / num;+ d.exec("project.setMarks", json!({"item": item, "in": tick(24), "out": tick(47)}));+ d.exec("playhead.set", json!({"frame": 0}));+ d.exec("source.insert", json!({}));+ let dur1 = d.sequence()["duration"].as_i64().unwrap();+ assert!(dur1 > dur0, "insert lengthens the sequence: {dur0} → {dur1}");+ d.frames(2);+}++/// First movie item id anywhere in `project.inspect` JSON.+fn first_media_item(v: &Value) -> Option<u64> {+ match v {+ Value::Object(m) => {+ if let Some(id) = m.get("item").and_then(Value::as_u64)+ && m.get("type").and_then(Value::as_str).is_some_and(|t| t == "Movie")+ {+ return Some(id);+ }+ m.values().find_map(first_media_item)+ }+ Value::Array(a) => a.iter().find_map(first_media_item),+ _ => None,+ }+}++#[test]+fn apply_effect_appears_in_effect_controls() {+ let mut d = Driver::demo();+ let clip = track_clips(&d.sequence(), 0)[0]["clip"].as_u64().unwrap();+ d.exec("timeline.select", json!({"clips": [clip]}));+ d.exec("effects.apply", json!({"effect": "Gaussian Blur"}));+ let seq = d.sequence();+ let fx: Vec<String> = track_clips(&seq, 0)[0]["effects"].as_array().unwrap().iter().filter_map(|e| e["effect"].as_str().map(str::to_string)).collect();+ assert!(fx.iter().any(|e| e == "gaussian_blur"), "{fx:?}");+ d.ok("ui.panel.show", json!({"panel": "Effect Controls"}));+ d.frames(3);+ let ids = d.element_ids("effectControls.effect.");+ assert!(ids.iter().any(|i| i == "effectControls.effect.gaussian_blur"), "{ids:?}");+ d.ok("ui.menu.invoke", json!({"id": "edit.undo"}));+ d.frames(2);+ assert!(!d.element_ids("effectControls.effect.").iter().any(|i| i == "effectControls.effect.gaussian_blur"));+}++#[test]+fn playback_toggle_and_stop() {+ let mut d = Driver::demo();+ assert_eq!(d.inspect()["playback"]["playing"], json!(false));+ let r = d.ok("ui.menu.invoke", json!({"id": "playback.toggle"}));+ assert_eq!(r["playing"], json!(true), "{r}");+ d.frames(5);+ assert_eq!(d.inspect()["playback"]["playing"], json!(true));+ let r = d.ok("ui.playback", json!({"action": "stop"}));+ assert_eq!(r["playing"], json!(false), "{r}");+ assert_eq!(d.inspect()["playback"]["playing"], json!(false));+}++#[test]+fn clicking_a_tool_button_by_automation_id() {+ let mut d = Driver::demo();+ assert_ne!(d.inspect()["ui"]["tool"], json!("Razor"));+ d.ok("ui.click", json!({"id": "tools.Razor"}));+ d.frames(2);+ let ui = d.inspect();+ assert_eq!(ui["ui"]["tool"], json!("Razor"), "{}", ui["ui"]["tool"]);+ d.ok("ui.set", json!({"tool": "selection"}));+ assert_eq!(d.inspect()["ui"]["tool"], json!("Selection"));+}++#[test]+fn unknown_methods_and_commands_fail_cleanly() {+ let mut d = Driver::demo();+ let v = d.call("ui.nope", json!({}));+ assert_eq!(v["ok"], json!(false));+ let v = d.call("engine.execute", json!({"command": "no.such.command"}));+ assert_eq!(v["ok"], json!(false));+}diff --git a/docs/testing.md b/docs/testing.mdindex db1180214..8d4304535 100644--- a/docs/testing.md+++ b/docs/testing.md@@ -16,6 +16,8 @@ FILMCRAFT_REQUIRE_ORACLES=1 cargo test --workspace # CI: missing ffmpeg fails| Unit | `src/` `#[cfg(test)]` modules, `src/tests.rs` | CABAC engines, VLC tables (prefix-freeness, Kraft sums), colour matrices, frame cache || Property (`proptest`) | `time`, `bitstream`, `edit`, `isobmff/tests/roundtrip.rs`, `interchange/tests/*` | tick↔frame round trips for every rate, DF timecode, edit invariants (no overlaps, durations conserved), mux→demux round trips, interchange export→import || Synthetic streams | `h264/src/synth_tests.rs`, `hevc/src/synth_tests.rs` | hand-built bitstreams for features the reference encoders don't emit; expected output known exactly |+| Scripted UI | `crates/ui-egui/tests/scripted.rs` | headless app (egui_kittest) driven over the control channel: razor/undo, insert, apply effect, playback, click by automation id (§4) |+| Golden images | `crates/golden/tests/golden.rs` | CPU renders vs committed PNGs; GPU vs CPU on the same scenes (§3) || Engine / command | `crates/engine/src/tests.rs` | run commands on the demo project, assert the sequence, undo/redo, disabled cases || Render | `crates/render/src/tests.rs` | compositing, opacity, Motion, cross dissolve midpoint, ½-res vs full, GPU plan vs reference, audio mix, audio-effect continuity || Oracle | `crates/*/tests/*oracle*.rs`, `conformance.rs` | compare with ffmpeg/ffprobe (§2) |@@ -106,7 +108,33 @@ Each codec README has the full fixture matrix and the measured results.## 4. UI: control channel and screenshots-There is no automated UI test suite yet. UI work is checked by driving the real app:+### Scripted UI tests (headless)++`crates/ui-egui/tests/scripted.rs` runs the real `FilmcraftApp` under+[`egui_kittest`](https://docs.rs/egui_kittest) (`build_eframe`): no window, no GPU, no OS event loop.+A small `Driver` opens the demo project, sends requests through the same control channel agents use+(`ControlRequest` → `control::handle`, exactly as the TCP server does) and steps egui frames until+each reply arrives. Synthetic input queued by `ui.click` / `ui.drag` / `ui.key` is moved into the+next frame by the app's own `raw_input_hook`, so clicks by automation id work headless. Covered:++| Test | Asserts |+|---|---|+| `demo_project_opens_headless_and_registers_widgets` | active sequence, > 50 registered widgets, panel and tool ids, 6 clips on V1 (`sequence.inspect`) |+| `razor_then_undo_through_the_control_channel` | `timeline.razor` adds a clip, the timeline can locate it on screen, `edit.undo` via `ui.menu.invoke`, redo |+| `insert_from_source_ripples_the_sequence` | `source.open` + marks + `source.insert` lengthens the sequence |+| `apply_effect_appears_in_effect_controls` | `effects.apply` adds Gaussian Blur in the model and `effectControls.effect.gaussian_blur` appears in the panel; undo removes it |+| `playback_toggle_and_stop` | `playback.toggle` / `ui.playback stop` and `ui.inspect` playback state |+| `clicking_a_tool_button_by_automation_id` | `ui.click {id: "tools.Razor"}` changes the tool (real egui input path) |+| `unknown_methods_and_commands_fail_cleanly` | errors come back as `{"ok": false}` |++Not covered headless: `ui.screenshot` (needs a real viewport), the wgpu monitor path (the harness+runs the CPU texture path), audio output, and wall-clock playback advance (kittest frames do not+advance real time). Use `Driver` for new UI regressions: `d.exec(command, params)`,+`d.ok(method, params)`, `d.frames(n)`.++### Interactive checks++For visual work, drive the real app:```shcargo run --release -p filmcraft -- --control 9876