Skip to content

Commit 4d3d57e

Browse files
committed
Decode OpenCLI DOM and AX state results by output format
1 parent e5eda2d commit 4d3d57e

15 files changed

Lines changed: 353 additions & 9 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# OpenCLI 1.8.8 的 DOM 和 AX 状态输出
2+
3+
使用 npm 发布包 `@jackwener/opencli@1.8.8`、Node 24.18.0 和 macOS arm64。调用 [锁定版 state 命令入口](https://github.com/jackwener/OpenCLI/blob/8271afc67e8504bda94c147f446ee29775d08274/src/cli.ts)。在 Browser Bridge 页面边界返回受控 DOM 文本,并用 [上游 AX 渲染器](https://github.com/jackwener/OpenCLI/blob/8271afc67e8504bda94c147f446ee29775d08274/src/browser/ax-snapshot.ts) 渲染合成 CDP 树。对比样本用固定时钟消除 elapsed_ms 波动,并另采集 AX 单侧失败场景。浏览器扩展和实际 CDP 链路仍待验证。
4+
5+
| 叶子调用 | stdout 样本 | 结果 |
6+
|---|---|---|
7+
| `state --source dom` | `state-dom.stdout` | URL 头、空行和 DOM 正文 |
8+
| `state --source ax` | `state-ax.stdout` | URL 头、空行和 AX 正文 |
9+
| `state --source ax --compare-sources` | `state-compare.json` | JSON,DOM 和 AX 各自报告指标 |
10+
| `state --compare-sources`,注入 AX 不可用 | `state-compare-ax-failure.json` | 进程成功,DOM 指标和 AX 错误并存 |
11+
12+
[样本目录](../../../../src/test/resources/opencli-contracts/v1/browser-output-1.8.8/) 保存原始 stdout 和 SHA-256。沿用 [前一批样本](2026-11-19-open-and-tab-list.md) 的启动方式,将命令替换为上表的 state 调用并设置 `OPENCLI_FIXTURE_FIXED_CLOCK=1`。最后一项额外设置 `OPENCLI_FIXTURE_AX_FAILURE=1`。
13+
14+
`session.snapshotTyped(tab, OpenCliBrowserSnapshotSource.AX)` 返回 URL 和原样的快照正文。DOM 和 AX 对比使用 `session.compareStateSourcesTyped(tab)`,每侧分别检查 `isOk()`,失败侧可读取错误信息。调用者仍可通过原有 `state(tab, options)` 传递未来的字符串 source 或组合已有参数。

‎openspec/changes/add-opencli-browser-result-models/tasks.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,7 @@
2828
## OC-BROWSER-001/002/003/005/006 首批叶子
2929

3030
`OpenCliBrowserResultTest` 首先因缺少结果类型无法编译。加入 tab list 和 open 的叶子模型后,验收真实 CLI action 的归档输出、合法空数组、畸形输出、截断、命令失败、精确 ID 和扩展元数据。两种命令通过现有会话入口运行,原始结果和既有 close 签名保留。见 [样本与复跑方式](evidence/2026-11-19-open-and-tab-list.md)。AX/state、其余叶子与真实浏览器接入继续验收。
31+
32+
## OC-BROWSER-001/002/004 state 与 AX
33+
34+
`OpenCliBrowserResultTest` 在缺少 state 类型模型和门面入口时无法编译。实现后验收 DOM 与 AX 文本、对比 JSON、单侧失败、无法识别的输出及 typed null source 拒绝。原有 String source 和 compareSources 继续由既有 Options 传递。见 [四份上游入口样本](evidence/2026-11-19-state-dom-and-ax.md)。

‎src/main/java/io/github/easy4j/opencli/browser/OpenCliBrowserSession.java‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import io.github.easy4j.opencli.browser.result.OpenCliBrowserDecoder;
44
import io.github.easy4j.opencli.browser.result.OpenCliBrowserOpenResult;
55
import io.github.easy4j.opencli.browser.result.OpenCliBrowserResult;
6+
import io.github.easy4j.opencli.browser.result.OpenCliBrowserSnapshot;
7+
import io.github.easy4j.opencli.browser.result.OpenCliBrowserSourceComparison;
68
import io.github.easy4j.opencli.browser.result.OpenCliBrowserTab;
79
import io.github.easy4j.opencli.browser.support.OpenCliBrowserConsoleOptions;
810
import io.github.easy4j.opencli.browser.support.OpenCliBrowserDragLocator;
@@ -11,6 +13,7 @@
1113
import io.github.easy4j.opencli.browser.support.OpenCliBrowserGetHtmlOptions;
1214
import io.github.easy4j.opencli.browser.support.OpenCliBrowserScreenshotOptions;
1315
import io.github.easy4j.opencli.browser.support.OpenCliBrowserSemanticLocator;
16+
import io.github.easy4j.opencli.browser.support.OpenCliBrowserSnapshotSource;
1417
import io.github.easy4j.opencli.browser.support.OpenCliBrowserStateOptions;
1518
import io.github.easy4j.opencli.browser.support.OpenCliBrowserTabOptions;
1619
import io.github.easy4j.opencli.core.OpenCliArgSupport;
@@ -221,6 +224,27 @@ public OpenCliResult state(OpenCliBrowserTabOptions tab, Boolean json) {
221224
return state(tab, options);
222225
}
223226

227+
/**
228+
* @param tab optional target tab
229+
* @param source explicit DOM or AX source
230+
* @return strict URL and snapshot text from the selected source
231+
*/
232+
public OpenCliBrowserResult<OpenCliBrowserSnapshot> snapshotTyped(OpenCliBrowserTabOptions tab,
233+
OpenCliBrowserSnapshotSource source) {
234+
OpenCliBrowserStateOptions options = OpenCliBrowserStateOptions.builder()
235+
.source(Objects.requireNonNull(source, "source").getOptionValue()).build();
236+
return OpenCliBrowserDecoder.snapshot(state(tab, options));
237+
}
238+
239+
/**
240+
* @param tab optional target tab
241+
* @return strict comparison with each source's independent outcome
242+
*/
243+
public OpenCliBrowserResult<OpenCliBrowserSourceComparison> compareStateSourcesTyped(OpenCliBrowserTabOptions tab) {
244+
return OpenCliBrowserDecoder.compareSources(
245+
state(tab, OpenCliBrowserStateOptions.builder().compareSources(true).build()));
246+
}
247+
224248
/** {@code browser frames} */
225249
public OpenCliResult frames(OpenCliBrowserTabOptions tab) {
226250
List<String> args = new ArrayList<>();

‎src/main/java/io/github/easy4j/opencli/browser/result/OpenCliBrowserDecoder.java‎

Lines changed: 84 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.fasterxml.jackson.databind.ObjectReader;
77
import com.fasterxml.jackson.databind.json.JsonMapper;
88
import io.github.easy4j.opencli.browser.result.OpenCliBrowserResult.Status;
9+
import io.github.easy4j.opencli.browser.result.OpenCliBrowserSourceComparison.Metrics;
910
import io.github.easy4j.opencli.core.OpenCliExecutionDetails;
1011
import io.github.easy4j.opencli.core.OpenCliResult;
1112
import java.util.ArrayList;
@@ -34,22 +35,60 @@ public static OpenCliBrowserResult<OpenCliBrowserOpenResult> open(OpenCliResult
3435
return decode(raw, OpenCliBrowserDecoder::readNavigation);
3536
}
3637

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) {
3869
Objects.requireNonNull(raw, "raw");
3970
OpenCliExecutionDetails execution = raw.getExecutionDetails();
4071
if (Objects.nonNull(execution) && execution.isStdoutTruncated()) {
41-
return new OpenCliBrowserResult<>(raw, Status.TRUNCATED, null);
72+
return Status.TRUNCATED;
4273
}
4374
if (!raw.isSuccess()) {
44-
return new OpenCliBrowserResult<>(raw, Status.EXECUTION_FAILED, null);
75+
return Status.EXECUTION_FAILED;
4576
}
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);
4988
}
5089
JsonNode json;
5190
try {
52-
json = JSON.readTree(stdout);
91+
json = JSON.readTree(raw.getStdout());
5392
} catch (JsonProcessingException ex) {
5493
return new OpenCliBrowserResult<>(raw, Status.INVALID_JSON, null);
5594
}
@@ -87,6 +126,44 @@ private static OpenCliBrowserOpenResult readNavigation(JsonNode json) {
87126
page.isMissingNode() ? null : pageId(page), json);
88127
}
89128

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+
90167
private static String pageId(JsonNode node) {
91168
if (node.isTextual() && !node.textValue().isEmpty()) {
92169
return node.textValue();
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package io.github.easy4j.opencli.browser.result;
2+
3+
import lombok.AccessLevel;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Getter;
6+
7+
/** The URL header and exact snapshot body from browser state. */
8+
@Getter
9+
@AllArgsConstructor(access = AccessLevel.PACKAGE)
10+
public final class OpenCliBrowserSnapshot {
11+
private final String url;
12+
private final String text;
13+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package io.github.easy4j.opencli.browser.result;
2+
3+
import com.fasterxml.jackson.databind.JsonNode;
4+
import lombok.AccessLevel;
5+
import lombok.AllArgsConstructor;
6+
import lombok.Getter;
7+
8+
/** DOM and AX observations from the same browser state comparison. */
9+
@Getter
10+
@AllArgsConstructor(access = AccessLevel.PACKAGE)
11+
public final class OpenCliBrowserSourceComparison {
12+
private final String url;
13+
private final Metrics dom;
14+
private final Metrics ax;
15+
private final JsonNode metadata;
16+
17+
/** @return an independent copy of the entire comparison object */
18+
public JsonNode getMetadata() {
19+
return metadata.deepCopy();
20+
}
21+
22+
/** A single source observation. Counts are absent when the source failed. */
23+
@Getter
24+
@AllArgsConstructor(access = AccessLevel.PACKAGE)
25+
public static final class Metrics {
26+
private final boolean ok;
27+
private final long elapsedMillis;
28+
private final Long chars;
29+
private final Long bytes;
30+
private final Long lines;
31+
private final Long approximateTokens;
32+
private final Long refs;
33+
private final Long frameSections;
34+
private final Long interactive;
35+
private final String errorCode;
36+
private final String errorMessage;
37+
}
38+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package io.github.easy4j.opencli.browser.support;
2+
3+
import lombok.Getter;
4+
import lombok.RequiredArgsConstructor;
5+
6+
/** Sources accepted by the typed browser snapshot entry point. */
7+
@Getter
8+
@RequiredArgsConstructor
9+
public enum OpenCliBrowserSnapshotSource {
10+
DOM("dom"),
11+
AX("ax");
12+
13+
private final String optionValue;
14+
}

‎src/test/java/io/github/easy4j/opencli/contract/OpenCliBrowserResultTest.java‎

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
import io.github.easy4j.opencli.browser.result.OpenCliBrowserResult;
88
import io.github.easy4j.opencli.browser.result.OpenCliBrowserResult.Status;
99
import io.github.easy4j.opencli.browser.result.OpenCliBrowserTab;
10+
import io.github.easy4j.opencli.browser.result.OpenCliBrowserSnapshot;
11+
import io.github.easy4j.opencli.browser.result.OpenCliBrowserSourceComparison;
12+
import io.github.easy4j.opencli.browser.support.OpenCliBrowserSnapshotSource;
13+
import io.github.easy4j.opencli.browser.support.OpenCliBrowserStateOptions;
1014
import io.github.easy4j.opencli.core.OpenCliExecutionDetails;
1115
import io.github.easy4j.opencli.core.OpenCliExecutor;
1216
import io.github.easy4j.opencli.core.OpenCliResult;
@@ -100,9 +104,79 @@ public OpenCliResult invoke(List<String> argv) {
100104
assertSame(closeResult, session.close());
101105
}
102106

107+
@Test
108+
void decodesDomAndAxTextAndPreservesSnapshotBody() {
109+
for (String source : Arrays.asList("dom", "ax")) {
110+
OpenCliResult raw = raw(fixture("state-" + source, ".stdout"));
111+
OpenCliBrowserResult<OpenCliBrowserSnapshot> snapshot = OpenCliBrowserDecoder.snapshot(raw);
112+
assertEquals(Status.PARSED, snapshot.getStatus());
113+
assertSame(raw, snapshot.getRaw());
114+
assertEquals("http://127.0.0.1:18765/basic.html", snapshot.getValue().getUrl());
115+
assertTrue(snapshot.getValue().getText().contains("Continue"));
116+
assertEquals(raw.getStdout().substring(raw.getStdout().indexOf("\n\n") + 2),
117+
snapshot.getValue().getText());
118+
}
119+
assertEquals(Status.INVALID_SHAPE, OpenCliBrowserDecoder.snapshot(raw("URL: fixture\n\n")).getStatus());
120+
assertEquals(Status.INVALID_SHAPE, OpenCliBrowserDecoder.snapshot(raw(fixture("state-compare"))).getStatus());
121+
}
122+
123+
@Test
124+
void comparisonDistinguishesSourceFailureFromProcessFailure() {
125+
OpenCliBrowserResult<OpenCliBrowserSourceComparison> result =
126+
OpenCliBrowserDecoder.compareSources(raw(fixture("state-compare")));
127+
assertEquals(Status.PARSED, result.getStatus());
128+
assertEquals(77L, result.getValue().getDom().getBytes());
129+
assertEquals(1L, result.getValue().getAx().getInteractive());
130+
assertTrue(result.getValue().getAx().isOk());
131+
assertNull(result.getValue().getDom().getInteractive());
132+
133+
OpenCliResult failedAx = raw(fixture("state-compare-ax-failure"));
134+
OpenCliBrowserResult<OpenCliBrowserSourceComparison> partial = OpenCliBrowserDecoder.compareSources(failedAx);
135+
assertEquals(Status.PARSED, partial.getStatus());
136+
assertTrue(partial.getValue().getDom().isOk());
137+
assertFalse(partial.getValue().getAx().isOk());
138+
assertEquals("BROWSER_AX_UNAVAILABLE", partial.getValue().getAx().getErrorCode());
139+
assertNull(partial.getValue().getAx().getBytes());
140+
assertSame(failedAx, partial.getRaw());
141+
142+
assertEquals(Status.INVALID_SHAPE, OpenCliBrowserDecoder.compareSources(
143+
raw("{\"url\":\"\",\"sources\":{\"dom\":{\"ok\":true},\"ax\":{\"ok\":true}}}")).getStatus());
144+
}
145+
146+
@Test
147+
void typedSnapshotRejectsMissingSourceAndRawPreservesFutureSource() {
148+
OpenCliExecutor executor = new OpenCliExecutor(new OpenCliProperties()) {
149+
@Override
150+
public OpenCliResult invoke(List<String> argv) {
151+
if (argv.contains("--compare-sources")) {
152+
assertEquals(Arrays.asList("browser", "fixture", "state", "--compare-sources"), argv);
153+
return raw(fixture("state-compare"));
154+
}
155+
assertEquals("state", argv.get(2));
156+
assertEquals("--source", argv.get(3));
157+
assertEquals(5, argv.size());
158+
if ("ax".equals(argv.get(4))) {
159+
return raw(fixture("state-ax", ".stdout"));
160+
}
161+
assertEquals("future", argv.get(4));
162+
return raw("future fixture");
163+
}
164+
};
165+
OpenCliBrowserSession session = new OpenCliBrowserSession(executor, "fixture", null);
166+
assertThrows(NullPointerException.class, () -> session.snapshotTyped(null, null));
167+
assertEquals(Status.PARSED, session.snapshotTyped(null, OpenCliBrowserSnapshotSource.AX).getStatus());
168+
assertEquals(Status.PARSED, session.compareStateSourcesTyped(null).getStatus());
169+
assertEquals("future fixture", session.state(null,
170+
OpenCliBrowserStateOptions.builder().source("future").build()).getStdout());
171+
}
172+
103173
static String fixture(String name) {
174+
return fixture(name, ".json");
175+
}
176+
177+
private static String fixture(String name, String extension) {
104178
try (InputStream stream = OpenCliBrowserResultTest.class.getResourceAsStream(
105-
"/opencli-contracts/v1/browser-output-1.8.8/" + name + ".json");
179+
"/opencli-contracts/v1/browser-output-1.8.8/" + name + extension);
106180
Scanner scanner = new Scanner(stream, StandardCharsets.UTF_8.name()).useDelimiter("\\A")) {
107181
return scanner.next();
108182
} catch (IOException ex) {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
*.json -text
2+
*.stdout -text
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
83ece2b39654da1ae482ea46fc3f00a65b01bbed80466cccf8049f4f7c937eae open.json
2+
ef189961873ec97cc155232e004efb54cc1c3bb2b77d925ace87d49b23529349 state-compare-ax-failure.json
3+
e6dc6071a233af759b96ed8948bf692be4ceb2f79edd71db2fc16b99b20ec429 state-compare.json
24
37517e5f3dc66819f61f5a7bb8ace1921282415f10551d2defa5c3eb0985b570 tab-list-empty.json
35
c81e67d4ed3c58ec33d80f9d0c2feceee33a3588792c5f51e4a74a6069fed8b2 tab-list.json
6+
a3ba63353d2f970d5edbcf8eb0d1ede2688605ff5a97013cbfd70a9e54f344fd state-ax.stdout
7+
592bd1b129c0687569d865cdc3d1d28152628267ef2a6732d2afa23f58c1f035 state-dom.stdout

0 commit comments

Comments
 (0)