edith
Clone
https://git.tahinli.com/Tahinli/edith.git
0 watching·0 forks·Rust·18.3 MiB·ff99ad6·Tahinli·6 days ago·1055 commits
| 1 | //! End-to-end JSON-RPC transcript against the real `edith_mcp` binary. |
| 2 | //! |
| 3 | //! Drives one sequential session over stdio: project, media, edits (including |
| 4 | //! dry runs, atomic rollback, undo/redo), save and reopen, and a full export. |
| 5 | |
| 6 | use std::fs; |
| 7 | use std::io::{BufRead, BufReader, Write}; |
| 8 | use std::path::{Path, PathBuf}; |
| 9 | use std::process::{Child, ChildStdin, ChildStdout, Command, Stdio}; |
| 10 | use std::thread::sleep; |
| 11 | use std::time::{Duration, Instant}; |
| 12 | |
| 13 | use serde_json::{json, Value}; |
| 14 | |
| 15 | struct Client { |
| 16 | child: Child, |
| 17 | stdin: ChildStdin, |
| 18 | stdout: BufReader<ChildStdout>, |
| 19 | next_id: u64, |
| 20 | } |
| 21 | |
| 22 | impl Client { |
| 23 | fn spawn() -> Self { |
| 24 | let mut child = Command::new(env!("CARGO_BIN_EXE_edith_mcp")) |
| 25 | .stdin(Stdio::piped()) |
| 26 | .stdout(Stdio::piped()) |
| 27 | .stderr(Stdio::inherit()) |
| 28 | .spawn() |
| 29 | .expect("spawn edith_mcp"); |
| 30 | let stdin = child.stdin.take().expect("server stdin"); |
| 31 | let stdout = BufReader::new(child.stdout.take().expect("server stdout")); |
| 32 | Self { child, stdin, stdout, next_id: 1 } |
| 33 | } |
| 34 | |
| 35 | fn send(&mut self, msg: &Value) { |
| 36 | let mut line = msg.to_string(); |
| 37 | line.push('\n'); |
| 38 | self.stdin.write_all(line.as_bytes()).expect("write request"); |
| 39 | self.stdin.flush().expect("flush request"); |
| 40 | } |
| 41 | |
| 42 | /// Sends one request and reads exactly one response line. |
| 43 | fn call(&mut self, method: &str, params: Value) -> Value { |
| 44 | let id = self.next_id; |
| 45 | self.next_id += 1; |
| 46 | self.send(&json!({"jsonrpc": "2.0", "id": id, "method": method, "params": params})); |
| 47 | let mut line = String::new(); |
| 48 | let n = self.stdout.read_line(&mut line).expect("read response"); |
| 49 | assert!(n > 0, "server closed stdout during {method}"); |
| 50 | let v: Value = serde_json::from_str(&line) |
| 51 | .unwrap_or_else(|e| panic!("bad JSON from server during {method}: {e}: {line}")); |
| 52 | assert_eq!(v["id"], json!(id), "response id mismatch for {method}"); |
| 53 | v |
| 54 | } |
| 55 | |
| 56 | /// Sends a notification (no id): the server must not reply, so nothing is read. |
| 57 | fn notify(&mut self, method: &str, params: Value) { |
| 58 | self.send(&json!({"jsonrpc": "2.0", "method": method, "params": params})); |
| 59 | } |
| 60 | |
| 61 | /// Calls a tool; returns (text, isError, structuredContent or Null). |
| 62 | fn tool(&mut self, name: &str, args: Value) -> (String, bool, Value) { |
| 63 | let r = self.call("tools/call", json!({"name": name, "arguments": args})); |
| 64 | let result = r.get("result").unwrap_or_else(|| panic!("tools/call {name} gave no result: {r}")); |
| 65 | let text = result["content"][0]["text"] |
| 66 | .as_str() |
| 67 | .unwrap_or_else(|| panic!("tools/call {name} gave no text content: {result}")) |
| 68 | .to_owned(); |
| 69 | let is_error = result["isError"].as_bool().unwrap_or(false); |
| 70 | let structured = result.get("structuredContent").cloned().unwrap_or(Value::Null); |
| 71 | eprintln!("[{name}] isError={is_error}\n{text}\n"); |
| 72 | (text, is_error, structured) |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | impl Drop for Client { |
| 77 | fn drop(&mut self) { |
| 78 | let _ = self.child.kill(); |
| 79 | let _ = self.child.wait(); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /// Removes the scratch directory when the test ends, even on panic. |
| 84 | struct Scratch(PathBuf); |
| 85 | |
| 86 | impl Drop for Scratch { |
| 87 | fn drop(&mut self) { |
| 88 | let _ = fs::remove_dir_all(&self.0); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | fn lane<'a>(tl: &'a Value, name: &str) -> Option<&'a Value> { |
| 93 | tl["lanes"].as_array()?.iter().find(|l| l["lane"] == name) |
| 94 | } |
| 95 | |
| 96 | fn clip_count(tl: &Value, name: &str) -> usize { |
| 97 | lane(tl, name).and_then(|l| l["clips"].as_array()).map_or(0, Vec::len) |
| 98 | } |
| 99 | |
| 100 | /// Lane-by-lane (start, in, out) projection of a timeline, for equality checks. |
| 101 | fn layout(tl: &Value) -> Value { |
| 102 | let lanes: Vec<Value> = tl["lanes"] |
| 103 | .as_array() |
| 104 | .expect("lanes array") |
| 105 | .iter() |
| 106 | .map(|l| { |
| 107 | let clips: Vec<Value> = l["clips"] |
| 108 | .as_array() |
| 109 | .expect("clips array") |
| 110 | .iter() |
| 111 | .map(|c| json!([c["start"], c["in"], c["out"]])) |
| 112 | .collect(); |
| 113 | json!({"lane": l["lane"], "clips": clips}) |
| 114 | }) |
| 115 | .collect(); |
| 116 | json!({"fps": tl["fps"], "duration_frames": tl["duration_frames"], "lanes": lanes}) |
| 117 | } |
| 118 | |
| 119 | #[test] |
| 120 | fn transcript() { |
| 121 | let assets = Path::new(env!("CARGO_MANIFEST_DIR")) |
| 122 | .join("../../assets") |
| 123 | .canonicalize() |
| 124 | .expect("assets directory exists"); |
| 125 | let scratch_dir = std::env::temp_dir().join(format!("edith_mcp-transcript-{}", std::process::id())); |
| 126 | let _ = fs::remove_dir_all(&scratch_dir); |
| 127 | fs::create_dir_all(&scratch_dir).expect("create scratch dir"); |
| 128 | // Declared first so it drops last: the server is killed before the directory goes. |
| 129 | let scratch = Scratch(scratch_dir); |
| 130 | let mut c = Client::spawn(); |
| 131 | let dir = scratch.0.clone(); |
| 132 | let asset = |name: &str| assets.join(name).display().to_string(); |
| 133 | |
| 134 | // 1. initialize, initialized notification, tools/list. |
| 135 | let init = c.call( |
| 136 | "initialize", |
| 137 | json!({"protocolVersion": "2025-06-18", "capabilities": {}, "clientInfo": {"name": "transcript", "version": "0"}}), |
| 138 | ); |
| 139 | assert!(init["result"]["protocolVersion"].is_string(), "initialize: {init}"); |
| 140 | c.notify("notifications/initialized", json!({})); |
| 141 | |
| 142 | let list = c.call("tools/list", json!({})); |
| 143 | let tools = list["result"]["tools"].as_array().expect("tools array"); |
| 144 | for t in tools { |
| 145 | let name = t["name"].as_str().expect("tool name"); |
| 146 | assert!(t["description"].is_string(), "{name} has no description"); |
| 147 | assert_eq!(t["inputSchema"]["type"], "object", "{name} inputSchema is not an object"); |
| 148 | } |
| 149 | let names: Vec<&str> = tools.iter().filter_map(|t| t["name"].as_str()).collect(); |
| 150 | for want in [ |
| 151 | "project_new", "project_open", "project_save", "project_status", "project_settings", |
| 152 | "media_probe", "media_import", "media_list", "timeline", "clip_info", "edit", "undo", |
| 153 | "redo", "export_start", "export_presets", "export_status", "export_cancel", |
| 154 | ] { |
| 155 | assert!(names.contains(&want), "missing tool {want}; have {names:?}"); |
| 156 | } |
| 157 | |
| 158 | // 2. project and media. |
| 159 | let cut = dir.join("cut.edith"); |
| 160 | let cut_s = cut.display().to_string(); |
| 161 | let (t, e, _) = c.tool("project_new", json!({"path": cut_s})); |
| 162 | assert!(!e, "project_new: {t}"); |
| 163 | |
| 164 | let (t, e, _) = c.tool("media_probe", json!({"glob": asset("test_tone.*")})); |
| 165 | assert!(!e && t.contains("test_tone.wav"), "media_probe: {t}"); |
| 166 | |
| 167 | let (t, e, _) = c.tool("media_import", json!({"paths": [asset("test_av.mp4"), asset("test_tone.wav")]})); |
| 168 | assert!(!e && t.contains("src:0") && t.contains("src:1"), "media_import: {t}"); |
| 169 | |
| 170 | // 3. rough cut with keep. |
| 171 | let (t, e, _) = c.tool( |
| 172 | "edit", |
| 173 | json!({"ops": [{"op": "keep", "source": 0, "ranges": [["0s", "1s"], ["2s", "3s"], ["4s", "4.5s"]]}]}), |
| 174 | ); |
| 175 | assert!(!e, "keep: {t}"); |
| 176 | let (_, _, tl) = c.tool("timeline", json!({})); |
| 177 | assert_eq!(clip_count(&tl, "V1"), 3, "V1 should hold three kept clips: {tl}"); |
| 178 | |
| 179 | // 4. dry run leaves the timeline untouched. |
| 180 | let before = c.tool("timeline", json!({})).2; |
| 181 | let (t, e, _) = c.tool("edit", json!({"ops": [{"op": "split", "at": "0.5s"}], "dry_run": true})); |
| 182 | assert!(!e, "dry_run split: {t}"); |
| 183 | let after = c.tool("timeline", json!({})).2; |
| 184 | assert_eq!(before, after, "dry_run changed the timeline"); |
| 185 | |
| 186 | // 5. a failing batch rolls back everything before it. |
| 187 | let (t, e, _) = c.tool( |
| 188 | "edit", |
| 189 | json!({"ops": [ |
| 190 | {"op": "fade", "clip": "V1#0", "in": "0.2s"}, |
| 191 | {"op": "trim", "clip": "V1#9", "edge": "out", "to": "1s"}, |
| 192 | ]}), |
| 193 | ); |
| 194 | assert!(e, "bad batch should be an error: {t}"); |
| 195 | assert!(t.contains("op 2") && t.contains("rolled back"), "bad batch text: {t}"); |
| 196 | let after_bad = c.tool("timeline", json!({})).2; |
| 197 | assert_eq!(before, after_bad, "failed batch changed the timeline"); |
| 198 | |
| 199 | // 6. place onto lane A2 (adding it first if needed), set its gain, then undo and redo. |
| 200 | let tl_now = c.tool("timeline", json!({})).2; |
| 201 | let mut ops = Vec::new(); |
| 202 | if lane(&tl_now, "A2").is_none() { |
| 203 | ops.push(json!({"op": "lane_add", "kind": "audio"})); |
| 204 | } |
| 205 | ops.push(json!({"op": "place", "source": 1, "lane": "A2", "at": 0})); |
| 206 | ops.push(json!({"op": "lane_gain", "lane": "A2", "db": -12})); |
| 207 | let (t, e, _) = c.tool("edit", json!({"ops": ops})); |
| 208 | assert!(!e, "A2 batch: {t}"); |
| 209 | let with_a2 = c.tool("timeline", json!({})).2; |
| 210 | assert_eq!(clip_count(&with_a2, "A2"), 1, "A2 should hold the placed clip: {with_a2}"); |
| 211 | |
| 212 | let (t, e, _) = c.tool("undo", json!({})); |
| 213 | assert!(!e, "undo: {t}"); |
| 214 | let undone = c.tool("timeline", json!({})).2; |
| 215 | assert_eq!(clip_count(&undone, "A2"), 0, "undo should remove the A2 clip: {undone}"); |
| 216 | |
| 217 | let (t, e, _) = c.tool("redo", json!({})); |
| 218 | assert!(!e, "redo: {t}"); |
| 219 | let redone = c.tool("timeline", json!({})).2; |
| 220 | assert_eq!(clip_count(&redone, "A2"), 1, "redo should bring the A2 clip back: {redone}"); |
| 221 | |
| 222 | // 7. clip_info resolves a clip back to its source. |
| 223 | let (t, e, _) = c.tool("clip_info", json!({"clip": "V1#0"})); |
| 224 | assert!(!e && t.contains("src:0"), "clip_info: {t}"); |
| 225 | |
| 226 | // 8. save, reopen, and compare the layout. |
| 227 | let pre_save = c.tool("timeline", json!({})).2; |
| 228 | let (t, e, _) = c.tool("project_save", json!({})); |
| 229 | assert!(!e, "project_save: {t}"); |
| 230 | assert!(cut.exists(), "project file missing after save"); |
| 231 | |
| 232 | let (t, e, st) = c.tool("project_open", json!({"path": cut_s})); |
| 233 | assert!(!e, "project_open: {t}"); |
| 234 | assert_eq!(st["dirty"], json!(false), "freshly opened project should not be dirty: {st}"); |
| 235 | let reopened = c.tool("timeline", json!({})).2; |
| 236 | assert_eq!(layout(&pre_save), layout(&reopened), "layout changed across save and reopen"); |
| 237 | |
| 238 | // 9. export a one-second range, poll to completion, then refuse to overwrite. |
| 239 | let out = dir.join("out.mp4"); |
| 240 | let out_s = out.display().to_string(); |
| 241 | let (t, e, _) = c.tool("export_start", json!({"out": out_s, "range": {"from": 0, "to": "1s"}})); |
| 242 | assert!(!e && t.contains("job:1"), "export_start: {t}"); |
| 243 | |
| 244 | // A one-second range is a few seconds of encode on an idle machine, but this |
| 245 | // runs an unoptimized test build alongside other builds (load average ~40 on |
| 246 | // 12 cores), where the software H.264/AAC encode took over two minutes while |
| 247 | // still making progress (75% to 81%). The budget is wall-clock, so it must |
| 248 | // allow for a loaded host; progress is what the test cares about, not speed. |
| 249 | let deadline = Instant::now() + Duration::from_secs(600); |
| 250 | loop { |
| 251 | let (t, e, _) = c.tool("export_status", json!({"job": 1})); |
| 252 | if e { |
| 253 | panic!("export failed: {t}"); |
| 254 | } |
| 255 | if t.contains("done") { |
| 256 | break; |
| 257 | } |
| 258 | assert!(Instant::now() < deadline, "export did not finish within 120s: {t}"); |
| 259 | sleep(Duration::from_millis(250)); |
| 260 | } |
| 261 | let size = fs::metadata(&out).expect("exported file exists").len(); |
| 262 | assert!(size > 0, "exported file is empty"); |
| 263 | |
| 264 | let (t, e, _) = c.tool("export_start", json!({"out": out_s, "range": {"from": 0, "to": "1s"}})); |
| 265 | assert!(e, "second export over an existing file should be refused: {t}"); |
| 266 | |
| 267 | // 10. unknown tool is a tool error; unknown method is a JSON-RPC error. |
| 268 | let (t, e, _) = c.tool("no_such_tool", json!({})); |
| 269 | assert!(e, "unknown tool should be an error: {t}"); |
| 270 | let bogus = c.call("bogus/method", json!({})); |
| 271 | assert!(bogus["error"].is_object(), "unknown method should be a JSON-RPC error: {bogus}"); |
| 272 | } |