Skip to content

Commit 2986a86

Browse files
committed
test: verify OpenCLI context remains isolated on command failure
1 parent ee64145 commit 2986a86

3 files changed

Lines changed: 40 additions & 4 deletions

File tree

‎openspec/changes/add-opencli-context-and-diagnostics/specs/opencli-context-diagnostics/spec.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# 隔离执行上下文与只读诊断
22

3-
> Proposal specification. Status: DRAFT / NOT_IMPLEMENTED.
3+
> Implementation verified on the feature branches. PR review and merge are pending.
44
55
## ADDED Requirements
66

‎openspec/changes/add-opencli-context-and-diagnostics/tasks.md‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
# C08 Tasks · 隔离执行上下文与只读诊断
22

3-
> 全部为待执行实现任务。文档/格式检查通过不允许提前勾选。
3+
> 上下文与只读诊断验收已在三条功能分支核对,待 PR 评审与合并。
44
55
关联规范:[spec](specs/opencli-context-diagnostics/spec.md)。
66

77
## 1. 上下文契约
88

99
- [ ] 1.1 核实锁定版本profile与Browser公共options的scope和真实输出fixture。
10-
- [ ] 1.2 编写并发context隔离、false覆盖、scope冲突与诊断UNKNOWN的失败测试。
10+
- [x] 1.2 编写并发context隔离、false覆盖、scope冲突与诊断UNKNOWN的失败测试。
1111

1212
## 2. 增量API
1313

14-
- [ ] 2.1 实现不可变context与优先级合并,复用原meta/auth/skills入口。
14+
- [x] 2.1 实现不可变context与优先级合并,复用原meta/auth/skills入口。
1515
- [x] 2.2 实现per-check只读诊断,审计每个probe副作用;不能确认时保留unknown。
1616
- [x] 2.3 保留sitemap raw/optional metadata,不自动访问overlay或trace文件。
1717

@@ -36,3 +36,12 @@
3636
`diagnose()` 独立报告可执行文件、版本、历史帮助、Browser Bridge、认证、daemon 和远端传输。历史帮助有来源和观察时刻,配置或版本不匹配时保持 UNKNOWN。远端只读诊断跳过本机命令并保持传输未验证。三个本地运行时状态无法确认只读探测,保持 UNKNOWN。见 [只读边界与集成验证](evidence/2026-11-19-read-only-diagnostics.md)。
3737

3838
`OpenCliDiagnosticsTest` 初始缺少公开 API,编译失败。实现后回归记录只运行 `--version`,并验证显式发现的历史证据不提升为当前健康。已发布 CLI 的 opt-in `OpenCliReadOnlyDiagnosticsIT` 区分版本探测与执行启动钩子的显式帮助。报告位于 `target/surefire-reports`。
39+
40+
41+
## C08 失败隔离与只读复验
42+
43+
一个 Java 客户端通过独立 JVM 子进程并发执行两个不同 profile/window 的调用。故意失败的调用及其 partial result 保留自己的 argv,另一调用保留自己的 argv。随后没有覆盖值的调用继续使用客户端的原默认值。现有回归同时验证显式 `false`、scope 冲突和未经验证诊断的 UNKNOWN。
44+
45+
使用发布版 OpenCLI 1.8.8 的独立配置目录验证默认诊断只取版本,不执行安装版 CLI 的 startup hook。显式发起帮助探测会触发 fixture hook,后续默认诊断不会再次执行它。sitemap 和 trace 路径保持数据,pipeline CLI 仍为 DEFERRED。
46+
47+
本分支以 `ee64145ca904f2384bed1265036cd7c2acc1f73b` 为基线,使用 JDK 8,提供安装版 CLI 对应的 `OPENCLI_INSTALLED_NODE` 与 `OPENCLI_INSTALLED_ENTRY` 后执行 `./mvnw -B -ntp -o -Dtest=OpenCliInvocationContextTest,OpenCliDiagnosticsTest,OpenCliReadOnlyDiagnosticsIT test`。8 项实际执行,零失败、零错误、零跳过,退出码为 0。报告位于 `target/surefire-reports`。

‎src/test/java/io/github/easy4j/opencli/capability/OpenCliInvocationContextTest.java‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io.github.easy4j.opencli.OpenCliInvocationContext.SiteSession;
66
import io.github.easy4j.opencli.OpenCliInvocationContext.WindowMode;
77
import io.github.easy4j.opencli.OpenCliProperties;
8+
import io.github.easy4j.opencli.exception.OpenCliNonZeroExitException;
89
import java.io.File;
910
import java.io.InputStream;
1011
import java.nio.charset.StandardCharsets;
@@ -33,6 +34,29 @@ void concurrentInvocationsKeepTheirProfileAndWindow() throws Exception {
3334
}
3435
}
3536

37+
@Test
38+
void failedInvocationKeepsItsContextWithoutChangingConcurrentOrDefaultCalls() throws Exception {
39+
OpenCliProperties properties = properties();
40+
properties.setInvocationDefaults(OpenCliInvocationContext.builder().profile("profile-default")
41+
.window(WindowMode.FOREGROUND).build());
42+
ExecutorService pool = Executors.newFixedThreadPool(2);
43+
try (OpenCliClient client = new OpenCliClient(properties)) {
44+
Future<OpenCliNonZeroExitException> failure = pool.submit(() -> assertThrows(
45+
OpenCliNonZeroExitException.class, () -> state(client, "profile-failure", WindowMode.BACKGROUND)));
46+
Future<String> success = pool.submit(() -> state(client, "profile-success", WindowMode.FOREGROUND));
47+
48+
assertEquals("--profile|profile-success|browser|session-a|--window|foreground|state",
49+
success.get(12, TimeUnit.SECONDS));
50+
assertEquals("--profile|profile-failure|browser|session-a|--window|background|state",
51+
failure.get(12, TimeUnit.SECONDS).getPartialResult().getStdout());
52+
assertEquals("--profile|profile-default|browser|session-a|--window|foreground|state",
53+
client.invokeDiscovered(OpenCliHelpScope.BROWSER_STATE, "opencli browser <session> state",
54+
"session-a", null, null).getStdout());
55+
} finally {
56+
pool.shutdownNow();
57+
}
58+
}
59+
3660
@Test
3761
void explicitFalseOverridesClientDefaultWithinVerifiedBrowserAdapter() {
3862
OpenCliProperties properties = properties();
@@ -99,6 +123,9 @@ public static void main(String[] args) throws Exception {
99123
System.exit(4);
100124
}
101125
System.out.print(String.join("|", args));
126+
if (argv.startsWith("--profile profile-failure ")) {
127+
System.exit(7);
128+
}
102129
}
103130
}
104131

0 commit comments

Comments
 (0)