fix(jellyfin): Add defensive checks for missing audio metadata (#2728)
authorericmammolenti <150827192+ericmammolenti@users.noreply.github.com>
Tue, 9 Dec 2025 15:28:10 +0000 (10:28 -0500)
committerGitHub <noreply@github.com>
Tue, 9 Dec 2025 15:28:10 +0000 (16:28 +0100)
commita4ebf33b6590016a8ba2215a6ac28533e1e48ca1
treee81435323c2d0498ea8261cf9affe15b0e7bc2a6
parentf9604ea0da35744d8d1179e0bdf2c0e6ff328408
fix(jellyfin): Add defensive checks for missing audio metadata (#2728)

* fix(jellyfin): Add defensive checks for missing audio metadata

Fixes KeyError when Jellyfin MediaStreamInfos are missing Channels field.

Changes:
- Handle empty MediaStreams arrays gracefully
- Use .get() with defaults for Channels (stereo) and codec
- Maintain consistency with existing SampleRate/BitDepth patterns

This prevents crashes on libraries with incomplete Jellyfin metadata
while using sensible defaults (2 channels, CD quality assumed).

Closes music-assistant/support#4447

* fix: Remove trailing whitespace and add test coverage

- Fix Ruff linting errors (blank lines with whitespace)
- Add test for empty MediaStreams array edge case
- Add test for missing Channels field with default value
- Addresses review feedback on PR #2728

* fix: Move test imports to top of file

Move inline imports from test functions to module level to comply
with linter requirements and improve code organization.

* Update tests/providers/jellyfin/test_parsers.py

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* test: add edge case coverage for audio_format

- Add test for empty MediaStreams array
- Add test for missing Channels field
- Ensure defensive checks prevent IndexError and KeyError

* fix: remove invalid ContentType import and correct bit_rate assertion

- Remove import from non-existent music_assistant.common.models.enums module
- Update test assertions to verify result structure without ContentType enum
- Fix bit_rate assertion to expect kbps (320) instead of bps (320000)
  AudioFormat model automatically converts bps to kbps in __post_init__
- Fixes ModuleNotFoundError in CI test runs

* test: clean jellyfin parser tests

- remove unused JellyTrack import
- avoid runtime casts; use typed dict literals
- ignore arg-type warnings for dict track fixtures

---------

Co-authored-by: Marvin Schenkel <marvinschenkel@gmail.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
music_assistant/providers/jellyfin/parsers.py
tests/providers/jellyfin/test_parsers.py