|
6 | 6 | import com.fasterxml.jackson.databind.ObjectReader; |
7 | 7 | import com.fasterxml.jackson.databind.json.JsonMapper; |
8 | 8 | import io.github.easy4j.opencli.browser.result.OpenCliBrowserResult.Status; |
| 9 | +import io.github.easy4j.opencli.browser.result.OpenCliBrowserSourceComparison.Metrics; |
9 | 10 | import io.github.easy4j.opencli.core.OpenCliExecutionDetails; |
10 | 11 | import io.github.easy4j.opencli.core.OpenCliResult; |
11 | 12 | import java.util.ArrayList; |
@@ -34,22 +35,60 @@ public static OpenCliBrowserResult<OpenCliBrowserOpenResult> open(OpenCliResult |
34 | 35 | return decode(raw, OpenCliBrowserDecoder::readNavigation); |
35 | 36 | } |
36 | 37 |
|
37 | | - private static <T> OpenCliBrowserResult<T> decode(OpenCliResult raw, Function<JsonNode, T> model) { |
| 38 | + /** @param raw browser state result @return strict text snapshot without interpreting its contents */ |
| 39 | + public static OpenCliBrowserResult<OpenCliBrowserSnapshot> snapshot(OpenCliResult raw) { |
| 40 | + Status failure = unavailableStatus(raw); |
| 41 | + if (Objects.nonNull(failure)) { |
| 42 | + return new OpenCliBrowserResult<>(raw, failure, null); |
| 43 | + } |
| 44 | + String output = raw.getStdout(); |
| 45 | + String prefix = "URL: "; |
| 46 | + int headerEnd = output.indexOf('\n'); |
| 47 | + int blankEnd = output.indexOf('\n', headerEnd + 1); |
| 48 | + if (!output.startsWith(prefix) || headerEnd < prefix.length() || blankEnd < 0 |
| 49 | + || !output.substring(headerEnd + 1, blankEnd).chars().allMatch(ch -> ch == '\r')) { |
| 50 | + return new OpenCliBrowserResult<>(raw, Status.INVALID_SHAPE, null); |
| 51 | + } |
| 52 | + String url = output.substring(prefix.length(), headerEnd); |
| 53 | + if (url.endsWith("\r")) { |
| 54 | + url = url.substring(0, url.length() - 1); |
| 55 | + } |
| 56 | + String body = output.substring(blankEnd + 1); |
| 57 | + if (isBlankOutput(body)) { |
| 58 | + return new OpenCliBrowserResult<>(raw, Status.INVALID_SHAPE, null); |
| 59 | + } |
| 60 | + return new OpenCliBrowserResult<>(raw, Status.PARSED, new OpenCliBrowserSnapshot(url, body)); |
| 61 | + } |
| 62 | + |
| 63 | + /** @param raw browser state --compare-sources result @return strict DOM and AX comparison */ |
| 64 | + public static OpenCliBrowserResult<OpenCliBrowserSourceComparison> compareSources(OpenCliResult raw) { |
| 65 | + return decode(raw, OpenCliBrowserDecoder::readComparison); |
| 66 | + } |
| 67 | + |
| 68 | + private static Status unavailableStatus(OpenCliResult raw) { |
38 | 69 | Objects.requireNonNull(raw, "raw"); |
39 | 70 | OpenCliExecutionDetails execution = raw.getExecutionDetails(); |
40 | 71 | if (Objects.nonNull(execution) && execution.isStdoutTruncated()) { |
41 | | - return new OpenCliBrowserResult<>(raw, Status.TRUNCATED, null); |
| 72 | + return Status.TRUNCATED; |
42 | 73 | } |
43 | 74 | if (!raw.isSuccess()) { |
44 | | - return new OpenCliBrowserResult<>(raw, Status.EXECUTION_FAILED, null); |
| 75 | + return Status.EXECUTION_FAILED; |
45 | 76 | } |
46 | | - String stdout = raw.getStdout(); |
47 | | - if (Objects.isNull(stdout) || stdout.chars().allMatch(value -> " \t\r\n".indexOf(value) >= 0)) { |
48 | | - return new OpenCliBrowserResult<>(raw, Status.EMPTY, null); |
| 77 | + return isBlankOutput(raw.getStdout()) ? Status.EMPTY : null; |
| 78 | + } |
| 79 | + |
| 80 | + private static boolean isBlankOutput(String output) { |
| 81 | + return Objects.isNull(output) || output.chars().allMatch(value -> " \t\r\n".indexOf(value) >= 0); |
| 82 | + } |
| 83 | + |
| 84 | + private static <T> OpenCliBrowserResult<T> decode(OpenCliResult raw, Function<JsonNode, T> model) { |
| 85 | + Status failure = unavailableStatus(raw); |
| 86 | + if (Objects.nonNull(failure)) { |
| 87 | + return new OpenCliBrowserResult<>(raw, failure, null); |
49 | 88 | } |
50 | 89 | JsonNode json; |
51 | 90 | try { |
52 | | - json = JSON.readTree(stdout); |
| 91 | + json = JSON.readTree(raw.getStdout()); |
53 | 92 | } catch (JsonProcessingException ex) { |
54 | 93 | return new OpenCliBrowserResult<>(raw, Status.INVALID_JSON, null); |
55 | 94 | } |
@@ -87,6 +126,44 @@ private static OpenCliBrowserOpenResult readNavigation(JsonNode json) { |
87 | 126 | page.isMissingNode() ? null : pageId(page), json); |
88 | 127 | } |
89 | 128 |
|
| 129 | + private static OpenCliBrowserSourceComparison readComparison(JsonNode json) { |
| 130 | + JsonNode url = json.path("url"); |
| 131 | + JsonNode sources = json.path("sources"); |
| 132 | + if (!url.isTextual() || !sources.isObject()) { |
| 133 | + throw invalidShape(); |
| 134 | + } |
| 135 | + return new OpenCliBrowserSourceComparison(url.textValue(), readMetrics(sources.path("dom")), |
| 136 | + readMetrics(sources.path("ax")), json); |
| 137 | + } |
| 138 | + |
| 139 | + private static Metrics readMetrics(JsonNode json) { |
| 140 | + JsonNode ok = json.path("ok"); |
| 141 | + if (!ok.isBoolean()) { |
| 142 | + throw invalidShape(); |
| 143 | + } |
| 144 | + long elapsed = counter(json.path("elapsed_ms")); |
| 145 | + if (!ok.booleanValue()) { |
| 146 | + JsonNode error = json.path("error"); |
| 147 | + JsonNode message = error.path("message"); |
| 148 | + if (!message.isTextual()) { |
| 149 | + throw invalidShape(); |
| 150 | + } |
| 151 | + return new Metrics(false, elapsed, null, null, null, null, null, null, null, |
| 152 | + optionalText(error.path("code")), message.textValue()); |
| 153 | + } |
| 154 | + JsonNode interactive = json.path("interactive"); |
| 155 | + return new Metrics(true, elapsed, counter(json.path("chars")), counter(json.path("bytes")), |
| 156 | + counter(json.path("lines")), counter(json.path("approx_tokens")), counter(json.path("refs")), |
| 157 | + counter(json.path("frame_sections")), interactive.isMissingNode() ? null : counter(interactive), null, null); |
| 158 | + } |
| 159 | + |
| 160 | + private static long counter(JsonNode node) { |
| 161 | + if (!node.isIntegralNumber() || !node.canConvertToLong() || node.longValue() < 0) { |
| 162 | + throw invalidShape(); |
| 163 | + } |
| 164 | + return node.longValue(); |
| 165 | + } |
| 166 | + |
90 | 167 | private static String pageId(JsonNode node) { |
91 | 168 | if (node.isTextual() && !node.textValue().isEmpty()) { |
92 | 169 | return node.textValue(); |
|
0 commit comments