Files
romm/backend/tests/utils/test_m3u.py
Terri 8a1e6280b1 feat(saves): add save channels and snapshots (#5198)
* feat(saves): add save channels and snapshots

* feat(saves): close spec gaps in save channels and hash with sigil

* feat(saves): play snapshots in the v2 player, which pushes snapshots

* fix(saves): code review: authorization, retention losses, held-save sync, batched responses

* refactor(saves): simplify pass

* refactor(saves): review-polish pass

* docs(saves): humanizer pass

* feat(saves): run every EmulatorJS page on the v2 player

* feat(saves): per-user snapshot pins

* feat(sigil): store the identity a save restore needs and pin sigil 0f05f25

* fix(saves): meet type-aware lint in the v2 player

* feat(sigil): store N64 header and playlist title ids, pin sigil 0847fda

* feat(saves): convert saves for a core on download and list save layouts

* feat(saves): prune unreachable archival snapshots and empty detached channels on delete

* feat(saves): device sync from the token, held snapshots and shared-card pushes

* feat(saves): arc restores past abandoned snapshots and color channels by age

* fix(saves): code-review and security-audit fixes

* refactor(saves): simplify pass

* chore(saves): review-polish pass

* docs(saves): humanizer pass

* fix(sync): file RetroArch autosave versions as their channel's next snapshot

* fix(saves): address review findings and trim docstrings

* fix(saves): share-lock planned rows by id so PostgreSQL accepts the lock

* test(saves): stub the detached-channels fetch in SaveDataTab stories

* feat(saves): carry the full current snapshot in a 409, pin sigil 2071dff

* chore(sigil): pin 5d4ddb4, which reads .cue and .gdi sheets

* docs(saves): drop v2 wording now that one UI remains

* chore(sigil): pin 0190443, which reads PS2 raw BIN images and cue sheets

* fix(sigil): treat a file too short for its format as routine during scans

* chore(migrations): renumber save channel migrations after upstream's 0154

* fix(snapshots): a hardcore switch on unchanged content makes a new current

* fix(sync): decide a channel's slot from its history and the device's snapshot

* chore(docker): pin sigil at 794d99f for PSP Store release save prefixes

* refactor(db): squash the save channel migrations into 0161

Fold the slot linking (0162) and the rom_files sigil identity columns (0163)
into 0161_save_channels, so the feature ships as one revision.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(saves): close visibility and input gaps found in review (pr-ready step 2)

code-review --fix:
- negotiate_sync drops channel slots on ROMs hidden from the caller
- channel PATCH/attach/DELETE answer 404 when the channel's ROM is hidden
- a push copying a save from a hidden ROM answers 404
- _own_device applies require_token_device to bound client tokens
- adopting a legacy save syncs its sharing with the channel it joins

security-audit follow-ups:
- another user's detached public save or state is no longer downloadable
- profile and option values must be a single path segment
- companions are de-duplicated and capped at 8

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* refactor(saves): share channel and asset lookups, drop duplicates (pr-ready step 3)

simplify:
- readable_channel_or_404 and channels_on_files replace four copies of the
  channel 404 check and two copies of the file-fed channel listing
- readable_rom_asset_or_404 serves both public save and state downloads
- _own_device resolves through require_device; _clone_origin takes the source
- reuse _unprocessable, DESCRIPTOR_EXTENSIONS, SAVE_SLOT_MAX_LENGTH, uniqueBy
  and uuidv7; neutral forms default their required pattern to the allowed one
- RetroArch delete checks held rows in one query, a guarded upload lists the
  slot only without a channel head, and uploads are read off the event loop
- snapshotSession builds push requests once; player comment cleanups

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(saves): trim two docstrings to two lines (pr-ready step 4)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(saves): answer held RetroArch paths with 423, mirror slot labels

- RetroArch Cloud Sync answers 423 Locked for a PUT over a held unslotted save
  and for a DELETE where only held rows sit at the path, instead of 409 and 404
- channelLabelForSlot returns null for blank and state_ slots, as the backend's
  channel_label does, so the player files them under no channel either
- document that sharing a channel lets every user read and push into it

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* chore(docker): pin sigil at 85f88a8 for Switch .nup containers

* fix(snapshots): a save pushed in another shape or format makes a new current

* refactor(saves): drop the always-true SAVE_SYNC.SNAPSHOTS heartbeat flag

Nothing reads it, and the SAVE_SYNC block's presence already tells a
client the server speaks the snapshot API. Also trim the rom detail
channel field comments to one line each.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* refactor(frontend): use uuidv7 for every client-generated id

Drop the randomToken wrapper and the notification inbox's Math.random tab
id, so uuidv7 is the one helper that works outside a secure context.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* refactor(saves): trim redundant snapshot API surface, dead code and duplicate logic

API:
- Drop PUT /snapshots/{id}/devices/{device_id}; GET /snapshots/{id}?hold=true holds.
- GET /snapshots is history only (channel_id required); GET /channels serves currents.
- A snapshot carries channel_id instead of an embedded channel.
- POST /saves/{id}/content takes the same query parameters as the GET, plus container_path.

Backend:
- Remove the archival write path, clone.py, DBStatesHandler.get_state, the sigil
  identity-hash path, unused relationships and composite snapshot indexes.
- One held/frozen lookup, one bulk-delete guard, SQLAlchemy dialect upserts, and
  the shared reserve_version_name in the writer.
- The writer takes a visibility filter and checks the channel and parent itself.
- The restore handler picks per-game or merge; fewer wrapper types and tables.

Frontend:
- Channel delete uses the shared typed confirm; DeleteChannelDialog is gone.
- Held offline pushes go through a per-channel SnapshotSession.
- Remove the slot-based pending upload path.
- The timeline shares the fan's badge logic.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

* refactor(saves): release archival backups through module functions

The saves and states handlers each built a private DBSnapshotsHandler to
call release_backup and refresh_backup_digests, since they can't import
the shared instance. Both only ever ran inside the caller's session, so
they are now module functions taking it, like channel_for_slot.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

* refactor(saves): share one deleted-assets handler instance

The saves handler built its own DBDeletedAssetsHandler because it can't
import the package's instance. The instance is now built in
deleted_assets_handler.py and re-exported by the package, so both use it.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

---------

Co-authored-by: Georges-Antoine Assi <contact@me.gantoine.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 21:59:19 -05:00

561 lines
20 KiB
Python
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from unittest.mock import MagicMock
import pytest
from models.rom import RomFileCategory
from utils.m3u import (
disc_files,
disc_number,
first_playlist_entry,
generate_m3u_content,
listing_playlist,
needs_generated_playlist,
playlist_discs,
playlist_files,
)
class TestListingPlaylist:
def test_names_the_playlist_listing_the_disc(self, tmp_path):
disc = tmp_path / "Game (Disc 1).chd"
disc.write_bytes(b"x")
(tmp_path / "Game.m3u").write_text("Game (Disc 1).chd\nGame (Disc 2).chd\n")
assert listing_playlist(disc) == "Game.m3u"
def test_matches_backslashed_entries_ignoring_case(self, tmp_path):
disc = tmp_path / "Game (Disc 1).chd"
disc.write_bytes(b"x")
(tmp_path / "GAME.M3U").write_bytes(b"\xef\xbb\xbf.\\GAME (DISC 1).CHD\r\n")
assert listing_playlist(disc) == "GAME.M3U"
def test_none_when_no_playlist_lists_the_disc(self, tmp_path):
disc = tmp_path / "Game (Disc 1).chd"
disc.write_bytes(b"x")
(tmp_path / "Other.m3u").write_text("Other (Disc 1).chd\n")
(tmp_path / "notes.txt").write_text("Game (Disc 1).chd\n")
assert listing_playlist(disc) is None
def test_none_when_the_playlist_lists_a_namesake_in_another_folder(self, tmp_path):
disc = tmp_path / "Game (Disc 1).chd"
disc.write_bytes(b"x")
(tmp_path / "Other.m3u").write_text(
"Other Set/Game (Disc 1).chd\nOther Set\\Game (Disc 1).chd\n"
)
assert listing_playlist(disc) is None
def test_matches_a_literal_backslash_in_the_file_name(self, tmp_path):
disc = tmp_path / "Game\\Disc 1.chd"
disc.write_bytes(b"x")
(tmp_path / "Game.m3u").write_text("Game\\Disc 1.chd\n")
assert listing_playlist(disc) == "Game.m3u"
def test_matches_an_entry_through_a_symlinked_folder(self, tmp_path):
disc = tmp_path / "Game (Disc 1).chd"
disc.write_bytes(b"x")
(tmp_path / "link").symlink_to(tmp_path)
(tmp_path / "Game.m3u").write_text("link/Game (Disc 1).chd\n")
assert listing_playlist(disc) == "Game.m3u"
def test_none_when_the_literal_backslash_file_is_the_one_listed(self, tmp_path):
(tmp_path / ".\\Disc 1.chd").write_bytes(b"x")
disc = tmp_path / "Disc 1.chd"
disc.write_bytes(b"x")
(tmp_path / "Game.m3u").write_text(".\\Disc 1.chd\n")
assert listing_playlist(disc) is None
def test_none_when_the_playlist_names_a_symlinked_discs_target(self, tmp_path):
target = tmp_path / "store" / "Game (Disc 1).chd"
target.parent.mkdir()
target.write_bytes(b"x")
disc = tmp_path / "Game (Disc 1).chd"
disc.symlink_to(target)
(tmp_path / "Game.m3u").write_text("store/Game (Disc 1).chd\n")
assert listing_playlist(disc) is None
def test_names_the_playlist_listing_a_symlink_to_the_disc(self, tmp_path):
disc = tmp_path / "Game (Disc 1).chd"
disc.write_bytes(b"x")
(tmp_path / "links").mkdir()
(tmp_path / "links" / "hop.chd").symlink_to(disc)
(tmp_path / "Disc 1.chd").symlink_to("links/hop.chd")
(tmp_path / "Game.m3u").write_text("Disc 1.chd\n")
assert listing_playlist(disc) == "Game.m3u"
def test_none_when_the_playlist_lists_a_hardlink_to_the_disc(self, tmp_path):
disc = tmp_path / "Game (Disc 1).chd"
disc.write_bytes(b"x")
(tmp_path / "Disc 1.chd").hardlink_to(disc)
(tmp_path / "Game.m3u").write_text("Disc 1.chd\n")
assert listing_playlist(disc) is None
def test_tolerates_an_entry_through_a_symlink_loop(self, tmp_path):
disc = tmp_path / "Game (Disc 1).chd"
disc.write_bytes(b"x")
(tmp_path / "a").symlink_to(tmp_path / "b")
(tmp_path / "b").symlink_to(tmp_path / "a")
(tmp_path / "Other.m3u").write_text("a/Other.chd\n")
assert listing_playlist(disc) is None
def test_ignores_comment_lines(self, tmp_path):
disc = tmp_path / "Game (Disc 1).chd"
disc.write_bytes(b"x")
(tmp_path / "Game.m3u").write_text("#Game (Disc 1).chd\n")
assert listing_playlist(disc) is None
def test_none_for_a_namesake_differing_only_in_case(self, tmp_path):
listed = tmp_path / "Game.chd"
listed.write_bytes(b"x")
namesake = tmp_path / "game.chd"
namesake.write_bytes(b"y")
if listed.samefile(namesake):
pytest.skip("case-insensitive filesystem")
(tmp_path / "Game.m3u").write_text("Game.chd\n")
assert listing_playlist(namesake) is None
assert listing_playlist(listed) == "Game.m3u"
def test_none_when_the_folder_is_missing(self, tmp_path):
assert listing_playlist(tmp_path / "gone" / "disc.chd") is None
class TestFirstPlaylistEntry:
def test_returns_first_entry_relative_to_playlist_folder(self, tmp_path):
disc1 = tmp_path / "game (Disc 1).rvz"
disc1.write_bytes(b"x" * 100)
(tmp_path / "game (Disc 2).rvz").write_bytes(b"x" * 100)
m3u = tmp_path / "game.m3u"
m3u.write_text("game (Disc 1).rvz\ngame (Disc 2).rvz\n")
assert first_playlist_entry(m3u) == disc1
def test_skips_comments_and_blank_lines(self, tmp_path):
disc = tmp_path / "disc.cue"
disc.write_bytes(b"x" * 10)
m3u = tmp_path / "game.m3u"
m3u.write_text("#EXTM3U\n\n# a comment\ndisc.cue\n")
assert first_playlist_entry(m3u) == disc
def test_handles_utf8_bom_and_crlf(self, tmp_path):
disc = tmp_path / "disc.rvz"
disc.write_bytes(b"x" * 10)
m3u = tmp_path / "game.m3u"
m3u.write_bytes(b"\xef\xbb\xbfdisc.rvz\r\n")
assert first_playlist_entry(m3u) == disc
def test_resolves_windows_separators(self, tmp_path):
disc = tmp_path / "Multi Disc" / "D2 (USA) (Disc 1).chd"
disc.parent.mkdir()
disc.write_bytes(b"x" * 10)
m3u = tmp_path / "D2 (USA) (Disc 1).m3u"
m3u.write_text("Multi Disc\\D2 (USA) (Disc 1).chd\r\n")
assert first_playlist_entry(m3u) == disc
def test_prefers_a_literal_backslash_in_the_file_name(self, tmp_path):
literal = tmp_path / "Game\\Disc 1.chd"
literal.write_bytes(b"x" * 10)
nested = tmp_path / "Game" / "Disc 1.chd"
nested.parent.mkdir()
nested.write_bytes(b"x" * 10)
m3u = tmp_path / "game.m3u"
m3u.write_text("Game\\Disc 1.chd\n")
assert first_playlist_entry(m3u) == literal
def test_resolves_absolute_entry_as_is(self, tmp_path):
disc = tmp_path / "elsewhere" / "disc.rvz"
disc.parent.mkdir()
disc.write_bytes(b"x" * 10)
m3u = tmp_path / "game.m3u"
m3u.write_text(f"{disc}\n")
assert first_playlist_entry(m3u) == disc
def test_returns_none_when_first_entry_missing(self, tmp_path):
(tmp_path / "game (Disc 2).rvz").write_bytes(b"x" * 10)
m3u = tmp_path / "game.m3u"
m3u.write_text("game (Disc 1).rvz\ngame (Disc 2).rvz\n")
assert first_playlist_entry(m3u) is None
def test_returns_none_for_unreadable_playlist(self, tmp_path):
assert first_playlist_entry(tmp_path / "missing.m3u") is None
def test_returns_none_for_empty_playlist(self, tmp_path):
m3u = tmp_path / "game.m3u"
m3u.write_text("#EXTM3U\n\n")
assert first_playlist_entry(m3u) is None
class TestPlaylistDiscs:
def test_lists_each_disc_on_disk_in_playlist_order(self, tmp_path):
disc2 = tmp_path / "Game (Disc 2).chd"
disc2.write_bytes(b"x")
disc1 = tmp_path / "Multi" / "Game (Disc 1).chd"
disc1.parent.mkdir()
disc1.write_bytes(b"x")
m3u = tmp_path / "Game.m3u"
m3u.write_text(
"#EXTM3U\nGame (Disc 2).chd\nMulti\\Game (Disc 1).chd\nGame (Disc 3).chd\n"
)
assert playlist_discs(m3u) == [disc2, disc1]
def test_an_unreadable_playlist_lists_nothing(self, tmp_path):
assert playlist_discs(tmp_path / "missing.m3u") == []
def _make_file(
name: str,
extension: str,
download_name: str | None = None,
category: RomFileCategory | None = None,
):
f = MagicMock()
f.file_extension = extension
f.file_name = name
f.category = category
f.file_name_for_download.return_value = download_name or name
return f
class TestBaseGamePlaylist:
disc1 = _make_file("Game (Disc 1).chd", "chd")
disc2 = _make_file("Game (Disc 2).chd", "chd")
playlist = _make_file("Game.m3u", "m3u")
t_disc1 = _make_file(
"Game (Disc 1) (T-Es).chd", "chd", category=RomFileCategory.TRANSLATION
)
t_playlist = _make_file(
"Game (T-Es).m3u", "m3u", category=RomFileCategory.TRANSLATION
)
patch = _make_file("Game (T-Es).ups", "ups", category=RomFileCategory.TRANSLATION)
manual = _make_file("Manual.pdf", "pdf", category=RomFileCategory.MANUAL)
def test_playlist_lists_the_base_discs_only(self):
files = [self.t_disc1, self.patch, self.manual, self.disc1, self.disc2]
assert playlist_files(files) == [self.disc1, self.disc2]
def test_a_variant_disc_still_boots_on_its_own(self):
files = [self.t_disc1, self.patch, self.manual, self.disc1]
assert set(disc_files(files)) == {self.disc1, self.t_disc1}
def test_a_variant_set_alone_gets_its_own_playlist(self):
assert playlist_files([self.t_disc1, self.patch]) == [self.t_disc1]
def test_a_variant_playlist_does_not_cover_the_base_discs(self):
files = [self.disc1, self.disc2, self.t_disc1, self.t_playlist]
assert needs_generated_playlist(files)
def test_the_base_playlist_is_not_doubled(self):
assert not needs_generated_playlist([self.disc1, self.disc2, self.playlist])
def test_media_alone_gets_no_playlist(self):
guide = _make_file("Guide.pdf", "pdf", category=RomFileCategory.MANUAL)
assert not needs_generated_playlist([self.manual, guide])
def test_a_patch_at_the_root_is_never_a_disc(self):
root_patch = _make_file("Game.ips", "ips")
assert playlist_files([root_patch, self.disc1]) == [self.disc1]
class TestGenerateM3uContent:
def test_single_file(self):
files = [_make_file("game.bin", "bin")]
result = generate_m3u_content(files, hidden_folder=False)
assert result == b"game.bin"
files[0].file_name_for_download.assert_called_once_with(False)
def test_multiple_files(self):
files = [
_make_file("disc1.chd", "chd"),
_make_file("disc2.chd", "chd"),
_make_file("disc3.chd", "chd"),
]
result = generate_m3u_content(files, hidden_folder=False)
assert result == b"disc1.chd\ndisc2.chd\ndisc3.chd"
def test_cue_files_preferred_over_bin(self):
files = [
_make_file("track01.bin", "bin"),
_make_file("track02.bin", "bin"),
_make_file("game.cue", "cue"),
]
result = generate_m3u_content(files, hidden_folder=False)
assert result == b"game.cue"
def test_cue_case_insensitive(self):
files = [
_make_file("track.bin", "bin"),
_make_file("game.CUE", "CUE", download_name="game.CUE"),
]
result = generate_m3u_content(files, hidden_folder=False)
assert result == b"game.CUE"
def test_hidden_folder_passed_through(self):
files = [_make_file("game.chd", "chd", download_name=".hidden/game.chd")]
result = generate_m3u_content(files, hidden_folder=True)
assert result == b".hidden/game.chd"
files[0].file_name_for_download.assert_called_once_with(True)
def test_no_descriptor_files_lists_all(self):
files = [
_make_file("disc1.chd", "chd"),
_make_file("disc2.chd", "chd"),
]
result = generate_m3u_content(files, hidden_folder=False)
assert result == b"disc1.chd\ndisc2.chd"
def test_gdi_preferred_over_raw_tracks(self):
files = [
_make_file("track01.bin", "bin"),
_make_file("track02.raw", "raw"),
_make_file("game.gdi", "gdi"),
]
result = generate_m3u_content(files, hidden_folder=False)
assert result == b"game.gdi"
def test_ccd_preferred_over_img_and_sub(self):
files = [
_make_file("game.img", "img"),
_make_file("game.sub", "sub"),
_make_file("game.ccd", "ccd"),
]
result = generate_m3u_content(files, hidden_folder=False)
assert result == b"game.ccd"
def test_mds_preferred_over_mdf(self):
files = [
_make_file("game.mdf", "mdf"),
_make_file("game.mds", "mds"),
]
result = generate_m3u_content(files, hidden_folder=False)
assert result == b"game.mds"
def test_descriptor_case_insensitive(self):
files = [
_make_file("track01.bin", "bin"),
_make_file("game.GDI", "GDI", download_name="game.GDI"),
]
result = generate_m3u_content(files, hidden_folder=False)
assert result == b"game.GDI"
def test_every_disc_descriptor_listed(self):
files = [
_make_file("disc1/track01.bin", "bin"),
_make_file("disc1.gdi", "gdi"),
_make_file("disc2/track01.bin", "bin"),
_make_file("disc2.gdi", "gdi"),
]
result = generate_m3u_content(files, hidden_folder=False)
assert result == b"disc1.gdi\ndisc2.gdi"
def test_standalone_disc_kept_beside_a_descriptor(self):
# A set is not required to be all one format. The .gdi's tracks are
# data, but the .chd is a disc that boots on its own.
files = [
_make_file("disc1.gdi", "gdi"),
_make_file("track01.bin", "bin"),
_make_file("disc2.chd", "chd"),
]
result = generate_m3u_content(files, hidden_folder=False)
assert result == b"disc1.gdi\ndisc2.chd"
def test_standalone_disc_kept_beside_a_cue(self):
files = [
_make_file("disc1.cue", "cue"),
_make_file("disc1.bin", "bin"),
_make_file("disc2.chd", "chd"),
]
result = generate_m3u_content(files, hidden_folder=False)
assert result == b"disc1.cue\ndisc2.chd"
def test_audio_tracks_are_not_discs(self):
# A cue's CDDA tracks are data the sheet names, not discs beside it.
files = [
_make_file("game.cue", "cue"),
_make_file("track01.bin", "bin"),
_make_file("track02.wav", "wav"),
]
result = generate_m3u_content(files, hidden_folder=False)
assert result == b"game.cue"
class TestPlaylistFiles:
"""The disc swapper reads this directly, so the files themselves matter."""
def test_m3u_is_never_a_disc(self):
m3u = _make_file("game.m3u", "m3u")
disc = _make_file("game.chd", "chd")
assert playlist_files([m3u, disc]) == [disc]
def test_raw_tracks_are_not_discs(self):
track = _make_file("track01.bin", "bin")
gdi = _make_file("game.gdi", "gdi")
assert playlist_files([track, gdi]) == [gdi]
def test_bare_files_are_all_discs(self):
discs = [_make_file("disc1.chd", "chd"), _make_file("disc2.chd", "chd")]
assert playlist_files(discs) == discs
def test_standalone_disc_survives_a_descriptor(self):
gdi = _make_file("disc1.gdi", "gdi")
track = _make_file("track01.bin", "bin")
chd = _make_file("disc2.chd", "chd")
assert playlist_files([gdi, track, chd]) == [gdi, chd]
def test_bare_tracks_without_a_descriptor_are_discs(self):
# Nothing describes them, so they are all there is to play.
tracks = [_make_file("track01.bin", "bin"), _make_file("track02.bin", "bin")]
assert playlist_files(tracks) == tracks
class TestDiscNumber:
def test_reads_the_spellings_a_dumper_uses(self):
for name in (
"G (Disc 2).chd",
"G (disc2).chd",
"G (CD 2).chd",
"G (Disque 2).chd",
):
assert disc_number(_make_file(name, "chd")) == 2
def test_reads_the_tosec_of_form(self):
assert disc_number(_make_file("G (Disk 1 of 2).adf", "adf")) == 1
assert disc_number(_make_file("G (Disk 10 of 12).adf", "adf")) == 10
def test_reads_a_number_of_any_length(self):
# An unparsed number would sort as 0, ahead of disc 1.
assert disc_number(_make_file("G (Disc 100).chd", "chd")) == 100
def test_a_disc_count_is_not_a_disc_number(self):
# "(2 CD)" says how many the release had, not which one this is.
assert disc_number(_make_file("G (2 CD).pbp", "pbp")) is None
def test_reads_a_split_disc(self):
assert disc_number(_make_file("G (Disc 2A).chd", "chd")) == 2
def test_reads_a_lettered_disc(self):
assert disc_number(_make_file("G (Disc A).chd", "chd")) == 1
assert disc_number(_make_file("G (disc c).chd", "chd")) == 3
assert disc_number(_make_file("G (Disk B of 2).adf", "adf")) == 2
def test_a_letter_needs_a_space_to_be_a_disc(self):
# "(CDi)" names the platform, not disc I.
assert disc_number(_make_file("G (CDi).chd", "chd")) is None
def test_a_non_ascii_letter_is_not_a_disc(self):
# Case folding would let "İ" and "ſ" through as i and s.
assert disc_number(_make_file("G (Disc İ).chd", "chd")) is None
assert disc_number(_make_file("G (Disc ſ).chd", "chd")) is None
def test_a_non_breaking_space_still_separates_the_tag(self):
assert disc_number(_make_file("G (Disc 2).chd", "chd")) == 2
assert disc_number(_make_file("G (Disc B).chd", "chd")) == 2
def test_a_name_that_claims_no_disc(self):
assert disc_number(_make_file("G (USA).chd", "chd")) is None
def test_a_bare_word_is_not_a_disc_number(self):
# The parentheses are what make it a tag rather than part of a title.
assert disc_number(_make_file("Disc Jockey 2.chd", "chd")) is None
class TestPlaylistOrder:
"""The playlist, the download and the disc swapper all read this order."""
def test_the_tenth_disc_follows_the_second(self):
files = [
_make_file("G (Disc 10).chd", "chd"),
_make_file("G (Disc 2).chd", "chd"),
_make_file("G (Disc 1).chd", "chd"),
]
assert [f.file_name for f in playlist_files(files)] == [
"G (Disc 1).chd",
"G (Disc 2).chd",
"G (Disc 10).chd",
]
def test_a_set_that_mixes_spellings_still_orders(self):
files = [
_make_file("G (CD 2).chd", "chd"),
_make_file("G (Disc 1).chd", "chd"),
]
assert [f.file_name for f in playlist_files(files)] == [
"G (Disc 1).chd",
"G (CD 2).chd",
]
def test_a_tosec_disk_set_orders_past_nine(self):
files = [
_make_file("G (Disk 10 of 12).adf", "adf"),
_make_file("G (Disk 2 of 12).adf", "adf"),
]
assert [f.file_name for f in playlist_files(files)] == [
"G (Disk 2 of 12).adf",
"G (Disk 10 of 12).adf",
]
def test_an_unnumbered_disc_follows_the_numbered_ones(self):
"""The first entry is what boots, so a bonus disc cannot take it."""
files = [
_make_file("Bonus Disc.chd", "chd"),
_make_file("G (Disc 2).chd", "chd"),
_make_file("G (Disc 1).chd", "chd"),
]
assert [f.file_name for f in playlist_files(files)] == [
"G (Disc 1).chd",
"G (Disc 2).chd",
"Bonus Disc.chd",
]
def test_a_lettered_set_boots_from_disc_a(self):
files = [
_make_file("A Making Of.chd", "chd"),
_make_file("G (disc b).chd", "chd"),
_make_file("G (Disc A).chd", "chd"),
]
assert [f.file_name for f in playlist_files(files)] == [
"G (Disc A).chd",
"G (disc b).chd",
"A Making Of.chd",
]
def test_an_unnumbered_set_keeps_its_name_order(self):
files = [
_make_file("beta.chd", "chd"),
_make_file("alpha.chd", "chd"),
]
assert [f.file_name for f in playlist_files(files)] == ["alpha.chd", "beta.chd"]
def test_the_playlist_lists_the_discs_in_order(self):
files = [
_make_file("G (Disc 10).chd", "chd"),
_make_file("G (Disc 2).chd", "chd"),
]
assert generate_m3u_content(files, hidden_folder=False) == (
b"G (Disc 2).chd\nG (Disc 10).chd"
)