@@ -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]
507510fn 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
0 commit comments