From a4224d7a1e836fc511fe56ec60470e599715d570 Mon Sep 17 00:00:00 2001 From: Simon Klee Date: Wed, 18 Nov 2026 06:56:52 +0100 Subject: [PATCH] test(cli-rs): compare the board's dialog surface on every carrier The board example gains a dialog surface with a field its command focuses, a long scroll its key focuses, and a toast on close; plugin_carriers compares its frames, toasts and status linked, through the C ABI shims and as the built library. --- .../crates/plugin/examples/board/src/board.rs | 44 ++++++++++++- .../crates/tui/tests/all/plugin_carriers.rs | 63 +++++++++++++++++++ 2 files changed, 104 insertions(+), 3 deletions(-) diff --git a/packages/cli-rs/crates/plugin/examples/board/src/board.rs b/packages/cli-rs/crates/plugin/examples/board/src/board.rs index 1e338958ee6..eeac9e7cf95 100644 --- a/packages/cli-rs/crates/plugin/examples/board/src/board.rs +++ b/packages/cli-rs/crates/plugin/examples/board/src/board.rs @@ -13,7 +13,9 @@ //! them in a row its layout bounds at both widths, and asks for a name in a prompt filled with a value under a //! description (contract ergonomics 2.6 and 2.9). `board.hotkey`, on `ctrl+p` and in the palette, starts disabled, so //! the host's own `ctrl+p` takes the key; `board.gate` enables or disables it, and `board.passing` makes its run decline -//! the key, which goes on to the host as if it were not there (contract ergonomics 2.5). +//! the key, which goes on to the host as if it were not there (contract ergonomics 2.5). `board.dialog` shows its +//! dialog surface `ask` (2.7), whose field the command focuses, whose key moves the focus to its long scroll, and whose +//! close the board toasts. //! //! Its `files` route is P3's: a windowed list of files whose estimates are not their heights, each an anchor of group //! `file` with its hunks in group `hunk`. Commands bring a file to the top, step between hunks and show a file with @@ -34,8 +36,8 @@ use opencode_plugin_abi::interact::{Anchor, Bind, Event, Handled, Interact, Laye use opencode_plugin_abi::task::Cx; use opencode_plugin_abi::types::SessionMessageAssistantToolState; use opencode_plugin_abi::{ - Action, Border, Command, Confirm, Dialog, Fetch, Host, Layout, Prompt, Rgba, Select, SelectAction, SelectOption, - SendError, Setup, Span, TitleAlign, Toast, Variant, Width, v2, + Action, Border, Command, Confirm, Dialog, DialogSurface, Fetch, Host, Layout, PanelSize, Prompt, Rgba, Select, + SelectAction, SelectOption, SendError, Setup, Span, TitleAlign, Toast, Variant, Width, v2, }; pub const CARDS: usize = 6; @@ -59,6 +61,8 @@ struct Board { facts: Option, /// A box with a top and a bottom border title shows under the header (CT-26). titled: bool, + /// What the dialog's field told its handler. + answered: Vec, } /// The files: how many `layout` calls ran, the header's width the last one was told, and where the last step landed. @@ -185,6 +189,7 @@ pub fn setup(setup: &mut Setup) -> Result<(), String> { board.values[0] += 1; }); notes(setup, &board); + asking(setup, &board); // The plugin's own layer hears the key the route's handler passes. A global bind takes a key with a modifier: a // plain letter is the user's typing in the composer (FT-14). let global = board.clone(); @@ -315,6 +320,39 @@ fn files_view(files: &Files) -> v2::Node { v2::Node::column(vec![head, list]).layout(Layout::default().grow()) } +/// The dialog surface `ask` (contract ergonomics 2.7): a field, which the command that shows it focuses, and a long +/// scroll, which its key `ctrl+l` focuses; its handler hears the field's edits and the dialog's close. +fn asking(setup: &mut Setup, board: &Rc>) { + setup.command(command("board.dialog"), |host, _| { + host.dialog(Dialog::Surface(DialogSurface::new("ask").title("Ask the board").size(PanelSize::Large))); + host.focus("answer"); + }); + let (viewed, heard) = (board.clone(), board.clone()); + let surface = v2::Surface::new("ask", v2::Place::Dialog("ask".into()), move |cx| { + let lines = (0..40).map(|line| v2::Node::text(vec![Span::new(format!("line {line}"))])).collect(); + v2::Node::column(vec![ + v2::Node::text(vec![Span::new(format!( + "answered {} at {}", + viewed.borrow().answered.join(" "), + cx.read.focused().node.unwrap_or_default() + ))]), + v2::Node::input("").with_id("answer").placeholder("answer"), + v2::Node::scroll(lines).with_id("lines").scrollbar().layout(Layout::default().height(Width::Cells(5))), + ]) + }) + .layers(vec![Layer::default().keys(vec![Bind::new("ctrl+l", "lines")])]) + .on(move |host, event| { + match event { + Event::Key(key) if key.bind.as_deref() == Some("lines") => host.focus("lines"), + Event::Change { text, .. } => heard.borrow_mut().answered.push(format!("a:{text}")), + Event::Closed { surface, .. } => host.toast(Toast::new(Variant::Info, format!("{surface} closed"))), + _ => return Handled::Pass, + } + Handled::Consume + }); + setup.surface(surface); +} + /// The panel `notes`, its overlay `menu`, and the select of cards with its search. fn notes(setup: &mut Setup, board: &Rc>) { setup.command(command("board.notes"), |host, _| { diff --git a/packages/cli-rs/crates/tui/tests/all/plugin_carriers.rs b/packages/cli-rs/crates/tui/tests/all/plugin_carriers.rs index 9fb174233e1..cbd2c456e11 100644 --- a/packages/cli-rs/crates/tui/tests/all/plugin_carriers.rs +++ b/packages/cli-rs/crates/tui/tests/all/plugin_carriers.rs @@ -789,6 +789,69 @@ fn panels_overlays_and_reads_are_equal_on_every_carrier() { } } +// Expected (contract ergonomics 2.7): the board's dialog surface opens with its field focused, so what the user types +// is the field's; the dialog's key moves the focus to its long scroll, which the arrows scroll, with its scrollbar; +// escape closes the dialog, whose handler toasts. Opened again, `ctrl+c` empties the field and leaves the dialog open, +// then closes it. Frames, toasts and status are the same on every carrier, the real library included. +#[test] +fn a_dialog_surface_is_equal_on_every_carrier() { + let run = |run: Run| { + let board = open(run); + let (app, tui) = (&board.harness.app, &board.harness.tui); + let mut seen = Vec::new(); + let step = |name: &'static str, seen: &mut Vec<_>| { + settle(&board.harness); + seen.push((name, app.render().text(), observe(&board.harness))); + }; + // Over the board's route: the home screen's composer draws its placeholder at random. + tui.navigate(Route::Plugin { id: ID.to_owned(), name: "board".to_owned() }); + settle(&board.harness); + tui.keymap.dispatch("board.dialog", None); + step("open", &mut seen); + app.type_text("hi"); + step("typed", &mut seen); + app.press("ctrl+l"); + settle(&board.harness); + app.press("down"); + app.press("down"); + step("scrolled", &mut seen); + app.press("escape"); + step("closed", &mut seen); + tui.keymap.dispatch("board.dialog", None); + settle(&board.harness); + app.type_text("yo"); + app.press("ctrl+c"); + step("cleared", &mut seen); + app.press("ctrl+c"); + step("gone", &mut seen); + seen + }; + let linked = run(Run::In(Carrier::Linked)); + let at = |name: &str| &linked.iter().find(|seen| seen.0 == name).unwrap().1; + assert!(at("open").contains("Ask the board") && at("open").contains("at answer"), "{}", at("open")); + assert!(at("typed").contains("answered a:h a:hi at answer"), "{}", at("typed")); + assert!(at("scrolled").contains("a:hi at lines") && !at("scrolled").contains("line 0 "), "{}", at("scrolled")); + let toasts = |name: &str| linked.iter().find(|seen| seen.0 == name).unwrap().2.1.clone(); + assert!(!at("closed").contains("Ask the board"), "{}", at("closed")); + assert!(toasts("closed").iter().any(|toast| toast.contains("ask closed")), "{:?}", toasts("closed")); + assert!( + at("cleared").contains("a:yo a: at answer") && at("cleared").contains("Ask the board"), + "{}", + at("cleared") + ); + assert!(!at("gone").contains("Ask the board"), "{}", at("gone")); + assert!(toasts("gone").iter().any(|toast| toast.contains("ask closed")), "{:?}", toasts("gone")); + for other in [Run::In(CARRIERS[1]), Run::Library] { + for ((name, _, linked), (_, text, seen)) in linked.iter().zip(run(other)) { + assert_eq!((&linked.1, &linked.2), (&seen.1, &seen.2), "{other:?} at {name}: toasts and status"); + if linked.0 != seen.0 { + let at = linked.0.iter().zip(&seen.0).position(|(a, b)| a != b); + panic!("{other:?} at {name}: the frames differ, first at cell {at:?} of 80 a row:\n{text}"); + } + } + } +} + /// The board's claims in a session whose answer streams a `board` call that finishes, one that fails, and a `board` /// fence, then takes another theme; what each checkpoint sees, with the composer's random placeholder blanked. fn claimed(run: Run) -> Vec<(&'static str, Seen)> {