mirror of
https://github.com/rommapp/romm.git
synced 2026-11-30 18:42:34 +01:00
Addresses the deeper issues left open by the previous cleanup pass: the jukebox downloaded the whole track catalog on mount and re-derived every facet client-side, and the view had grown to hold all of that state plus eight screens' worth of markup. Backend — close the gaps that forced the client-side grouping: - filter tracks by the owning game's genre and by a year range - facet games, platforms and game genres, so each sidebar is a paginated server query with counts instead of a grouping over the full catalog - a stats endpoint for the home cards' totals - game genres are counted per rom then folded in Python, mirroring how the ROM filters aggregate their JSON arrays Frontend: - Jukebox becomes a shell over HomeMode / BrowseMode / SessionMode; the five browse dimensions share one component and differ only by the facet they load and the filter they pass - the soundtrack panel takes one normalized PanelTrack[] instead of a rom-or-catalog prop union, and moves to components/Soundtrack/ - its track list is virtualised, so rows (and their menus) scale with the viewport rather than the library - favorites move into a store, ending the four-way hand-fanned copies - new composables for rom soundtracks, delete confirmation and URL state - CardRow moves to shared/; platform and jukebox tiles share one Tile composite instead of two copies of the same chrome Also fixes a viewer-permission test that the jukebox's PLAYLIST_EDIT action key had been failing since it was added. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
560 lines
18 KiB
Python
560 lines
18 KiB
Python
import pytest
|
|
from fastapi import status
|
|
from fastapi.testclient import TestClient
|
|
|
|
from config import FRONTEND_RESOURCES_PATH
|
|
from endpoints.responses.music import MusicTrackSchema
|
|
from handler.database import db_platform_handler, db_rom_handler
|
|
from models.platform import Platform
|
|
from models.rom import Rom, RomFile, RomFileCategory, TrackMeta
|
|
from models.user import User
|
|
|
|
|
|
def _auth(token: str) -> dict[str, str]:
|
|
return {"Authorization": f"Bearer {token}"}
|
|
|
|
|
|
def _make_platform(slug: str) -> Platform:
|
|
return db_platform_handler.add_platform(
|
|
Platform(name=slug, slug=slug, fs_slug=slug)
|
|
)
|
|
|
|
|
|
def _make_track(
|
|
admin_id: int,
|
|
platform: Platform,
|
|
*,
|
|
name: str,
|
|
title: str,
|
|
artist: str,
|
|
album: str,
|
|
genre: str,
|
|
year: int,
|
|
duration: float,
|
|
cover_path: str | None = None,
|
|
path_cover_l: str | None = None,
|
|
) -> Rom:
|
|
rom = db_rom_handler.add_rom(
|
|
Rom(
|
|
platform_id=platform.id,
|
|
name=name,
|
|
slug=f"{name}-slug",
|
|
fs_name=f"{name}.zip",
|
|
fs_name_no_tags=name,
|
|
fs_name_no_ext=name,
|
|
fs_extension="zip",
|
|
fs_path=f"{platform.slug}/roms",
|
|
path_cover_l=path_cover_l,
|
|
)
|
|
)
|
|
db_rom_handler.add_rom_user(rom_id=rom.id, user_id=admin_id)
|
|
db_rom_handler.add_rom_file(
|
|
RomFile(
|
|
rom_id=rom.id,
|
|
file_name=f"{title}.mp3",
|
|
file_path=f"{rom.fs_path}/{name}/soundtrack",
|
|
file_size_bytes=2048,
|
|
category=RomFileCategory.SOUNDTRACK,
|
|
track_meta=TrackMeta(
|
|
rom_id=rom.id,
|
|
title=title,
|
|
artist=artist,
|
|
album=album,
|
|
genre=genre,
|
|
year=year,
|
|
duration_seconds=duration,
|
|
has_embedded_cover=cover_path is not None,
|
|
cover_path=cover_path,
|
|
),
|
|
)
|
|
)
|
|
return rom
|
|
|
|
|
|
@pytest.fixture
|
|
def music_library(admin_user: User):
|
|
pa = _make_platform("genesis")
|
|
pb = _make_platform("nes")
|
|
sonic = _make_track(
|
|
admin_user.id,
|
|
pa,
|
|
name="Sonic",
|
|
title="Green Hill",
|
|
artist="Nakamura",
|
|
album="Sonic OST",
|
|
genre="Game",
|
|
year=1991,
|
|
duration=120.0,
|
|
cover_path="roms/1/1/soundtrack/cover.jpg",
|
|
)
|
|
_make_track(
|
|
admin_user.id,
|
|
pa,
|
|
name="Streets",
|
|
title="Jingle",
|
|
artist="Koshiro",
|
|
album="SOR OST",
|
|
genre="Dance",
|
|
year=1992,
|
|
duration=20.0,
|
|
path_cover_l="roms/2/2/cover/big.png",
|
|
)
|
|
_make_track(
|
|
admin_user.id,
|
|
pb,
|
|
name="Mario",
|
|
title="Overworld",
|
|
artist="Kondo",
|
|
album="SMB OST",
|
|
genre="Game",
|
|
year=1985,
|
|
duration=90.0,
|
|
)
|
|
# A rom with no soundtrack, to exercise the has_soundtrack filter.
|
|
no_st = db_rom_handler.add_rom(
|
|
Rom(
|
|
platform_id=pb.id,
|
|
name="Tetris",
|
|
slug="tetris-slug",
|
|
fs_name="Tetris.gb",
|
|
fs_name_no_tags="Tetris",
|
|
fs_name_no_ext="Tetris",
|
|
fs_extension="gb",
|
|
fs_path=f"{pb.slug}/roms",
|
|
)
|
|
)
|
|
db_rom_handler.add_rom_user(rom_id=no_st.id, user_id=admin_user.id)
|
|
return {"platform_a": pa, "platform_b": pb, "sonic": sonic, "no_soundtrack": no_st}
|
|
|
|
|
|
# ---------- /api/music/tracks ----------
|
|
|
|
|
|
def test_tracks_lists_all(client: TestClient, access_token: str, music_library):
|
|
r = client.get("/api/music/tracks", headers=_auth(access_token))
|
|
assert r.status_code == status.HTTP_200_OK
|
|
body = r.json()
|
|
assert body["total"] == 3
|
|
item = next(i for i in body["items"] if i["title"] == "Green Hill")
|
|
assert item["year"] == 1991 and isinstance(item["year"], int)
|
|
assert item["platform_slug"] == "genesis"
|
|
assert item["stream_url"].endswith("/files/content/Green%20Hill.mp3")
|
|
# track cover takes precedence
|
|
assert item["cover_url"].endswith("/soundtrack/cover.jpg")
|
|
|
|
|
|
def test_tracks_cover_falls_back_to_game(
|
|
client: TestClient, access_token: str, music_library
|
|
):
|
|
r = client.get("/api/music/tracks?artist=koshiro", headers=_auth(access_token))
|
|
item = r.json()["items"][0]
|
|
assert item["cover_url"].endswith("/cover/big.png")
|
|
|
|
|
|
def test_cover_url_for_priority():
|
|
base = FRONTEND_RESOURCES_PATH
|
|
assert (
|
|
MusicTrackSchema.cover_url_for("track.jpg", "game.png") == f"{base}/track.jpg"
|
|
)
|
|
assert MusicTrackSchema.cover_url_for(None, "game.png") == f"{base}/game.png"
|
|
assert MusicTrackSchema.cover_url_for(None, None) is None
|
|
|
|
|
|
def test_tracks_artist_exact_case_insensitive(
|
|
client: TestClient, access_token: str, music_library
|
|
):
|
|
r = client.get("/api/music/tracks?artist=nakamura", headers=_auth(access_token))
|
|
items = r.json()["items"]
|
|
assert len(items) == 1 and items[0]["artist"] == "Nakamura"
|
|
|
|
|
|
def test_tracks_search_substring(client: TestClient, access_token: str, music_library):
|
|
r = client.get("/api/music/tracks?search=hill", headers=_auth(access_token))
|
|
assert [i["title"] for i in r.json()["items"]] == ["Green Hill"]
|
|
|
|
|
|
def test_tracks_search_escapes_like_wildcards(
|
|
client: TestClient, access_token: str, music_library
|
|
):
|
|
wild = client.get("/api/music/tracks?search=hi_l", headers=_auth(access_token))
|
|
assert wild.json()["total"] == 0
|
|
literal = client.get("/api/music/tracks?search=hill", headers=_auth(access_token))
|
|
assert [i["title"] for i in literal.json()["items"]] == ["Green Hill"]
|
|
|
|
|
|
def test_tracks_year_and_duration(client: TestClient, access_token: str, music_library):
|
|
assert (
|
|
client.get("/api/music/tracks?year=1992", headers=_auth(access_token)).json()[
|
|
"total"
|
|
]
|
|
== 1
|
|
)
|
|
# min_duration excludes the 20s jingle
|
|
body = client.get(
|
|
"/api/music/tracks?min_duration=60", headers=_auth(access_token)
|
|
).json()
|
|
assert body["total"] == 2
|
|
assert all(i["duration_seconds"] >= 60 for i in body["items"])
|
|
|
|
|
|
def test_tracks_rom_id_filter(client: TestClient, access_token: str, music_library):
|
|
rid = music_library["sonic"].id
|
|
body = client.get(
|
|
f"/api/music/tracks?rom_id={rid}", headers=_auth(access_token)
|
|
).json()
|
|
assert body["total"] == 1 and body["items"][0]["rom_id"] == rid
|
|
|
|
|
|
def test_tracks_platform_filter(client: TestClient, access_token: str, music_library):
|
|
pid = music_library["platform_b"].id
|
|
body = client.get(
|
|
f"/api/music/tracks?platform_ids={pid}", headers=_auth(access_token)
|
|
).json()
|
|
assert body["total"] == 1 and body["items"][0]["title"] == "Overworld"
|
|
|
|
|
|
def test_tracks_order_and_paginate(
|
|
client: TestClient, access_token: str, music_library
|
|
):
|
|
body = client.get(
|
|
"/api/music/tracks?order_by=duration&order_dir=asc&limit=2",
|
|
headers=_auth(access_token),
|
|
).json()
|
|
assert body["total"] == 3 and len(body["items"]) == 2
|
|
assert body["items"][0]["duration_seconds"] == 20.0
|
|
|
|
|
|
# ---------- facets ----------
|
|
|
|
|
|
def test_facet_artists_distinct_and_counts(
|
|
client: TestClient, access_token: str, music_library
|
|
):
|
|
body = client.get("/api/music/artists", headers=_auth(access_token)).json()
|
|
assert body["total"] == 3
|
|
assert {i["value"] for i in body["items"]} == {"Nakamura", "Koshiro", "Kondo"}
|
|
assert all(i["count"] == 1 for i in body["items"])
|
|
|
|
|
|
def test_facet_artists_typeahead(client: TestClient, access_token: str, music_library):
|
|
body = client.get(
|
|
"/api/music/artists?search=ko", headers=_auth(access_token)
|
|
).json()
|
|
assert {i["value"] for i in body["items"]} == {"Koshiro", "Kondo"}
|
|
|
|
|
|
def test_facet_artists_contextual(client: TestClient, access_token: str, music_library):
|
|
pid = music_library["platform_b"].id
|
|
body = client.get(
|
|
f"/api/music/artists?platform_ids={pid}", headers=_auth(access_token)
|
|
).json()
|
|
assert [i["value"] for i in body["items"]] == ["Kondo"]
|
|
|
|
|
|
def test_facet_years_are_ints(client: TestClient, access_token: str, music_library):
|
|
body = client.get("/api/music/years", headers=_auth(access_token)).json()
|
|
assert {i["value"] for i in body["items"]} == {1991, 1992, 1985}
|
|
assert all(isinstance(i["value"], int) for i in body["items"])
|
|
|
|
|
|
def test_facet_years_typeahead(client: TestClient, access_token: str, music_library):
|
|
body = client.get("/api/music/years?search=199", headers=_auth(access_token)).json()
|
|
assert {i["value"] for i in body["items"]} == {1991, 1992}
|
|
|
|
|
|
# ---------- visibility (handler level) ----------
|
|
|
|
|
|
def test_tracks_excludes_hidden_platform(music_library):
|
|
pb = music_library["platform_b"].id
|
|
rows, total = db_rom_handler.get_music_tracks(hidden_platform_ids=[pb])
|
|
assert total == 2
|
|
assert all(r.platform_id != pb for r in rows)
|
|
|
|
|
|
def test_facet_excludes_hidden_platform(music_library):
|
|
pb = music_library["platform_b"].id
|
|
rows, total = db_rom_handler.get_music_facet(
|
|
field="artists", hidden_platform_ids=[pb]
|
|
)
|
|
assert total == 2
|
|
assert "Kondo" not in {r.value for r in rows}
|
|
|
|
|
|
# ---------- favorites ----------
|
|
|
|
|
|
def _track_id(client: TestClient, token: str, title: str) -> int:
|
|
body = client.get("/api/music/tracks", headers=_auth(token)).json()
|
|
return next(i["rom_file_id"] for i in body["items"] if i["title"] == title)
|
|
|
|
|
|
def test_favorites_add_list_remove(
|
|
client: TestClient, access_token: str, music_library
|
|
):
|
|
rf_id = _track_id(client, access_token, "Green Hill")
|
|
|
|
r = client.post(
|
|
"/api/music/favorites",
|
|
json={"rom_file_ids": [rf_id]},
|
|
headers=_auth(access_token),
|
|
)
|
|
assert r.status_code == status.HTTP_200_OK and r.json()["added"] == 1
|
|
|
|
# idempotent re-add
|
|
r = client.post(
|
|
"/api/music/favorites",
|
|
json={"rom_file_ids": [rf_id]},
|
|
headers=_auth(access_token),
|
|
)
|
|
assert r.json()["added"] == 0
|
|
|
|
favs = client.get("/api/music/favorites", headers=_auth(access_token)).json()
|
|
assert favs["total"] == 1
|
|
assert favs["items"][0]["title"] == "Green Hill"
|
|
assert favs["items"][0]["is_favorite"] is True
|
|
|
|
# flag shows up on the flat track list too
|
|
tracks = client.get("/api/music/tracks", headers=_auth(access_token)).json()
|
|
flags = {i["title"]: i["is_favorite"] for i in tracks["items"]}
|
|
assert flags == {"Green Hill": True, "Jingle": False, "Overworld": False}
|
|
|
|
r = client.request(
|
|
"DELETE",
|
|
"/api/music/favorites",
|
|
json={"rom_file_ids": [rf_id]},
|
|
headers=_auth(access_token),
|
|
)
|
|
assert r.status_code == status.HTTP_200_OK and r.json()["removed"] == 1
|
|
assert (
|
|
client.get("/api/music/favorites", headers=_auth(access_token)).json()["total"]
|
|
== 0
|
|
)
|
|
|
|
|
|
def test_favorites_are_per_user(
|
|
client: TestClient,
|
|
access_token: str,
|
|
editor_access_token: str,
|
|
music_library,
|
|
):
|
|
rf_id = _track_id(client, access_token, "Green Hill")
|
|
client.post(
|
|
"/api/music/favorites",
|
|
json={"rom_file_ids": [rf_id]},
|
|
headers=_auth(access_token),
|
|
)
|
|
|
|
other = client.get("/api/music/favorites", headers=_auth(editor_access_token))
|
|
assert other.json()["total"] == 0
|
|
tracks = client.get("/api/music/tracks", headers=_auth(editor_access_token)).json()
|
|
assert all(i["is_favorite"] is False for i in tracks["items"])
|
|
|
|
|
|
def test_favorites_survive_a_rescan(
|
|
client: TestClient, access_token: str, music_library
|
|
):
|
|
"""Rescans reconcile rom_files in place, so favorites keep resolving."""
|
|
rf_id = _track_id(client, access_token, "Green Hill")
|
|
client.post(
|
|
"/api/music/favorites",
|
|
json={"rom_file_ids": [rf_id]},
|
|
headers=_auth(access_token),
|
|
)
|
|
|
|
rom = music_library["sonic"]
|
|
existing = db_rom_handler.rom_files_for_rom_id(rom.id)
|
|
synced = db_rom_handler.sync_rom_files(
|
|
rom.id,
|
|
[
|
|
RomFile(
|
|
file_name=f.file_name,
|
|
file_path=f.file_path,
|
|
file_size_bytes=f.file_size_bytes,
|
|
category=RomFileCategory.SOUNDTRACK,
|
|
track_meta=TrackMeta(title="Green Hill"),
|
|
)
|
|
for f in existing
|
|
],
|
|
)
|
|
assert [f.id for f in synced.files] == [f.id for f in existing]
|
|
|
|
favs = client.get("/api/music/favorites", headers=_auth(access_token)).json()
|
|
assert favs["total"] == 1
|
|
assert favs["items"][0]["rom_file_id"] == rf_id
|
|
|
|
|
|
def test_favorites_reject_non_track_and_unknown_ids(
|
|
client: TestClient, access_token: str, music_library
|
|
):
|
|
non_track = db_rom_handler.add_rom_file(
|
|
RomFile(
|
|
rom_id=music_library["sonic"].id,
|
|
file_name="manual.pdf",
|
|
file_path="genesis/roms/Sonic/manual",
|
|
file_size_bytes=1,
|
|
category=RomFileCategory.MANUAL,
|
|
)
|
|
)
|
|
r = client.post(
|
|
"/api/music/favorites",
|
|
json={"rom_file_ids": [non_track.id]},
|
|
headers=_auth(access_token),
|
|
)
|
|
assert r.status_code == status.HTTP_400_BAD_REQUEST
|
|
assert "not music tracks" in r.json()["detail"]
|
|
|
|
r = client.post(
|
|
"/api/music/favorites",
|
|
json={"rom_file_ids": [999_999]},
|
|
headers=_auth(access_token),
|
|
)
|
|
assert r.status_code == status.HTTP_400_BAD_REQUEST
|
|
assert "not found" in r.json()["detail"]
|
|
|
|
|
|
# ---------- has_soundtrack roms filter ----------
|
|
|
|
|
|
def test_roms_has_soundtrack_filter(
|
|
client: TestClient, access_token: str, music_library
|
|
):
|
|
with_st = client.get(
|
|
"/api/roms?has_soundtrack=true", headers=_auth(access_token)
|
|
).json()
|
|
names = {i["name"] for i in with_st["items"]}
|
|
assert "Sonic" in names and "Tetris" not in names
|
|
|
|
without = client.get(
|
|
"/api/roms?has_soundtrack=false", headers=_auth(access_token)
|
|
).json()
|
|
assert "Tetris" in {i["name"] for i in without["items"]}
|
|
|
|
|
|
# ---------- game genres / platforms / games facets ----------
|
|
|
|
|
|
def _set_game_genres(rom_id: int, genres: list[str]) -> None:
|
|
# `roms_metadata` is a view over generated columns on `roms`, so game
|
|
# genres are seeded through the manual metadata that feeds them.
|
|
db_rom_handler.update_rom(rom_id, {"manual_metadata": {"genres": genres}})
|
|
|
|
|
|
@pytest.fixture
|
|
def music_library_with_game_genres(music_library):
|
|
_set_game_genres(music_library["sonic"].id, ["Platform", "Action"])
|
|
return music_library
|
|
|
|
|
|
def test_game_genres_facet_counts_tracks_per_genre(
|
|
client: TestClient, access_token: str, music_library_with_game_genres
|
|
):
|
|
r = client.get("/api/music/game-genres", headers=_auth(access_token))
|
|
assert r.status_code == status.HTTP_200_OK
|
|
by_value = {i["value"]: i["count"] for i in r.json()["items"]}
|
|
assert by_value == {"Platform": 1, "Action": 1}
|
|
|
|
|
|
def test_game_genres_facet_typeahead(
|
|
client: TestClient, access_token: str, music_library_with_game_genres
|
|
):
|
|
r = client.get(
|
|
"/api/music/game-genres", params={"search": "plat"}, headers=_auth(access_token)
|
|
)
|
|
assert [i["value"] for i in r.json()["items"]] == ["Platform"]
|
|
|
|
|
|
def test_tracks_filter_by_game_genre(
|
|
client: TestClient, access_token: str, music_library_with_game_genres
|
|
):
|
|
r = client.get(
|
|
"/api/music/tracks",
|
|
params={"game_genre": "Platform"},
|
|
headers=_auth(access_token),
|
|
)
|
|
body = r.json()
|
|
assert body["total"] == 1
|
|
assert body["items"][0]["title"] == "Green Hill"
|
|
|
|
|
|
def test_tracks_filter_by_year_range(
|
|
client: TestClient, access_token: str, music_library
|
|
):
|
|
r = client.get(
|
|
"/api/music/tracks",
|
|
params={"min_year": 1990, "max_year": 1999},
|
|
headers=_auth(access_token),
|
|
)
|
|
body = r.json()
|
|
assert body["total"] == 2
|
|
assert {i["year"] for i in body["items"]} == {1991, 1992}
|
|
|
|
|
|
def test_platforms_facet_lists_only_platforms_with_tracks(
|
|
client: TestClient, access_token: str, music_library
|
|
):
|
|
r = client.get("/api/music/platforms", headers=_auth(access_token))
|
|
assert r.status_code == status.HTTP_200_OK
|
|
items = r.json()["items"]
|
|
by_slug = {i["slug"]: i["count"] for i in items}
|
|
assert by_slug == {"genesis": 2, "nes": 1}
|
|
assert all(i["id"] and i["name"] for i in items)
|
|
|
|
|
|
def test_games_facet_is_the_album_list(
|
|
client: TestClient, access_token: str, music_library
|
|
):
|
|
r = client.get("/api/music/games", headers=_auth(access_token))
|
|
assert r.status_code == status.HTTP_200_OK
|
|
items = r.json()["items"]
|
|
assert [i["name"] for i in items] == ["Mario", "Sonic", "Streets"]
|
|
streets = next(i for i in items if i["name"] == "Streets")
|
|
assert streets["count"] == 1
|
|
assert streets["platform_slug"] == "genesis"
|
|
assert streets["cover_url"] == f"{FRONTEND_RESOURCES_PATH}/roms/2/2/cover/big.png"
|
|
mario = next(i for i in items if i["name"] == "Mario")
|
|
assert mario["cover_url"] is None
|
|
|
|
|
|
def test_games_facet_search_matches_game_and_track_fields(
|
|
client: TestClient, access_token: str, music_library
|
|
):
|
|
by_game = client.get(
|
|
"/api/music/games", params={"search": "sonic"}, headers=_auth(access_token)
|
|
).json()
|
|
assert [i["name"] for i in by_game["items"]] == ["Sonic"]
|
|
|
|
by_artist = client.get(
|
|
"/api/music/games", params={"search": "koshiro"}, headers=_auth(access_token)
|
|
).json()
|
|
assert [i["name"] for i in by_artist["items"]] == ["Streets"]
|
|
|
|
|
|
def test_games_facet_excludes_hidden_platform(music_library):
|
|
pa = music_library["platform_a"].id
|
|
rows, total = db_rom_handler.get_music_game_facet(hidden_platform_ids=[pa])
|
|
assert total == 1
|
|
assert [r.name for r in rows] == ["Mario"]
|
|
|
|
|
|
def test_platform_facet_excludes_hidden_platform(music_library):
|
|
pa = music_library["platform_a"].id
|
|
rows, total = db_rom_handler.get_music_platform_facet(hidden_platform_ids=[pa])
|
|
assert total == 1
|
|
assert [r.slug for r in rows] == ["nes"]
|
|
|
|
|
|
def test_stats_reports_totals(client: TestClient, access_token: str, music_library):
|
|
r = client.get("/api/music/stats", headers=_auth(access_token))
|
|
assert r.status_code == status.HTTP_200_OK
|
|
body = r.json()
|
|
assert body["total_tracks"] == 3
|
|
assert body["total_duration_seconds"] == pytest.approx(230.0)
|
|
|
|
|
|
def test_stats_excludes_hidden_platform(music_library):
|
|
pa = music_library["platform_a"].id
|
|
total, duration = db_rom_handler.get_music_stats(hidden_platform_ids=[pa])
|
|
assert total == 1
|
|
assert duration == pytest.approx(90.0)
|