@@ -173,7 +173,7 @@ void lateRefreshCannotOverwriteNewerSnapshot(@TempDir Path directory) throws Exc
173173 props .setMaxConcurrentExecutions (2 );
174174 ExecutorService callers = Executors .newSingleThreadExecutor ();
175175 try (OpenCliClient client = new OpenCliClient (props )) {
176- Future <OpenCliCapabilitySnapshot > older = callers .submit (client :: discoverCapabilities );
176+ Future <OpenCliCapabilitySnapshot > older = callers .submit (() -> client . discoverCapabilities () );
177177 long deadline = System .nanoTime () + TimeUnit .SECONDS .toNanos (5 );
178178 while (!Files .exists (entered ) && System .nanoTime () < deadline ) {
179179 Thread .sleep (10 );
@@ -196,6 +196,50 @@ void lateRefreshCannotOverwriteNewerSnapshot(@TempDir Path directory) throws Exc
196196 }
197197 }
198198
199+ @ Test
200+ void fixedQueriesReturnTheirCapturedScopeAndKeepUnsupportedFormats () {
201+ OpenCliHelpScope [] scopes = {OpenCliHelpScope .BROWSER_TAB , OpenCliHelpScope .BROWSER_GET ,
202+ OpenCliHelpScope .BROWSER_STATE , OpenCliHelpScope .META_AUTH , OpenCliHelpScope .SITE_NPM ,
203+ OpenCliHelpScope .SITE_NPM_SEARCH };
204+ try (OpenCliClient client = new OpenCliClient (properties ("structured" ))) {
205+ for (OpenCliHelpScope scope : scopes ) {
206+ OpenCliCapabilitySnapshot snapshot = client .discoverCapabilities (scope );
207+ assertEquals (OpenCliCapabilitySnapshot .Status .AVAILABLE , snapshot .getStatus (), scope .name ());
208+ assertEquals (scope .name (), snapshot .getScope ());
209+ assertEquals ("UNKNOWN" , snapshot .getCommandOrigin ());
210+ }
211+ assertEquals ("opencli browser <session> state" ,
212+ client .discoverCapabilities (OpenCliHelpScope .BROWSER_STATE ).getHelp ().getSchema ().getCommand ());
213+ assertEquals ("UNSUPPORTED_FORMAT" , client .discoverCapabilities (OpenCliHelpScope .META_LIST ).getReason ());
214+ assertEquals ("INVALID_JSON" , client .discoverCapabilities (OpenCliHelpScope .BROWSER ).getReason ());
215+ }
216+ }
217+
218+ @ Test
219+ void freshnessIsScopedAndVersionChangesClearOldQueries (@ TempDir Path directory ) throws Exception {
220+ Path control = directory .resolve ("mode" );
221+ writeMode (control , "structured" );
222+ try (OpenCliClient client = new OpenCliClient (controlledProperties (control ))) {
223+ OpenCliCapabilitySnapshot root = client .discoverCapabilities ();
224+ OpenCliCapabilitySnapshot state = client .discoverCapabilities (OpenCliHelpScope .BROWSER_STATE );
225+ assertEquals (OpenCliCapabilitySnapshot .Status .AVAILABLE , state .getStatus ());
226+
227+ writeMode (control , "wrong-help" );
228+ OpenCliCapabilitySnapshot stale = client .discoverCapabilities (OpenCliHelpScope .BROWSER_STATE );
229+ assertEquals (OpenCliCapabilitySnapshot .Status .STALE , stale .getStatus ());
230+ assertEquals ("UNEXPECTED_HELP_SCOPE" , stale .getReason ());
231+ assertEquals (state .getHelpSha256 (), stale .getHelpSha256 ());
232+ assertNotEquals (root .getHelpSha256 (), stale .getHelpSha256 ());
233+
234+ writeMode (control , "new-version-error" );
235+ OpenCliCapabilitySnapshot upgraded = client .discoverCapabilities (OpenCliHelpScope .BROWSER_STATE );
236+ assertEquals (OpenCliCapabilitySnapshot .Status .UNSUPPORTED , upgraded .getStatus ());
237+ assertEquals ("UNVERIFIED_HELP_VERSION" , upgraded .getReason ());
238+ writeMode (control , "error" );
239+ assertEquals (OpenCliCapabilitySnapshot .Status .UNKNOWN , client .discoverCapabilities ().getStatus ());
240+ }
241+ }
242+
199243 private static OpenCliProperties controlledProperties (Path control ) {
200244 OpenCliProperties props = properties ("unused" );
201245 props .getEnvironment ().put ("OPENCLI_TEST_HELP_CONTROL" , control .toString ());
@@ -232,6 +276,27 @@ public static void main(String[] args) throws Exception {
232276 System .out .print ("raw available" );
233277 return ;
234278 }
279+ if (args .length > 3 && Arrays .asList ("--help" , "-f" , "json" )
280+ .equals (Arrays .asList (args ).subList (args .length - 3 , args .length ))) {
281+ if ("new-version-error" .equals (mode ) || "error" .equals (mode )) {
282+ System .exit (3 );
283+ }
284+ String path = String .join (" " , Arrays .asList (args ).subList (0 , args .length - 3 ));
285+ String fixture ;
286+ switch (path ) {
287+ case "browser" : fixture = "browser.stdout" ; break ;
288+ case "browser fixture tab" : fixture = "browser-tab.json" ; break ;
289+ case "browser fixture get" : fixture = "browser-get.json" ; break ;
290+ case "browser fixture state" : fixture = "browser-state.json" ; break ;
291+ case "auth" : fixture = "meta-auth.json" ; break ;
292+ case "list" : fixture = "meta-list.txt" ; break ;
293+ case "npm" : fixture = "site-npm.json" ; break ;
294+ case "npm search" : fixture = "site-npm-search.json" ; break ;
295+ default : throw new IllegalArgumentException ("Unapproved fixture command" );
296+ }
297+ System .out .print (readFixture ("wrong-help" .equals (mode ) ? "root.json" : fixture ));
298+ return ;
299+ }
235300 if (!Arrays .equals (args , new String []{"--help" , "-f" , "json" })) {
236301 System .exit (2 );
237302 }
@@ -256,12 +321,18 @@ public static void main(String[] args) throws Exception {
256321 System .out .print ("{\" name\" :\" opencli\" ,\" commands\" :[],\" site_adapters\" :{}}" );
257322 return ;
258323 }
324+ String root = readFixture ("root.json" ).trim ();
325+ System .out .print (root .substring (0 , root .length () - 1 )
326+ + ",\" description_fixture\" :\" do not run this example " + mode + "\" }" );
327+ }
328+
329+ private static String readFixture (String fixture ) {
259330 try (InputStream input = DiscoveryFixture .class .getResourceAsStream (
260- "/opencli-contracts/v1/structured-help-1.8.8/root.json" );
331+ "/opencli-contracts/v1/structured-help-1.8.8/" + fixture );
261332 Scanner scan = new Scanner (input , "UTF-8" ).useDelimiter ("\\ A" )) {
262- String root = scan .next (). trim ();
263- System . out . print ( root . substring ( 0 , root . length () - 1 )
264- + ", \" description_fixture \" : \" do not run this example " + mode + " \" }" );
333+ return scan .next ();
334+ } catch ( java . io . IOException ex ) {
335+ throw new java . io . UncheckedIOException ( ex );
265336 }
266337 }
267338 }
0 commit comments