Skip to content

test(edge-api): 3792 行/121 Test 的 handlers_test.go 按域归位——包内生产代码与另 13 个测试文件早已按域切分,它是唯一没归位的 leftovers;拆成 10 个域测试文件后只剩 184 行/3 个 handlers.go 级 Test + 一份共享夹具(测试名集合与 helper 集合逐个相同、=== RUN 413=413、0 SKIP,净 +113 行是拆文件固有成本) - #2288

Merged
DeliciousBuding merged 2 commits into
masterfrom
test/edge-api-split
Sep 3, 2026

Conversation

@DeliciousBuding

Copy link
Copy Markdown
Collaborator

做了什么

edge-server/internal/api/handlers_test.go 是全仓最大的 Go 测试文件:3792 行 / 121 个 func Test,而它所在的包生产代码早已按域切分handlers_runs.gohandlers_projects.gohandlers_settings.gohandlers_artifacts.gohandlers_instances.gohandlers_agents.gohandlers_approvals.gohandlers_events.gohandlers_delivery_journal.gomodel_catalog*.goresponse_dto.go),测试也早已按域命名handlers_settings_test.gohandlers_runtime_sessions_test.gohandlers_approvals_ownership_test.godeploy_*_test.go×5、diff_apply_*_test.go×3 …)。这个巨型文件是这套约定下唯一没归位的 leftovers。

本 PR 是纯搬家:121 个 Test 按被测生产文件归位,handlers_test.go 从 3792 行/121 Test 变成 184 行/3 Test(只留真正属于 handlers.goTestErrorResponseFormatTestMuxUnknownPathTestEnsureStoreNilRepoCreatesInMemory + 一份共享夹具 newTestHandler/fakeRunExecutor/allowTestWorkspace/drainEvents/assertNextEvent/assertErrorCode/unwrapSuccess/fakeCallbackJournal)。

新建/扩充(Test 数):handlers_runs_test.go 53|handlers_projects_test.go 30|handlers_approvals_test.go 10|handlers_artifacts_test.go 4|handlers_events_test.go 4|handlers_delivery_journal_test.go 2|handlers_agents_test.go 2|response_dto_test.go 2|model_catalog_test.go 1|handlers_instances_test.go 1|handlers_settings_test.go +9(原有 2 个保留)。

守恒证明(主机独立复跑,比「数量对得上」更强)

                       master      branch
func Test 总数(包内)    303    ==   303
测试名集合 diff            —      rc=0(303 行,逐名相同,无新增/丢失/改名)
小写 helper/夹具名集合 diff —      rc=0(无复制、无丢失)
go test -v 的 === RUN      413   ==   413      (0 SKIP)
--- PASS/FAIL 行           413   ==   413

即:既没有测试被删/被改名,也没有夹具被复制成两份。

证据

$ go build ./...                          rc=0
$ go vet ./internal/api/...               rc=0
$ go test ./internal/api/... -count=1     ok 2.158s
$ gofmt -l internal/api/                  空
$ python3 scripts/verify/verify-test-sleep-ratchet.py   PASS(47 sleeps / 21 files)
$ bash /tmp/run-validate.sh <worktree>    PASS=61 FAIL=0 SKIP(merge-ref)=1   ← 本地复跑 CI validate job 全量
$ git diff --check origin/master...HEAD   rc=0
$ bash scripts/verify/verify-commit-messages.sh master HEAD   rc=0(2 commits)
$ bash scripts/verify/check-secrets.sh --range origin/master...HEAD  → Secret guard passed
$ git diff --name-only origin/master...HEAD | grep -v '_test\.go$'   → 空(写集只有测试文件)

暗卷(主机自己做的一发,证明搬走的测试仍然会咬人):把 internal/api/handlers_projects.go:188role := strings.TrimSpace(req.Role) 改成 role := "user",已搬到新文件的 TestPostThreadMessageUsesRequestedRole 立刻红:

handlers_projects_test.go:271: item role = "user", want assistant
FAIL github.com/agenthub/edge-server/internal/api 0.014s

git checkout 还原后复跑 ok 0.013s。(车道自己另做了 2 发:GetDeliveryJournal 跳过 entries、GetThreadPins 跨线程过滤,均红→revert 复绿。)

诚实记账

  • 净 +113 行(12 files changed, 3760 insertions, 3647 deletions):拆文件的固有成本是每个新文件自己的 package 行与 import 块。换来的是「按域名找测试」而不是在 3792 行里搜函数名,且与包内既有 13 个域测试文件的约定一致。这不是行数消融批,是结构归位批。
  • 提交时用过一次 LEAK_GUARD_SKIP=1:本机 pre-commit leak-guard 对既有测试夹具 const testCapSecret = "my-secret-key-for-capability-test-32"(master 的 handlers_test.go:3141,随 Test 搬到 handlers_runs_test.go:1132)报假阳性。主机复核:git grep -hoE "\"[A-Za-z0-9_-]*(secret|token|password|apikey|api_key)[A-Za-z0-9_-]*\"" 在 master 与 branch 两侧的字面量集合逐字相同(各 4 条,diff rc=0),且 CI 的 check-secrets.sh --range 通过 ⇒ 没有引入任何新敏感串。
  • TestErrorResponseFormat 测的是 errcode.ErrorBody 形状,留在 handlers_test.gohandlers.go 级);TestArtifactDiffPreviewReadOnlyRoutes… 横跨 diff/artifacts/previews,按名归到 artifacts,可再议。
  • 生产代码一行未改;scripts/verify/** 与各基线 json 未动(已核:test-sleep-budget.jsoninternal/api 条目,handlers_test.gotime.Sleep = 0,全部 scripts/verify/*.json grep handlers_test.go = 0 命中 ⇒ 拆分不踩任何 per-file ratchet)。

DeliciousBuding and others added 2 commits September 3, 2026 20:42
…to 域 95 个 Test 按生产文件归位(纯搬家,Test 总数 303 守恒,含共享夹具与单测私有夹具归属整理)

Co-authored-by: Cursor <cursor@vectorcontrol.tech>
…l 域 23 个 Test 归位,handlers_test.go 只留 3 个 handlers.go 级测试与共享夹具

Co-authored-by: Cursor <cursor@vectorcontrol.tech>
@coderabbitai

coderabbitai Bot commented Sep 3, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Team

Run ID: 37252a22-a415-4b42-bd0e-55614a7d95b6

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

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