mirror of
https://github.com/rommapp/romm.git
synced 2026-11-29 01:58:41 +01:00
Pull the cover priority (embedded track cover, then the game's cover art) out of from_row into a named helper on the schema, and cover the no-art case that the endpoint tests didn't reach. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
432 lines
14 KiB
Python
432 lines
14 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"]}
|