Skip to content

Commit 128e1b0

Browse files
committed
test(cli-rs): save after the approved copy's load is sent, not before it is staged
1 parent 4a9dcff commit 128e1b0

3 files changed

Lines changed: 21 additions & 3 deletions

File tree

‎packages/cli-rs/crates/plugin/src/pipe/mod.rs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,12 @@ impl Scripts {
658658
self.waiting.borrow().values().all(|waiting| matches!(waiting, Waiting::Ping))
659659
}
660660

661+
/// Whether a load of `target` was sent and the JS host has not answered it.
662+
#[doc(hidden)]
663+
pub fn loading(&self, target: &str) -> bool {
664+
self.waiting.borrow().values().any(|waiting| matches!(waiting, Waiting::Load(known) if known == target))
665+
}
666+
661667
/// How many answers the host waits for, pings included.
662668
#[doc(hidden)]
663669
pub fn waiting(&self) -> usize {

‎packages/cli-rs/crates/tui/tests/all/plugin_compat.rs‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,10 @@ fn a_project_v2_plugin_waits_for_approval() {
502502

503503
// Expected (bigboi P8-1): what runs is what was approved. A save between the approval and the JS host's import (here
504504
// while the JS host is busy with another plugin's command) runs neither the saved module nor its setup: the approved
505-
// files run, and the saved ones wait for a decision of their own.
505+
// files run, and the saved ones wait for a decision of their own. The approval stages the approved copy off the UI
506+
// thread and only then sends its load (js-followups), so the save waits for that load: one that came before it would
507+
// change the files before they were copied, and the approved build would never load (the saved one is held instead).
508+
// Without this wait the race lost 7 of 40 runs under load with jsnpm-3 and 6 of 40 without it.
506509
#[test]
507510
fn a_save_after_approval_runs_nothing_unapproved() {
508511
let Some(config) = config() else { return };
@@ -511,14 +514,23 @@ fn a_save_after_approval_runs_nothing_unapproved() {
511514
let tui = &pipe.harness.tui;
512515
tui.keymap.dispatch("probe.hold", Some("1500"));
513516
approve(&pipe, &dir);
517+
let target = dir.to_string_lossy().into_owned();
518+
// Turns of the UI only, not the JS host's settle: the host is in the hold, which the save is to come before. On a
519+
// machine slow enough for the hold to end first, the approved module may have run already.
520+
pipe.harness.wait("the approved copy's load is sent", || {
521+
crate::plugin_pipe::turn(&pipe.harness);
522+
pipe.scripts().loading(&target) || marker.exists()
523+
});
514524
let file = dir.join("tui.tsx");
515525
let source = std::fs::read_to_string(&file).unwrap();
516526
std::fs::write(&file, source.replace("const MODULE = true", "const MODULE = \"saved\"")).unwrap();
517527
pipe.settle();
518528
opencode_tui::plugins::host::reconcile(tui);
519529
pipe.settle();
520530
let ran = std::fs::read_to_string(&marker).unwrap_or_default();
521-
assert_eq!(ran.lines().collect::<Vec<_>>(), [r#"{"module":true}"#], "an unapproved module ran");
531+
let ran: Vec<&str> = ran.lines().collect();
532+
assert!(!ran.iter().any(|line| line.contains("saved")), "an unapproved module ran: {ran:?}");
533+
assert_eq!(ran, [r#"{"module":true}"#], "the approved module did not run once");
522534
assert_eq!(listed(&pipe, &dir, PROBE), Some(Status::Pending));
523535
}
524536

‎packages/cli-rs/crates/tui/tests/all/plugin_pipe.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ pub fn decide(
208208

209209
/// One turn of the UI thread, and its frame when it wants one, as the run loop paints: a revision past a frame's rows
210210
/// waits for the next paint (Pl).
211-
fn turn(harness: &Harness) {
211+
pub(crate) fn turn(harness: &Harness) {
212212
harness.app.advance(Duration::from_millis(1));
213213
if harness.app.needs_frame() {
214214
harness.app.render();

0 commit comments

Comments
 (0)