Skip to content

refactor: trim redundant tests and indirection in the HID++ module - #9

Merged
softmaxe merged 1 commit into
mainfrom
claude/brave-bohr-7tedkg
Oct 15, 2026
Merged

softmaxe merged 1 commit into
mainfrom
claude/brave-bohr-7tedkg

Conversation

@softmaxe

Copy link
Copy Markdown
Owner

Summary

src/hidpp.rs was ~2,500 lines, ~1,800 of them tests, and many tests checked the same behaviour at two or three levels. This PR removes that duplication and the abstractions that existed only to support it. Runtime behaviour is unchanged.

Production code

  • Drop src/lib.rs and make hidpp a private module of the binary. Only main.rs used the library. With the module private, rustc now flags dead code that pub used to hide; the unused get_feature_index was one example.
  • Remove the BatteryDevice trait, its HidppTransport impl and the BatterySession wrapper. Reading the battery is now a free function over RequestIo, like the rest of the request path.
  • Fold ProtocolError (it had a single variant) into HidppError::ShortResponse. Fold ProbeOutcome into Discovery, which had the same three cases.
  • Remove the sw_id parameter (every caller passed the same constant) and the two per-feature parser wrappers.

Tests (hidpp: 54 → 24; covered behaviour is unchanged)

  • Delete the FakeTransport suite. It re-tested the unified→legacy fallback rules that the scripted battery_recovery tests already cover at the byte level.
  • Replace the scattered match_report unit tests with one table test. Delete the parser, request_timeout and discover unit tests, which the scripted tests already cover.
  • Delete the tests that only exercised the test double.
  • ScriptedIo now checks on drop that every scripted step was played. This replaces the assert_finished() / writes() calls in each test and the IoCall log. Shared request constants and exchange / reply / legacy_fallback helpers replace the copies in each module.

Verification

  • cargo fmt --check, cargo clippy --all-targets --locked -- -D warnings and cargo test --locked pass. They were run on Linux with libudev; CI runs them on macOS.
  • Mutation testing with cargo-mutants, before and after: the trimmed suite catches every mutant the original suite caught in code that still exists. The only survivors are:
    • hardware glue that needs a real HID device (candidate_interfaces, probe_device, the HidDevice impl, main), which also survived before;
    • two equivalent mutants (|→^ on non-overlapping bits, >→>= on a read length).
  • Adding a stray step to a script makes the test fail with unplayed script steps: …, so the check on drop works.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Pu5j1fqQD9iMmDuP3TnJR3


Generated by Claude Code

Production code:
- Make hidpp a private module of the binary and drop src/lib.rs. Nothing
  else consumed the library, and with the module private, rustc now flags
  dead code that `pub` used to hide (e.g. the unused get_feature_index).
- Remove the BatteryDevice trait, its HidppTransport impl and the
  BatterySession wrapper; reading is a free function over RequestIo like
  the rest of the request path.
- Fold ProtocolError (a single variant) into HidppError::ShortResponse and
  ProbeOutcome into Discovery, which had the same three cases.
- Drop the sw_id parameter that every caller set to the same constant, and
  the per-feature parser wrappers.

Tests (54 -> 24 in hidpp, same behaviours covered):
- Delete the FakeTransport suite, which re-tested the fallback rules that
  the scripted battery_recovery tests already cover byte for byte.
- Replace the scattered match_report tests with one table test, and drop
  parser, request_timeout and discover unit tests the scripted tests cover.
- Delete tests of the test double itself.
- ScriptedIo now checks for unplayed steps on drop, so the per-test
  assert_finished/writes() bookkeeping and the IoCall log are gone; shared
  request constants and exchange helpers replace per-module copies.

cargo-mutants catches the same mutants as before; the only survivors are
hardware glue that needs a real device and two equivalent mutants.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Pu5j1fqQD9iMmDuP3TnJR3
@softmaxe
softmaxe merged commit bc2b3a0 into main Oct 15, 2026
5 checks passed
@softmaxe
softmaxe deleted the claude/brave-bohr-7tedkg branch October 15, 2026 05:45
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