From 3432fcdb0e492a08b26a6e38a460e2de8af16882 Mon Sep 17 00:00:00 2001 From: Simon Klee Date: Thu, 5 Nov 2026 00:06:00 +0100 Subject: [PATCH] test(cli-rs): cover the client MCP tools and the composer tabs TestApp tests for the client-mcp experiment and both tools, interrupting a subagent, picking a terminal, and the shell output text. A key that opens or closes the composer now syncs the session view, so the queued dock hides under it. --- packages/cli-rs/crates/tui/src/app.rs | 8 + .../cli-rs/crates/tui/src/composer/mod.rs | 7 + packages/cli-rs/crates/tui/tests/composer.rs | 426 ++++++++++++++++++ 3 files changed, 441 insertions(+) create mode 100644 packages/cli-rs/crates/tui/tests/composer.rs diff --git a/packages/cli-rs/crates/tui/src/app.rs b/packages/cli-rs/crates/tui/src/app.rs index 34afaa9286e..5b7769c8204 100644 --- a/packages/cli-rs/crates/tui/src/app.rs +++ b/packages/cli-rs/crates/tui/src/app.rs @@ -233,6 +233,14 @@ impl Tui { self.app.request_redraw(); } + /// Syncs the route view with no store changes, after view state outside the store changed (E.13b's composer). + pub(crate) fn sync_view(&self) { + if let Some(view) = self.view.borrow().as_ref() { + view.sync(self, &opencode_store::Changes::default()); + } + self.app.request_redraw(); + } + /// Runs `ThemeState` effects off the UI thread, shows a theme error, and repaints on a change. pub(crate) fn theme_changed(&self) { let (effects, error) = { diff --git a/packages/cli-rs/crates/tui/src/composer/mod.rs b/packages/cli-rs/crates/tui/src/composer/mod.rs index 55874d0d8e2..942eada4bc6 100644 --- a/packages/cli-rs/crates/tui/src/composer/mod.rs +++ b/packages/cli-rs/crates/tui/src/composer/mod.rs @@ -377,6 +377,13 @@ impl Inner { mode.dispose(); } (self.on_toggle)(tui, open); + // The session view hides the queued dock while the composer shows; a key that toggled it changed no store state. + let weak = tui.weak(); + tui.app.defer(move |_| { + if let Some(tui) = weak.upgrade() { + tui.sync_view(); + } + }); } /// Redraws on the next turn, outside the mouse handler of a row it replaces. diff --git a/packages/cli-rs/crates/tui/tests/composer.rs b/packages/cli-rs/crates/tui/tests/composer.rs new file mode 100644 index 00000000000..e16aaca292a --- /dev/null +++ b/packages/cli-rs/crates/tui/tests/composer.rs @@ -0,0 +1,426 @@ +//! The composer, the shell output dialog, and the client MCP tools (E.13b) through the whole TUI, over the in-memory +//! transport, for the paths the P-APP scenario `E.13b/composer` does not reach: the client MCP experiment and its +//! tools, interrupting a subagent, picking a terminal, the queued dock, and the output text. + +#[path = "support/wait.rs"] +mod wait; + +use std::path::PathBuf; +use std::rc::Rc; +use std::sync::{Arc, Mutex}; +use std::time::Duration; + +use opencode_api::client::Client; +use opencode_api::client_mcp; +use opencode_api::generated::SessionID; +use opencode_api::memory::{Logged, Memory, Route as MemoryRoute}; +use opencode_api::pty::{self, Incoming}; +use opencode_local::cli_config::{self, Resolved}; +use opencode_local::paths::Paths; +use opencode_tui::app::Roots; +use opencode_tui::{Deps, Route, Tui}; +use opentui::testing::TestApp; +use serde_json::{Value, json}; + +const CHANNEL: &str = "test"; + +fn session(id: &str, parent: Option<&str>, title: &str, updated: u32) -> Value { + json!({ + "id": id, "parentID": parent, "title": title, "projectID": "proj_test", "cost": 0, + "tokens": { "input": 0, "output": 0, "reasoning": 0, "cache": { "read": 0, "write": 0 } }, + "time": { "created": updated, "updated": updated }, + "location": { "directory": "/project" }, + }) +} + +fn terminal() -> Value { + json!({ + "id": "pty_1", "title": "Terminal", "command": "/bin/sh", "args": [], "cwd": "/project", + "status": "running", "pid": 42, "sessionID": "ses_a", "foregroundProcess": "vim", + "size": { "cols": 80, "rows": 24 }, "output": { "head": 0, "tail": 0 }, + }) +} + +fn shell() -> Value { + json!({ + "id": "sh_1", "status": "running", "command": "make watch", "cwd": "/project", "shell": "/bin/sh", + "file": "/tmp/sh_1.log", "pid": 7, "metadata": { "sessionID": "ses_a" }, "time": { "started": 1 }, + }) +} + +fn routes() -> Vec { + let location = json!({ "directory": "/project", "project": { "id": "proj_test", "directory": "/project" } }); + let mut routes = vec![ + MemoryRoute::json("GET", "/api/location", 200, &location), + MemoryRoute::json( + "GET", + "/api/info", + 200, + &json!({ "version": "test", "pid": 0, "urls": [], "paths": { "tmp": "/tmp" }, + "capabilities": { "persistentPty": true } }), + ), + MemoryRoute::json( + "GET", + "/api/session/ses_a", + 200, + &json!({ "data": session("ses_a", None, "Fix the parser", 3) }), + ), + MemoryRoute::json( + "GET", + "/api/session/ses_other", + 200, + &json!({ "data": session("ses_other", None, "Elsewhere", 1) }), + ), + MemoryRoute::json( + "GET", + "/api/session", + 200, + &json!({ "data": [session("ses_a", None, "Fix the parser", 3), session("ses_c", Some("ses_a"), "Scan (@explore subagent)", 2)], "cursor": {} }), + ), + MemoryRoute::json( + "GET", + "/api/session/active", + 200, + &json!({ "data": { "ses_c": { "type": "running" } } }), + ), + MemoryRoute::json( + "POST", + "/api/session/ses_c/interrupt", + 200, + &json!({ "interrupted": true }), + ), + MemoryRoute::json( + "GET", + "/api/session/ses_a/message", + 200, + &json!({ "data": [], "cursor": {} }), + ), + MemoryRoute::json( + "GET", + "/api/session/ses_a/inbox", + 200, + &json!({ "data": [{ "id": "msg_q", "sessionID": "ses_a", "type": "user", "payload": { "text": "Later" }, + "delivery": "queue", "time": { "created": 5 } }] }), + ), + MemoryRoute::json( + "GET", + "/api/experimental/session/ses_a/terminal", + 200, + &json!({ "data": [terminal()] }), + ), + MemoryRoute::json( + "GET", + "/api/shell", + 200, + &json!({ "location": location, "data": [shell()] }), + ), + MemoryRoute::json( + "GET", + "/api/shell/sh_1", + 200, + &json!({ "location": location, "data": shell() }), + ), + MemoryRoute::json( + "GET", + "/api/shell/sh_1/output", + 200, + &json!({ "location": location, + "data": { "output": "hello \u{1b}[31mred\u{1b}[0m\r\nworld", "cursor": 20, "size": 20, "truncated": false } }), + ), + ]; + for list in ["permission", "form"] { + routes.push(MemoryRoute::json( + "GET", + &format!("/api/session/ses_a/{list}"), + 200, + &json!({ "data": [] }), + )); + } + routes +} + +/// The far end of the client MCP socket and the URL and headers it opened with. +type Opened = Arc, client_mcp::Remote)>>>; + +struct Harness { + app: TestApp, + tui: Rc, + memory: Memory, + opened: Opened, + state: PathBuf, + next: std::cell::Cell, + _home: tempfile::TempDir, +} + +fn mount() -> Harness { + let home = tempfile::tempdir().unwrap(); + let root = home.path().to_owned(); + let paths = + Paths::from_vars(|name| (name == "HOME").then(|| root.clone().into_os_string())).unwrap(); + let state = paths.state.clone(); + let memory = Memory::new(routes()); + let client = Client::with_transport("http://opencode.local", Arc::new(memory.clone())).unwrap(); + let app = TestApp::new(100, 30); + let info = cli_config::decode_text(r#"{"animations":false}"#).unwrap(); + let tui = opencode_tui::mount( + &app, + Deps { + client, + directory: "/project".into(), + config: Resolved::new(info, |_| None), + roots: Some(Roots { + paths, + storage_channel: CHANNEL.into(), + }), + route: Route::Session { + id: SessionID("ses_a".into()), + }, + continue_: false, + events: true, + }, + ); + // Terminal panes get sockets that never answer. + tui.terminals.set_connector(Arc::new(|_| { + let (socket, remote) = pty::pair(); + std::mem::forget(remote); + Ok(socket) + })); + let opened: Opened = Arc::default(); + let record = opened.clone(); + tui.client_mcp.set_connector(Arc::new( + move |url: url::Url, headers: Vec<(String, String)>| { + let (socket, remote) = client_mcp::pair(); + record + .lock() + .unwrap() + .push((url.to_string(), headers, remote)); + Ok(socket) + }, + )); + let harness = Harness { + app, + tui, + memory, + opened, + state, + next: std::cell::Cell::new(0), + _home: home, + }; + assert!(harness.wait(|harness| harness.app.text().contains("1 queued"))); + assert!(harness.wait(|harness| { + !harness + .requests("GET", "/api/experimental/session/ses_a/terminal") + .is_empty() + })); + harness +} + +impl Harness { + fn wait(&self, check: impl Fn(&Self) -> bool) -> bool { + self.app.wait_until(wait::WAIT, || { + self.app.render(); + check(self) + }) + } + + fn settle(&self) { + self.app.wait_until(Duration::from_millis(100), || false); + self.app.render(); + } + + fn requests(&self, method: &str, path: &str) -> Vec { + self.memory + .requests() + .into_iter() + .filter(|logged| logged.method == method && logged.path == path) + .collect() + } + + fn selection(&self) -> Value { + let path = self + .state + .join(CHANNEL) + .join("tui") + .join("session-terminal-selection.json"); + std::fs::read_to_string(path) + .ok() + .and_then(|text| serde_json::from_str(&text).ok()) + .unwrap_or(Value::Null) + } + + fn experiment(&self, on: bool) { + opencode_tui::dialogs::settings::update(&self.tui, move |value| { + opencode_tui::dialogs::settings::set_path(value, &["experimental", "client-mcp"], json!(on)); + }); + } + + /// Sends a request on the newest client MCP socket and waits for its reply. + fn request(&self, method: &str, params: Value) -> Value { + let id = self.next.get() + 1; + self.next.set(id); + let message = json!({ "jsonrpc": "2.0", "id": id, "method": method, "params": params }); + let deliver = self.opened.lock().unwrap().last().unwrap().2.deliver.clone(); + deliver + .try_send(Incoming::Text(message.to_string())) + .unwrap(); + let reply = std::cell::RefCell::new(None); + self.app.wait_until(wait::WAIT, || { + let mut opened = self.opened.lock().unwrap(); + let remote = &mut opened.last_mut().unwrap().2; + while let Ok(text) = remote.sent.try_recv() { + let value: Value = serde_json::from_str(&text).unwrap(); + if value["id"] == json!(id) { + *reply.borrow_mut() = Some(value); + } + } + reply.borrow().is_some() + }); + reply.into_inner().expect("a reply") + } + + fn call(&self, tool: &str, arguments: Value, session: Option<&str>) -> Value { + let mut params = json!({ "name": tool, "arguments": arguments }); + if let Some(session) = session { + params["_meta"] = json!({ "ai.opencode/sessionID": session }); + } + self.request("tools/call", params)["result"].clone() + } +} + +fn text(result: &Value) -> (&str, bool) { + ( + result["content"][0]["text"].as_str().unwrap_or_default(), + result["isError"] == json!(true), + ) +} + +#[test] +fn the_client_mcp_experiment_serves_the_terminal_tools() { + let harness = mount(); + harness.settle(); + assert!(harness.opened.lock().unwrap().is_empty()); + assert!(!harness.tui.client_mcp.connected()); + + harness.experiment(true); + assert!(harness.wait(|harness| !harness.opened.lock().unwrap().is_empty())); + let (url, headers) = { + let opened = harness.opened.lock().unwrap(); + (opened[0].0.clone(), opened[0].1.clone()) + }; + assert_eq!( + url, + "ws://opencode.local/api/experimental/client-mcp?location%5Bdirectory%5D=%2Fproject&name=tui" + ); + assert!(headers.is_empty()); + + let tools = harness.request("tools/list", json!({}))["result"]["tools"].clone(); + let names: Vec<&str> = tools + .as_array() + .unwrap() + .iter() + .map(|tool| tool["name"].as_str().unwrap()) + .collect(); + assert_eq!(names, ["open_terminal", "write_terminal"]); + assert_eq!(tools[1]["inputSchema"]["required"], json!(["text"])); + + assert_eq!( + text(&harness.call("open_terminal", json!({}), None)), + ("Unknown session", true) + ); + assert_eq!( + text(&harness.call("open_terminal", json!({}), Some("ses_missing"))), + ("Unknown session", true) + ); + assert_eq!( + text(&harness.call("open_terminal", json!({}), Some("ses_other"))), + ("session not open in this TUI", true) + ); + assert_eq!( + text(&harness.call("write_terminal", json!({ "text": 1 }), Some("ses_a"))), + ("text must be a string", true) + ); + // A subagent's call opens its family's terminal: the newest one, shown without focus. + assert_eq!( + text(&harness.call("open_terminal", json!({}), Some("ses_c"))), + ("Opened terminal pty_1", false) + ); + assert!(harness.wait(|harness| harness.selection()["sessions"]["ses_a"] == "pty_1")); + assert_eq!( + text(&harness.call("write_terminal", json!({ "text": "ls\n" }), Some("ses_a"))), + ("Wrote to terminal pty_1", false) + ); + assert!( + harness.requests("POST", "/api/experimental/session/ses_a/terminal").is_empty(), + "an existing terminal is reused" + ); + + harness.experiment(false); + assert!(harness.wait(|harness| !harness.tui.client_mcp.connected())); + let closed = { + let mut opened = harness.opened.lock().unwrap(); + let remote = &mut opened[0].2; + harness.app.wait_until(wait::WAIT, || { + matches!( + remote.sent.try_recv(), + Err(tokio::sync::mpsc::error::TryRecvError::Disconnected) + ) + }) + }; + assert!(closed, "turning the experiment off closes the socket"); +} + +#[test] +fn the_subagents_tab_interrupts_a_running_subagent_and_hides_the_queued_dock() { + let harness = mount(); + harness.tui.keymap.dispatch("session.child.first", None); + assert!(harness.wait(|harness| harness.app.text().contains("Explore: Scan ()"))); + let text = harness.app.text(); + assert!(text.contains("interrupt ctrl+d"), "{text}"); + assert!(!text.contains("1 queued"), "the dock hides under the composer"); + harness + .tui + .keymap + .dispatch("composer.subagent.interrupt", None); + assert!(harness.wait(|harness| !harness.requests("POST", "/api/session/ses_c/interrupt").is_empty())); + // Escape closes the composer, and the dock and the prompt come back. + harness.app.press("escape"); + assert!(harness.wait(|harness| harness.app.text().contains("1 queued"))); + assert!(!harness.app.text().contains("Subagents")); +} + +#[test] +fn terminal_select_opens_the_terminal_list_and_enter_shows_the_terminal() { + let harness = mount(); + harness.tui.keymap.dispatch("terminal.select", None); + assert!(harness.wait(|harness| harness.app.text().contains("+ New terminal"))); + assert!(harness.app.text().contains("vim"), "the foreground process names the terminal"); + harness.app.press("down"); + harness.app.press("enter"); + assert!(harness.wait(|harness| harness.selection()["sessions"]["ses_a"] == "pty_1")); + assert!(!harness.app.text().contains("+ New terminal")); +} + +#[test] +fn the_shell_output_dialog_shows_the_output_without_escapes() { + let harness = mount(); + harness.tui.keymap.dispatch("session.child.first", None); + harness.memory.emit(&json!({ + "id": "evt_shell", "created": 1, "type": "shell.created", + "location": { "directory": "/project", "project": { "id": "proj_test", "directory": "/project" } }, + "data": { "info": shell() }, + })); + harness.app.press("right"); + assert!(harness.wait(|harness| harness.app.text().contains("make watch"))); + harness.app.press("enter"); + assert!(harness.wait(|harness| harness.app.text().contains("hello red"))); + let text = harness.app.text(); + assert!(text.contains("Shell output") && text.contains("Running"), "{text}"); + assert!(text.contains("world")); + assert!(!text.contains("Earlier output omitted")); + let outputs = harness.requests("GET", "/api/shell/sh_1/output"); + let query = |logged: &Logged, name: &str| logged.query.get(name).and_then(|values| values.first().cloned()); + assert_eq!(query(&outputs[0], "cursor").as_deref(), Some("9007199254740991")); + assert_eq!(query(&outputs[1], "cursor").as_deref(), Some("0")); + assert_eq!(query(&outputs[1], "limit").as_deref(), Some("65536")); + assert_eq!(query(&outputs[1], "location[directory]").as_deref(), Some("/project")); +}