|
| 1 | +package io.github.easy4j.opencli.contract; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 7 | + |
| 8 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 9 | +import com.fasterxml.jackson.databind.DeserializationFeature; |
| 10 | +import com.fasterxml.jackson.databind.JsonNode; |
| 11 | +import com.fasterxml.jackson.databind.ObjectReader; |
| 12 | +import com.fasterxml.jackson.databind.json.JsonMapper; |
| 13 | +import java.io.IOException; |
| 14 | +import java.io.InputStream; |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | + |
| 17 | +class OpenCliStructuredHelpFixtureTest { |
| 18 | + |
| 19 | + private static final String FIXTURES = "/opencli-contracts/v1/structured-help-1.8.8/"; |
| 20 | + private static final ObjectReader STRICT_JSON = new JsonMapper().reader() |
| 21 | + .with(DeserializationFeature.FAIL_ON_TRAILING_TOKENS); |
| 22 | + |
| 23 | + @Test |
| 24 | + void preservesTheRootMetaAndSiteShapes() throws IOException { |
| 25 | + JsonNode root = read("root.json"); |
| 26 | + assertEquals("opencli", root.path("name").asText()); |
| 27 | + assertTrue(includesNamed(root.path("commands"), "list")); |
| 28 | + assertTrue(root.path("site_adapters").isObject()); |
| 29 | + assertTrue(root.path("site_adapters").path("sites").isArray()); |
| 30 | + |
| 31 | + JsonNode auth = read("meta-auth.json"); |
| 32 | + assertEquals("opencli auth", auth.path("command").asText()); |
| 33 | + assertTrue(auth.path("namespace_options").isArray()); |
| 34 | + assertTrue(auth.path("global_options").isArray()); |
| 35 | + |
| 36 | + JsonNode site = read("site-npm.json"); |
| 37 | + assertEquals("npm", site.path("site").asText()); |
| 38 | + assertTrue(includesNamed(site.path("commands"), "search")); |
| 39 | + assertTrue(site.path("common_options").isArray()); |
| 40 | + |
| 41 | + JsonNode leaf = read("site-npm-search.json"); |
| 42 | + assertEquals("opencli npm search", leaf.path("command").asText()); |
| 43 | + assertEquals("query", leaf.path("positionals").get(0).path("name").asText()); |
| 44 | + assertTrue(leaf.path("positionals").get(0).path("required").booleanValue()); |
| 45 | + assertTrue(includesNamed(leaf.path("command_options"), "limit")); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + void preservesBrowserSessionPlaceholdersAndOptionScopes() throws IOException { |
| 50 | + JsonNode tabs = read("browser-tab.json"); |
| 51 | + assertEquals("opencli browser <session> tab", tabs.path("command").asText()); |
| 52 | + assertTrue(includesNamed(tabs.path("commands"), "tab list")); |
| 53 | + assertTrue(includesNamed(tabs.path("namespace_options"), "window")); |
| 54 | + assertTrue(includesNamed(tabs.path("global_options"), "profile")); |
| 55 | + assertTrue(includesNamed(tabs.path("commands").get(0).path("command_options"), "tab")); |
| 56 | + |
| 57 | + JsonNode get = read("browser-get.json"); |
| 58 | + assertEquals("opencli browser <session> get", get.path("command").asText()); |
| 59 | + assertTrue(includesNamed(get.path("commands"), "get text")); |
| 60 | + |
| 61 | + JsonNode state = read("browser-state.json"); |
| 62 | + assertEquals("opencli browser <session> state", state.path("command").asText()); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + void identifiesRealOutputsThatAreNotSingleJsonDocuments() { |
| 67 | + assertThrows(JsonProcessingException.class, () -> read("browser.stdout")); |
| 68 | + assertThrows(JsonProcessingException.class, () -> read("meta-list.txt")); |
| 69 | + } |
| 70 | + |
| 71 | + private static boolean includesNamed(JsonNode entries, String name) { |
| 72 | + for (JsonNode entry : entries) { |
| 73 | + if (name.equals(entry.path("name").asText())) { |
| 74 | + return true; |
| 75 | + } |
| 76 | + } |
| 77 | + return false; |
| 78 | + } |
| 79 | + |
| 80 | + private static JsonNode read(String name) throws IOException { |
| 81 | + try (InputStream stream = OpenCliStructuredHelpFixtureTest.class.getResourceAsStream(FIXTURES + name)) { |
| 82 | + assertNotNull(stream, name); |
| 83 | + return STRICT_JSON.readTree(stream); |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments