soulsync/tests/media_server
Broque Thomas c02d51d60d Plex: trigger_library_scan + is_library_scanning use auto-detected section — fixes #535
# Bug

Plex servers with the music library named anything other than "Music"
(Música, Musique, Musik, Musica, 音乐, موسيقى, etc.) hit this error
after every import cycle:

    soulsync.plex_client - ERROR - Failed to trigger library scan
    for 'Music': Invalid library section: Music
    soulsync.web_scan_manager - ERROR - Failed to initiate PLEX
    library scan via web

Side effect: `wishlist.processing` kept reporting "Missing from
media server after sync" for tracks that DID import correctly, so
they got perpetually re-added to the wishlist.

# Root cause

`_find_music_library` correctly auto-detects the music section by
`section.type == 'artist'` and stores it on `self.music_library` —
works for any locale because the type is language-neutral. Read
methods (`get_artists`, etc.) route through `_get_music_sections`
which returns `[self.music_library]`, so they never had the bug.

But `trigger_library_scan` and `is_library_scanning` ignored
`self.music_library` and called
`self.server.library.section(library_name)` directly with the
hardcoded `"Music"` default. `server.library.section('Music')`
raises `NotFound` on any server whose section isn't literally
named "Music".

# Fix

Both methods now prefer `self.music_library` first, fall back to
literal `library_name` lookup only when auto-detection hasn't
populated the cached reference (test fixtures, edge cases).

`is_library_scanning`'s activity-feed match also corrected to
filter by the resolved section's actual title — the prior code
matched `library_name.lower() in activity_title.lower()` which
defaults to "music" and would never match activities for
non-English sections.

`trigger_library_scan`'s success log line now surfaces the actual
section title (`Música`) instead of the unused `library_name`
default ("Music") — confusing when debugging on non-English servers.

# Tests added (13)

`tests/media_server/test_plex_non_english_section_name.py`:

- `test_uses_auto_detected_section_regardless_of_locale` — parametrised
  across 6 locale variants (Música, Musique, Musik, Musica, 音乐, موسيقى).
  Each verifies trigger_library_scan calls the auto-detected
  section's `update()`, NOT a literal-name fallback. Stub raises
  AssertionError on `server.library.section()` so a regression that
  re-introduces the fallback fails loudly.
- `test_falls_back_to_literal_lookup_when_no_auto_detection` —
  backward compat: music_library=None → literal lookup as before.
- `test_explicit_library_name_arg_used_only_when_no_auto_detection` —
  auto-detected wins over explicit kwarg when both available.
- `test_logs_correct_section_label_on_success` — log line surfaces
  resolved section title.
- 4 symmetric tests for is_library_scanning covering refreshing-attr
  check, activity-feed title match, no-match for unrelated sections,
  fallback path.

# Verification

- 13 new tests pass
- 84/84 media_server tests pass (no regression in the existing
  Plex / Jellyfin / Navidrome suite)
- 2458 full suite passes (+13 from baseline)
- Ruff clean
2026-05-10 10:22:32 -07:00
..
__init__.py Phase 0: Add MediaServerClient contract + registry 2026-05-04 20:49:25 -07:00
test_conformance.py MS Cin-1 + Cin-2: Explicit contract inheritance + generic accessors 2026-05-05 16:59:05 -07:00
test_engine.py MS Cin/JohnBaumb honesty pass — drop dead wrappers, sync contract to reality 2026-05-05 22:36:05 -07:00
test_jellyfin_pinning.py A2: Pin JellyfinClient observable behavior 2026-05-04 20:53:55 -07:00
test_navidrome_pinning.py A3: Pin NavidromeClient observable behavior 2026-05-04 21:01:11 -07:00
test_plex_all_libraries.py Add cross-section dedup to all-libraries listing layer 2026-05-06 16:01:37 -07:00
test_plex_non_english_section_name.py Plex: trigger_library_scan + is_library_scanning use auto-detected section — fixes #535 2026-05-10 10:22:32 -07:00
test_plex_pinning.py Add "All Libraries (combined)" mode to PlexClient 2026-05-06 15:39:19 -07:00
test_soulsync_pinning.py A4: Pin SoulSyncClient observable behavior 2026-05-04 21:03:37 -07:00