use crate::support::{self, WAIT}; use std::rc::Rc; use std::sync::{Arc, Mutex}; use std::time::Duration; use opencode_api::memory::{Memory, Route as MemoryRoute}; use opencode_api::transport::{BoxFuture, Request, Response, Transport}; use opencode_local::model_pref::Model; use opencode_tui::{Args, Deps, Route, Tui}; use opentui::testing::TestApp; use serde_json::{Value, json}; const DIRECTORY: &str = "/project"; const WORKTREE: &str = "/project/wt"; const LIMITS: &str = "/api/experimental/usage/limits"; /// The memory server, with the usage limits changeable while it runs. struct Server { memory: Memory, limits: Mutex, } impl Transport for Server { fn send(&self, request: Request) -> BoxFuture<'_, Result> { if request.url.path() != LIMITS { return self.memory.send(request); } drop(self.memory.send(request)); let body = json!({ "location": location(), "data": *self.limits.lock().unwrap() }); Box::pin(async move { Ok(support::respond(200, &body)) }) } } fn location() -> Value { location_at(DIRECTORY) } fn location_at(directory: &str) -> Value { json!({ "directory": directory, "project": { "id": "proj", "directory": DIRECTORY } }) } fn limits(spent: f64) -> Value { json!({ "workspace": { "id": "wrk_1", "name": "Acme", "usageURL": "https://opencode.ai/console/wrk_1/usage" }, "budgets": [{ "id": "big", "limit": 10, "spent": 0, "scope": "each", "per": "model", "coverage": "models", "models": [{ "providerID": "opencode", "modelID": "big", "name": "Big Pickle", "spent": spent }], "vendors": [], "picked": false, "shared": false, "resetsAt": 1894708800000_i64, }], }) } fn model(provider: &str, id: &str, name: &str, input: f64) -> Value { json!({ "id": id, "modelID": id, "providerID": provider, "name": name, "status": "active", "enabled": true, "capabilities": { "tools": true, "input": ["text"], "output": ["text"] }, "cost": [{ "input": input, "output": input * 3.0, "cache": { "read": 0, "write": 0 } }], "limit": { "context": 200000, "output": 32000 }, "time": { "released": 0 }, "variants": [], }) } fn agent(id: &str, model: Option) -> Value { json!({ "id": id, "name": id, "mode": "primary", "hidden": false, "permissions": [], "model": model, "request": { "settings": {}, "headers": {}, "body": {} }, }) } fn session(id: &str, directory: &str, title: &str, agent: Option<&str>) -> Value { json!({ "id": id, "title": title, "projectID": "proj", "cost": 0, "agent": agent, "tokens": { "input": 0, "output": 0, "reasoning": 0, "cache": { "read": 0, "write": 0 } }, "time": { "created": 1, "updated": 1 }, "location": { "directory": directory }, }) } fn grouped() -> Value { let read = |id: &str, path: &str| { json!({ "type": "tool", "id": id, "name": "read", "state": { "status": "completed", "input": { "filePath": path }, "content": [] }, "time": { "created": 1100, "completed": 1200 }, }) }; json!([ { "id": "msg_2", "type": "assistant", "agent": "build", "model": { "providerID": "demo", "id": "other" }, "content": [read("call_a", "src/a.rs"), read("call_b", "src/b.rs"), { "type": "text", "text": "All read." }], "finish": "stop", "time": { "created": 1000, "completed": 3000 }, }, { "id": "msg_1", "type": "user", "text": "Read both", "time": { "created": 1 } }, ]) } /// A running answer whose second read waits on a permission request. fn blocked() -> Value { json!([ { "id": "msg_2", "type": "assistant", "agent": "build", "model": { "providerID": "demo", "id": "other" }, "content": [ { "type": "tool", "id": "call_a", "name": "read", "state": { "status": "completed", "input": { "filePath": "src/a.rs" }, "content": [] }, "time": { "created": 1100, "completed": 1200 }, }, { "type": "tool", "id": "call_b", "name": "read", "state": { "status": "running", "input": { "filePath": "src/b.rs" }, "metadata": {} }, "time": { "created": 1300 }, }, ], "time": { "created": 1000 }, }, { "id": "msg_1", "type": "user", "text": "Read both", "time": { "created": 1 } }, ]) } fn routes() -> Vec { // The worktree's catalogs answer for the worktree, so the store files them there. let mut routes: Vec = [(DIRECTORY, None), (WORKTREE, Some(WORKTREE))] .into_iter() .rev() .flat_map(|(directory, query)| catalogs(directory, query)) .collect(); routes.extend(sessions()); routes } fn catalogs(directory: &str, query: Option<&str>) -> Vec { let list = |data: Value| json!({ "location": location_at(directory), "data": data }); let models = json!([model("opencode", "big", "Big Pickle", 10.0), model("demo", "other", "Other Model", 1.0)]); let providers = json!([ { "id": "opencode", "name": "OpenCode", "integrationID": "opencode", "activation": "auto", "package": "@ai-sdk/test" }, { "id": "demo", "name": "Demo", "activation": "auto", "package": "@ai-sdk/test" }, ]); let agents = json!([ agent("build", Some(json!({ "providerID": "opencode", "id": "big" }))), agent("plan", None), agent("code-review", None), ]); let routes = [ MemoryRoute::json("GET", "/api/location", 200, &location_at(directory)), MemoryRoute::json("GET", "/api/agent", 200, &list(agents)), MemoryRoute::json("GET", "/api/model", 200, &list(models)), MemoryRoute::json("GET", "/api/provider", 200, &list(providers)), MemoryRoute::json("GET", "/api/integration", 200, &list(json!([]))), ]; let Some(query) = query else { return routes.into() }; routes .into_iter() .map(|route| route.with_query([("location[directory]".to_owned(), vec![query.to_owned()])].into())) .collect() } fn sessions() -> Vec { let mut routes = vec![ MemoryRoute::json("GET", "/api/config", 200, &json!([])), MemoryRoute::json("GET", "/api/session", 200, &json!({ "data": [], "cursor": {} })), MemoryRoute::json("GET", "/api/session/ses_g/message", 200, &json!({ "data": grouped(), "cursor": {} })), MemoryRoute::json("GET", "/api/session/ses_p/message", 200, &json!({ "data": blocked(), "cursor": {} })), MemoryRoute::json( "GET", "/api/session/ses_p/permission", 200, &json!({ "data": [{ "id": "per_1", "sessionID": "ses_p", "action": "read", "resources": ["elsewhere"], "source": { "type": "tool", "messageID": "msg_2", "id": "call_b" }, }] }), ), MemoryRoute::json("GET", "/api/session/:id/message", 200, &json!({ "data": [], "cursor": {} })), ]; for (id, directory, title, agent) in [ ("ses_a", DIRECTORY, "Fix the parser", None), ("ses_b", DIRECTORY, "Write the docs", Some("build")), ("ses_g", DIRECTORY, "Grouped reads", None), ("ses_p", DIRECTORY, "Blocked read", None), ("ses_wt", WORKTREE, "Worktree work", None), ] { routes.push(MemoryRoute::json( "GET", &format!("/api/session/{id}"), 200, &json!({ "data": session(id, directory, title, agent) }), )); } let saved = support::merge( session("ses_m", DIRECTORY, "Saved model", None), json!({ "model": { "providerID": "opencode", "id": "big" } }), ); routes.push(MemoryRoute::json("GET", "/api/session/ses_m", 200, &json!({ "data": saved }))); for list in ["inbox", "permission", "form"] { routes.push(MemoryRoute::json("GET", &format!("/api/session/:id/{list}"), 200, &json!({ "data": [] }))); } routes } struct Harness { app: TestApp, tui: Rc, server: Arc, } fn mount(route: Route, args: Args, config: Value) -> Harness { let server = Arc::new(Server { memory: Memory::new(routes()), limits: Mutex::new(limits(5.0)) }); let deps = Deps { config: support::config(support::decode(&config.to_string())), route, events: true, ..support::deps(server.clone()) }; let support::Harness { app, tui } = support::mount_on(TestApp::new(100, 30), deps, args); let h = Harness { app, tui, server }; let local = h.tui.local.clone(); h.wait("the catalog", || local.catalog_ready() && !local.agents().is_empty()); h } fn animations_off() -> Value { json!({ "animations": false }) } fn medium_verbosity() -> Value { json!({ "animations": false, "session": { "verbosity": "medium" } }) } use support::session_route; impl Harness { fn wait(&self, what: &str, check: impl Fn() -> bool) { let found = self.app.wait_until(WAIT, || { self.app.render(); check() }); assert!(found, "{what} never happened:\n{}\n{:#?}", self.app.text(), self.log()); } fn wait_for(&self, text: &str) { self.wait(text, || self.app.text().contains(text)); } fn settle(&self) { self.app.wait_until(Duration::from_millis(150), || false); self.app.render(); } fn log(&self) -> Vec { self.server.memory.requests().iter().map(|logged| format!("{} {}", logged.method, logged.path)).collect() } fn count(&self, method: &str, path: &str) -> usize { self.server.memory.requests().iter().filter(|logged| logged.method == method && logged.path == path).count() } fn writes(&self) -> Vec { self.log() .into_iter() .filter(|line| !line.starts_with("GET ") && !line.ends_with("/opencode.goal/get")) .collect() } fn agent(&self) -> Option { self.tui.local.agent().map(|agent| agent.id.0) } fn prompt_text(&self) -> String { self.tui.textarea.borrow().as_ref().map(|textarea| textarea.text()).unwrap_or_default() } fn row(&self, text: &str) -> (u16, u16) { let screen = self.app.text(); screen .lines() .enumerate() .find_map(|(y, line)| { let x = line.find(text)?; Some((line[..x].chars().count() as u16, y as u16)) }) .unwrap_or_else(|| panic!("no {text:?} on screen:\n{screen}")) } } #[test] fn session_new_keeps_the_location_agent_and_model_and_closes_dialogs() { let h = mount( session_route("ses_wt"), Args::default(), json!({ "animations": false, "session": { "new_location": "inherit" } }), ); h.wait_for("Worktree work"); // The worktree's catalog loads after the session moves the location there: until then the agent and model are not // valid there, and the new session's home, in the worktree too, would drop them. h.wait("the worktree's catalog", || { opencode_tui::features::location::current(&h.tui).is_some_and(|location| location.directory == WORKTREE) && h.tui.local.catalog_ready() && !h.tui.local.agents().is_empty() }); h.tui.local.set_agent("plan"); h.tui.local.set(&Model::new("demo", "other"), false); assert_eq!(h.tui.local.current(), Some(Model::new("demo", "other"))); h.tui.keymap.dispatch("help.show", None); h.wait("the help dialog", || h.tui.dialogs.is_open()); h.tui.keymap.dispatch("session.new", None); h.wait("home", || *h.tui.route() == Route::Home); assert!(!h.tui.dialogs.is_open(), "the dialog stayed open"); assert_eq!( opencode_tui::features::location::current(&h.tui).map(|location| location.directory), Some(WORKTREE.to_owned()), "`inherit` keeps the session's location" ); assert_eq!(h.agent().as_deref(), Some("plan")); assert_eq!(h.tui.local.current(), Some(Model::new("demo", "other"))); assert_eq!(h.writes(), Vec::::new()); } #[test] fn tui_prompt_append_appends_to_the_prompt_of_the_current_location() { let h = mount(Route::Home, Args::default(), animations_off()); h.wait("the prompt", || h.tui.textarea.borrow().is_some()); h.app.type_text("hello "); let append = |id: &str, directory: &str, text: &str| { json!({ "id": id, "created": 1, "type": "tui.prompt.append", "location": { "directory": directory }, "data": { "text": text }, }) }; h.server.memory.emit(&append("evt_elsewhere", "/elsewhere", "nope ")); h.server.memory.emit(&append("evt_here", DIRECTORY, "world")); h.wait("the append", || h.prompt_text() == "hello world"); h.app.type_text("!"); h.wait("typing after it", || h.prompt_text() == "hello world!"); } #[test] fn console_limits_load_show_in_the_prompt_reload_after_a_turn_and_warn_once() { let h = mount(session_route("ses_a"), Args::default(), animations_off()); h.wait_for("Fix the parser"); // Big Pickle is served by OpenCode Console, so its limits load without opening `/models` or `/usage`. h.wait("the limits", || h.count("GET", LIMITS) == 1); h.wait_for("$5 left"); h.wait_for("Running low on Big Pickle"); *h.server.limits.lock().unwrap() = limits(10.0); let succeeded = |id: &str, session: &str| { json!({ "id": id, "created": 2, "type": "session.execution.succeeded", "durable": { "aggregateID": session, "seq": 1, "version": 1 }, "location": { "directory": DIRECTORY }, "data": { "sessionID": session }, }) }; h.server.memory.emit(&succeeded("evt_other", "ses_other")); h.settle(); assert_eq!(h.count("GET", LIMITS), 1); h.server.memory.emit(&succeeded("evt_done", "ses_a")); h.wait("the reload", || h.count("GET", LIMITS) == 2); h.wait_for("Limit reached"); h.wait_for("Big Pickle limit reached"); assert_eq!(h.writes(), Vec::::new()); } #[test] fn a_model_another_client_selects_warns_of_its_limits() { let h = mount(session_route("ses_a"), Args::default(), animations_off()); h.wait_for("Running low on Big Pickle"); let event = |seq: u64, kind: &str, data: Value| { json!({ "id": format!("evt_{seq}"), "created": seq, "type": kind, "durable": { "aggregateID": "ses_a", "seq": seq, "version": 1 }, "location": { "directory": DIRECTORY }, "data": data, }) }; let select = |seq, provider: &str, id: &str| { let model = json!({ "sessionID": "ses_a", "model": { "providerID": provider, "id": id } }); h.server.memory.emit(&event(seq, "session.model.selected", model)); h.wait("the other client's model", || h.tui.local.current() == Some(Model::new(provider, id))); }; select(1, "demo", "other"); // The limits run out while the session is on a model they do not cover: the reload warns of nothing. *h.server.limits.lock().unwrap() = limits(10.0); h.tui.local.load_usage(true); h.wait("the reload", || h.count("GET", LIMITS) == 2); h.settle(); assert!(!h.app.text().contains("Big Pickle limit reached"), "{}", h.app.text()); // Only the store's sync of the session's selection warns now. select(2, "opencode", "big"); h.wait_for("Big Pickle limit reached"); } #[test] fn startup_agent_and_model_reach_the_selection() { let h = mount( Route::Home, Args { agent: Some("plan".into()), model: Some("demo/other".into()), ..Args::default() }, animations_off(), ); h.wait("the startup choice", || { h.agent().as_deref() == Some("plan") && h.tui.local.current() == Some(Model::new("demo", "other")) }); assert_eq!(h.tui.local.recent(), [Model::new("demo", "other")]); h.tui.navigate(session_route("ses_b")); h.wait_for("Write the docs"); assert_eq!(h.agent().as_deref(), Some("plan")); assert_eq!(h.writes(), Vec::::new()); } // An existing session keeps its saved model; the flag still leads fallback selection. #[test] fn startup_model_keeps_a_saved_session_model_and_leads_the_fallback() { let h = mount(session_route("ses_m"), Args { model: Some("demo/other".into()), ..Args::default() }, animations_off()); h.wait_for("Saved model"); h.wait("the recent model", || h.tui.local.recent() == [Model::new("demo", "other")]); assert_eq!(h.tui.local.current(), Some(Model::new("opencode", "big"))); // On home, a recent model chosen later does not displace `--model` for an agent without its own. h.tui.navigate(Route::Home); h.tui.local.set(&Model::new("opencode", "big"), true); h.tui.local.set_agent("plan"); h.wait("the fallback", || h.tui.local.current() == Some(Model::new("demo", "other"))); } #[test] fn the_prompt_footer_shows_the_local_agent_titlecased_per_word() { let h = mount(Route::Home, Args::default(), animations_off()); h.tui.local.set_agent("code-review"); h.tui.local.set(&Model::new("demo", "other"), false); h.wait_for("Code-Review ยท Other Model Demo"); } #[test] fn an_opened_group_stays_open_when_the_session_shows_again() { let h = mount(session_route("ses_g"), Args::default(), medium_verbosity()); h.wait_for("Explored"); // A closed group shows its header only. assert!(!h.app.text().contains("Reading file"), "{}", h.app.text()); let (x, y) = h.row("Explored"); h.app.click(x + 2, y); h.wait_for("Reading file"); h.tui.navigate(Route::Home); h.wait("home", || !h.app.text().contains("Explored")); h.tui.navigate(session_route("ses_g")); h.wait_for("Explored"); h.wait_for("Reading file"); } #[test] fn a_tool_waiting_on_a_permission_shows_outside_its_closed_group() { let h = mount(session_route("ses_p"), Args::default(), medium_verbosity()); // `partitionPending`: the header counts the finished read only, and the blocked one shows below it. h.wait_for("Explored: 1 read"); let screen = h.app.text(); assert!(!screen.contains("src/a.rs"), "{screen}"); let header = screen.find("Explored: 1 read").unwrap(); assert!(screen[header..].contains("src/b.rs"), "{screen}"); }