Skip to content

test: raise Go coverage 65.6% -> 80.6%, add the coverage gate, label the badges - #20

Merged
crossoverJie merged 6 commits into
mainfrom
test/coverage-batch-1
Nov 12, 2026
Merged

crossoverJie merged 6 commits into
mainfrom
test/coverage-batch-1

Conversation

@crossoverJie

@crossoverJie crossoverJie commented Nov 12, 2026 •

Copy link
Copy Markdown
Owner

Two things in one PR: the unit tests, and the badge confusion.

1. Unit tests — Go coverage 65.6% → 80.6%

Measured the CI way (-coverpkg=./internal/..., the honest cross-package number):

package before after
internal/tools (schedule.go was 1.6%) 33.3% 90.1%
internal/push 61.0% 93.2%
internal/safety 42.1% 90.2%
internal/channels/wechat 35.8% 81.2%
internal/agentrt/adk 35.5% 55.1%
internal/initcmd 0% 82.4%
internal/version 55.6% 88.9%
total 65.6% 80.6%

What each file pins down:

  • internal/tools/schedule_test.go — the chat-facing schedule tool had no direct test (only the store under it did). Every action (list/create/update/delete/enable/disable) plus the validation that keeps a half-specified entry out of schedules.json: exactly one trigger, exactly one action, future-only once, valid timezone, duplicate ids, and the script-only fields (args/commit/retry). Also pins recipient injection from the task context — the model must never guess who to push to.
  • internal/push/push_test.go — the degradation chain decides whether a scheduled result reaches the user at all. Primary success, fallback after a primary failure, text-only degradation (images only ever go out on the primary WeChat channel), skipping unconfigured channels and empty address lists, and contacts.json (dedupe, 0600, corrupt-file recovery).
  • internal/safety/git_test.go — hermetic repos in t.TempDir() with a local bare remote: "commit only the repos that changed", the project filter, the four push skip cases (no origin / detached HEAD / stale upstream / unborn branch), and that the first push uses -u so @{u} resolves afterwards.
  • internal/channels/wechat/{monitor_flow,lifecycle}_test.go — the monitor needed two small seams first (see below): allowlist (an unknown sender never reaches the pipeline and never gets a context token recorded — that token would be a push target), image batching (buffered → flushed by the timer / joined by a caption / immediate when the window is 0), the reply path (markdown stripped, context_token echoed, dedup sentinel never echoed, fixed fallback copy on failure, panic becomes an error instead of killing the poll loop, local + https images uploaded), RunMonitor against a local API server (inbound message reaches the pipeline, cursor persisted, expired session backs off instead of hammering), lifecycle (Start gates, EnsureMonitor idempotence for a late WebUI pair, Stop, Logout, Status), the renewal reminder (one nudge per quiet stretch, nothing inside the window, a failed delivery still recorded so it cannot spam, overlapping ticks skipped) and pushSender.
  • internal/channels/wechat/image_download_test.go — pure helpers (bodyFromItemList / isAllowed / truncate / orEmpty / env getters) plus the CDN path against httptest: 4xx is terminal while 5xx retries, the ciphertext is decrypted and the mime sniffed, and the remote image is written 0600.
  • internal/initcmd / internal/version — botler init scaffolds all four templates with the right modes, never overwrites an edited file, and keeps going when one write fails; String() and the UUID v4 shape.
  • internal/agentrt/adk — systemInstruction / mergeCall / stopReason / convertUsage / the two constructors.

One production fix the tests found

mergeCall folds streamed function calls into the accumulated list. Its "ignore an empty payload" guard was len(args) > 2, which catches {} — but json.Marshal(nil) produces "null" (4 bytes), so a later chunk carrying no arguments wiped the arguments an earlier chunk had already delivered. The new test caught it; the guard now excludes "null" explicitly.

Known limitation, documented in the test

mergeCall keys a call by id and falls back to name. A chunk that only starts carrying an id later is therefore treated as a new call. Every provider seen so far sends the id in the first chunk, so this is documented rather than changed.

2. Coverage gate

A badge reports the number; it does not stop it from dropping. scripts/covercheck (a Go program, matching the "scripts are Go programs" convention) reads the same profile CI produces and enforces a committed baseline (coverage.baseline.json, 80.6% total + 23 packages):

make cover-check   # the gate: same profile, then the floor check
make cover-update  # raise the floor after adding tests
  • Total floor with a 0.2pt tolerance, so rounding noise on a brand-new file does not fail a legitimate PR.
  • Per-package floors (packages ≥ 50 statements, 2pt tolerance): the real risk is not the average but one package quietly losing its tests while another gains some.
  • It merges the duplicated blocks a -coverpkg profile contains (every block is listed once per test binary, so a naive sum is wrong) — that merge is what main_test.go covers, being the part that could silently be wrong.

CI runs the gate after the coverage step and before the Codecov upload, so a red build never publishes a green badge. Verified locally: half the hits zeroed fails on total, one package zeroed fails on that package, -min 95 fails, the real profile passes.

The baseline is committed data rather than a magic number inside a YAML, so raising it is an explicit, reviewable diff.

3. README badges now say which language

Codecov's own badge hardcodes the label "codecov", so ?flag=go and ?flag=ts side by side were indistinguishable. Switched to shields.io's codecov wrapper, which accepts a label:

[![golang coverage](https://img.shields.io/codecov/c/gh/crossoverJie/botler-agent?flag=go&label=golang)](…)
[![typescript (v1) coverage](https://img.shields.io/codecov/c/gh/crossoverJie/botler-agent?flag=ts&label=typescript%20(v1))](…)

Verified both render as intended: golang 66% and typescript (v1) 39%.

Verification

gofmt -l .        -> clean
go vet ./...      -> clean
go test -race ./... -> pass
make cover        -> 80.6%
make cover-check  -> ok (floor 80.4% from baseline)

Two seams added for testability

The wechat monitor had zero coverage for a structural reason, not laziness: session.client was a concrete *ilink.Client whose every call needs a live long poll, so nothing could drive processMessage / dispatchAndReply / runTask. Both seams follow a pattern the package already uses (timeNow, logf):

// send.go — *ilink.Client satisfies this unchanged; so does a fake.
type ilinkSender interface {
	SendText(ctx context.Context, to, text, contextToken string) (string, error)
	UploadFile(ctx context.Context, plaintext []byte, toUserID string, mediaType ilink.UploadMediaType) (*ilink.UploadResult, error)
	SendImage(ctx context.Context, to, contextToken string, uploaded *ilink.UploadResult) (string, error)
}

// monitor.go — the monitor's own decisions, without the model behind them.
var dispatchTask = dispatch.Dispatch

Not covered here (next step)

internal/agentrt/adk's Complete (the streaming aggregate) is the last meaningful gap outside internal/webui; covering it means emulating an SSE stream from a provider, which is a larger job than the rest of this PR.

internal/webui (63%) is mostly WireRuntime, the production wiring — testing it means installing real global backends, which is worse than leaving it uncovered.

Five new test files plus one bug fix; no production behaviour changes apart
from the mergeCall fix called out below.

internal/tools/schedule_test.go (schedule.go 1.6% -> 97%):
the chat-facing schedule tool had no direct test - only the store underneath
it did. Covers every action (list/create/update/delete/enable/disable) and
the validation that keeps a half-specified entry out of schedules.json: one
trigger, one action, future-only `once`, valid timezone, duplicate ids, and
the script-only fields (args/commit/retry). Also pins the recipient injection
from the task context: the model must never guess who to push to.

internal/push/push_test.go (61% -> 93%):
the degradation chain was untested even though it decides whether a scheduled
result reaches the user at all. Covers primary success, fallback after a
primary failure, text-only degradation (images only ever go out on the
primary WeChat channel), skipping unconfigured channels and empty address
lists, and contacts.json (dedupe, 0600, corrupt-file recovery).

internal/safety/git_test.go (42% -> 90%):
hermetic git repos in t.TempDir with a local bare remote. Covers "commit only
the repos that changed", the project filter, the four push skip cases (no
origin, detached HEAD, stale upstream, unborn branch) and that the first push
uses -u so @{u} resolves afterwards.

internal/channels/wechat/{monitor,image_download}_test.go:
pure helpers (bodyFromItemList / isAllowed / truncate / orEmpty / env
getters) plus the CDN path against httptest: 4xx is terminal while 5xx
retries, the ciphertext is decrypted and the mime sniffed, and the remote
image is written 0600.

internal/initcmd + internal/version:
`botler init` scaffolds all four templates with the right modes, never
overwrites an edited file, and keeps going when one write fails; version
String() and the UUID v4 shape.

internal/agentrt/adk:
systemInstruction / mergeCall / stopReason / convertUsage / the two
constructors. Writing the mergeCall test exposed a real edge case: a later
stream chunk carrying no arguments marshals to "null" (4 bytes), which passed
the len > 2 "empty" guard and wiped the arguments an earlier chunk had
delivered. Fixed by excluding "null" explicitly.
Codecov's own badge hardcodes the label "codecov", so two flag-scoped badges
side by side were indistinguishable. shields.io's codecov wrapper accepts a
label, so the badges now read "golang" and "typescript (v1)" on their face
(verified: both render with the expected label and percentage).
@codecov-commenter

codecov-commenter commented Nov 12, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 54.93%. Comparing base (1d5ecc5) to head (4d7f30d).

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #20      +/-   ##
==========================================
+ Coverage   49.48%   54.93%   +5.44%     
==========================================
  Files         118      118              
  Lines       14982    14982              
  Branches      510      510              
==========================================
+ Hits         7414     8230     +816     
+ Misses       7548     6732     -816     
  Partials       20       20              
Flag Coverage Δ
go 80.06% <100.00%> (+14.22%) ⬆️
ts 39.34% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

…table

internal/channels/wechat 46.8% -> 81.2% (package), Go total 74.8% -> 80.6%.

The monitor had zero coverage for a structural reason: `session.client` was a
concrete *ilink.Client, and every one of its calls needs a live long poll, so
nothing could drive processMessage / dispatchAndReply / runTask. Two small
seams, both following the pattern this package already uses (timeNow, logf):

- `ilinkSender` (send.go) narrows the client to the three methods the reply
  path actually calls (SendText / UploadFile / SendImage). *ilink.Client
  satisfies it unchanged, and so does a fake.
- `dispatchTask` (monitor.go) makes the pipeline entry point a variable, so
  the monitor's own decisions can be tested without the model behind them.
  Production keeps calling dispatch.Dispatch.

Now covered (monitor_flow_test.go / lifecycle_test.go):
- allowlist: unknown sender never reaches the pipeline and never gets a
  context token recorded (a recorded token would be a push target)
- batching: an uncaptioned image is buffered, the timer flushes it when the
  window expires, a caption arriving in time joins the image into one task and
  cancels the timer, and window=0 dispatches immediately
- reply path: markdown stripped, context_token echoed, dedup sentinel never
  echoed, failure replies with the fixed fallback copy, missing context_token
  sends nothing, a panic in dispatch becomes an error instead of killing the
  poll loop, and both local and https images are uploaded and sent
- RunMonitor against a local API server: an inbound message reaches the
  pipeline and the cursor is persisted; an expired session backs off instead
  of hammering; cancel returns context.Canceled
- lifecycle: Start's two gates, EnsureMonitor idempotence for a late WebUI
  pair, Stop, Logout clearing account/cursors/contexts/contacts, the Status
  snapshot
- renewal reminder: one nudge per quiet stretch, none while still inside the
  window, none without an owner context, a failed delivery still recorded (so
  it cannot spam), overlapping ticks skipped, loop gated on the threshold
- pushSender: not-configured / missing-context errors, markdown stripping on a
  real sendmessage round trip, missing image file surfaces as an error
@crossoverJie crossoverJie changed the title test: raise Go coverage 65.6% -> 74.8%, and label the README badges by language test: raise Go coverage 65.6% -> 80.6%, and label the README badges by language Nov 12, 2026
…ted baseline)

A badge shows the number; it does not stop it from falling. This adds the gate.

scripts/covercheck (Go, matching the "scripts are Go programs" convention):
reads the same profile CI produces, merges the duplicated blocks a -coverpkg
profile contains (every block is listed once per test binary, so the naive sum
is wrong), then enforces two floors from coverage.baseline.json:

- total statements coverage, with a 0.2pt tolerance so rounding noise on a
  brand-new file does not fail a legitimate PR;
- per-package coverage (packages >= 50 statements, 2pt tolerance), because the
  real risk is not the average but one package quietly losing its tests while
  another gains some.

`make cover-check` is the gate, `make cover-update` raises it. CI runs the gate
after the coverage step and before the Codecov upload, so a red build never
publishes a green badge. The baseline is committed data, not a magic number in
a YAML: 80.6% total, 23 packages.

Verified locally: a profile with half the hits zeroed fails on total, a profile
with one package zeroed fails on that package, and -min 95 fails while the real
profile passes. main_test.go covers the merge/aggregation logic, which is the
part that could silently be wrong.

Also corrects the adk figure in the PR body (55.1%, not 67.4% - that number came
from `go tool cover -func`'s per-function view, not the statement total).
@crossoverJie crossoverJie changed the title test: raise Go coverage 65.6% -> 80.6%, and label the README badges by language test: raise Go coverage 65.6% -> 80.6%, add the coverage gate, label the badges Nov 12, 2026
…ev machine

The first gate run failed at 79.9% while the same commit measured 80.6% here.
Same 5072 statements, so ~35 statements simply execute on a developer machine
but not on a clean runner: internal/config (80.2% locally vs 57.7% in CI) reads
a real ~/.botler-agent/.env, which exercises the load/parse branches that CI
never reaches, and internal/paths/greeting follow the same pattern.

The floor has to describe the lower of the two, otherwise the first PR after
every merge flaps. So the baseline now carries CI's numbers, and both the
script doc and AGENTS.md say to refresh it from a CI run.
Both from review of the gate.

High: the ci-go path filter did not list the gate's own inputs, so a PR that
only lowered coverage.baseline.json (or the Makefile target) skipped the job
that enforces the floor — the floor could be weakened with no validation at
all. Makefile, coverage.baseline.json and scripts/covercheck/** are now in both
path lists (the `**.go` glob already covered the checker source).

Low: the new git integration tests ran assertions inside the bare remote with
cmd.Dir, which is refused outright when safe.bareRepository=explicit is set —
a common hardening in CI images and on dev machines, and unrelated to what
these tests assert. runGitBare passes -c safe.bareRepository=all so the tests
test the code instead of the machine's git config.

Verified by reproducing first: with the policy injected
(GIT_CONFIG_COUNT/KEY/VALUE) all four bare-remote tests failed with "cannot use
bare repository ... (safe.bareRepository is 'explicit')"; after the fix the
whole suite passes both with and without the policy.
@crossoverJie
crossoverJie merged commit 7d576a3 into main Nov 12, 2026
5 checks passed
@crossoverJie
crossoverJie deleted the test/coverage-batch-1 branch November 12, 2026 06:06
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.

2 participants