Skip to content

Commit f2feb18

Browse files
committed
Add strict OpenCLI browser tab and navigation results
1 parent ff75cd7 commit f2feb18

14 files changed

Lines changed: 379 additions & 0 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# OpenCLI 1.8.8 浏览器叶子输出
2+
3+
使用 npm 发布的 `@jackwener/opencli@1.8.8`、Node 24.18.0 和 macOS arm64。对版本锁定的 [CLI action](https://github.com/jackwener/OpenCLI/blob/8271afc67e8504bda94c147f446ee29775d08274/src/cli.ts) 使用真实命令入口和参数预处理。仅在 Browser Bridge 连接边界接入隔离页面桩,页面字段依据 [扩展输出](https://github.com/jackwener/OpenCLI/blob/8271afc67e8504bda94c147f446ee29775d08274/extension/src/background.ts) 提供。未连接浏览器账户、真实标签页或站点。
4+
5+
| 叶子 | 归档样本 | 验证内容 |
6+
|---|---|---|
7+
| `browser fixture tab list` | `tab-list.json`、`tab-list-empty.json` | 数组,保留前导零的 page 字符串及合法空列表 |
8+
| `browser fixture open <url>` | `open.json` | 对象,包含实际 URL 和可选 page,允许缺少 sitemap |
9+
| state、find、console、network、analyze | 待采集 | 原有 raw API |
10+
11+
在 [样本目录](../../../../src/test/resources/opencli-contracts/v1/browser-output-1.8.8/) 保存 stdout 原文、SHA-256 和可复跑的 preload。设置 `OPENCLI_ENTRY` 为锁定包的 `dist/src/main.js` 绝对路径后,可从该目录运行以下命令。
12+
13+
```bash
14+
fixture_home=$(mktemp -d)
15+
env HOME="$fixture_home" CI=1 NO_COLOR=1 node --import "$PWD/capture.mjs" "$OPENCLI_ENTRY" browser fixture tab list > "$fixture_home/tab-list.json"
16+
env HOME="$fixture_home" CI=1 NO_COLOR=1 OPENCLI_FIXTURE_EMPTY=1 node --import "$PWD/capture.mjs" "$OPENCLI_ENTRY" browser fixture tab list > "$fixture_home/tab-list-empty.json"
17+
env HOME="$fixture_home" CI=1 NO_COLOR=1 node --import "$PWD/capture.mjs" "$OPENCLI_ENTRY" browser fixture open http://127.0.0.1:18765/basic.html > "$fixture_home/open.json"
18+
for specimen in tab-list.json tab-list-empty.json open.json; do
19+
cmp "$specimen" "$fixture_home/$specimen"
20+
done
21+
```
22+
23+
`session.tabListTyped()` 和 `session.openTyped(url, tab)` 返回带原始结果的严格视图。原有执行异常仍携带 partial result,可显式传给 `OpenCliBrowserDecoder` 查看截断或执行失败状态。扩展字段保留在 JSON 视图,路径和提示只作为数据。

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,7 @@
2424
## 证据交付模板(实施时填写)
2525

2626
每项任务的完成记录包含:Requirement/Scenario、分支HEAD、测试命令、失败→通过证据、测试数/跳过数、退出码、报告位置。没有记录的任务保持未完成。
27+
28+
## OC-BROWSER-001/002/003/005/006 首批叶子
29+
30+
`OpenCliBrowserResultTest` 首先因缺少结果类型无法编译。加入 tab list 和 open 的叶子模型后,验收真实 CLI action 的归档输出、合法空数组、畸形输出、截断、命令失败、精确 ID 和扩展元数据。两种命令通过现有会话入口运行,原始结果和既有 close 签名保留。见 [样本与复跑方式](evidence/2026-11-19-open-and-tab-list.md)。AX/state、其余叶子与真实浏览器接入继续验收。

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package io.github.easy4j.opencli.browser;
22

3+
import io.github.easy4j.opencli.browser.result.OpenCliBrowserDecoder;
4+
import io.github.easy4j.opencli.browser.result.OpenCliBrowserOpenResult;
5+
import io.github.easy4j.opencli.browser.result.OpenCliBrowserResult;
6+
import io.github.easy4j.opencli.browser.result.OpenCliBrowserTab;
37
import io.github.easy4j.opencli.browser.support.OpenCliBrowserConsoleOptions;
48
import io.github.easy4j.opencli.browser.support.OpenCliBrowserDragLocator;
59
import io.github.easy4j.opencli.browser.support.OpenCliBrowserExtractOptions;
@@ -86,6 +90,11 @@ public OpenCliResult tabList() {
8690
return invokeSub(OpenCliLists.of("tab", "list"));
8791
}
8892

93+
/** @return strict interpretation of the tab list; execution exceptions retain their partial results */
94+
public OpenCliBrowserResult<List<OpenCliBrowserTab>> tabListTyped() {
95+
return OpenCliBrowserDecoder.tabList(tabList());
96+
}
97+
8998
/**
9099
* {@code browser tab new [url]}。
91100
*
@@ -149,6 +158,15 @@ public OpenCliResult open(String url, OpenCliBrowserTabOptions tab) {
149158
return invokeSub(args);
150159
}
151160

161+
/**
162+
* @param url requested navigation URL
163+
* @param tab optional target tab
164+
* @return strict interpretation of the navigation result
165+
*/
166+
public OpenCliBrowserResult<OpenCliBrowserOpenResult> openTyped(String url, OpenCliBrowserTabOptions tab) {
167+
return OpenCliBrowserDecoder.open(open(url, tab));
168+
}
169+
152170
/** {@code browser back} */
153171
public OpenCliResult back(OpenCliBrowserTabOptions tab) {
154172
List<String> args = new ArrayList<>();
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package io.github.easy4j.opencli.browser.result;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.databind.DeserializationFeature;
5+
import com.fasterxml.jackson.databind.JsonNode;
6+
import com.fasterxml.jackson.databind.ObjectReader;
7+
import com.fasterxml.jackson.databind.json.JsonMapper;
8+
import io.github.easy4j.opencli.browser.result.OpenCliBrowserResult.Status;
9+
import io.github.easy4j.opencli.core.OpenCliExecutionDetails;
10+
import io.github.easy4j.opencli.core.OpenCliResult;
11+
import java.util.ArrayList;
12+
import java.util.Collections;
13+
import java.util.List;
14+
import java.util.Objects;
15+
import java.util.function.Function;
16+
17+
/** Decodes known OpenCLI 1.8.8 browser leaf shapes without interpreting metadata as instructions. */
18+
public final class OpenCliBrowserDecoder {
19+
private static final ObjectReader JSON = new JsonMapper().reader().with(
20+
DeserializationFeature.FAIL_ON_TRAILING_TOKENS,
21+
DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY,
22+
DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS);
23+
24+
private OpenCliBrowserDecoder() {
25+
}
26+
27+
/** @param raw browser tab list execution result @return strict tab-list observation */
28+
public static OpenCliBrowserResult<List<OpenCliBrowserTab>> tabList(OpenCliResult raw) {
29+
return decode(raw, OpenCliBrowserDecoder::readTabs);
30+
}
31+
32+
/** @param raw browser open execution result @return strict navigation observation */
33+
public static OpenCliBrowserResult<OpenCliBrowserOpenResult> open(OpenCliResult raw) {
34+
return decode(raw, OpenCliBrowserDecoder::readNavigation);
35+
}
36+
37+
private static <T> OpenCliBrowserResult<T> decode(OpenCliResult raw, Function<JsonNode, T> model) {
38+
Objects.requireNonNull(raw, "raw");
39+
OpenCliExecutionDetails execution = raw.getExecutionDetails();
40+
if (Objects.nonNull(execution) && execution.isStdoutTruncated()) {
41+
return new OpenCliBrowserResult<>(raw, Status.TRUNCATED, null);
42+
}
43+
if (!raw.isSuccess()) {
44+
return new OpenCliBrowserResult<>(raw, Status.EXECUTION_FAILED, null);
45+
}
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);
49+
}
50+
JsonNode json;
51+
try {
52+
json = JSON.readTree(stdout);
53+
} catch (JsonProcessingException ex) {
54+
return new OpenCliBrowserResult<>(raw, Status.INVALID_JSON, null);
55+
}
56+
try {
57+
return new OpenCliBrowserResult<>(raw, Status.PARSED, model.apply(json));
58+
} catch (IllegalArgumentException ex) {
59+
return new OpenCliBrowserResult<>(raw, Status.INVALID_SHAPE, null);
60+
}
61+
}
62+
63+
private static List<OpenCliBrowserTab> readTabs(JsonNode json) {
64+
if (!json.isArray()) {
65+
throw invalidShape();
66+
}
67+
List<OpenCliBrowserTab> tabs = new ArrayList<>(json.size());
68+
for (JsonNode item : json) {
69+
JsonNode index = item.path("index");
70+
JsonNode active = item.path("active");
71+
if (!item.isObject() || !index.isIntegralNumber() || !index.canConvertToInt()
72+
|| index.intValue() < 0 || !active.isBoolean()) {
73+
throw invalidShape();
74+
}
75+
tabs.add(new OpenCliBrowserTab(index.intValue(), pageId(item.path("page")), active.booleanValue(),
76+
optionalText(item.path("url")), optionalText(item.path("title")), item));
77+
}
78+
return Collections.unmodifiableList(tabs);
79+
}
80+
81+
private static OpenCliBrowserOpenResult readNavigation(JsonNode json) {
82+
if (!json.isObject() || !json.path("url").isTextual()) {
83+
throw invalidShape();
84+
}
85+
JsonNode page = json.path("page");
86+
return new OpenCliBrowserOpenResult(json.path("url").textValue(),
87+
page.isMissingNode() ? null : pageId(page), json);
88+
}
89+
90+
private static String pageId(JsonNode node) {
91+
if (node.isTextual() && !node.textValue().isEmpty()) {
92+
return node.textValue();
93+
}
94+
if (node.isIntegralNumber()) {
95+
return node.bigIntegerValue().toString();
96+
}
97+
throw invalidShape();
98+
}
99+
100+
private static String optionalText(JsonNode node) {
101+
if (node.isMissingNode() || node.isNull()) {
102+
return null;
103+
}
104+
if (!node.isTextual()) {
105+
throw invalidShape();
106+
}
107+
return node.textValue();
108+
}
109+
110+
private static IllegalArgumentException invalidShape() {
111+
return new IllegalArgumentException("Unrecognized browser result shape");
112+
}
113+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
/** Navigation metadata from the pinned OpenCLI browser open contract. */
9+
@Getter
10+
@AllArgsConstructor(access = AccessLevel.PACKAGE)
11+
public final class OpenCliBrowserOpenResult {
12+
private final String url;
13+
/** Present when OpenCLI reports the active page identifier. */
14+
private final String pageId;
15+
private final JsonNode metadata;
16+
17+
/** @return all reported fields, including optional sitemap or trace data */
18+
public JsonNode getMetadata() {
19+
return metadata.deepCopy();
20+
}
21+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package io.github.easy4j.opencli.browser.result;
2+
3+
import io.github.easy4j.opencli.core.OpenCliResult;
4+
import lombok.AccessLevel;
5+
import lombok.AllArgsConstructor;
6+
import lombok.Getter;
7+
8+
/** A strict browser result paired with the original process evidence. */
9+
@Getter
10+
@AllArgsConstructor(access = AccessLevel.PACKAGE)
11+
public final class OpenCliBrowserResult<T> {
12+
public enum Status { PARSED, TRUNCATED, EXECUTION_FAILED, EMPTY, INVALID_JSON, INVALID_SHAPE }
13+
14+
private final OpenCliResult raw;
15+
private final Status status;
16+
/** Typed data, present only when status is PARSED. */
17+
private final T value;
18+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
/** Tab metadata from the pinned OpenCLI browser tab list contract. */
9+
@Getter
10+
@AllArgsConstructor(access = AccessLevel.PACKAGE)
11+
public final class OpenCliBrowserTab {
12+
private final int index;
13+
private final String pageId;
14+
private final boolean active;
15+
private final String url;
16+
private final String title;
17+
private final JsonNode metadata;
18+
19+
/** @return an independent JSON view containing all fields and the original identifier type */
20+
public JsonNode getMetadata() {
21+
return metadata.deepCopy();
22+
}
23+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package io.github.easy4j.opencli.contract;
2+
3+
import io.github.easy4j.opencli.OpenCliProperties;
4+
import io.github.easy4j.opencli.browser.OpenCliBrowserSession;
5+
import io.github.easy4j.opencli.browser.result.OpenCliBrowserDecoder;
6+
import io.github.easy4j.opencli.browser.result.OpenCliBrowserOpenResult;
7+
import io.github.easy4j.opencli.browser.result.OpenCliBrowserResult;
8+
import io.github.easy4j.opencli.browser.result.OpenCliBrowserResult.Status;
9+
import io.github.easy4j.opencli.browser.result.OpenCliBrowserTab;
10+
import io.github.easy4j.opencli.core.OpenCliExecutionDetails;
11+
import io.github.easy4j.opencli.core.OpenCliExecutor;
12+
import io.github.easy4j.opencli.core.OpenCliResult;
13+
import java.io.IOException;
14+
import java.io.InputStream;
15+
import java.io.UncheckedIOException;
16+
import java.nio.charset.StandardCharsets;
17+
import java.util.Arrays;
18+
import java.util.List;
19+
import java.util.Scanner;
20+
import org.junit.jupiter.api.Test;
21+
22+
import static org.junit.jupiter.api.Assertions.*;
23+
24+
class OpenCliBrowserResultTest {
25+
@Test
26+
void parsesPinnedArrayAndObjectAsTheirOwnContracts() {
27+
OpenCliResult tabRaw = raw(fixture("tab-list"));
28+
OpenCliBrowserResult<List<OpenCliBrowserTab>> tabs = OpenCliBrowserDecoder.tabList(tabRaw);
29+
assertEquals(Status.PARSED, tabs.getStatus());
30+
assertSame(tabRaw, tabs.getRaw());
31+
assertEquals("00017", tabs.getValue().get(0).getPageId());
32+
assertEquals(0, tabs.getValue().get(0).getIndex());
33+
assertTrue(tabs.getValue().get(0).isActive());
34+
35+
OpenCliBrowserResult<OpenCliBrowserOpenResult> open = OpenCliBrowserDecoder.open(raw(fixture("open")));
36+
assertEquals(Status.PARSED, open.getStatus());
37+
assertEquals("http://127.0.0.1:18765/basic.html", open.getValue().getUrl());
38+
assertEquals("00017", open.getValue().getPageId());
39+
assertFalse(open.getValue().getMetadata().has("sitemap"));
40+
41+
OpenCliBrowserResult<List<OpenCliBrowserTab>> empty = OpenCliBrowserDecoder.tabList(raw(fixture("tab-list-empty")));
42+
assertEquals(Status.PARSED, empty.getStatus());
43+
assertTrue(empty.getValue().isEmpty());
44+
assertThrows(UnsupportedOperationException.class, () -> tabs.getValue().clear());
45+
}
46+
47+
@Test
48+
void retainsLosslessIdentifiersAndUnknownMetadata() {
49+
OpenCliBrowserOpenResult open = OpenCliBrowserDecoder.open(raw(
50+
"{\"url\":\"https://example.invalid\",\"page\":9007199254740993,"
51+
+ "\"sitemap\":{\"path\":\"/untrusted/metadata.yaml\"},\"trace\":{\"id\":\"fixture\"}}")).getValue();
52+
assertEquals("9007199254740993", open.getPageId());
53+
assertTrue(open.getMetadata().get("page").isIntegralNumber());
54+
assertEquals("/untrusted/metadata.yaml", open.getMetadata().get("sitemap").get("path").asText());
55+
assertEquals("fixture", open.getMetadata().get("trace").get("id").asText());
56+
57+
OpenCliBrowserTab tab = OpenCliBrowserDecoder.tabList(raw(
58+
"[{\"index\":0,\"page\":\"0017\",\"active\":false,\"future\":true}]")).getValue().get(0);
59+
assertEquals("0017", tab.getPageId());
60+
assertTrue(tab.getMetadata().get("future").asBoolean());
61+
assertFalse(tab.isActive());
62+
}
63+
64+
@Test
65+
void malformedTruncatedAndWrongLeafCannotBecomeEmptySuccess() {
66+
assertEquals(Status.EMPTY, OpenCliBrowserDecoder.tabList(raw(" ")).getStatus());
67+
assertEquals(Status.INVALID_JSON, OpenCliBrowserDecoder.tabList(raw("[{")).getStatus());
68+
assertEquals(Status.INVALID_JSON, OpenCliBrowserDecoder.tabList(raw("[]\u0000")).getStatus());
69+
assertEquals(Status.INVALID_SHAPE, OpenCliBrowserDecoder.tabList(raw("[{}]")).getStatus());
70+
assertEquals(Status.INVALID_SHAPE, OpenCliBrowserDecoder.open(raw("[]")).getStatus());
71+
OpenCliResult truncated = OpenCliResult.builder().stdout("[]").success(false)
72+
.executionDetails(OpenCliExecutionDetails.builder().stdoutTruncated(true).build()).build();
73+
assertEquals(Status.TRUNCATED, OpenCliBrowserDecoder.tabList(truncated).getStatus());
74+
assertNull(OpenCliBrowserDecoder.tabList(truncated).getValue());
75+
OpenCliResult failed = OpenCliResult.builder().stdout("[]").success(false).exitCode(2).build();
76+
assertEquals(Status.EXECUTION_FAILED, OpenCliBrowserDecoder.tabList(failed).getStatus());
77+
}
78+
79+
@Test
80+
void typedSessionMethodsUseExistingBrowserCommandsAndKeepCloseResult() {
81+
OpenCliResult closeResult = raw("closed");
82+
OpenCliExecutor executor = new OpenCliExecutor(new OpenCliProperties()) {
83+
@Override
84+
public OpenCliResult invoke(List<String> argv) {
85+
assertEquals(Arrays.asList("browser", "fixture"), argv.subList(0, 2));
86+
List<String> command = argv.subList(2, argv.size());
87+
if (command.equals(Arrays.asList("tab", "list"))) {
88+
return raw(fixture("tab-list"));
89+
}
90+
if (command.equals(Arrays.asList("open", "http://127.0.0.1:18765/basic.html"))) {
91+
return raw(fixture("open"));
92+
}
93+
assertEquals(Arrays.asList("close"), command);
94+
return closeResult;
95+
}
96+
};
97+
OpenCliBrowserSession session = new OpenCliBrowserSession(executor, "fixture", null);
98+
assertEquals("00017", session.tabListTyped().getValue().get(0).getPageId());
99+
assertEquals("00017", session.openTyped("http://127.0.0.1:18765/basic.html", null).getValue().getPageId());
100+
assertSame(closeResult, session.close());
101+
}
102+
103+
static String fixture(String name) {
104+
try (InputStream stream = OpenCliBrowserResultTest.class.getResourceAsStream(
105+
"/opencli-contracts/v1/browser-output-1.8.8/" + name + ".json");
106+
Scanner scanner = new Scanner(stream, StandardCharsets.UTF_8.name()).useDelimiter("\\A")) {
107+
return scanner.next();
108+
} catch (IOException ex) {
109+
throw new UncheckedIOException(ex);
110+
}
111+
}
112+
113+
private static OpenCliResult raw(String stdout) {
114+
return OpenCliResult.builder().stdout(stdout).success(true).exitCode(0).build();
115+
}
116+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.json -text
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
83ece2b39654da1ae482ea46fc3f00a65b01bbed80466cccf8049f4f7c937eae open.json
2+
37517e5f3dc66819f61f5a7bb8ace1921282415f10551d2defa5c3eb0985b570 tab-list-empty.json
3+
c81e67d4ed3c58ec33d80f9d0c2feceee33a3588792c5f51e4a74a6069fed8b2 tab-list.json

0 commit comments

Comments
 (0)