buzz icon indicating copy to clipboard operation
buzz copied to clipboard

test(pairing-cli): add unit coverage for buzz-pairing-cli

Open SeanGearin opened this issue 4 months ago • 0 comments

Problem

crates/buzz-pairing-cli (623 LOC) had zero tests. All of its relay-protocol parsing, CLI argument handling, and error paths were unverified — regressions in QR/offer/abort handling or NIP-42 auth framing would only surface during a live interop run against a real relay.

Fix

Adds a #[cfg(test)] unit suite (42 tests) inside src/main.rs, mirroring the inline-test idiom used in buzz-auth (crates/buzz-auth/src/nip42.rs). No live relay is used anywhere: WebSocket I/O is stubbed with in-memory futures_util::stream sources and a small CaptureSink that records outgoing frames, and time-dependent paths run under #[tokio::test(start_paused = true)] so timeout tests complete instantly.

Coverage by area:

  • CLI arg parsing (8): clap debug_assert, source default relay + --relay/--nsec, target defaults + --relay/--show-secret, test-vectors, missing-subcommand and unknown-flag rejection.
  • parse_auth_challenge (6): valid AUTH frame, non-AUTH types, malformed JSON, short array, non-string challenge, non-array JSON.
  • parse_relay_event (6): valid EVENT for the right sub id, wrong sub id, OK/EOSE frames, short array, invalid event payload, non-array JSON.
  • resolve_payload (3): valid nsec passthrough, invalid nsec -> CliError::InvalidNsec, generated test key parses as an nsec.
  • hex_to_32 (3): round trip, non-hex input, wrong length.
  • parse_yes_no (2): affirmative and negative variants. (Tiny seam extraction: the matches! in read_yes_no moved to a pure parse_yes_no(&str) so the logic is testable without stdin — behavior unchanged.)
  • cmd_test_vectors (1): spec vectors derive without error.
  • check_for_abort (2): non-abort events pass through; a real peer abort (full offline source->target handshake via PairingSession::new_source / new_target / handle_offer) is surfaced with its reason.
  • publish_event (1): emits a ["EVENT", ...] frame that round-trips to the same event id.
  • wait_for_event (4): skips junk/OK/EOSE/binary/wrong-sub frames then returns the matching event; timeout -> CliError::Timeout; closed stream -> CliError::Other; websocket error propagates as CliError::WebSocket.
  • wait_for_eose (2): matches only its sub id; timeout path.
  • handle_nip42_auth (4): no-challenge relays are skipped cleanly (nothing sent); a challenge is answered with a kind-22242 event signed by the session's ephemeral key (signature verified, challenge tag present); missing OK after AUTH is tolerated; invalid relay URL is rejected.

Only non-test changes: the 4-line parse_yes_no extraction and a [dev-dependencies] entry adding tokio's test-util feature (same pattern as buzz-pair-relay, buzz-media, buzz-relay-mesh, buzz-acp).

Test evidence

cargo fmt -p buzz-pairing-cli          # clean
cargo clippy -p buzz-pairing-cli --all-targets   # no warnings
cargo nextest run -p buzz-pairing-cli  # 42/42 passed

Links

  • Crate: crates/buzz-pairing-cli/src/main.rs
  • Test idiom mirrored from: crates/buzz-auth/src/nip42.rs

🤖 Generated with Claude Code

SeanGearin avatar Jul 23 '26 20:07 SeanGearin