//! Script (psychoscript) plugins in the real TUI: memo keeps its subtree while its value stays, a plugin that spins is //! failed on CPU time while the TUI keeps drawing, `--no-plugins` skips configured script plugins, and a dialog kept //! out by the host answers `undefined`. Example twins live in `plugin_twins`, and the ownership table's Script column //! is `plugin_ownership`. use std::path::Path; use std::sync::Arc; use std::time::Duration; use opencode_plugin::script::{Sources, define}; use opencode_tui::{Deps, Route}; use crate::support::{self, Harness}; fn home(definition: opencode_plugin::Definition) -> Harness { let h = support::mount(80, 24, Deps { route: Route::Home, ..support::deps(Arc::new(support::empty())) }); opencode_tui::plugins::host::add(&h.tui, definition); h } fn frames(h: &Harness) { for _ in 0..3 { h.app.advance(Duration::from_millis(16)); h.app.render(); } } #[test] fn a_script_memo_keeps_its_subtree_while_its_value_stays() { let init = r#" import { define } from "opencode:plugin" let builds = 0 let views = 0 export default define((setup) => { setup.command({ name: "poke" }, () => {}) setup.surface({ name: "page", place: { type: "route", name: "page" } }, (view) => { views += 1 const value = builds >= 2 ? 2 : 1 const cards = view.memo("cards", value, () => { builds += 1 return { type: "text", spans: [{ text: "cards built" }] } }) const count = { type: "text", spans: [{ text: "builds " + builds + " views " + views }] } return { type: "box", id: "root", children: [cards, count] } }) }) "#; let sources = Sources::from_files([("plugin.toml", "id = \"test.page\""), ("init.ts", init)]); let h = home(define(&sources, None).unwrap().0); h.tui.navigate(Route::Plugin { id: "test.page".into(), name: "page".into() }); h.wait_for_all(&["cards built", "builds 1"]); for _ in 0..3 { _ = h.tui.keymap.dispatch("poke", None); frames(&h); } let text = h.app.text(); let views: u32 = text.split("views ").nth(1).and_then(|rest| rest.split_whitespace().next()?.parse().ok()).unwrap(); assert!(text.contains("cards built") && text.contains("builds 1 views") && views > 1, "{text}"); } #[test] fn a_spinning_script_plugin_is_failed_and_the_tui_keeps_drawing() { let init = r#" import { define, show, text } from "opencode:plugin" export default define((setup) => { setup.command({ name: "spin" }, () => { while (true) {} }) setup.slot({ path: "home.footer" }, () => show(text("spinner claim"))) }) "#; let sources = Sources::from_files([("plugin.toml", "id = \"test.spin\""), ("init.ts", init)]); let h = home(define(&sources, None).unwrap().0); h.wait_for("spinner claim"); _ = h.tui.keymap.dispatch("spin", None); h.wait_gone("spinner claim"); let entries = h.tui.plugins.entries(); let spin = entries.iter().find(|entry| entry.id == "test.spin").unwrap(); let opencode_plugin::Status::Failed(error) = &spin.status else { panic!("{:?}", spin.status) }; assert!(error.contains("script budget"), "{error}"); h.app.type_text("still here"); h.wait_for("still here"); } // `--no-plugins` (D01-no-plugins): the quick start loads and draws its footer line when external plugins are enabled; // with the flag it does not, and `/plugins` lists its target as skipped, with why. #[test] fn a_configured_script_plugin_does_not_load_with_no_plugins() { for no_plugins in [false, true] { let root = opencode_util::temp::tempdir().unwrap(); let config = root.path().join("config"); let dir = config.join("plugins").join("hello"); std::fs::create_dir_all(&dir).unwrap(); let example = Path::new(env!("CARGO_MANIFEST_DIR")).join("../plugin/examples/script/hello"); for file in ["plugin.toml", "init.ts"] { std::fs::copy(example.join(file), dir.join(file)).unwrap(); } let target = dir.to_string_lossy().into_owned(); let deps = Deps { route: Route::Home, config: support::config(support::decode(&serde_json::json!({ "plugins": [target] }).to_string())), ..support::deps(Arc::new(support::empty())) }; let args = opencode_tui::Args { no_plugins, ..opencode_tui::Args::default() }; let h = support::mount_on(opentui::testing::TestApp::new(80, 24), deps, args); let external = opencode_tui::plugins::host::External { config_dir: config, cwd: root.path().to_path_buf() }; opencode_tui::plugins::host::external_with(&h.tui, external, None); let expected = match no_plugins { true => opencode_plugin::Status::Skipped(opencode_plugin::SKIPPED_NO_PLUGINS.to_owned()), false => opencode_plugin::Status::Active, }; h.wait("the plugin's status", || { frames(&h); h.tui .plugins .entries() .into_iter() .find(|entry| entry.target.as_deref() == Some(&*target)) .is_some_and(|entry| entry.status == expected) }); frames(&h); let footer = h.app.text().lines().any(|line| line.trim_end().ends_with("hello")); assert_eq!(footer, !no_plugins, "no_plugins {no_plugins}:\n{}", h.app.text()); } } #[test] fn a_dialog_the_host_keeps_out_answers_its_task_undefined() { let init = r#" import { define } from "opencode:plugin" export default define((setup) => { setup.command({ name: "ask" }, async (host) => { const answer = await host.confirm({ title: "Kept out?", message: "asked under the host's dialog" }) host.toast({ variant: "info", message: "answer=" + String(answer) }) }) }) "#; let sources = Sources::from_files([("plugin.toml", "id = \"test.ask\""), ("init.ts", init)]); let h = home(define(&sources, None).unwrap().0); frames(&h); let options = opencode_ui::dialog::DialogOptions::new(opencode_ui::dialog::DialogSize::Medium).key("host.approval"); h.tui.dialogs.replace(|cx| opentui::prelude::View::new(&cx.app), None, options); _ = h.tui.keymap.dispatch("ask", None); h.wait_for("answer=undefined"); assert!(!h.app.text().contains("Kept out?")); }