1111import java .nio .file .Paths ;
1212import java .util .Arrays ;
1313import java .util .Collections ;
14+ import java .util .List ;
1415import java .util .Objects ;
1516import java .util .Scanner ;
17+ import java .util .concurrent .CopyOnWriteArrayList ;
1618import java .util .concurrent .ExecutorService ;
1719import java .util .concurrent .Executors ;
1820import java .util .concurrent .Future ;
@@ -276,6 +278,34 @@ void freshnessIsScopedAndVersionChangesClearOldQueries(@TempDir Path directory)
276278 }
277279 }
278280
281+ @ Test
282+ void invocationSnapshotsPositionalsBeforeWaitingForHelp (@ TempDir Path directory ) throws Exception {
283+ Path entered = directory .resolve ("entered" );
284+ Path release = directory .resolve ("release" );
285+ OpenCliProperties properties = properties ("structured" );
286+ properties .getEnvironment ().put ("OPENCLI_TEST_HOLD_SCOPED_HELP" , directory .toString ());
287+ List <String > positionals = new CopyOnWriteArrayList <>(Collections .singletonList ("original" ));
288+ ExecutorService caller = Executors .newSingleThreadExecutor ();
289+
290+ try (OpenCliClient client = new OpenCliClient (properties )) {
291+ Future <String > invocation = caller .submit (() -> client .invokeDiscovered (OpenCliHelpScope .SITE_NPM_SEARCH ,
292+ "opencli npm search" , null , positionals , null ).getStdout ());
293+ try {
294+ long deadline = System .nanoTime () + TimeUnit .SECONDS .toNanos (5 );
295+ while (!Files .exists (entered ) && System .nanoTime () < deadline ) {
296+ Thread .sleep (10 );
297+ }
298+ assertTrue (Files .exists (entered ), "discovery must reach the help probe" );
299+ positionals .set (0 , "updated" );
300+ } finally {
301+ Files .createFile (release );
302+ }
303+ assertEquals ("original" , invocation .get (5 , TimeUnit .SECONDS ));
304+ } finally {
305+ caller .shutdownNow ();
306+ }
307+ }
308+
279309 @ Test
280310 void invokesBoundBrowserLeafThroughLocalExecutor () {
281311 try (OpenCliClient client = new OpenCliClient (properties ("structured" ))) {
@@ -329,6 +359,12 @@ public static void main(String[] args) throws Exception {
329359 System .out .print ("raw available" );
330360 return ;
331361 }
362+ String scopedHelp = System .getenv ("OPENCLI_TEST_HOLD_SCOPED_HELP" );
363+ if (Objects .nonNull (scopedHelp ) && args .length == 4
364+ && Arrays .asList ("npm" , "search" , "--" ).equals (Arrays .asList (args ).subList (0 , 3 ))) {
365+ System .out .print (args [3 ]);
366+ return ;
367+ }
332368 if (args .length > 3 && Arrays .asList ("--help" , "-f" , "json" )
333369 .equals (Arrays .asList (args ).subList (args .length - 3 , args .length ))) {
334370 if ("new-version-error" .equals (mode ) || "error" .equals (mode )) {
@@ -347,6 +383,13 @@ public static void main(String[] args) throws Exception {
347383 case "npm search" : fixture = "site-npm-search.json" ; break ;
348384 default : throw new IllegalArgumentException ("Unapproved fixture command" );
349385 }
386+ if (Objects .nonNull (scopedHelp ) && "npm search" .equals (path )) {
387+ Path directory = Paths .get (scopedHelp );
388+ Files .createFile (directory .resolve ("entered" ));
389+ while (!Files .exists (directory .resolve ("release" ))) {
390+ Thread .sleep (10 );
391+ }
392+ }
350393 String help = readFixture ("wrong-help" .equals (mode ) ? "root.json" : fixture );
351394 if ("unverified-override" .equals (mode ) && "npm search" .equals (path )) {
352395 String json = help .trim ();
0 commit comments