Files
shelfmark/tests/core/test_format_defaults.py
T
splitsec2 8e425da527 refactor: one default and one reader for the Supported Book Formats list (#1532)
The default for Supported Book Formats (`epub, mobi, azw3, fb2, djvu,
cbz, cbr`) was written out in five places: the settings registry,
`postprocess/policy.py`, `irc/parser.py`, Prowlarr's `source.py` (added
in #1490) and slskd's `source.py` (added in #1428). Both of those two
arrived in 1.5.0, and both are mine, so this is cleaning up after myself
as much as anything.

The setting was also read three different ways. Post-processing split on
commas only. slskd split on commas and spaces and stripped a leading
dot. IRC split on commas and kept the dot. Prowlarr split on commas and
kept `""` as a format when the value was an empty string. So `.epub` or
`epub mobi` worked in one source and not another.

This puts `DEFAULT_EBOOK_FORMATS` and `normalize_format_list` in
`core/utils.py` next to `AUDIOBOOK_FORMATS`, and the registry,
post-processing, IRC, Prowlarr and slskd all use them. I took the most
lenient reading of the three (comma or space split, leading dot dropped,
empties ignored, default when nothing is left), so no existing config
gets stricter. I put the reader in `core/utils.py` and not in
`policy.py` because the release sources can't import `policy` without
pulling in the whole post-processing package.

A config with a sensible list reads exactly as before. The only
differences are for malformed values: an emptied setting or an empty
string now falls back to the default in every place, where IRC used to
filter everything out and Prowlarr treated `""` as a format.

The audiobook default is a separate story and I left it alone: the
registry and slskd default to every processable audiobook format plus
the archives, while post-processing and IRC fall back to `m4b, mp3`.
They only differ when the setting is unset or emptied, but they do
differ, and I'd rather ask which one you want than pick one here.

Ruff, basedpyright on the changed files and the backend suite pass (4400
passed, e2e not run). New tests cover the reader and the two fallbacks.
2026-10-22 15:32:00 -04:00

47 lines
1.8 KiB
Python

"""One default for the Supported Book Formats setting, read the same way everywhere."""
import pytest
from shelfmark.config import settings as app_settings
from shelfmark.core.utils import DEFAULT_EBOOK_FORMATS, normalize_format_list
from shelfmark.download.postprocess import policy
DEFAULT = ["epub", "mobi"]
class TestNormalizeFormatList:
def test_a_string_splits_on_commas_and_spaces_and_drops_leading_dots(self):
assert normalize_format_list("epub, .MOBI azw3", DEFAULT) == ["epub", "mobi", "azw3"]
def test_a_list_is_lowercased_and_stripped(self):
assert normalize_format_list([" EPUB ", ".mobi", "", " "], DEFAULT) == ["epub", "mobi"]
@pytest.mark.parametrize("value", ["", " , ", [], [""], (), set()])
def test_nothing_left_falls_back_to_the_default(self, value):
assert normalize_format_list(value, DEFAULT) == DEFAULT
@pytest.mark.parametrize("value", [None, 3, 1.5, {"epub": 1}, object()])
def test_a_value_of_the_wrong_kind_falls_back_to_the_default(self, value):
assert normalize_format_list(value, DEFAULT) == DEFAULT
def test_the_default_is_returned_as_a_new_list(self):
assert normalize_format_list(None, DEFAULT_EBOOK_FORMATS) == list(DEFAULT_EBOOK_FORMATS)
def test_the_registry_default_is_the_shared_default():
field = next(
f for f in app_settings.general_settings() if getattr(f, "key", "") == "SUPPORTED_FORMATS"
)
assert field.default == list(DEFAULT_EBOOK_FORMATS)
def test_post_processing_falls_back_to_the_shared_default(monkeypatch):
class _Config:
def get(self, key, default=None, user_id=None):
return default
monkeypatch.setattr("shelfmark.core.config.config", _Config())
assert policy.get_supported_formats() == list(DEFAULT_EBOOK_FORMATS)