Repository navigation
test: 容忍回环端口扫描探针,消除 mock server 空 body JSON.parse 导致的随机失败 - #1965
Merged
Merged
Conversation
## 改了什么
新增 `test/helpers/read-request-body.ts`,把 mock HTTP server 读 body 的
`JSON.parse(body)` 换成容错读取,并应用到 12 个绑定回环端口的测试 fixture。
## 为什么
本机(以及任何带端口扫描器的 CI/开发机)上,扫描器会对每个 listening 的回环
端口发 `GET /`、**空 body**。fixture 里的裸 `JSON.parse('')` 抛
`SyntaxError: Unexpected end of JSON input`:在 async handler 里是 unhandled
rejection,在 `req.on('end')` 回调里是直接崩。实测(详见下方验证)这会让
`test/auth-request.test.ts` 约 50% 的运行以非零码退出,而断言本身全绿——
CI 与本地结论随机不一致。
探针不是 fixture 契约的问题:真实 HTTP server 本就要容忍空 body,所以
fixture 也该容忍。
## 影响面
纯测试代码,不碰 src/。只影响这 12 个 fixture 的 body 读取路径;每个 fixture
仍对**非空但畸形**的 body 保持 fail-closed(默认 strict,空 body 之外照旧报错)。
不含任何平台 / CLI / 后端分支。
## 验证
- 新增 `test/read-request-body.test.ts`(7 项)钉住契约:空 body 不抛、
JSON 正常解析、畸形 body 分类为 other、`0`/`null` 等合法 falsy payload
不被误当空 body 吞掉(`JSON.parse('')` 与 `JSON.parse('null')` 的区别)、
strict 与非 strict 两种模式的行为
- 反变异:把 helper 对空 body 的容错删掉 → 新增测试 1 项转红,证明承重
- `npx vitest run` 覆盖全部 12 个被改 fixture + 新测试:293 passed | 3 skipped
- `test/auth-request.test.ts` 连续 6 次:修复前 exit code 0,1,0,1,0,1(3/6 非零,
与扫描器触发时序一致);修复后 6/6 全 0
- `npx tsc --noEmit -p tsconfig.test-mocks.json` 通过;helper 单文件 strict
typecheck 通过
deepcoldy
requested review from
47seek,
LucasIcarus,
xiaoxueSunn and
xiongz-c
as code owners
October 15, 2026 20:53
|
🚀 Released in v3.45.0 |
deepcoldy
added a commit
that referenced
this pull request
Oct 23, 2026
refused / timedOut / interrupt-error 分支在请求日志里看到 turn/interrupt 后立即断言 stdout 已含 "Codex user input failed"。runner 确实先写这行再发 interrupt,但请求日志 是假 codex 直接写盘、测试同步读到,stdout 要经管道由事件循环送进 harness.stdout, 两者在测试进程里没有先后保证(bun 下 runner 的 stdout 还可能先缓冲),bun-test 间歇失败(#2232)。改为 waitFor 等这行到达。 同文件同类:async-question overlap 分支等到 control socket 的 final 后立即读 stdout,同样改为先等再断言;async-question 夹具的 HTTP server 遇到宿主机端口 扫描器的空 body 探针会 JSON.parse 抛错(#1965 漏掉的一处),空 body 直接回 400。 Fixes #2232 Co-Authored-By: Claude Opus 5.5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
改了什么
新增
test/helpers/read-request-body.ts,把 mock HTTP server 读 body 的裸JSON.parse(body)换成容错读取,并应用到 12 个绑定回环端口的测试 fixture。被修的 fixture:
test/auth-request.test.tsJSON.parse(body)test/ask-cli.test.tstest/report-recipient-cli.test.tsJSON.parse(body)test/input-capture-gateway.test.tstest/schedule-cli-update.test.tsJSON.parse(body)test/cli-send-hook-context.test.tsreq.on('end')里JSON.parse(body)test/codex-app-runner.integration.test.tsreq.on('end')里JSON.parse(body)test/lark-tool-real-cli.integration.test.tsJSON.parse(raw)test/model-only-opencode.e2e.ts、test/model-only-gemini.e2e.tsJSON.parse(raw)test/minimax-mcode.e2e.ts、test/minimax-tmux.e2e.tsJSON.parse(raw)为什么
本机跑着一份系统性回环端口扫描器(
/etc/sysop/mongoosev3-agent/plugin/scanner/scanner),它会对每个 listening 的127.0.0.1端口发GET /、空 body、User-Agent: Go-http-client/1.1。fixture 里的裸
JSON.parse('')抛SyntaxError: Unexpected end of JSON input:async (req,res)handler 里 → unhandled rejection,Vitest 报Unhandled Errors;req.on('end', …)回调里 → 直接崩进程。实测影响(同一份代码、同一台机器,只改测试文件重复跑):
断言本身始终全绿,红的是 unhandled error 造成的退出码——所以症状是「CI 绿、本地随机红」或反之,且失败不指向任何真实回归。
探针本身不是 fixture 契约的问题:真实 HTTP server 本就要容忍空 body,fixture 也该容忍。
探针确认
用真实
node:httpserver 监听127.0.0.1抓包,在无任何测试运行时也能稳定收到:同一形态在三个不同端口上各收到 2 次 ⟹ 是主机级周期扫描,不是某个测试的产物。
影响面
纯测试代码,不碰
src/。只影响上述 fixture 的 body 读取路径;每个 fixture 仍对非空但畸形的 body 保持 fail-closed(helper 默认strict: true,只有空 body 被当作可忽略的探针)。不含任何平台 / CLI / 后端分支,不影响生产行为。验证
test/read-request-body.test.ts(7 项)钉住契约:空 body 不抛、JSON 正常解析、畸形 body 分类为other、0/null等合法 falsy payload 不被误当空 body 吞掉、strict 与非 strict 两种模式的行为;npx vitest run(10 个被改 fixture + 新测试):308 passed | 3 skipped;bun build --no-bundle);npx tsc --noEmit -p tsconfig.test-mocks.json通过;helper 与测试单文件 strict typecheck 通过;test/auth-request.test.ts连续 6 次全部 exit 0(见上表)。