import pytest
from bot.db import DEFAULT_CAPTION, Database
from bot.formats import FormatSettings
from bot.parser import DEFAULT_TEMPLATE
from bot.settings_ops import META_COMMANDS, META_KINDS, apply_setting, parse_channel, preview_template
UID = 7
@pytest.fixture
async def db(tmp_path):
d = Database(f"sqlite+aiosqlite:///{tmp_path}/s.db")
await d.init()
yield d
await d.close()
async def get(db):
return await db.get_settings(UID)
async def auto(db):
"""The auto format in use: the template and the caption are saved there, not in the user's settings."""
return await db.get_auto_format(UID)
@pytest.fixture
async def store(db):
"""Where the template and the caption are saved: an auto format (they live in formats, not in the user's settings)."""
fmt = await db.create_format(UID, "Default", kind="auto")
return FormatSettings(db, fmt["id"])
async def test_template_saved_with_preview(db, store):
ok, msg = await apply_setting(store, UID, "template", "[S{season}E{episode}] {title} [{quality}]")
assert ok and "Template saved" in msg and "Preview" in msg
assert (await auto(db))["template"] == "[S{season}E{episode}] {title} [{quality}]"
assert "template" not in await get(db) # the user's own settings don't hold one
async def test_template_validation_and_reset(db, store):
assert (await apply_setting(store, UID, "template", " "))[0] is False
ok, msg = await apply_setting(store, UID, "template", "x" * 201)
assert not ok and "too long" in msg
assert (await auto(db))["template"] == DEFAULT_TEMPLATE # nothing was saved
await apply_setting(store, UID, "template", "{title}")
assert (await auto(db))["template"] == "{title}"
ok, _ = await apply_setting(store, UID, "template", "reset")
assert ok and (await auto(db))["template"] == DEFAULT_TEMPLATE
async def test_template_unknown_variables_are_reported_but_saved(db, store):
ok, msg = await apply_setting(store, UID, "template", "{title} {nope}")
assert ok and "{nope}" in msg and "Unknown variables" in msg
assert (await auto(db))["template"] == "{title} {nope}"
async def test_the_padded_episode_variables_are_known_ones(db, store):
ok, msg = await apply_setting(store, UID, "template", "{title} E{1episode} {2episode} {3episode}")
assert ok and "Unknown" not in msg
assert "Tokyo Revengers E05 005 0005" in msg # the preview shows the padding
ok, msg = await apply_setting(store, UID, "caption", "Episode {2episode} of {title}")
assert ok and "Unknown" not in msg
async def test_caption_set_validate_reset(db, store):
ok, msg = await apply_setting(store, UID, "caption", "{filename} · {size}")
assert ok and (await auto(db))["caption"] == "{filename} · {size}"
assert "Unknown" not in msg
assert (await apply_setting(store, UID, "caption", "x" * 801))[0] is False
assert (await apply_setting(store, UID, "caption", ""))[0] is False
ok, msg = await apply_setting(store, UID, "caption", "{bogus}")
assert ok and "{bogus}" in msg
ok, _ = await apply_setting(store, UID, "caption", "reset")
assert ok and (await auto(db))["caption"] == DEFAULT_CAPTION == "{filename}"
async def test_remove_words(db):
ok, msg = await apply_setting(db, UID, "remove", "@Chan, [Group]\n www.site.com ,, ")
assert ok
assert (await get(db))["remove_words"] == ["@Chan", "[Group]", "www.site.com"]
assert (await apply_setting(db, UID, "remove", " , ,"))[0] is False
assert (await apply_setting(db, UID, "remove", ", ".join(f"w{i}" for i in range(51))))[0] is False
assert (await apply_setting(db, UID, "remove", "x" * 61))[0] is False
ok, _ = await apply_setting(db, UID, "remove", "off")
assert ok and (await get(db))["remove_words"] == []
@pytest.mark.parametrize("kind", sorted(META_KINDS))
async def test_every_metadata_field_can_be_set_and_cleared(db, kind):
field = META_KINDS[kind]
ok, msg = await apply_setting(db, UID, kind, f"{field} value")
assert ok and (await get(db))["meta"][field] == f"{field} value"
assert "Metadata is Off" in msg # reminder that nothing applies until it is switched on
await db.update_settings(UID, metadata_on=True)
ok, msg = await apply_setting(db, UID, kind, "another")
assert ok and "Metadata is Off" not in msg
ok, _ = await apply_setting(db, UID, kind, "off")
assert ok and (await get(db))["meta"][field] == ""
async def test_metadata_value_validation(db):
assert (await apply_setting(db, UID, "mt_title", ""))[0] is False
ok, msg = await apply_setting(db, UID, "mt_title", "x" * 201)
assert not ok and "too long" in msg
assert (await get(db))["meta"]["title"] == ""
async def test_meta_commands_map_to_known_kinds():
assert set(META_COMMANDS.values()) == set(META_KINDS)
async def test_metadata_values_are_html_escaped_in_the_reply(db):
ok, msg = await apply_setting(db, UID, "mt_title", "