Skip to content

Commit 1021ad4

Browse files
committed
Verify OpenCLI capability provenance with real local and plugin overrides
1 parent 3b97f2c commit 1021ad4

2 files changed

Lines changed: 109 additions & 5 deletions

File tree

‎openspec/changes/add-opencli-capability-discovery/tasks.md‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@
77
## 1. schema基线
88

99
- [x] 1.1 从锁定 OpenCLI 采集 root、browser、meta 和 site 帮助,保存原文与 SHA-256。不能读取为单一 JSON 文档的输出单列为负例。见 [1.8.8 采集证据](evidence/2026-11-19-structured-help.md)。
10-
- [ ] 1.2 为scope、optionalvalue、negate、false、aliases、session占位和旧格式编写失败测试。
10+
- [x] 1.2 为scope、optionalvalue、negate、false、aliases、session占位和旧格式编写失败测试。
1111

1212
## 2. 服务实现
1313

14-
- [ ] 2.1 新增Java8可表达schema与快照模型,保留unknown字段和raw;复用已有adapter入口。
15-
- [ ] 2.2 实现有限version/help探测与可信executable校验,禁止自动业务操作。
16-
- [ ] 2.3 实现cache key、内容hash、失效与UNKNOWN/STALE/UNSUPPORTED原因。
14+
- [x] 2.1 新增Java8可表达schema与快照模型,保留unknown字段和raw;复用已有adapter入口。
15+
- [x] 2.2 实现有限version/help探测与可信executable校验,禁止自动业务操作。
16+
- [x] 2.3 实现cache key、内容hash、失效与UNKNOWN/STALE/UNSUPPORTED原因。
1717

1818
## 3. 边界验证
1919

20-
- [ ] 3.1 测试本地override/插件来源未知时不伪造来源和全量覆盖率。
20+
- [x] 3.1 测试本地override/插件来源未知时不伪造来源和全量覆盖率。
2121
- [ ] 3.2 测试原有raw入口不会因为发现故障被无声禁用,remote拒绝范围仍受C04控制。
2222
- [ ] 3.3 三线同一schema fixture结果一致;报告未运行的真实上游采集项。
2323

@@ -55,3 +55,9 @@
5555
`OpenCliClient.invokeDiscovered` 对本地配置取快照,刷新指定范围,选择显式给定的叶子路径,校验后通过同一执行器运行时提交字面参数。需要提供浏览器会话时,必须填入合法会话标识。校验覆盖必选项和值、声明 choices、可选值 flag、显式 false、否定拼写和可变位置参数;用字面分隔符隔离可能像选项的位置参数。不可用或过期的帮助无法进入类型化执行。原有本地 raw 入口独立可用。
5656

5757
`OpenCliCapabilityCommandTest` 和 `OpenCliCapabilityDiscoveryTest` 以失败→通过验证新 API、字段级错误和真实 JVM 子进程收到的 argv。当前仅对已验证的 OpenCLI 1.8.8 执行发现绑定;测试不依赖浏览器账户。
58+
59+
## OC-CAP-004/006 运行时覆盖验收
60+
61+
`OpenCliCapabilityOverrideIT` 使用 Node 24.18.0 和 npm 发布的 OpenCLI 1.8.8。测试以独立临时用户目录运行,每次刷新均查询 npm search 的同一叶子。依次观察内置定义、本地 adapter 覆盖、插件覆盖、移除插件后恢复本地定义。每次均返回 `AVAILABLE`,保留观测版本 1.8.8、原文、`SITE_NPM_SEARCH` 范围和 `UNKNOWN` 来源。配置指纹相同,内容摘要随有效覆盖变化。覆盖 handler 仅在业务执行时写入标记,帮助探测后标记始终不存在。
62+
63+
在配置 `OPENCLI_INSTALLED_NODE` 和 `OPENCLI_INSTALLED_ENTRY` 后执行 `./mvnw -B -ntp -Dtest=OpenCliCapabilityOverrideIT test`。报告为 `target/surefire-reports/TEST-io.github.easy4j.opencli.contract.OpenCliCapabilityOverrideIT.xml`。本测试仅覆盖受控目录下的两个覆盖定义。真实 Browser Bridge 会话与外部插件生态仍未验收。
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package io.github.easy4j.opencli.contract;
2+
3+
import io.github.easy4j.opencli.OpenCliClient;
4+
import io.github.easy4j.opencli.OpenCliProperties;
5+
import io.github.easy4j.opencli.capability.OpenCliCapabilitySnapshot;
6+
import io.github.easy4j.opencli.capability.OpenCliCapabilitySnapshot.Status;
7+
import io.github.easy4j.opencli.capability.OpenCliHelpScope;
8+
import java.io.IOException;
9+
import java.nio.charset.StandardCharsets;
10+
import java.nio.file.Files;
11+
import java.nio.file.Path;
12+
import java.nio.file.Paths;
13+
import java.util.Arrays;
14+
import java.util.Objects;
15+
import org.junit.jupiter.api.Test;
16+
import org.junit.jupiter.api.io.TempDir;
17+
18+
import static org.junit.jupiter.api.Assertions.assertEquals;
19+
import static org.junit.jupiter.api.Assertions.assertFalse;
20+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
21+
import static org.junit.jupiter.api.Assertions.assertTrue;
22+
23+
/** Opt-in real-package fixture, using the same launcher inputs as OpenCliInstalledLauncherIT. */
24+
class OpenCliCapabilityOverrideIT {
25+
@Test
26+
void localAndPluginOverridesRefreshWithoutClaimingOriginOrExecutingHandler(@TempDir Path home) throws IOException {
27+
Path entry = requireFixture("OPENCLI_INSTALLED_ENTRY");
28+
Path preload = home.resolve("isolated-home.mjs");
29+
write(preload, "import os from 'node:os';\n"
30+
+ "import { syncBuiltinESMExports } from 'node:module';\n"
31+
+ "os.homedir = () => process.env.OPENCLI_FIXTURE_HOME;\n"
32+
+ "syncBuiltinESMExports();\n");
33+
Path marker = home.resolve("business-handler-ran");
34+
OpenCliProperties properties = new OpenCliProperties();
35+
properties.setExecutable(requireFixture("OPENCLI_INSTALLED_NODE").toString());
36+
properties.setLeadingArguments(Arrays.asList("--import", preload.toString(), entry.toString()));
37+
properties.getEnvironment().put("OPENCLI_FIXTURE_HOME", home.toString());
38+
properties.getEnvironment().put("OPENCLI_CONFIG_DIR", home.resolve("config").toString());
39+
properties.getEnvironment().put("OPENCLI_CACHE_DIR", home.resolve("cache").toString());
40+
properties.getEnvironment().put("OPENCLI_FIXTURE_MARKER", marker.toString());
41+
properties.getEnvironment().put("HOME", home.toString());
42+
properties.getEnvironment().put("CI", "1");
43+
properties.getEnvironment().put("NO_COLOR", "1");
44+
45+
Path sdkHome = home.resolve(".opencli");
46+
try (OpenCliClient client = new OpenCliClient(properties)) {
47+
OpenCliCapabilitySnapshot bundled = discover(client);
48+
assertFalse(bundled.getHelp().getRawHelp().contains("fixture metadata"));
49+
50+
installOverride(sdkHome.resolve("clis/npm"), entry, "local fixture metadata");
51+
OpenCliCapabilitySnapshot local = discover(client);
52+
assertTrue(local.getHelp().getRawHelp().contains("local fixture metadata"));
53+
assertNotEquals(bundled.getHelpSha256(), local.getHelpSha256());
54+
55+
Path plugin = installOverride(sdkHome.resolve("plugins/fixture"), entry, "plugin fixture metadata");
56+
OpenCliCapabilitySnapshot overridden = discover(client);
57+
assertTrue(overridden.getHelp().getRawHelp().contains("plugin fixture metadata"));
58+
assertFalse(overridden.getHelp().getRawHelp().contains("local fixture metadata"));
59+
assertEquals(bundled.getConfigurationSha256(), overridden.getConfigurationSha256());
60+
assertNotEquals(local.getHelpSha256(), overridden.getHelpSha256());
61+
62+
Files.delete(plugin);
63+
assertEquals(local.getHelpSha256(), discover(client).getHelpSha256());
64+
assertFalse(Files.exists(marker), "metadata discovery must not invoke the adapter handler");
65+
}
66+
}
67+
68+
private static OpenCliCapabilitySnapshot discover(OpenCliClient client) {
69+
OpenCliCapabilitySnapshot snapshot = client.discoverCapabilities(OpenCliHelpScope.SITE_NPM_SEARCH);
70+
assertEquals(Status.AVAILABLE, snapshot.getStatus(), snapshot.getReason());
71+
assertEquals("1.8.8", snapshot.getObservedVersion());
72+
assertEquals("UNKNOWN", snapshot.getCommandOrigin());
73+
assertEquals("SITE_NPM_SEARCH", snapshot.getScope());
74+
assertEquals("opencli npm search", snapshot.getHelp().getSchema().getCommand());
75+
return snapshot;
76+
}
77+
78+
private static Path installOverride(Path directory, Path entry, String description) throws IOException {
79+
Files.createDirectories(directory);
80+
write(directory.resolve("package.json"), "{\"type\":\"module\"}");
81+
Path override = directory.resolve("search.js");
82+
write(override, "import { cli, Strategy } from '" + entry.resolveSibling("registry.js").toUri() + "';\n"
83+
+ "import { writeFileSync } from 'node:fs';\n"
84+
+ "cli({site:'npm', name:'search', strategy:Strategy.PUBLIC, browser:false, access:'read',\n"
85+
+ "description:'" + description + "',\n"
86+
+ "args:[{name:'query', positional:true, required:true}],\n"
87+
+ "async func() { writeFileSync(process.env.OPENCLI_FIXTURE_MARKER, 'executed'); return []; }});\n");
88+
return override;
89+
}
90+
91+
private static void write(Path file, String text) throws IOException {
92+
Files.write(file, text.getBytes(StandardCharsets.UTF_8));
93+
}
94+
95+
private static Path requireFixture(String name) {
96+
return Paths.get(Objects.requireNonNull(System.getenv(name), name + " fixture is required"));
97+
}
98+
}

0 commit comments

Comments
 (0)