Skip to content

Reject invalid player seek and volume values in the CLI - #90

Closed
bfs2021 wants to merge 1 commit into
Aran404:mainfrom
bfs2021:fix/player-arg-ranges
Closed

bfs2021 wants to merge 1 commit into
Aran404:mainfrom
bfs2021:fix/player-arg-ranges

Conversation

@bfs2021

@bfs2021 bfs2021 commented Nov 14, 2026

Copy link
Copy Markdown

Problem

spotapi player seek and spotapi player volume accepted any integer at the CLI layer, so negative seek positions and volumes outside 0–100 were only rejected after the CLI had read SPOTIFY_COOKIE, authorized an account, opened a Connect player, and reached Player.seek() / Player.volume().

Fixes #87

Root cause

The argparse entries for the two player subcommands used type=int. The library validates both values, but that validation lives below account and session setup, so obviously invalid input paid the connection cost and surfaced the error at the wrong layer.

Fix

Added two small local argparse type functions in spotapi/cli.py:

  • _seek_position: requires an integer >= 0, otherwise reports choose a non-negative number of milliseconds.
  • _volume: requires an integer from 0 through 100, otherwise reports choose a number from 0 to 100.

Both raise argparse.ArgumentTypeError, so argparse prints the usage line plus the range message and exits with status 2 before main() reaches _run. The existing validation inside Player.seek() and Player.volume() is unchanged as the library-level safety boundary.

Testing

$ python -m pytest tests/test_cli.py -q
16 passed in 0.59s

$ python -m pytest -q
433 passed, 31 skipped in 8.46s

$ python -m ruff check spotapi/cli.py tests/test_cli.py
All checks passed!

$ python -m black --check spotapi/cli.py tests/test_cli.py
2 files would be left unchanged

$ python -m pyright spotapi/cli.py tests/test_cli.py
0 errors, 0 warnings, 0 informations

New offline tests:

  • test_cli_rejects_invalid_player_values_before_connecting covers seek -1, volume -1 and volume 101; asserts exit status 2, the range message, and that Private.from_cookies is never called.
  • test_cli_accepts_player_boundary_values covers seek 0, volume 0 and volume 100, asserting the value reaches the player method unchanged.

Before the fix, the three rejection cases reached _run and failed with Set SPOTIFY_COOKIE...; after the fix they exit during parsing.

The player subcommands parsed their arguments as unrestricted integers, so negative seek positions and out-of-range volumes reached account setup and the Connect connection before Player.seek() or Player.volume() rejected them.

Validate both at argparse level with small local type functions that report the accepted range and exit with status 2. The library-level checks remain as the safety boundary.

Tests cover negative seek, volume below zero and above 100, and the 0/100 boundaries, and assert invalid input never opens a session.
@bfs2021

bfs2021 commented Nov 14, 2026

Copy link
Copy Markdown
Author

Closing this for now: CONTRIBUTING.md asks contributors to wait for the issue to be assigned before opening a PR, and #87 is still unassigned. The work is ready on the fork branch fix/player-arg-ranges (argparse validators for seek >= 0 and volume 0..100, offline tests, 433 passed) — happy to reopen the moment you assign the issue.

@bfs2021 bfs2021 closed this Nov 14, 2026
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.

CLI: reject invalid player seek and volume values before connecting

1 participant