Skip to content

test(http-core): [#1349] pin the announce service and error mapping decisions with unit tests - #2575

Merged
josecelano merged 5 commits into
torrust:developfrom
josecelano:1349-1347-add-tests-http-core-announce
Oct 20, 2026
Merged

josecelano merged 5 commits into
torrust:developfrom
josecelano:1349-1347-add-tests-http-core-announce

Conversation

@josecelano

Copy link
Copy Markdown
Member

Summary

Slice S2 of #1349 (tests for the torrust-tracker-http-core package, EPIC #1347): the unit tests for the announce service and its error mapping. It changes test code, one dev-dependency and documentation only. Production code is unchanged: everything before the #[cfg(test)] module of services/announce.rs is byte-identical to develop, and services/error_mapping.rs only gains a test module after the function. The remaining files follow in the slices S3-S6 of the specification, each starting after the previous one merges.

Related to #1349
Parent EPIC: #1347

What this slice adds

  • services/announce.rs. The 8 existing tests are refactored onto a small in-file fixture, a recording event sender and rstest tables, and the approved plan adds the rest: 50 cases in total. They pin private-mode authentication (missing, unknown and registered key), public mode ignoring a key, listed mode, the order authenticate, authorize, resolve the client IP, select the peer IP and announce (each order test also asserts that no event was published and no peer registered after the early failure), peer IP selection (connection address, literal ip, loopback substitution by the external IP, a client-supplied loopback literal, an empty ip), the request-to-announcement mapping, numwant, an absent event sender, the public URL in the event, a failing persisted-downloads store (through tracker-core's MockTorrentMetricsStore, no database) and the client-visible failure reasons. The old precedence test never gave the service an external IP, so it could not prove its name; the new one does.
  • services/error_mapping.rs. It had no unit coverage. One rstest table with a case per TrackerCoreError variant pins the package-owned prefix of each failure reason.
  • Dependency. rstest = "0.28.0" becomes a dev-dependency of torrust-tracker-http-core, for the case tables. It is the latest stable version. The workspace requirement "0" used elsewhere would have locked 0.27.0 here, because Cargo reuses an existing compatible lock entry.
  • Records. A re-measurement of the baseline (develop changed packages/http-core after S1), the two file plans with their prose-first Arrange, Act and Assert reviews and mutation results, the per-file evidence, the ledger, and the EPIC Overhaul: Packages Testing #1347 rows and both coverage tables.

Evidence

  • Coverage (cargo-llvm-cov 0.6.16, stable Rust 1.99.0, clean unit-only runs). Package lines 1,751 / 1,970 (88.88%) to 1,919 / 2,101 (91.34%), regions 90.41% to 92.45%, functions 84.29% to 89.59%, and 33 to 79 unit tests. services/announce.rs goes from 92.85% to 99.06% of lines, and every production line, region and function of the file is covered; the uncovered lines are the failure branches of matches! assertions in the test module. The totals include the test modules, so the mutation results below are the stronger signal.
  • Mutations. Each named mutation was applied by hand, run and reverted, and each made exactly the test named for it fail: the check order (three variants), the loopback guard, the Stopped event mapping, the public URL (two variants), the precedence arm, and the prefix and wrapped text of the failure reasons. Two mutants of the peer IP selection survived the first examples and were each fixed with one added example, so no property-testing dependency is added: a client-supplied loopback ip literal replaced by the external IP, and an empty ip= that skipped the loopback substitution. The second was found by the independent review.
  • Not testable. The wildcard arm of error_mapping.rs (three lines, added by Mark public error enums #[non_exhaustive] before first publish #2454) cannot be reached: TrackerCoreError is #[non_exhaustive] with four variants, so no code outside tracker-core can build a fifth. The plan records it as tracker-core's to unlock. This corrects an earlier note in the spec that suggested testing it.
  • No bug found, so no hand-off was needed. From<TrackerCoreError> for HttpAnnounceError is a public conversion with no caller in the repository; it is pinned by one test and kept, because removing it would be a breaking API change. That is a possible separate cleanup, not part of this issue.

Review guidance

The production diff is empty, so review the tests, not the behavior. Each plan lists every test with the Arrange, Act and Assert it was written from, the mutation that proves it, and the maintainer's approval of the plan, of the R1 refactor and of each completed file. The four commits are the baseline re-measurement, one commit per file, and the fixes from the independent Task Reviewer, all kept as separate steps.

Files

  • packages/http-core/src/services/announce.rs (test module only)
  • packages/http-core/src/services/error_mapping.rs (test module only)
  • packages/http-core/Cargo.toml and Cargo.lock (rstest)
  • docs/issues/open/1349-1347-add-tests-http-core/ (ISSUE.md, coverage-evidence.md, agent-review-reports.md, test-refactor-plans/README.md, test-refactor-plans/services-announce-tests.md, test-refactor-plans/services-error-mapping-tests.md (new))
  • docs/issues/open/1347-overhaul-packages-testing/EPIC.md

Validation

  • cargo test -p torrust-tracker-http-core: 79 unit tests and the 3 doc tests pass, also under --test-threads=1 (the frozen clock is thread-local).
  • cargo +nightly fmt --all -- --check, cargo clippy -p torrust-tracker-http-core --all-targets --all-features -- -D warnings, cargo machete --with-metadata, git diff --check, linter markdown and linter cspell pass.
  • Pre-commit (./contrib/dev-tools/git/hooks/pre-commit.sh) passed for every commit.
  • Pre-push (./contrib/dev-tools/git/hooks/pre-push.sh: nightly Rust format, check and docs, then the stable Rust 1.99.0 test suite) passed on the final head.
  • The independent Task Reviewer passed the slice with one MEDIUM and two LOW findings, all fixed; the report is in agent-review-reports.md.
  • The commit-id citation check from the open-pull-request skill prints nothing (no branch commit ids cited).
  • The branch is not rebased onto the latest develop: it changed only outside packages/http-core since this branch was cut, and git merge-tree against it reports no conflict.

josecelano and others added 4 commits October 20, 2026 13:27
… slice S2

develop changed packages/http-core after the S1 baseline: torrust#2454 marked the public
error enums non_exhaustive and added a wildcard arm to error_mapping.rs, and torrust#2560
replaced whole_total with counter_total and added the first metrics.rs test.

Re-measured at 87690b1: lines 1,751 / 1,970 (88.88%), regions 90.41%, functions
84.29%, 33 unit tests. Update the evidence, the ledger, the announce plan line
references and the EPIC rows. Record that the new wildcard arm cannot be reached
by a test while TrackerCoreError has four variants.

Related to torrust#1349

Co-authored-by: Copilot <[email protected]>
…th unit tests

Refactor the 8 existing announce tests onto a small in-file fixture, a recording
event sender and rstest tables, then add the tests the file plan approved. The 8
tests become 48 cases that protect:

- private-mode authentication, public mode ignoring a key, and listed mode
- the order authenticate, authorize, resolve the client IP, select the peer IP
  and announce, proven by nothing being published or registered after an early
  failure
- peer IP selection: the connection address, a literal IP, the loopback
  substitution by the external IP, and a client-supplied loopback literal
- the request-to-announcement mapping, numwant, an absent event sender, the
  public URL in the event, a failing persisted-downloads store, and the
  client-visible failure reasons

Each named mutation (M1-M5 and variants) makes exactly its test fail. A probe
found one surviving mutant, a loopback ip literal replaced by the external IP;
one added example kills it, so no property-test dependency is added.

Unit-only coverage of the file: lines 92.85% to 99.06%, functions 100%.
Production code is unchanged.

Add rstest 0.28.0 as a dev-dependency of http-core for the case tables. It is the
latest stable version; the workspace requirement "0" would have locked 0.27.0 here
because Cargo reuses an existing compatible lock entry.

Related to torrust#1349

Co-authored-by: Copilot <[email protected]>
…r core error

services/error_mapping.rs had no unit coverage. Add one rstest table with a case
for each TrackerCoreError variant (announce, scrape, whitelist, authentication).
Each case expects the literal package-owned prefix followed by the wrapped
error's own text, so swapping a prefix or dropping the wrapped text fails its
case. Mutations of both kinds were applied, caught and reverted.

The wildcard arm added by torrust#2454 for an unknown variant stays uncovered: no code
outside tracker-core can build a fifth variant of the non_exhaustive enum. The
file plan records it as tracker-core's to unlock.

Unit-only coverage of the file: lines 0 / 18 to 26 / 29 (the three uncovered
lines are the wildcard arm). Production code is unchanged. The commit also
records the interim package coverage after slice S2 in the evidence, the
ledger and the EPIC tables.

Related to torrust#1349

Co-authored-by: Copilot <[email protected]>
… record the S2 review

The independent Task Reviewer found a mutant of select_peer_ip_with_policy that
survived all 77 tests: PeerIp::Empty returning the connection address, which
skips the loopback substitution that an absent ip gets. That contradicted the
announce plan's claim that no mutation survives the examples.

Add a two-case test (absent and empty ip, loopback connection, external IP)
that fails the mutant, and rework the tracker-core delegation test to compare
with error_mapping's own result so the failure-reason prefix is pinned once.

Record the review in agent-review-reports.md, fix the E2 mutation wording in
the error mapping plan (an unused pattern variable does not compile under the
workspace lints), and update the plans, evidence, ledger and EPIC rows to the
final slice S2 numbers: 79 unit tests; package lines 91.34%, regions 92.45%,
functions 89.59%. Production code is unchanged.

Related to torrust#1349

Co-authored-by: Copilot <[email protected]>
Copilot AI balanced review requested due to automatic review settings October 20, 2026 16:17
…ication

Set related-pr to torrust#2575 and the S2 slice row to the pull request, and log that
the pre-push checks passed and the pull request was opened.

Related to torrust#1349

Co-authored-by: Copilot <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

Production code is unchanged, the tests have documented validation, and no blocking issues remain.

No open issues 🎉

What changed in this PR

Implements slice S2 of #1349, strengthening HTTP-core announce and error-mapping tests without changing production code.

Changes:

  • Expands announce coverage to 50 cases and adds four error-mapping cases.
  • Adds rstest as a development dependency.
  • Records coverage, mutation results, review outcomes, and issue progress.
File Description
packages/​http-core/​src/​services/​error_mapping.rs Tests each error variant’s failure reason.
packages/​http-core/​src/​services/​announce.rs Refactors fixtures and expands decision coverage.
packages/​http-core/​Cargo.toml Adds the rstest development dependency.
docs/​issues/​open/​1349-1347-add-tests-http-core/​test-refactor-plans/​services-error-mapping-tests.md Records error-mapping tests and evidence.
docs/​issues/​open/​1349-1347-add-tests-http-core/​test-refactor-plans/​services-announce-tests.md Records announce tests and mutation results.
docs/​issues/​open/​1349-1347-add-tests-http-core/​test-refactor-plans/​README.md Marks both plans complete.
docs/​issues/​open/​1349-1347-add-tests-http-core/​ISSUE.md Updates slice progress and baseline.
docs/​issues/​open/​1349-1347-add-tests-http-core/​coverage-evidence.md Records baseline and S2 coverage.
docs/​issues/​open/​1349-1347-add-tests-http-core/​agent-review-reports.md Adds review results and follow-up resolutions.
docs/​issues/​open/​1347-overhaul-packages-testing/​EPIC.md Updates package progress and coverage tables.
Cargo.lock Records HTTP-core’s rstest dependency.

Review effort: Balanced

@josecelano

Copy link
Copy Markdown
Member Author

ACK 61c3460

Posted on behalf of @josecelano, at his instruction. Copilot reviewed this head with no open issues, all checks are green and there are no unresolved threads. @da2ce7 was requested but did not reply within 15 minutes, so the review round is considered approved without waiting longer.

@josecelano
josecelano merged commit fe70cf9 into torrust:develop Oct 20, 2026
20 checks passed
josecelano added a commit that referenced this pull request Oct 20, 2026
Mark slice S2 done after the merge of #2575, point the specification at the
S3 branch, and log that the baseline for S3 is the final S2 measurement
because packages/http-core changed on develop only through S2. Update the
EPIC row.

Related to #1349

Co-authored-by: Copilot <[email protected]>
@da2ce7

da2ce7 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Post-merge note: this pull request requested a review from this account and was closed without one at its final head. A post-merge review will not be done: All 1,044 executable lines it added are tests, benchmarks or test support with no production behaviour, and 1,009 of them are still present.

This branch was previously deployed

1 inactive deployment
coverage — 61c34601 Deployed Oct 20, 2026 by josecelano via Generate Coverage Report #2639
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.

3 participants