Skip to content

test: 容忍回环端口扫描探针,消除 mock server 空 body JSON.parse 导致的随机失败 - #1965

Merged
deepcoldy merged 1 commit into
masterfrom
fix/auth-request-test-flaky-empty-body
Oct 15, 2026
Merged

deepcoldy merged 1 commit into
masterfrom
fix/auth-request-test-flaky-empty-body

Conversation

@deepcoldy

Copy link
Copy Markdown
Owner

改了什么

新增 test/helpers/read-request-body.ts,把 mock HTTP server 读 body 的裸 JSON.parse(body) 换成容错读取,并应用到 12 个绑定回环端口的测试 fixture。

被修的 fixture:

文件 原形态
test/auth-request.test.ts JSON.parse(body)
test/ask-cli.test.ts 6 处
test/report-recipient-cli.test.ts JSON.parse(body)
test/input-capture-gateway.test.ts 2 处
test/schedule-cli-update.test.ts JSON.parse(body)
test/cli-send-hook-context.test.ts req.on('end') 里 JSON.parse(body)
test/codex-app-runner.integration.test.ts req.on('end') 里 JSON.parse(body)
test/lark-tool-real-cli.integration.test.ts JSON.parse(raw)
test/model-only-opencode.e2e.ts、test/model-only-gemini.e2e.ts JSON.parse(raw)
test/minimax-mcode.e2e.ts、test/minimax-tmux.e2e.ts JSON.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', …) 回调里 → 直接崩进程。

实测影响(同一份代码、同一台机器,只改测试文件重复跑):

修复前  test/auth-request.test.ts  exit code: 0, 1, 0, 1, 0, 1   ← 3/6 非零
修复后  test/auth-request.test.ts  exit code: 0, 0, 0, 0, 0, 0   ← 6/6 全零

断言本身始终全绿,红的是 unhandled error 造成的退出码——所以症状是「CI 绿、本地随机红」或反之,且失败不指向任何真实回归。

探针本身不是 fixture 契约的问题:真实 HTTP server 本就要容忍空 body,fixture 也该容忍。

探针确认

用真实 node:http server 监听 127.0.0.1 抓包,在无任何测试运行时也能稳定收到:

{"m":"GET","u":"/","ua":"Go-http-client/1.1","peer":"127.0.0.1"}

同一形态在三个不同端口上各收到 2 次 ⟹ 是主机级周期扫描,不是某个测试的产物。

影响面

纯测试代码,不碰 src/。只影响上述 fixture 的 body 读取路径;每个 fixture 仍对非空但畸形的 body 保持 fail-closed(helper 默认 strict: true,只有空 body 被当作可忽略的探针)。不含任何平台 / CLI / 后端分支,不影响生产行为。

验证

  1. 新增 test/read-request-body.test.ts(7 项)钉住契约:空 body 不抛、JSON 正常解析、畸形 body 分类为 other、0/null 等合法 falsy payload 不被误当空 body 吞掉、strict 与非 strict 两种模式的行为;
  2. 反变异:删掉 helper 对空 body 的容错 → 新增测试 1 项转红(证明测试承重,非摆设);
  3. npx vitest run(10 个被改 fixture + 新测试):308 passed | 3 skipped;
  4. 4 个 e2e 文件在本机既有无 runner 条件下 skip,已确认语法可解析(bun build --no-bundle);
  5. npx tsc --noEmit -p tsconfig.test-mocks.json 通过;helper 与测试单文件 strict typecheck 通过;
  6. test/auth-request.test.ts 连续 6 次全部 exit 0(见上表)。

## 改了什么

新增 `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
deepcoldy merged commit 19fa491 into master Oct 15, 2026
14 checks passed
@github-actions

Copy link
Copy Markdown

🚀 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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant