mirror of
https://github.com/rommapp/romm.git
synced 2026-12-05 23:44:11 +01:00
- A sheet naming both Track.bin and TRACK.BIN read both tracks from one file on a case-sensitive filesystem, since neither was checked by its exact name. An exact match now wins over the case-insensitive one. - Cancelling while a finished track was moving into place left the FLAC on disk but unregistered. The move now finishes before the cancel propagates, and a track it landed is registered. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017hbFZXXaYLy9ApWatJ5WCV
896 lines
29 KiB
Python
896 lines
29 KiB
Python
import asyncio
|
|
import ctypes
|
|
import math
|
|
import shutil
|
|
import struct
|
|
import subprocess
|
|
import threading
|
|
from collections.abc import Generator
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
from tests.factories import make_rom
|
|
|
|
from exceptions.fs_exceptions import RomListedByPlaylistException
|
|
from handler import cd_audio, rom_upload
|
|
from handler.cd_audio import (
|
|
CdAudioEncodeException,
|
|
CdAudioNeedsFolderException,
|
|
CdAudioUnavailableException,
|
|
)
|
|
from handler.database import db_rom_handler
|
|
from handler.filesystem import fs_rom_handler
|
|
from handler.rom_files import RomFilesRefresh
|
|
from handler.rom_upload import UploadNotRegisteredException, UploadRejectedException
|
|
from models.platform import Platform
|
|
from models.rom import Rom, RomFile, RomFileCategory, TrackMeta
|
|
from models.user import User
|
|
|
|
AUDIO_SECTOR = 2352
|
|
SECTORS_PER_SECOND = 75
|
|
|
|
CUE_SHEET = """FILE "Disc (Track 1).bin" BINARY
|
|
TRACK 01 MODE2/2352
|
|
INDEX 01 00:00:00
|
|
FILE "Disc (Track 2).bin" BINARY
|
|
TRACK 02 AUDIO
|
|
TITLE "Opening"
|
|
INDEX 00 00:00:00
|
|
INDEX 01 00:00:10
|
|
FILE "Disc (Track 3).bin" BINARY
|
|
TRACK 03 AUDIO
|
|
INDEX 01 00:00:00
|
|
"""
|
|
|
|
|
|
# Track 3 is left off a multiple of four sectors, which CHD pads.
|
|
TRACK_2_SECTORS = 75
|
|
TRACK_3_SECTORS = 38
|
|
|
|
|
|
def _tone(sectors: int) -> bytes:
|
|
"""Little-endian 16-bit stereo PCM, the layout of a raw audio track."""
|
|
count = sectors * AUDIO_SECTOR // 4
|
|
samples = (
|
|
int(8000 * math.sin(2 * math.pi * 440 * i / 44100)) for i in range(count)
|
|
)
|
|
return b"".join(struct.pack("<hh", s, s) for s in samples)
|
|
|
|
|
|
def write_cue_disc(folder: Path) -> dict[str, bytes]:
|
|
"""Write a data track and two audio tracks as a cue/bin set."""
|
|
folder.mkdir(parents=True, exist_ok=True)
|
|
contents = {
|
|
"Disc.cue": CUE_SHEET.encode(),
|
|
"Disc (Track 1).bin": b"\x01" * AUDIO_SECTOR * 4,
|
|
# Ten sectors of pregap silence ahead of the tone.
|
|
"Disc (Track 2).bin": b"\0" * AUDIO_SECTOR * 10 + _tone(TRACK_2_SECTORS),
|
|
"Disc (Track 3).bin": _tone(TRACK_3_SECTORS),
|
|
}
|
|
for name, data in contents.items():
|
|
(folder / name).write_bytes(data)
|
|
return contents
|
|
|
|
|
|
def write_gdi_disc(folder: Path) -> dict[str, bytes]:
|
|
"""Write a Dreamcast data track and two audio tracks as a .gdi set."""
|
|
folder.mkdir(parents=True, exist_ok=True)
|
|
# 150-sector gaps between tracks, as on a real disc.
|
|
third_lba = 154 + TRACK_2_SECTORS + 150
|
|
contents = {
|
|
"disc.gdi": (
|
|
f"3\n1 0 4 2352 track01.bin 0\n2 154 0 2352 track02.raw 0\n"
|
|
f"3 {third_lba} 0 2352 track03.raw 0\n"
|
|
).encode(),
|
|
"track01.bin": b"\x01" * AUDIO_SECTOR * 4,
|
|
"track02.raw": _tone(TRACK_2_SECTORS),
|
|
"track03.raw": _tone(TRACK_3_SECTORS),
|
|
}
|
|
for name, data in contents.items():
|
|
(folder / name).write_bytes(data)
|
|
return contents
|
|
|
|
|
|
def add_disc_rom(
|
|
admin_user: User,
|
|
platform: Platform,
|
|
fs_name: str,
|
|
files: dict[str, int],
|
|
file_path: str,
|
|
) -> Rom:
|
|
rom = make_rom(
|
|
platform,
|
|
"Disc Game",
|
|
fs_extension=fs_name.rpartition(".")[2] if "." in fs_name else "",
|
|
slug="disc_game",
|
|
fs_name=fs_name,
|
|
)
|
|
db_rom_handler.add_rom_user(rom_id=rom.id, user_id=admin_user.id)
|
|
for name, size in files.items():
|
|
db_rom_handler.add_rom_file(
|
|
RomFile(
|
|
rom_id=rom.id,
|
|
file_name=name,
|
|
file_path=file_path,
|
|
file_size_bytes=size,
|
|
category=RomFileCategory.GAME,
|
|
)
|
|
)
|
|
loaded = db_rom_handler.get_rom(rom.id)
|
|
assert loaded is not None
|
|
return loaded
|
|
|
|
|
|
def _soundtrack_metas(rom_id: int) -> dict[str, TrackMeta | None]:
|
|
rom = db_rom_handler.get_rom(rom_id)
|
|
assert rom is not None
|
|
return {
|
|
f.file_name: f.track_meta
|
|
for f in rom.files
|
|
if f.category == RomFileCategory.SOUNDTRACK
|
|
}
|
|
|
|
|
|
@pytest.fixture
|
|
def cd_rom(admin_user: User, platform: Platform, real_library: Path) -> Rom:
|
|
"""A disc folder with a data track and two audio tracks, written to disk."""
|
|
fs_path = f"{platform.slug}/roms/Disc Game"
|
|
contents = write_cue_disc(real_library / fs_path)
|
|
return add_disc_rom(
|
|
admin_user,
|
|
platform,
|
|
"Disc Game",
|
|
{name: len(data) for name, data in contents.items()},
|
|
fs_path,
|
|
)
|
|
|
|
|
|
def _create_chd(source: Path, output: Path) -> None:
|
|
chdman = shutil.which("chdman")
|
|
assert chdman, "chdman (mame-tools) is needed to build CHD fixtures"
|
|
subprocess.run(
|
|
[chdman, "createcd", "-i", str(source), "-o", str(output)],
|
|
check=True,
|
|
capture_output=True,
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def gdi_rom(admin_user: User, platform: Platform, real_library: Path) -> Rom:
|
|
fs_path = f"{platform.slug}/roms/Disc Game"
|
|
contents = write_gdi_disc(real_library / fs_path)
|
|
return add_disc_rom(
|
|
admin_user,
|
|
platform,
|
|
"Disc Game",
|
|
{name: len(data) for name, data in contents.items()},
|
|
fs_path,
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def gdrom_chd_rom(
|
|
admin_user: User, platform: Platform, real_library: Path, tmp_path: Path
|
|
) -> Rom:
|
|
"""The Dreamcast disc compressed to a GD-ROM CHD."""
|
|
fs_path = f"{platform.slug}/roms/Disc Game"
|
|
(real_library / fs_path).mkdir(parents=True)
|
|
chd = real_library / fs_path / "disc.chd"
|
|
write_gdi_disc(tmp_path / "source")
|
|
_create_chd(tmp_path / "source" / "disc.gdi", chd)
|
|
return add_disc_rom(
|
|
admin_user, platform, "Disc Game", {"disc.chd": chd.stat().st_size}, fs_path
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def chd_rom(
|
|
admin_user: User, platform: Platform, real_library: Path, tmp_path: Path
|
|
) -> Rom:
|
|
"""The same disc compressed to a CHD, alone in its own folder."""
|
|
fs_path = f"{platform.slug}/roms/Disc Game"
|
|
(real_library / fs_path).mkdir(parents=True)
|
|
chd = real_library / fs_path / "Disc.chd"
|
|
write_cue_disc(tmp_path / "source")
|
|
_create_chd(tmp_path / "source" / "Disc.cue", chd)
|
|
return add_disc_rom(
|
|
admin_user, platform, "Disc Game", {"Disc.chd": chd.stat().st_size}, fs_path
|
|
)
|
|
|
|
|
|
async def test_extracts_each_audio_track_to_flac(cd_rom: Rom, real_library: Path):
|
|
result = await cd_audio.extract_cd_audio(cd_rom)
|
|
|
|
assert result.extracted == ["Disc - Track 02.flac", "Disc - Track 03.flac"]
|
|
assert result.skipped == []
|
|
soundtrack = real_library / cd_rom.full_path / "soundtrack"
|
|
assert sorted(p.name for p in soundtrack.iterdir()) == [
|
|
"Disc - Track 02.flac",
|
|
"Disc - Track 03.flac",
|
|
]
|
|
assert (soundtrack / "Disc - Track 02.flac").read_bytes()[:4] == b"fLaC"
|
|
|
|
metas = _soundtrack_metas(cd_rom.id)
|
|
opening = metas["Disc - Track 02.flac"]
|
|
assert opening is not None
|
|
assert opening.title == "Opening"
|
|
assert opening.album == "Disc Game"
|
|
assert opening.track == 2
|
|
assert opening.duration_seconds == pytest.approx(
|
|
TRACK_2_SECTORS / SECTORS_PER_SECOND, abs=0.01
|
|
)
|
|
untitled = metas["Disc - Track 03.flac"]
|
|
assert untitled is not None
|
|
assert untitled.title == "Track 03"
|
|
assert untitled.duration_seconds == pytest.approx(
|
|
TRACK_3_SECTORS / SECTORS_PER_SECOND, abs=0.01
|
|
)
|
|
|
|
|
|
async def test_extracts_the_audio_tracks_of_a_chd(chd_rom: Rom):
|
|
result = await cd_audio.extract_cd_audio(chd_rom)
|
|
|
|
assert result.extracted == ["Disc - Track 02.flac", "Disc - Track 03.flac"]
|
|
assert result.skipped == []
|
|
metas = _soundtrack_metas(chd_rom.id)
|
|
second = metas["Disc - Track 02.flac"]
|
|
assert second is not None
|
|
# CHD keeps no CD-Text, and its stored pregap stays out of the track.
|
|
assert second.title == "Track 02"
|
|
assert second.album == "Disc Game"
|
|
assert second.duration_seconds == pytest.approx(
|
|
TRACK_2_SECTORS / SECTORS_PER_SECOND, abs=0.01
|
|
)
|
|
third = metas["Disc - Track 03.flac"]
|
|
assert third is not None
|
|
assert third.duration_seconds == pytest.approx(
|
|
TRACK_3_SECTORS / SECTORS_PER_SECOND, abs=0.01
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize("fixture", ["gdi_rom", "gdrom_chd_rom"])
|
|
async def test_extracts_the_audio_tracks_of_a_dreamcast_disc(
|
|
fixture: str, request: pytest.FixtureRequest
|
|
):
|
|
rom: Rom = request.getfixturevalue(fixture)
|
|
|
|
result = await cd_audio.extract_cd_audio(rom)
|
|
|
|
assert result.extracted == ["disc - Track 02.flac", "disc - Track 03.flac"]
|
|
assert result.skipped == []
|
|
metas = _soundtrack_metas(rom.id)
|
|
second = metas["disc - Track 02.flac"]
|
|
assert second is not None
|
|
assert second.title == "Track 02"
|
|
assert second.duration_seconds == pytest.approx(
|
|
TRACK_2_SECTORS / SECTORS_PER_SECOND, abs=0.01
|
|
)
|
|
third = metas["disc - Track 03.flac"]
|
|
assert third is not None
|
|
# The gap to where the next track would start isn't audio.
|
|
assert third.duration_seconds == pytest.approx(
|
|
TRACK_3_SECTORS / SECTORS_PER_SECOND, abs=0.01
|
|
)
|
|
|
|
|
|
async def test_names_the_tracks_of_discs_that_share_a_sheet_name(
|
|
admin_user: User, platform: Platform, real_library: Path
|
|
):
|
|
fs_path = f"{platform.slug}/roms/Disc Game"
|
|
rom = add_disc_rom(admin_user, platform, "Disc Game", {}, fs_path)
|
|
for disc in ("Disc 1", "Disc 2"):
|
|
for name, data in write_gdi_disc(real_library / fs_path / disc).items():
|
|
db_rom_handler.add_rom_file(
|
|
RomFile(
|
|
rom_id=rom.id,
|
|
file_name=name,
|
|
file_path=f"{fs_path}/{disc}",
|
|
file_size_bytes=len(data),
|
|
category=RomFileCategory.GAME,
|
|
)
|
|
)
|
|
loaded = db_rom_handler.get_rom(rom.id)
|
|
assert loaded is not None
|
|
|
|
result = await cd_audio.extract_cd_audio(loaded)
|
|
|
|
assert sorted(result.extracted) == [
|
|
"Disc 1 - disc - Track 02.flac",
|
|
"Disc 1 - disc - Track 03.flac",
|
|
"Disc 2 - disc - Track 02.flac",
|
|
"Disc 2 - disc - Track 03.flac",
|
|
]
|
|
# The folder order numbers the discs when their names don't.
|
|
second_disc = _soundtrack_metas(rom.id)["Disc 2 - disc - Track 02.flac"]
|
|
assert second_disc is not None
|
|
assert second_disc.title == "Track 02 (Disc 2)"
|
|
assert second_disc.disc == 2
|
|
|
|
|
|
async def test_extracts_every_disc_of_a_set(
|
|
admin_user: User, platform: Platform, real_library: Path, tmp_path: Path
|
|
):
|
|
fs_path = f"{platform.slug}/roms/Disc Game"
|
|
folder = real_library / fs_path
|
|
folder.mkdir(parents=True)
|
|
write_cue_disc(tmp_path / "source")
|
|
discs = ["Disc Game (Disc 1).chd", "Disc Game (Disc 2).chd"]
|
|
for disc in discs:
|
|
_create_chd(tmp_path / "source" / "Disc.cue", folder / disc)
|
|
(folder / "Disc Game.m3u").write_text("\n".join(discs))
|
|
rom = add_disc_rom(
|
|
admin_user,
|
|
platform,
|
|
"Disc Game",
|
|
{name: (folder / name).stat().st_size for name in [*discs, "Disc Game.m3u"]},
|
|
fs_path,
|
|
)
|
|
|
|
result = await cd_audio.extract_cd_audio(rom)
|
|
|
|
assert sorted(result.extracted) == [
|
|
"Disc Game (Disc 1) - Track 02.flac",
|
|
"Disc Game (Disc 1) - Track 03.flac",
|
|
"Disc Game (Disc 2) - Track 02.flac",
|
|
"Disc Game (Disc 2) - Track 03.flac",
|
|
]
|
|
first_disc = _soundtrack_metas(rom.id)["Disc Game (Disc 1) - Track 03.flac"]
|
|
assert first_disc is not None
|
|
assert first_disc.title == "Track 03 (Disc 1)"
|
|
assert first_disc.disc == 1
|
|
|
|
|
|
def _add_playlist_listed_chd(
|
|
admin_user: User, platform: Platform, real_library: Path, tmp_path: Path
|
|
) -> tuple[Rom, Path]:
|
|
"""A lone CHD in the platform folder that an .m3u beside it lists."""
|
|
fs_path = f"{platform.slug}/roms"
|
|
(real_library / fs_path).mkdir(parents=True)
|
|
write_cue_disc(tmp_path / "source")
|
|
chd = real_library / fs_path / "Disc Game (Disc 1).chd"
|
|
_create_chd(tmp_path / "source" / "Disc.cue", chd)
|
|
(real_library / fs_path / "Disc Game.m3u").write_text(
|
|
"Disc Game (Disc 1).chd\nDisc Game (Disc 2).chd\n"
|
|
)
|
|
rom = add_disc_rom(
|
|
admin_user, platform, chd.name, {chd.name: chd.stat().st_size}, fs_path
|
|
)
|
|
return rom, chd
|
|
|
|
|
|
async def test_refuses_to_move_a_lone_disc_a_playlist_lists(
|
|
admin_user: User, platform: Platform, real_library: Path, tmp_path: Path
|
|
):
|
|
rom, chd = _add_playlist_listed_chd(admin_user, platform, real_library, tmp_path)
|
|
|
|
with pytest.raises(RomListedByPlaylistException, match="Disc Game.m3u"):
|
|
await cd_audio.extract_cd_audio(rom)
|
|
|
|
assert chd.is_file()
|
|
|
|
|
|
async def test_moves_a_lone_chd_into_its_own_folder(
|
|
admin_user: User,
|
|
platform: Platform,
|
|
real_library: Path,
|
|
tmp_path: Path,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
):
|
|
fs_path = f"{platform.slug}/roms"
|
|
(real_library / fs_path).mkdir(parents=True)
|
|
write_cue_disc(tmp_path / "source")
|
|
chd = real_library / fs_path / "Disc Game.chd"
|
|
_create_chd(tmp_path / "source" / "Disc.cue", chd)
|
|
rom = add_disc_rom(
|
|
admin_user,
|
|
platform,
|
|
"Disc Game.chd",
|
|
{"Disc Game.chd": chd.stat().st_size},
|
|
fs_path,
|
|
)
|
|
real_sources = cd_audio._chd_sources
|
|
reads: list[Path] = []
|
|
|
|
def counting_sources(
|
|
lib: ctypes.CDLL, chd_path: Path
|
|
) -> list[cd_audio.AudioSource]:
|
|
reads.append(chd_path)
|
|
return real_sources(lib, chd_path)
|
|
|
|
monkeypatch.setattr(cd_audio, "_chd_sources", counting_sources)
|
|
|
|
result = await cd_audio.extract_cd_audio(rom)
|
|
|
|
assert result.extracted == [
|
|
"Disc Game - Track 02.flac",
|
|
"Disc Game - Track 03.flac",
|
|
]
|
|
folder = real_library / fs_path / "Disc Game"
|
|
assert (folder / "Disc Game.chd").is_file()
|
|
assert (folder / "soundtrack" / "Disc Game - Track 02.flac").is_file()
|
|
assert reads == [chd]
|
|
|
|
|
|
async def test_reports_missing_chd_support(
|
|
chd_rom: Rom, monkeypatch: pytest.MonkeyPatch
|
|
):
|
|
monkeypatch.setattr(cd_audio, "load_libchdr", lambda: None)
|
|
|
|
with pytest.raises(CdAudioUnavailableException):
|
|
await cd_audio.extract_cd_audio(chd_rom)
|
|
|
|
|
|
async def test_skips_tracks_already_extracted(cd_rom: Rom):
|
|
await cd_audio.extract_cd_audio(cd_rom)
|
|
reloaded = db_rom_handler.get_rom(cd_rom.id)
|
|
assert reloaded is not None
|
|
|
|
result = await cd_audio.extract_cd_audio(reloaded)
|
|
|
|
assert result.extracted == []
|
|
assert result.skipped == ["Disc - Track 02.flac", "Disc - Track 03.flac"]
|
|
|
|
|
|
async def test_extracts_nothing_without_a_cue_sheet(
|
|
game_folder_rom: Rom, game_folder_on_disk: Path
|
|
):
|
|
result = await cd_audio.extract_cd_audio(game_folder_rom)
|
|
|
|
assert result.extracted == []
|
|
assert result.skipped == []
|
|
|
|
|
|
@pytest.mark.parametrize("sheet", ["Loose.cue", "Loose.gdi"])
|
|
async def test_refuses_a_sheet_loose_in_the_platform_folder(
|
|
admin_user: User, platform: Platform, sheet: str
|
|
):
|
|
fs_path = f"{platform.slug}/roms"
|
|
rom = add_disc_rom(admin_user, platform, sheet, {sheet: 10}, fs_path)
|
|
|
|
with pytest.raises(CdAudioNeedsFolderException):
|
|
await cd_audio.extract_cd_audio(rom)
|
|
|
|
|
|
async def test_reports_a_missing_encoder(cd_rom: Rom, monkeypatch: pytest.MonkeyPatch):
|
|
monkeypatch.setattr(cd_audio, "FLAC_BINARY", "romm-no-such-flac")
|
|
|
|
with pytest.raises(CdAudioUnavailableException):
|
|
await cd_audio.extract_cd_audio(cd_rom)
|
|
|
|
|
|
def _fail_on_third_track(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
real_encode = cd_audio.encode_track
|
|
|
|
async def fail_on_third(
|
|
source: cd_audio.AudioSource, output: Path, album: str | None
|
|
) -> None:
|
|
if source.number == 3:
|
|
raise CdAudioEncodeException("boom")
|
|
await real_encode(source, output, album)
|
|
|
|
monkeypatch.setattr(cd_audio, "encode_track", fail_on_third)
|
|
|
|
|
|
async def _refresh_fails(rom: Rom) -> RomFilesRefresh:
|
|
raise RuntimeError("lock timeout")
|
|
|
|
|
|
async def test_registers_the_tracks_written_before_a_failure(
|
|
cd_rom: Rom, monkeypatch: pytest.MonkeyPatch
|
|
):
|
|
_fail_on_third_track(monkeypatch)
|
|
|
|
with pytest.raises(CdAudioEncodeException):
|
|
await cd_audio.extract_cd_audio(cd_rom)
|
|
|
|
assert set(_soundtrack_metas(cd_rom.id)) == {"Disc - Track 02.flac"}
|
|
|
|
|
|
async def test_encodes_the_tracks_concurrently(
|
|
cd_rom: Rom, monkeypatch: pytest.MonkeyPatch
|
|
):
|
|
monkeypatch.setattr(cd_audio, "ENCODE_CONCURRENCY", 2)
|
|
real_encode = cd_audio.encode_track
|
|
started: set[int] = set()
|
|
both_started = asyncio.Event()
|
|
|
|
async def wait_for_each_other(
|
|
source: cd_audio.AudioSource, output: Path, album: str | None
|
|
) -> None:
|
|
started.add(source.number)
|
|
if len(started) == 2:
|
|
both_started.set()
|
|
await asyncio.wait_for(both_started.wait(), 5)
|
|
await real_encode(source, output, album)
|
|
|
|
monkeypatch.setattr(cd_audio, "encode_track", wait_for_each_other)
|
|
|
|
result = await cd_audio.extract_cd_audio(cd_rom)
|
|
|
|
assert result.extracted == ["Disc - Track 02.flac", "Disc - Track 03.flac"]
|
|
|
|
|
|
async def test_starts_no_track_after_a_failure(
|
|
cd_rom: Rom, monkeypatch: pytest.MonkeyPatch
|
|
):
|
|
monkeypatch.setattr(cd_audio, "ENCODE_CONCURRENCY", 1)
|
|
started: list[int] = []
|
|
|
|
async def fail_on_second(
|
|
source: cd_audio.AudioSource, output: Path, album: str | None
|
|
) -> None:
|
|
started.append(source.number)
|
|
raise CdAudioEncodeException("boom")
|
|
|
|
monkeypatch.setattr(cd_audio, "encode_track", fail_on_second)
|
|
|
|
with pytest.raises(CdAudioEncodeException):
|
|
await cd_audio.extract_cd_audio(cd_rom)
|
|
|
|
assert started == [2]
|
|
assert _soundtrack_metas(cd_rom.id) == {}
|
|
|
|
|
|
async def test_registers_a_track_whose_move_finished_after_a_cancel(
|
|
cd_rom: Rom, monkeypatch: pytest.MonkeyPatch
|
|
):
|
|
monkeypatch.setattr(cd_audio, "ENCODE_CONCURRENCY", 1)
|
|
real_move = rom_upload.move_into_place
|
|
moving = threading.Event()
|
|
release = threading.Event()
|
|
|
|
def slow_move(location: Path, staged: Path, *, overwrite: bool) -> None:
|
|
moving.set()
|
|
release.wait(5)
|
|
real_move(location, staged, overwrite=overwrite)
|
|
|
|
monkeypatch.setattr(cd_audio, "move_into_place", slow_move)
|
|
task = asyncio.create_task(cd_audio.extract_cd_audio(cd_rom))
|
|
await asyncio.to_thread(moving.wait, 5)
|
|
|
|
task.cancel()
|
|
await asyncio.sleep(0.05)
|
|
release.set()
|
|
|
|
with pytest.raises(asyncio.CancelledError):
|
|
await task
|
|
assert set(_soundtrack_metas(cd_rom.id)) == {"Disc - Track 02.flac"}
|
|
|
|
|
|
async def test_asks_for_a_scan_when_the_tracks_cannot_be_registered(
|
|
cd_rom: Rom, real_library: Path, monkeypatch: pytest.MonkeyPatch
|
|
):
|
|
monkeypatch.setattr(cd_audio, "refresh_rom_files", _refresh_fails)
|
|
|
|
with pytest.raises(UploadNotRegisteredException, match="quick scan"):
|
|
await cd_audio.extract_cd_audio(cd_rom)
|
|
|
|
soundtrack = real_library / cd_rom.full_path / "soundtrack"
|
|
assert (soundtrack / "Disc - Track 02.flac").is_file()
|
|
assert (soundtrack / "Disc - Track 03.flac").is_file()
|
|
|
|
|
|
async def test_keeps_the_encode_error_when_registering_also_fails(
|
|
cd_rom: Rom, monkeypatch: pytest.MonkeyPatch
|
|
):
|
|
_fail_on_third_track(monkeypatch)
|
|
monkeypatch.setattr(cd_audio, "refresh_rom_files", _refresh_fails)
|
|
|
|
with pytest.raises(CdAudioEncodeException, match="boom"):
|
|
await cd_audio.extract_cd_audio(cd_rom)
|
|
|
|
|
|
async def test_leaves_no_partial_file_when_the_image_cannot_be_read(
|
|
cd_rom: Rom, real_library: Path, monkeypatch: pytest.MonkeyPatch
|
|
):
|
|
async def unreadable(stdin: asyncio.StreamWriter, pcm: cd_audio.PcmChunks) -> None:
|
|
raise OSError("read failed")
|
|
|
|
monkeypatch.setattr(cd_audio, "_feed", unreadable)
|
|
|
|
with pytest.raises(CdAudioEncodeException):
|
|
await cd_audio.extract_cd_audio(cd_rom)
|
|
|
|
soundtrack = real_library / cd_rom.full_path / "soundtrack"
|
|
assert list(soundtrack.iterdir()) == []
|
|
|
|
|
|
async def test_reports_an_unreadable_chd(
|
|
admin_user: User, platform: Platform, real_library: Path
|
|
):
|
|
fs_path = f"{platform.slug}/roms/Disc Game"
|
|
(real_library / fs_path).mkdir(parents=True)
|
|
(real_library / fs_path / "Disc.chd").write_bytes(b"not a chd")
|
|
rom = add_disc_rom(admin_user, platform, "Disc Game", {"Disc.chd": 9}, fs_path)
|
|
|
|
with pytest.raises(CdAudioEncodeException):
|
|
await cd_audio.extract_cd_audio(rom)
|
|
|
|
|
|
async def test_extracts_a_disc_kept_as_both_a_sheet_and_a_chd_once(
|
|
admin_user: User, platform: Platform, real_library: Path
|
|
):
|
|
fs_path = f"{platform.slug}/roms/Disc Game"
|
|
contents = write_cue_disc(real_library / fs_path)
|
|
chd = real_library / fs_path / "Disc.chd"
|
|
_create_chd(real_library / fs_path / "Disc.cue", chd)
|
|
sizes = {name: len(data) for name, data in contents.items()}
|
|
rom = add_disc_rom(
|
|
admin_user,
|
|
platform,
|
|
"Disc Game",
|
|
{**sizes, "Disc.chd": chd.stat().st_size},
|
|
fs_path,
|
|
)
|
|
|
|
result = await cd_audio.extract_cd_audio(rom)
|
|
|
|
assert result.extracted == ["Disc - Track 02.flac", "Disc - Track 03.flac"]
|
|
assert result.skipped == []
|
|
# The sheet's CD-Text title shows it was read rather than the CHD.
|
|
opening = _soundtrack_metas(rom.id)["Disc - Track 02.flac"]
|
|
assert opening is not None
|
|
assert opening.title == "Opening"
|
|
|
|
|
|
async def test_skips_a_track_a_concurrent_extraction_finished_first(
|
|
cd_rom: Rom, real_library: Path, monkeypatch: pytest.MonkeyPatch
|
|
):
|
|
real_encode = cd_audio.encode_track
|
|
soundtrack = real_library / cd_rom.full_path / "soundtrack"
|
|
|
|
async def race_on_third(
|
|
source: cd_audio.AudioSource, output: Path, album: str | None
|
|
) -> None:
|
|
await real_encode(source, output, album)
|
|
if source.number == 3:
|
|
(soundtrack / "Disc - Track 03.flac").write_bytes(b"theirs")
|
|
|
|
monkeypatch.setattr(cd_audio, "encode_track", race_on_third)
|
|
|
|
result = await cd_audio.extract_cd_audio(cd_rom)
|
|
|
|
assert result.extracted == ["Disc - Track 02.flac"]
|
|
assert result.skipped == ["Disc - Track 03.flac"]
|
|
assert (soundtrack / "Disc - Track 03.flac").read_bytes() == b"theirs"
|
|
assert sorted(p.name for p in soundtrack.iterdir()) == [
|
|
"Disc - Track 02.flac",
|
|
"Disc - Track 03.flac",
|
|
]
|
|
|
|
|
|
async def test_cancelling_mid_read_closes_the_image_and_cleans_up(tmp_path: Path):
|
|
reading = threading.Event()
|
|
release = threading.Event()
|
|
closed = threading.Event()
|
|
|
|
def slow_pcm(path: Path) -> Generator[bytes, None, None]:
|
|
try:
|
|
reading.set()
|
|
release.wait(5)
|
|
yield b"\0" * 4
|
|
finally:
|
|
closed.set()
|
|
|
|
source = cd_audio.AudioSource(
|
|
number=2,
|
|
big_endian=False,
|
|
title=None,
|
|
performer=None,
|
|
path=tmp_path / "Disc.bin",
|
|
read=slow_pcm,
|
|
)
|
|
output = tmp_path / "Track 02.flac"
|
|
task = asyncio.create_task(cd_audio.encode_track(source, output, None))
|
|
await asyncio.to_thread(reading.wait, 5)
|
|
|
|
task.cancel()
|
|
await asyncio.sleep(0.05)
|
|
release.set()
|
|
|
|
with pytest.raises(asyncio.CancelledError):
|
|
await task
|
|
assert closed.is_set()
|
|
assert not output.exists()
|
|
|
|
|
|
async def test_falls_back_to_the_chd_when_the_sheet_lost_its_tracks(
|
|
admin_user: User, platform: Platform, real_library: Path, tmp_path: Path
|
|
):
|
|
fs_path = f"{platform.slug}/roms/Disc Game"
|
|
folder = real_library / fs_path
|
|
folder.mkdir(parents=True)
|
|
write_cue_disc(tmp_path / "source")
|
|
_create_chd(tmp_path / "source" / "Disc.cue", folder / "Disc.chd")
|
|
(folder / "Disc.cue").write_text(CUE_SHEET)
|
|
rom = add_disc_rom(
|
|
admin_user,
|
|
platform,
|
|
"Disc Game",
|
|
{"Disc.cue": len(CUE_SHEET), "Disc.chd": (folder / "Disc.chd").stat().st_size},
|
|
fs_path,
|
|
)
|
|
|
|
result = await cd_audio.extract_cd_audio(rom)
|
|
|
|
assert result.extracted == ["Disc - Track 02.flac", "Disc - Track 03.flac"]
|
|
|
|
|
|
def _add_sheet_rom(
|
|
admin_user: User,
|
|
platform: Platform,
|
|
real_library: Path,
|
|
contents: dict[str, bytes],
|
|
) -> Rom:
|
|
fs_path = f"{platform.slug}/roms/Disc Game"
|
|
folder = real_library / fs_path
|
|
folder.mkdir(parents=True)
|
|
for name, data in contents.items():
|
|
(folder / name).write_bytes(data)
|
|
return add_disc_rom(
|
|
admin_user,
|
|
platform,
|
|
"Disc Game",
|
|
{name: len(data) for name, data in contents.items()},
|
|
fs_path,
|
|
)
|
|
|
|
|
|
async def test_reads_a_file_named_in_several_cases_once(
|
|
admin_user: User, platform: Platform, real_library: Path
|
|
):
|
|
names = ["Track.bin", "TRACK.BIN", "track.bin"]
|
|
sheet = "".join(
|
|
f'FILE "{name}" BINARY\n TRACK {number:02d} AUDIO\n'
|
|
f" INDEX 01 00:00:{(number - 2) * 25:02d}\n"
|
|
for number, name in enumerate(names, 2)
|
|
)
|
|
rom = _add_sheet_rom(
|
|
admin_user,
|
|
platform,
|
|
real_library,
|
|
{"Disc.cue": sheet.encode(), "Track.bin": _tone(TRACK_2_SECTORS)},
|
|
)
|
|
|
|
await cd_audio.extract_cd_audio(rom)
|
|
|
|
durations = [
|
|
meta.duration_seconds
|
|
for meta in _soundtrack_metas(rom.id).values()
|
|
if meta is not None and meta.duration_seconds is not None
|
|
]
|
|
# Split three ways rather than each spanning the whole file.
|
|
assert sum(durations) == pytest.approx(
|
|
TRACK_2_SECTORS / SECTORS_PER_SECOND, abs=0.02
|
|
)
|
|
|
|
|
|
async def test_keeps_files_differing_only_in_case_apart(
|
|
admin_user: User, platform: Platform, real_library: Path
|
|
):
|
|
sheet = (
|
|
'FILE "Track.bin" BINARY\n TRACK 02 AUDIO\n INDEX 01 00:00:00\n'
|
|
'FILE "TRACK.BIN" BINARY\n TRACK 03 AUDIO\n INDEX 01 00:00:00\n'
|
|
)
|
|
rom = _add_sheet_rom(
|
|
admin_user,
|
|
platform,
|
|
real_library,
|
|
{
|
|
"Disc.cue": sheet.encode(),
|
|
"Track.bin": _tone(TRACK_2_SECTORS),
|
|
"TRACK.BIN": _tone(TRACK_3_SECTORS),
|
|
},
|
|
)
|
|
if len(list((real_library / rom.full_path).iterdir())) < 3:
|
|
pytest.skip("the library is on a case-insensitive filesystem")
|
|
|
|
await cd_audio.extract_cd_audio(rom)
|
|
|
|
metas = _soundtrack_metas(rom.id)
|
|
durations = {
|
|
name: meta.duration_seconds for name, meta in metas.items() if meta is not None
|
|
}
|
|
assert durations["Disc - Track 02.flac"] == pytest.approx(
|
|
TRACK_2_SECTORS / SECTORS_PER_SECOND, abs=0.02
|
|
)
|
|
assert durations["Disc - Track 03.flac"] == pytest.approx(
|
|
TRACK_3_SECTORS / SECTORS_PER_SECOND, abs=0.02
|
|
)
|
|
|
|
|
|
async def test_takes_each_gdi_track_file_and_number_once(
|
|
admin_user: User, platform: Platform, real_library: Path
|
|
):
|
|
sheet = (
|
|
"5\n2 150 0 2352 track02.raw 0\n3 300 0 2352 TRACK02.RAW 0\n"
|
|
"4 450 0 2352 track02.raw 0\n0 600 0 2352 track05.raw 0\n"
|
|
"100 750 0 2352 track06.raw 0\n"
|
|
)
|
|
tone = _tone(TRACK_3_SECTORS)
|
|
rom = _add_sheet_rom(
|
|
admin_user,
|
|
platform,
|
|
real_library,
|
|
{
|
|
"disc.gdi": sheet.encode(),
|
|
"track02.raw": tone,
|
|
"track05.raw": tone,
|
|
"track06.raw": tone,
|
|
},
|
|
)
|
|
|
|
result = await cd_audio.extract_cd_audio(rom)
|
|
|
|
assert result.extracted == ["disc - Track 02.flac"]
|
|
|
|
|
|
async def test_refuses_a_sheet_too_large_to_be_one(
|
|
admin_user: User, platform: Platform, real_library: Path
|
|
):
|
|
rom = _add_sheet_rom(
|
|
admin_user,
|
|
platform,
|
|
real_library,
|
|
{"Disc.cue": b"REM" + b" " * cd_audio.MAX_SHEET_BYTES},
|
|
)
|
|
|
|
with pytest.raises(CdAudioEncodeException):
|
|
await cd_audio.extract_cd_audio(rom)
|
|
|
|
|
|
async def test_reports_a_track_name_the_scanner_would_ignore(
|
|
cd_rom: Rom, real_library: Path, monkeypatch: pytest.MonkeyPatch
|
|
):
|
|
monkeypatch.setattr(
|
|
fs_rom_handler,
|
|
"is_excluded_multi_part",
|
|
lambda file_name, cnfg=None: file_name.endswith(".flac"),
|
|
)
|
|
|
|
with pytest.raises(UploadRejectedException):
|
|
await cd_audio.extract_cd_audio(cd_rom)
|
|
|
|
assert not (real_library / cd_rom.full_path / "soundtrack").exists()
|
|
|
|
|
|
async def test_leaves_a_lone_disc_without_audio_where_it_is(
|
|
admin_user: User, platform: Platform, real_library: Path, tmp_path: Path
|
|
):
|
|
fs_path = f"{platform.slug}/roms"
|
|
(real_library / fs_path).mkdir(parents=True)
|
|
(tmp_path / "Data.cue").write_text(
|
|
'FILE "Data.bin" BINARY\n TRACK 01 MODE2/2352\n INDEX 01 00:00:00\n'
|
|
)
|
|
# libchdr can't open a CHD of a single hunk, so the track spans several.
|
|
(tmp_path / "Data.bin").write_bytes(b"\x01" * AUDIO_SECTOR * 300)
|
|
chd = real_library / fs_path / "Data Game.chd"
|
|
_create_chd(tmp_path / "Data.cue", chd)
|
|
rom = add_disc_rom(
|
|
admin_user, platform, chd.name, {chd.name: chd.stat().st_size}, fs_path
|
|
)
|
|
|
|
result = await cd_audio.extract_cd_audio(rom)
|
|
|
|
assert result.extracted == []
|
|
assert result.skipped == []
|
|
assert chd.is_file()
|
|
assert not (real_library / fs_path / "Data Game").exists()
|
|
|
|
|
|
async def test_reports_a_track_flac_cannot_be_started_for(tmp_path: Path):
|
|
def silence(path: Path) -> Generator[bytes, None, None]:
|
|
yield b"\0" * 4
|
|
|
|
source = cd_audio.AudioSource(
|
|
number=2,
|
|
big_endian=False,
|
|
title="Open\0ing",
|
|
performer=None,
|
|
path=tmp_path / "Disc.bin",
|
|
read=silence,
|
|
)
|
|
|
|
with pytest.raises(cd_audio.CdAudioEncodeException):
|
|
await cd_audio.encode_track(source, tmp_path / "Track 02.flac", None)
|