diff --git a/packages/cli-rs/crates/plugin/src/mailbox.rs b/packages/cli-rs/crates/plugin/src/mailbox.rs index f63e4707c51..7d9dce2adcd 100644 --- a/packages/cli-rs/crates/plugin/src/mailbox.rs +++ b/packages/cli-rs/crates/plugin/src/mailbox.rs @@ -36,6 +36,8 @@ pub struct Stats { /// Sends refused as `Full`. pub refused: u64, pub closed: bool, + /// A thread's panic report waits, to be taken after the messages put in before it. + pub reported: bool, } /// The longest panic report the host keeps, in bytes. @@ -282,7 +284,13 @@ impl Mailbox { pub fn stats(&self) -> Stats { let state = self.state(); - Stats { waiting: state.entries.len(), high: state.high, refused: state.refused, closed: state.closed } + Stats { + waiting: state.entries.len(), + high: state.high, + refused: state.refused, + closed: state.closed, + reported: state.panic.is_some(), + } } } @@ -369,13 +377,13 @@ mod tests { } mailbox.panicked("é".repeat(REPORT)); mailbox.panicked("second".to_owned()); - assert_eq!(mailbox.stats(), Stats { waiting: 3, high: 3, refused: 0, closed: false }); + assert_eq!(mailbox.stats(), Stats { waiting: 3, high: 3, refused: 0, closed: false, reported: true }); let (_, panic) = mailbox.take(); assert!(panic.is_some_and(|panic| panic.len() <= REPORT && panic.starts_with('é'))); assert!(mailbox.put(Some("k"), Message::Json("1".into())).is_ok()); mailbox.close(); mailbox.panicked("late".to_owned()); - assert_eq!(mailbox.stats(), Stats { waiting: 0, high: 3, refused: 0, closed: true }); + assert_eq!(mailbox.stats(), Stats { waiting: 0, high: 3, refused: 0, closed: true, reported: false }); assert!(matches!(mailbox.put(None, Message::Json("9".into())), Err((Refused::Closed, _)))); let (messages, panic) = mailbox.take(); assert!(messages.is_empty() && panic.is_none()); diff --git a/packages/cli-rs/crates/plugin/src/pipe/child.rs b/packages/cli-rs/crates/plugin/src/pipe/child.rs index d199c345691..d782e37243d 100644 --- a/packages/cli-rs/crates/plugin/src/pipe/child.rs +++ b/packages/cli-rs/crates/plugin/src/pipe/child.rs @@ -1258,7 +1258,7 @@ mod tests { // reaches the host. #[test] fn a_child_that_does_not_say_hello_is_refused() { - let mut frames = control(&Up::Pong { seq: 1, waiting: false }); + let mut frames = control(&Up::Pong { seq: 1, waiting: false, retiring: false }); frames.extend(hello()); let (child, _dir) = fake(&frames, Limits::default()); let events = take_until(&child, |events| gone(events).is_some()); diff --git a/packages/cli-rs/crates/plugin/src/pipe/host.ts b/packages/cli-rs/crates/plugin/src/pipe/host.ts index 2e9a3395e4d..da9fcb7ab29 100644 --- a/packages/cli-rs/crates/plugin/src/pipe/host.ts +++ b/packages/cli-rs/crates/plugin/src/pipe/host.ts @@ -369,6 +369,9 @@ const plugins = new Map() const generations = new Map() // How long a component's command outlives its mount, in milliseconds (P8). const UNCOMMAND = 250 +// The commands whose retirement waits for its `UNCOMMAND`, across plugins, which a ping reports (compat-harness): a +// test waits until none does, rather than for the 250 ms on this process's clock. +let retiring = 0 // The failed reply of a fetch the host did not take, as `REFUSED` in `lib.rs` says it. const REFUSED = "the host takes no request now: the effects that wait fill its queue" // What a fetch rejects with when the host cancels it, as the mount that asked for it closed. @@ -716,7 +719,9 @@ function context(plugin: Plugin) { plugin.afterCall.push(run) }, uncommand(name: string) { + retiring += 1 setTimeout(() => { + retiring -= 1 const index = plugin.commands.findIndex((command) => command.name === name) if (index === -1) return const command = plugin.commands[index]! @@ -1391,7 +1396,7 @@ async function handle(message: Json) { [...plugin.mounts.values()].some((mount) => mount.inflight || mount.dirty) || [...plugin.claimMounts.values()].some((mount) => mount.inflight || mount.dirty), ) - return send({ type: "pong", seq: message.seq, waiting }) + return send({ type: "pong", seq: message.seq, waiting, retiring: retiring > 0 }) } } } diff --git a/packages/cli-rs/crates/plugin/src/pipe/mod.rs b/packages/cli-rs/crates/plugin/src/pipe/mod.rs index 6a674501367..b8ffc4358af 100644 --- a/packages/cli-rs/crates/plugin/src/pipe/mod.rs +++ b/packages/cli-rs/crates/plugin/src/pipe/mod.rs @@ -763,6 +763,8 @@ pub struct Scripts { stopped: RefCell>, /// The ping last answered, and whether a tree waited then. pong: Cell>, + /// Whether the last pong said a command still lingers on the JS host, to retire ([`Scripts::retiring`]). + retiring: Cell, hold: RefCell, /// What the input put back now goes past ([`Keyed`]). replayed: RefCell, @@ -959,6 +961,7 @@ impl Scripts { ends: RefCell::default(), stopped: RefCell::default(), pong: Cell::new(None), + retiring: Cell::new(false), hold: RefCell::default(), replayed: RefCell::default(), recent: RefCell::default(), @@ -1046,6 +1049,13 @@ impl Scripts { self.pong.get().filter(|(answered, _)| *answered >= seq).map(|(_, waiting)| waiting) } + /// Whether the JS host still retires a command a component dropped, by the last ping it answered: its + /// registrations change once more when it does. + #[doc(hidden)] + pub fn retiring(&self) -> bool { + self.retiring.get() + } + /// Whether ping `seq` waits for its answer: not once answered, nor when a full overflow refused it, which no answer /// follows. pub fn pinging(&self, seq: u64) -> bool { @@ -1867,7 +1877,8 @@ impl Scripts { | Up::Fail { .. } | Up::Taking { .. } | Up::Manifest { .. }) => self.from_plugin(up), - Up::Pong { seq, waiting } => { + Up::Pong { seq, waiting, retiring } => { + self.retiring.set(retiring); self.take_waiting(seq); self.pong.set(Some((seq, waiting))); } @@ -2067,6 +2078,8 @@ impl Scripts { log::error!("{why}"); // A ping on its way is lost with it: answered, as one a tree waits behind, so its sender asks again. self.pong.set(Some((self.seq.get(), true))); + // Its timers went with it. + self.retiring.set(false); let started = std::mem::take(&mut *self.started.borrow_mut()); let mut waiting: Vec<(u64, Waiting)> = self.waiting.borrow_mut().drain().collect(); waiting.sort_by_key(|(seq, _)| *seq); diff --git a/packages/cli-rs/crates/plugin/src/pipe/proto.rs b/packages/cli-rs/crates/plugin/src/pipe/proto.rs index dababac16c1..179e2ed55f2 100644 --- a/packages/cli-rs/crates/plugin/src/pipe/proto.rs +++ b/packages/cli-rs/crates/plugin/src/pipe/proto.rs @@ -368,8 +368,14 @@ pub enum Up { listener: Listener, on: bool, }, - /// Everything before ping `seq` was handled; `waiting` when a tree waits for the host to adopt the last one. - Pong { seq: u64, waiting: bool }, + /// Everything before ping `seq` was handled; `waiting` when a tree waits for the host to adopt the last one, and + /// `retiring` when a command a component dropped still lingers, to go a moment later (`UNCOMMAND`). + Pong { + seq: u64, + waiting: bool, + #[serde(default)] + retiring: bool, + }, Log { #[serde(default, skip_serializing_if = "Option::is_none")] id: Option, diff --git a/packages/cli-rs/crates/tui/tests/all/plugin_compat.rs b/packages/cli-rs/crates/tui/tests/all/plugin_compat.rs index ca5aa9052f4..0daaaa21837 100644 --- a/packages/cli-rs/crates/tui/tests/all/plugin_compat.rs +++ b/packages/cli-rs/crates/tui/tests/all/plugin_compat.rs @@ -36,8 +36,7 @@ fn probe(config: Config) -> (Pipe, PathBuf) { /// waits for it without the JS host saying so to a ping (its mount is neither dirty nor in flight), so they may draw /// after a settle. fn drawn(pipe: &Pipe) { - pipe.harness.wait("the probe's claims drew", || { - pipe.harness.app.advance(Duration::from_millis(1)); + pipe.until("the probe's claims drew", || { line(pipe, "v2probe commands").is_some() && line(pipe, "v2probe route").is_some() }); pipe.settle(); @@ -63,10 +62,7 @@ fn calls(log: &Path) -> Vec { /// [`calls`] once there are `count`: a plugin's code after an answer it awaited runs some promise turns after the call /// that answered it, which a settle's ping may pass. fn logged(pipe: &Pipe, log: &Path, count: usize) -> Vec { - pipe.harness.wait(&format!("{count} calls logged"), || { - pipe.harness.app.advance(Duration::from_millis(1)); - calls(log).len() >= count - }); + pipe.until(&format!("{count} calls logged"), || calls(log).len() >= count); calls(log) } @@ -102,16 +98,10 @@ fn the_guides_typescript_hello_runs_on_the_js_host() { let pipe = open(&[("hello", None)], config, (80, 30)); pipe.active("example.ts-hello"); let (app, tui) = (&pipe.harness.app, &pipe.harness.tui); - pipe.harness.wait("its line closes the home footer", || { - app.advance(Duration::from_millis(1)); - app.text().lines().any(|line| line.trim_end().ends_with("hello")) - }); + pipe.until("its line closes the home footer", || app.text().lines().any(|line| line.trim_end().ends_with("hello"))); pipe.settle(); let toasted = |what: &str| { - pipe.harness.wait(what, || { - app.advance(Duration::from_millis(1)); - tui.toasts.current().is_some() - }); + pipe.until(what, || tui.toasts.current().is_some()); assert_eq!(toasts(&pipe), ["Hello from a plugin"]); }; tui.keymap.dispatch("hello.greet", None); @@ -146,10 +136,7 @@ fn a_v2_plugins_commands_arrive_once_each_in_order() { assert_eq!(logged(&pipe, &log, sent.len()), sent); // The last item shows; a restart resets the plugin's state, as it resets any plugin's on the JS host. The restarted // footer's first render waits for its reads, which a settle does not (`drawn`). - pipe.harness.wait("the restarted footer drew", || { - pipe.harness.app.advance(Duration::from_millis(1)); - footer(&pipe).ends_with("33|") - }); + pipe.until("the restarted footer drew", || footer(&pipe).ends_with("33|")); let shown = footer(&pipe); assert!(shown.starts_with("v2probe ") && shown.ends_with("33|"), "{shown}"); assert_eq!(pipe.status(PROBE), Some(Status::Active)); @@ -236,12 +223,7 @@ fn confirm_prompt_and_alert_answer_their_promises() { let (pipe, log) = probe(config); let app = &pipe.harness.app; // Each next dialog opens in the plugin's code after the answer it awaited, some promise turns after the answer. - let shows = |text: &str| { - pipe.harness.wait(text, || { - app.advance(Duration::from_millis(1)); - app.text().contains(text) - }) - }; + let shows = |text: &str| pipe.until(text, || app.text().contains(text)); pipe.harness.tui.keymap.dispatch("v2probe.ask", None); shows("Go on?"); app.press("return"); @@ -269,16 +251,10 @@ fn a_field_shown_and_focused_in_one_handler_takes_the_focus_once_shown() { pipe.scripts().tamper(opencode_plugin::pipe::Tamper::Hold); tui.keymap.dispatch("v2probe.find", None); // The call answered, with its focus; a settle would wait for the held revision. - pipe.harness.wait("the call answered", || { - app.advance(Duration::from_millis(1)); - pipe.scripts().idle() - }); + pipe.until("the call answered", || pipe.scripts().idle()); // Its revision may come after the answer: a replay before it delivers nothing, and the revision is then held for // good (pipe-flakes). - pipe.harness.wait("the revision is held", || { - app.advance(Duration::from_millis(1)); - pipe.scripts().kept() - }); + pipe.until("the revision is held", || pipe.scripts().kept()); for _ in 0..20 { app.advance(Duration::from_millis(1)); app.render(); @@ -305,22 +281,13 @@ fn a_batch_of_a_closed_mount_moves_nothing_where_the_activations_would() { let route = Route::Plugin { id: "example.closed".into(), name: "pf".into() }; for _ in 0..2 { tui.navigate(route.clone()); - pipe.harness.wait("the route shows", || { - app.advance(Duration::from_millis(1)); - app.text().contains("closed page") && mounts().len() == 1 - }); + pipe.until("the route shows", || app.text().contains("closed page") && mounts().len() == 1); pipe.settle(); tui.navigate(Route::Home); - pipe.harness.wait("the route closes", || { - app.advance(Duration::from_millis(1)); - mounts().is_empty() - }); + pipe.until("the route closes", || mounts().is_empty()); } tui.navigate(route); - pipe.harness.wait("the route shows again", || { - app.advance(Duration::from_millis(1)); - mounts().len() == 1 - }); + pipe.until("the route shows again", || mounts().len() == 1); pipe.settle(); let focus = |command: &str| { tui.keymap.dispatch("pf.guard", None); @@ -384,7 +351,7 @@ fn a_thrown_calls_mount_effects_go_with_it() { tui.keymap.dispatch("pf.throw", None); pipe.harness.wait("the throw failed the plugin", || { pipe.scripts().release_one(); - app.advance(Duration::from_millis(1)); + crate::plugin_pipe::still(app); app.render(); matches!(pipe.status("example.ordered"), Some(Status::Failed(_))) }); @@ -412,10 +379,7 @@ fn a_pending_focus_goes_when_the_user_takes_the_focus_back() { pipe.settle(); pipe.scripts().tamper(opencode_plugin::pipe::Tamper::Hold); tui.keymap.dispatch("sp.show", None); - pipe.harness.wait("the call answered", || { - app.advance(Duration::from_millis(1)); - pipe.scripts().idle() - }); + pipe.until("the call answered", || pipe.scripts().idle()); assert!(!app.text().contains("sp field"), "{moved}: the revision was not held:\n{}", app.text()); match by { "ctrl+c" => app.press("ctrl+c"), @@ -453,10 +417,7 @@ fn a_reload_gives_the_focus_back_only_to_a_field_that_held_it() { let reload = || { let before = more(); tui.keymap.dispatch("rf.reload", None); - pipe.harness.wait("the registrations changed", || { - pipe.harness.app.advance(Duration::from_millis(1)); - more() > before - }); + pipe.until("the registrations changed", || more() > before); pipe.settle(); }; pipe.harness.wait_for("refocus slot"); @@ -632,10 +593,8 @@ fn a_route_draws_a_bordered_box_and_a_scroll_with_its_data() { let Some(config) = config() else { return }; let (pipe, _log) = probe(config); pipe.harness.tui.keymap.dispatch("v2probe.page", None); - pipe.settle(); - // The page registers a command as it renders, which settles once the command's 250 ms passed. - std::thread::sleep(Duration::from_millis(400)); - pipe.settle(); + // The page registers a command as it renders, which settles once the command it replaced retired. + pipe.retired(); assert_eq!(pipe.status(PROBE), Some(Status::Active)); let screen = pipe.harness.app.text(); assert_eq!(pipe.harness.tui.route(), Route::Plugin { id: PROBE.to_owned(), name: "page".to_owned() }); @@ -734,10 +693,7 @@ fn a_dialog_of_its_own_shows_its_jsx_and_closes() { let (x, y) = pipe.harness.find("bump it"); pipe.harness.app.click(x, y); // The layer sends the changed dialog in a microtask after the click's handler. - pipe.harness.wait("the dialog follows its signal", || { - pipe.harness.app.advance(Duration::from_millis(1)); - pipe.harness.app.text().contains("shown 1") - }); + pipe.until("the dialog follows its signal", || pipe.harness.app.text().contains("shown 1")); pipe.harness.app.press("escape"); pipe.settle(); assert!(!pipe.harness.app.text().contains("Own dialog"), "{}", pipe.harness.app.text()); @@ -762,10 +718,8 @@ fn a_components_command_lives_with_its_mount_across_a_reload() { let tui = &pipe.harness.tui; assert_eq!(line(&pipe, "v2probe route").as_deref(), Some("v2probe route home")); tui.keymap.dispatch("v2probe.page", None); - pipe.settle(); - // The change the page's command made settles within the 250 ms a command lingers. - std::thread::sleep(Duration::from_millis(400)); - pipe.settle(); + // The change the page's command made settles once the command it replaced retired. + pipe.retired(); let quiet = opencode_plugin::pipe::crossed(); std::thread::sleep(Duration::from_millis(400)); for _ in 0..5 { @@ -789,8 +743,7 @@ fn a_components_command_lives_with_its_mount_across_a_reload() { tui.keymap.dispatch("v2probe.onpage", None); pipe.settle(); assert_eq!(calls(&log), [json!({ "onpage": true })], "the page's command ran after the page left"); - std::thread::sleep(Duration::from_millis(400)); - pipe.settle(); + pipe.retired(); assert!(!matches!(tui.keymap.dispatch("v2probe.onpage", None), opentui::keymap::RunResult::Ok), "it stayed"); } @@ -802,9 +755,7 @@ fn a_page_whose_commands_have_no_id_draws_and_settles() { let (pipe, log) = probe(config); let tui = &pipe.harness.tui; tui.keymap.dispatch("v2probe.inline", None); - pipe.settle(); - std::thread::sleep(Duration::from_millis(400)); - pipe.settle(); + pipe.retired(); let quiet = opencode_plugin::pipe::crossed(); std::thread::sleep(Duration::from_millis(400)); for _ in 0..5 { @@ -826,9 +777,7 @@ fn a_page_that_names_its_commands_anew_each_mount_settles() { let Some(config) = config() else { return }; let (pipe, _log) = probe(config); pipe.harness.tui.keymap.dispatch("v2probe.churn", None); - pipe.settle(); - std::thread::sleep(Duration::from_millis(400)); - pipe.settle(); + pipe.retired(); let quiet = opencode_plugin::pipe::crossed(); std::thread::sleep(Duration::from_millis(400)); for _ in 0..5 { @@ -847,10 +796,7 @@ fn a_plugin_whose_registrations_never_settle_fails() { let Some(config) = config() else { return }; let (pipe, _log) = probe(config); pipe.harness.tui.keymap.dispatch("v2probe.restless", None); - pipe.harness.wait("the churn failed the plugin", || { - pipe.harness.app.advance(Duration::from_millis(1)); - matches!(pipe.status(PROBE), Some(Status::Failed(_))) - }); + pipe.until("the churn failed the plugin", || matches!(pipe.status(PROBE), Some(Status::Failed(_)))); let Some(Status::Failed(error)) = pipe.status(PROBE) else { unreachable!() }; assert!(error.contains("changed its registrations more than 64 times"), "{error}"); } @@ -875,10 +821,8 @@ fn a_change_of_commands_alone_keeps_the_route_and_its_field() { assert!(field.is_some(), "the field has no focus:\n{}", app.text()); // The first key mounts the matches and their command: the keys after it go on while the change crosses. app.type_text("notes"); - pipe.settle(); - // Past the 250 ms a command the plugin dropped lingers. - std::thread::sleep(Duration::from_millis(400)); - pipe.settle(); + // Once the command the plugin dropped retired. + pipe.retired(); assert!(app.text().contains("query notes."), "{}", app.text()); assert_eq!(focused(), field, "the field was built again"); assert!(matches!(tui.keymap.dispatch("v2probe.clear", None), opentui::keymap::RunResult::Ok)); @@ -897,21 +841,13 @@ fn a_session_panel_stays_open_across_a_reload() { let pipe = crate::plugin_pipe::open_on(&[("v2probe", Some(options))], config, (120, 30), Vec::new(), server); pipe.active(PROBE); pipe.harness.tui.navigate(Route::Session { id: opencode_api::generated::SessionID("ses_a".into()) }); - pipe.harness.wait("the session shows", || { - pipe.harness.app.advance(Duration::from_millis(1)); - matches!(pipe.harness.tui.route(), Route::Session { .. }) - }); + pipe.until("the session shows", || matches!(pipe.harness.tui.route(), Route::Session { .. })); pipe.settle(); pipe.harness.tui.keymap.dispatch("v2probe.panel", None); assert_eq!(logged(&pipe, &log, 1), [json!({ "opened": true })]); - pipe.settle(); - std::thread::sleep(Duration::from_millis(400)); - pipe.settle(); + pipe.retired(); let shows = || pipe.harness.app.text().contains("probe panel probe.panel"); - pipe.harness.wait("the panel shows", || { - pipe.harness.app.advance(Duration::from_millis(1)); - shows() - }); + pipe.until("the panel shows", shows); assert!(pipe.harness.tui.panels.current().is_some(), "the panel closed"); assert_eq!(pipe.status(PROBE), Some(Status::Active)); } @@ -928,17 +864,11 @@ fn a_session_panel_toggles_focuses_and_closes_itself() { let pipe = crate::plugin_pipe::open_on(&[("v2probe", Some(options))], config, (120, 30), Vec::new(), server); pipe.active(PROBE); pipe.harness.tui.navigate(Route::Session { id: opencode_api::generated::SessionID("ses_a".into()) }); - pipe.harness.wait("the session shows", || { - pipe.harness.app.advance(Duration::from_millis(1)); - matches!(pipe.harness.tui.route(), Route::Session { .. }) - }); + pipe.until("the session shows", || matches!(pipe.harness.tui.route(), Route::Session { .. })); pipe.settle(); pipe.harness.tui.keymap.dispatch("v2probe.panel", None); assert_eq!(logged(&pipe, &log, 1), [json!({ "opened": true })]); - pipe.harness.wait("the panel shows", || { - pipe.harness.app.advance(Duration::from_millis(1)); - pipe.harness.app.text().contains("probe panel probe.panel") - }); + pipe.until("the panel shows", || pipe.harness.app.text().contains("probe panel probe.panel")); pipe.settle(); let run = |command: &str| { pipe.harness.tui.keymap.dispatch(command, None); @@ -1082,7 +1012,7 @@ fn a_disabled_commands_key_is_not_held() { fn a_restarted_js_hosts_commands_take_its_setups_states() { let Some(config) = config() else { return }; let (pipe, _log) = probe(config); - let (harness, tui) = (&pipe.harness, &pipe.harness.tui); + let tui = &pipe.harness.tui; let reachable = || tui.keymap.commands().iter().any(|command| command.id == "v2probe.caret"); tui.keymap.dispatch("v2probe.caretOn", Some("on")); pipe.settle(); @@ -1092,10 +1022,7 @@ fn a_restarted_js_hosts_commands_take_its_setups_states() { pipe.scripts().fault(Fault::Swap); tui.keymap.dispatch("v2probe.add", Some("x")); tui.keymap.dispatch("v2probe.add", Some("y")); - harness.wait("the JS host started again", || { - harness.app.advance(Duration::from_millis(1)); - pipe.scripts().pid().is_some_and(|now| Some(now) != pid) - }); + pipe.until("the JS host started again", || pipe.scripts().pid().is_some_and(|now| Some(now) != pid)); pipe.settle(); assert_eq!(pipe.status(PROBE), Some(Status::Active)); assert!(!reachable(), "the restarted plugin's disabled command is reachable"); @@ -1109,10 +1036,7 @@ fn generation(pipe: &Pipe) -> Option { /// Waits until the host took a reload of the probe's registrations past activation `before`. The reload is the host's /// own work after the JS host answered (the loader reconciles), so a settle that saw the JS host idle may come first. fn reloaded(pipe: &Pipe, before: Option) { - pipe.harness.wait("the probe's registrations reloaded", || { - pipe.harness.app.advance(Duration::from_millis(1)); - generation(pipe).is_some_and(|now| Some(now) != before) - }); + pipe.until("the probe's registrations reloaded", || generation(pipe).is_some_and(|now| Some(now) != before)); pipe.settle(); } @@ -1127,8 +1051,7 @@ fn a_state_set_as_the_registrations_reload_goes_to_the_new_guest() { let before = generation(&pipe); pipe.scripts().hold_answers(true); tui.keymap.dispatch("v2probe.lateCaret", None); - harness.wait("the reload and the state came", || { - harness.app.advance(Duration::from_millis(1)); + pipe.until("the reload and the state came", || { let types = pipe.scripts().held_types(); types .iter() @@ -1264,13 +1187,9 @@ fn a_js_host_that_ends_while_keys_are_held_releases_them() { pipe.settle(); harness.app.type_text("%ab"); assert_eq!(harness.text(), "", "the keys after the bound key wait for its command"); - harness.wait("the ended JS host released the keys", || { - harness.app.advance(Duration::from_millis(1)); - harness.text() == "%ab" - }); + pipe.until("the ended JS host released the keys", || harness.text() == "%ab"); // The plugin whose command ended it fails, blamed, as any such does. - harness.wait("the end was noticed", || { - harness.app.advance(Duration::from_millis(1)); + pipe.until("the end was noticed", || { pipe.scripts().pid() != pid && matches!(pipe.status(PROBE), Some(Status::Failed(_))) }); pipe.settle(); @@ -1350,10 +1269,7 @@ fn two_quick_reloads_leave_the_plugin_working() { tui.keymap.dispatch("v2probe.twice", None); // Both reloads wait for the UI thread, which takes them in one go; then both commands are the keymap's. let registered = |name: &str| tui.keymap.commands().iter().any(|command| command.id == name); - pipe.harness.wait("both reloads were taken", || { - pipe.harness.app.advance(Duration::from_millis(1)); - registered("v2probe.first") && registered("v2probe.second") - }); + pipe.until("both reloads were taken", || registered("v2probe.first") && registered("v2probe.second")); pipe.settle(); for name in ["v2probe.first", "v2probe.second"] { tui.keymap.dispatch(name, None); @@ -1371,10 +1287,7 @@ fn a_v2_render_that_spins_fails_its_plugin_alone() { let (pipe, _log) = probe(config); let pid = pipe.scripts().pid().unwrap(); pipe.harness.tui.keymap.dispatch("v2probe.loop", None); - pipe.harness.wait("the spin ended the JS host", || { - pipe.harness.app.advance(Duration::from_millis(1)); - matches!(pipe.status(PROBE), Some(Status::Failed(_))) - }); + pipe.until("the spin ended the JS host", || matches!(pipe.status(PROBE), Some(Status::Failed(_)))); let Some(Status::Failed(error)) = pipe.status(PROBE) else { unreachable!() }; assert!(error.contains("ended the JS host"), "{error}"); assert_ne!(pipe.scripts().pid(), Some(pid)); @@ -1394,10 +1307,7 @@ fn a_v2_plugin_that_floods_fails_alone() { pipe.settle(); // The UI thread takes nothing while the flood comes. std::thread::sleep(Duration::from_millis(1500)); - pipe.harness.wait("the flood ended the JS host", || { - pipe.harness.app.advance(Duration::from_millis(1)); - matches!(pipe.status(PROBE), Some(Status::Failed(_))) - }); + pipe.until("the flood ended the JS host", || matches!(pipe.status(PROBE), Some(Status::Failed(_)))); pipe.settle(); let Some(Status::Failed(error)) = pipe.status(PROBE) else { unreachable!() }; assert!(error.contains("flooded the host with effects"), "{error}"); @@ -1690,8 +1600,7 @@ fn a_dependency_changed_after_approval_runs_nothing_unapproved() { opencode_tui::plugins::host::reconcile(&pipe.harness.tui); pipe.settle(); pipe.scripts().kill(); - pipe.harness.wait("the plugin was set up again", || { - pipe.harness.app.advance(Duration::from_millis(1)); + pipe.until("the plugin was set up again", || { std::fs::read_to_string(&marker).unwrap_or_default().lines().count() == 2 }); assert_eq!(std::fs::read_to_string(&marker).unwrap(), "approved\napproved\n"); @@ -1768,10 +1677,7 @@ fn a_plugin_imports_runtime_modules_and_calls_its_own_rpc() { let pipe = Pipe { harness, root }; pipe.active("example.rpc"); pipe.harness.tui.keymap.dispatch("rpc.say", None); - pipe.harness.wait("the RPC answered", || { - pipe.harness.app.advance(Duration::from_millis(1)); - std::fs::read_to_string(&log).is_ok_and(|text| text.ends_with('\n')) - }); + pipe.until("the RPC answered", || std::fs::read_to_string(&log).is_ok_and(|text| text.ends_with('\n'))); assert_eq!(std::fs::read_to_string(&log).unwrap(), "{\"said\":\"hi\"}\n"); let called = requests.lock().unwrap().iter().find(|seen| seen.path == "/api/rpc/echo/say").cloned().unwrap(); assert_eq!((called.method.as_str(), called.body), ("POST", json!({ "input": { "text": "hi" } }))); @@ -1796,10 +1702,7 @@ fn a_local_plugin_that_fails_is_listed_by_its_package() { let entries = pipe.harness.tui.plugins.entries(); entries.into_iter().find(|entry| entry.target.as_deref().is_some_and(|target| target.ends_with("broken"))) }; - pipe.harness.wait("the load failed", || { - pipe.harness.app.advance(Duration::from_millis(1)); - entry().is_some_and(|entry| matches!(entry.status, Status::Failed(_))) - }); + pipe.until("the load failed", || entry().is_some_and(|entry| matches!(entry.status, Status::Failed(_)))); let entry = entry().unwrap(); assert_eq!(entry.id, "opencode-broken-thing", "{entry:?}"); } @@ -1835,10 +1738,7 @@ fn a_plugin_with_eighty_stores_reads_each() { let pipe = Pipe { harness, root }; // The UI thread takes nothing while the setup runs, as when it is busy at startup: once the hello, the load and // the setup went, what the setup sends waits for it. - pipe.harness.wait("the setup went to the JS host", || { - pipe.harness.app.advance(Duration::from_millis(1)); - opencode_plugin::pipe::crossed().sent - sent >= 3 - }); + pipe.until("the setup went to the JS host", || opencode_plugin::pipe::crossed().sent - sent >= 3); std::thread::sleep(Duration::from_millis(1500)); pipe.active("example.shards"); pipe.settle(); @@ -1897,10 +1797,7 @@ fn a_plugin_past_the_stored_values_bound_goes_on_and_follows_a_large_value() { pipe.active("example.big"); pipe.settle(); let marked = |what: &str, done: &dyn Fn(&str) -> bool| { - pipe.harness.wait(what, || { - pipe.harness.app.advance(Duration::from_millis(1)); - std::fs::read_to_string(&marker).is_ok_and(|text| done(&text)) - }) + pipe.until(what, || std::fs::read_to_string(&marker).is_ok_and(|text| done(&text))) }; pipe.harness.tui.keymap.dispatch("big.small", None); marked("the small value was stored", &|text| text == "5"); @@ -1938,11 +1835,12 @@ fn a_store_update_of_a_value_answered_past_a_frame_keeps_it() { let root = opencode_util::temp::tempdir().unwrap(); let dir = root.path().join("config/plugins/notes"); std::fs::create_dir_all(&dir).unwrap(); - let marker = root.path().join("notes.lengths"); + let (marker, part) = (root.path().join("notes.lengths"), root.path().join("notes.lengths.part")); let module = format!( "export default {{ id: \"example.notes\", setup(context) {{\n\ const [store, update] = context.storage.store(\"notes\", {{ initial: {{ items: [] }} }})\n\ - const write = (text) => require(\"node:fs\").writeFileSync({marker:?}, text)\n\ + const fs = require(\"node:fs\")\n\ + const write = (text) => {{ fs.writeFileSync({part:?}, text); fs.renameSync({part:?}, {marker:?}) }}\n\ context.keymap.layer(() => ({{ mode: \"global\", commands: [{{ id: \"notes.append\", title: \"Append\", run: () =>\n\ update((draft) => {{ draft.items.push(\"tail\") }}).then(() => write(JSON.stringify(store.items.map((item) => item.length))),\n\ (error) => write(`refused ${{error.message}}`)) }}] }}))\n\ @@ -1961,10 +1859,8 @@ fn a_store_update_of_a_value_answered_past_a_frame_keeps_it() { pipe.active("example.notes"); pipe.settle(); pipe.harness.tui.keymap.dispatch("notes.append", None); - pipe.harness.wait("the update ended", || { - pipe.harness.app.advance(Duration::from_millis(1)); - marker.exists() - }); + // The plugin writes the marker whole, then renames it into place: it shows only with its text (compat-harness). + pipe.until("the update ended", || marker.exists()); assert_eq!(std::fs::read_to_string(&marker).unwrap(), "[110000,4]"); opencode_tui::plugins::host::flush_storage(&pipe.harness.tui); let stored: Value = serde_json::from_str(&opencode_plugin::Host::storage_read(&*host, key).unwrap()).unwrap(); diff --git a/packages/cli-rs/crates/tui/tests/all/plugin_mail.rs b/packages/cli-rs/crates/tui/tests/all/plugin_mail.rs index 4e90d92fced..f385bfb1dcc 100644 --- a/packages/cli-rs/crates/tui/tests/all/plugin_mail.rs +++ b/packages/cli-rs/crates/tui/tests/all/plugin_mail.rs @@ -540,7 +540,6 @@ fn a_refilled_mailbox_holds_back_no_report_and_no_wake() { assert_eq!(woken.first(), Some(&1), "{carrier:?}: the wake ran after the messages {woken:?}"); assert!(refill > 0, "{carrier:?}: the refills ended first"); // A report: the thread's panic comes after one message, while the refills go on. - let before = mail(|mail| mail.arrived.len()); let (panicked, done) = std::sync::mpsc::channel(); mail(|mail| mail.spawner.clone()) .unwrap() @@ -550,6 +549,16 @@ fn a_refilled_mailbox_holds_back_no_report_and_no_wake() { }) .unwrap(); done.recv_timeout(Duration::from_secs(10)).unwrap(); + // The report counts from when it is put in, not from when the thread began to panic: under load the refills + // the frames drain in between were counted as coming before it (compat-harness). Nothing drains until then. + let before = { + let begun = std::time::Instant::now(); + while !opencode_plugin::mailbox::stats(ID).is_some_and(|stats| stats.reported) { + assert!(begun.elapsed() < Duration::from_secs(10), "{carrier:?}: no report was put in"); + std::thread::sleep(Duration::from_millis(1)); + } + mail(|mail| mail.arrived.len()) + }; harness.wait("the panic failed the plugin", || { harness.app.advance(Duration::from_millis(1)); harness.app.render(); diff --git a/packages/cli-rs/crates/tui/tests/all/plugin_pipe.rs b/packages/cli-rs/crates/tui/tests/all/plugin_pipe.rs index 0873a7f25d3..8ba26fa0f95 100644 --- a/packages/cli-rs/crates/tui/tests/all/plugin_pipe.rs +++ b/packages/cli-rs/crates/tui/tests/all/plugin_pipe.rs @@ -55,6 +55,24 @@ impl Pipe { self.harness.tui.plugins.entries().into_iter().find(|entry| entry.id == id).map(|entry| entry.status) } + /// Waits until `done` holds, turning the UI thread without moving its clock ([`still`]). The wait for anything of the + /// JS host's or the loader's (a reply, a reload, a drain, a load) takes real time, which no deadline of the host's + /// (the key hold's 50 ms, a debounce) may see: a row that advanced the clock as it polled raced those deadlines + /// against the machine's load (compat-harness). A row that wants a deadline to pass advances the clock itself. + pub fn until(&self, what: &str, done: impl Fn() -> bool) { + until(&self.harness, what, done); + } + + /// Waits until the JS host retires no command a component dropped ([`Scripts::retiring`]): such a command lingers + /// 250 ms on the JS host's own clock (`UNCOMMAND`), then its registrations change once more. A row waited for it + /// with a sleep (compat-harness). + pub fn retired(&self) { + self.until("the JS host retired its dropped commands", || { + self.settle(); + !self.scripts().retiring() + }); + } + /// Waits until plugin `id` is active. pub fn active(&self, id: &str) { let entries = || { @@ -62,10 +80,9 @@ impl Pipe { self.harness.tui.plugins.entries().into_iter().map(|entry| format!("{} {:?}", entry.id, entry.status)); entries.collect::>().join("\n") }; - self.harness.wait(&format!("{id} is active"), || { + self.until(&format!("{id} is active"), || { let failed = matches!(self.status(id), Some(opencode_plugin::Status::Failed(_))); assert!(!failed, "{id} failed:\n{}", entries()); - self.harness.app.advance(Duration::from_millis(1)); self.status(id) == Some(opencode_plugin::Status::Active) }); } @@ -155,7 +172,25 @@ pub fn config() -> Option { Some(config) } -/// Waits until the JS host handled everything sent before, and no tree waits for the host; then three frames. +/// [`Pipe::until`] for a harness. +pub fn until(harness: &Harness, what: &str, done: impl Fn() -> bool) { + harness.wait(what, || { + still(&harness.app); + done() + }); +} + +/// One turn of the UI thread that moves no clock: what came from the JS host, the loader's threads and the deferred +/// work runs, and a frame paints if one is wanted, but no timer comes due that was not due already. +pub(crate) fn still(app: &TestApp) { + app.advance(Duration::ZERO); + if app.needs_frame() { + app.render(); + } +} + +/// Waits until the JS host handled everything sent before, and no tree waits for the host; then three frames. However +/// long the JS host takes, the clock moves only by the frames' turns, never as it waits ([`Pipe::until`]). pub fn settle(harness: &Harness, scripts: &Scripts) { settle_app(&harness.app, &harness.tui, scripts); } @@ -191,7 +226,7 @@ pub(crate) fn settle_app(app: &TestApp, tui: &Tui, scripts: &Scripts) { // as the JS host began to read a full overflow waited for good). while pong.is_none() && scripts.pinging(seq) { unsettled(); - turn_app(app); + still(app); pong = scripts.ponged(seq); if pong.is_none() { std::thread::sleep(Duration::from_micros(200)); @@ -215,7 +250,7 @@ fn walked_app(app: &TestApp, scripts: &Scripts) { let begun = Instant::now(); while scripts.walking() { assert!(begun.elapsed() < Duration::from_secs(20), "a walk of a plugin's tree never ended"); - turn_app(app); + still(app); std::thread::sleep(Duration::from_micros(200)); } for _ in 0..3 {