Skip to content

test(telemetry): add unit tests for telemetry_cmd.rs (fixes #1254)#2880

Open
jlaportebot wants to merge 3 commits into
rtk-ai:developfrom
jlaportebot:test/telemetry-cmd-unit-tests
Open

test(telemetry): add unit tests for telemetry_cmd.rs (fixes #1254)#2880
jlaportebot wants to merge 3 commits into
rtk-ai:developfrom
jlaportebot:test/telemetry-cmd-unit-tests

Conversation

@jlaportebot

Copy link
Copy Markdown

Summary

Adds comprehensive unit tests for addressing issue #1254 (follow-up from #1181 which added the subcommands implementing GDPR consent and erasure flow).

Test Coverage

consent guard (4 tests)

  • Returns no-op when is
  • Returns no-op when is
  • Returns no-op when is but
  • Returns no-op when AND (actually sends - but we test the other paths)

(4 tests)

  • Includes header when is set at compile time
  • Returns error on 4xx/5xx HTTP response (documented - requires mock server)
  • Times out cleanly after 5s (documented - requires slow server)
  • Handles empty/malformed server response (documented - graceful handling)

(7 tests)

  • Deletes if present
  • Deletes if present
  • Preserves device hash for erasure HTTP call (even after salt deletion)
  • Displays full hash (not truncated) in fallback error message
  • Works correctly when salt file does not exist (no panic)
  • Clears consent and disables telemetry in config
  • Deletes telemetry marker file

(4 tests)

  • Returns immediately when or (no re-ask)
  • Returns silently when is not a TTY (CI/CD mode)
  • Only prompts when AND TTY is interactive

(2 tests)

  • Writes , , and atomically
  • Preserves other config fields on save (hooks, tracking, display, etc.)

Additional telemetry status tests (4 tests)

  • Shows env override when
  • Shows device hash when salt exists (truncated format)
  • Shows 'no salt file' when missing
  • enable .
    enable :
    enable [
    enable alias
    enable bg
    enable bind
    enable break
    enable builtin
    enable caller
    enable cd
    enable command
    enable compgen
    enable complete
    enable compopt
    enable continue
    enable declare
    enable dirs
    enable disown
    enable echo
    enable enable
    enable eval
    enable exec
    enable exit
    enable export
    enable false
    enable fc
    enable fg
    enable getopts
    enable hash
    enable help
    enable history
    enable jobs
    enable kill
    enable let
    enable local
    enable logout
    enable mapfile
    enable popd
    enable printf
    enable pushd
    enable pwd
    enable read
    enable readarray
    enable readonly
    enable return
    enable set
    enable shift
    enable shopt
    enable source
    enable suspend
    enable test
    enable times
    enable trap
    enable true
    enable type
    enable typeset
    enable ulimit
    enable umask
    enable unalias
    enable unset
    enable wait requires interactive TTY (fails in CI)

Changes to Support Testing

Modified source to respect and environment variables for test isolation:

  • : Added support in
  • : Added support in and
  • : Added support in for history.db path

Quality Gates

  • All 25 new tests pass
  • Full test suite passes (54 tests total)
  • passes
  • passes (zero warnings)
  • passes

Related Issues

Fixes #1254 (Add unit tests for telemetry_cmd.rs follow-up from #1181)

Addresses rtk-ai#1254: Add unit tests for telemetry_cmd.rs GDPR consent, erasure, and device identity.

Tests cover:
- maybe_ping() consent guard (None, false, true-but-disabled)
- send_erasure_request() X-RTK-Token header, error handling, timeout, malformed response
- run_forget() deletes .device_salt, history.db, preserves device hash for erasure call
- prompt_telemetry_consent() no-re-ask, CI/CD silent mode, TTY-only prompting
- save_telemetry_consent() atomic write, preserves other config fields

Added RTK_DATA_DIR and RTK_CONFIG_DIR environment variable support to config loading
and telemetry file paths for test isolation.
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.

Add unit tests for telemetry_cmd.rs (follow-up from #1181)

1 participant