Files
LihonT/tests/test_profile_music_server.py
cheb0chik 3540ac84ea LihonT 2.3.0 (Serena): profile music, AI video dubbing, PiP & bg play, fonts, cleaner
- Shared moderated MP3 profile music (with floating mini-player and MediaSession controls, zero bubble when absent)
- AI video dubbing into Russian with burned-in ASS subtitles inside AgentChat (`services/members/dub.py`)
- Picture-in-Picture 9:16 and background audio playback (`PlayControl` + `play_patch.py`)
- System/custom .ttf fonts & emoji packs (`FontsAndEmoji`), account cleaner (`AccountCleaner`)
- Feed blacklist, muted sound restoration, OLED burn-in shift, A/B flag editor, presets, long comments
- Updated site homepage (`src/web/index.html`) for LihonT 2.3.0 Serena
2026-11-26 15:40:32 +03:00

117 lines
4.5 KiB
Python

import base64
import hashlib
import importlib.util
import sys
import tempfile
import unittest
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
MEMBERS_DIR = ROOT / "services" / "members"
if str(MEMBERS_DIR) not in sys.path:
sys.path.insert(0, str(MEMBERS_DIR))
SPEC = importlib.util.spec_from_file_location("members_server", MEMBERS_DIR / "server.py")
try:
server = importlib.util.module_from_spec(SPEC)
SPEC.loader.exec_module(server)
except ModuleNotFoundError:
server = None
MODBOT_PATH = MEMBERS_DIR / "modbot.py"
modbot = None
if MODBOT_PATH.is_file():
MODBOT_SPEC = importlib.util.spec_from_file_location("members_modbot", MODBOT_PATH)
modbot = importlib.util.module_from_spec(MODBOT_SPEC)
MODBOT_SPEC.loader.exec_module(modbot)
UID = "6812345678901234"
UID2 = "6812345678909999"
INST = "a" * 64
class FakeTelegram:
def __init__(self):
self.calls = []
self.updates = []
def call(self, method, fields=None, files=None, timeout=40):
self.calls.append((method, fields or {}, files or {}))
if method == "getUpdates":
u = list(self.updates)
self.updates.clear()
return u
return {"message_id": 77}
@unittest.skipIf(server is None, "private moderation module excluded")
class ProfileMusicServerTest(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.addCleanup(self.tmp.cleanup)
self.now = 1_790_000_000
self.bio = ""
self.registry = server.Registry(
Path(self.tmp.name) / "r.sqlite3",
clock=lambda: self.now,
fetcher=lambda uid_name: (UID, self.bio),
)
# Stub normalize_mp3 so test runs without needing ffmpeg binary.
self.registry.music.normalize = lambda raw: (b"ID3test-mp3-data-" + raw, 18)
def _prove(self):
digest = hashlib.sha256(INST.encode("ascii")).hexdigest()
self.bio = "bio " + self.registry.code(UID, digest)
def test_upload_moderation_lookup_and_clear(self):
# Empty lookup before any upload -> track is None.
self.assertIsNone(self.registry.music.lookup(unique_id="alice")["track"])
# First upload asks for bio proof.
mp3_b64 = base64.b64encode(b"\xff\xfb\x90\x00" + b"0" * 300).decode("ascii")
first = self.registry.music.upload(UID, INST, "Alice", "Midnight", "DJ Alice", mp3_b64)
self.assertFalse(first["published"])
self.assertTrue(first["code"].startswith("LT-"))
# Prove account and upload.
self._prove()
res = self.registry.music.upload(UID, INST, "@Alice", "Midnight", "DJ Alice", mp3_b64)
self.assertTrue(res["published"])
tid = res["track"]["id"]
self.assertEqual("new", res["track"]["status"])
# While pending moderation, visitors still get track: None!
self.assertIsNone(self.registry.music.lookup(unique_id="alice")["track"])
# Moderator bot picks it up via sendAudio and owner clicks Accept (when private modbot is present).
if modbot is not None:
tg = FakeTelegram()
bot = modbot.Moderator(self.registry, tg, owner_id=12345)
bot.sweep()
send_audio = [c for c in tg.calls if c[0] == "sendAudio"]
self.assertEqual(1, len(send_audio))
self.assertIn("Midnight", send_audio[0][1]["title"])
bot.handle({"callback_query": {"id": "cb1", "from": {"id": 12345}, "data": "ma:" + tid, "message": {"message_id": 77}}})
else:
self.registry.music.decide(tid, "approved", "owner")
# Approved + active: lookup by handle (mixed case / @) and by uid both return track.
got = self.registry.music.lookup(unique_id="@ALICE")["track"]
self.assertIsNotNone(got)
self.assertEqual("Midnight", got["title"])
self.assertEqual("DJ Alice", got["artist"])
self.assertEqual(18, got["duration"])
self.assertEqual("music/" + tid + ".mp3", got["path"])
self.assertEqual(got["id"], self.registry.music.lookup(uid=UID)["track"]["id"])
self.assertIsNotNone(self.registry.music.bytes(tid, approved_only=True))
# Owner clears music -> lookup returns None again, can re-select later.
self.registry.music.clear(UID, INST)
self.assertIsNone(self.registry.music.lookup(unique_id="alice")["track"])
self.registry.music.select(UID, INST, tid)
self.assertEqual(tid, self.registry.music.lookup(unique_id="alice")["track"]["id"])
if __name__ == "__main__":
unittest.main()