Files
LihonT/tests/test_catalog.py
cheb0chik 5df4cddf7d LihonT 3 Luna pre-alpha: the streak pet becomes an animal
A pair of people can have a kitten, a puppy or a lion cub in place of TikTok's streak creature: it grows with the days of the
streak (cub, teen, adult, elder), goes grey while the streak is about to burn out, can be dragged in the chat and is also shown
on the big streak screen. An animal is given by a link (lihont.space/pet) that the friend accepts. The drawings are made by
scripts/make_pet_designs.py and served as strips of frames. Also here: the promo video scripts (make_pet_video.py on top of
make_store_video.py), a retry pause for strips that failed to load, stricter checks in the answer to a gift.

Work in progress: the gift service is not switched on for everyone yet and there is no public build with the animals.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-10 15:14:48 +03:00

266 lines
14 KiB
Python

import base64
import hashlib
import importlib.util
import io
import tempfile
import unittest
from pathlib import Path
from PIL import Image
ROOT = Path(__file__).resolve().parents[1] / "services" / "members"
SPEC = importlib.util.spec_from_file_location("members_server", ROOT / "server.py")
server = importlib.util.module_from_spec(SPEC)
SPEC.loader.exec_module(server)
UID = "6812345678901234"
OTHER = "6899999999999999"
INSTALL_A = "a" * 64
INSTALL_B = "b" * 64
def sha(installation):
return hashlib.sha256(installation.encode("ascii")).hexdigest()
def picture(shade, size=(120, 80)):
out = io.BytesIO()
Image.new("RGBA", size, (shade, 40, 90, 255)).save(out, "PNG")
return base64.b64encode(out.getvalue()).decode("ascii")
class CatalogTest(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.addCleanup(self.tmp.cleanup)
self.now = 1_790_000_000
self.who, self.inst = UID, INSTALL_A
self.registry = server.Registry(Path(self.tmp.name) / "r.sqlite3", clock=lambda: self.now,
fetcher=lambda unique_id: (self.who, self.registry.code(self.who, sha(self.inst))))
self.cat = self.registry.catalog
def publish(self, kind="frame", shade=10, name="Венок", description="Из веток", uid=UID, installation=INSTALL_A):
self.who, self.inst = uid, installation
return self.cat.call("publish", {"uid": uid, "installation": installation, "unique_id": "someone", "kind": kind,
"name": name, "description": description, "png": picture(shade)})
def approve(self, art_id):
self.registry.art_decide(art_id, "approved")
def listing(self, **payload):
return self.cat.call("catalog", payload)
def test_a_published_item_is_not_listed_until_approved(self):
reply = self.publish()
self.assertEqual("new", reply["status"])
self.assertEqual([], self.listing()["items"])
self.approve(reply["id"])
items = self.listing()["items"]
self.assertEqual(1, len(items))
self.assertEqual(("frame", "Венок", "Из веток", UID), (items[0]["kind"], items[0]["name"], items[0]["description"], items[0]["author"]["uid"]))
self.assertEqual("someone", items[0]["author"]["name"])
def test_the_picture_is_remade_by_kind(self):
frame = self.registry.art_bytes(self.publish("frame", 1)["id"], approved_only=False)
self.who, self.inst = UID, INSTALL_A
wide = self.cat.call("publish", {"uid": UID, "installation": INSTALL_A, "unique_id": "someone", "kind": "bubble",
"name": "B", "png": picture(2, (1024, 683))})
bubble = self.registry.art_bytes(wide["id"], approved_only=False)
small = self.registry.art_bytes(self.publish("bubble", 3)["id"], approved_only=False)
self.assertEqual((256, 256), Image.open(io.BytesIO(frame)).size)
self.assertEqual((512, 342), Image.open(io.BytesIO(bubble)).size)
self.assertEqual((120, 80), Image.open(io.BytesIO(small)).size) # never enlarged
def test_newest_first_with_a_cursor_and_kind_filter(self):
ids = []
for shade in range(5):
self.now += 10
self.registry.art_decide(self.publish("frame" if shade % 2 == 0 else "bubble", shade * 20)["id"], "approved")
ids.append(self.registry.catalog.listing({"limit": 1})["items"][0]["id"])
everything = self.listing(limit=2)
self.assertEqual([ids[4], ids[3]], [i["id"] for i in everything["items"]])
rest = self.listing(limit=30, cursor=everything["next"])
self.assertEqual([ids[2], ids[1], ids[0]], [i["id"] for i in rest["items"]])
self.assertIsNone(rest["next"])
self.assertEqual({"frame"}, {i["kind"] for i in self.listing(kind="frame")["items"]})
with self.assertRaises(ValueError):
self.listing(kind="sticker")
with self.assertRaises(ValueError):
self.listing(cursor="nonsense")
def test_item_and_profile(self):
first = self.publish("frame", 5)["id"]
self.approve(first)
self.now += 5
second = self.publish("bubble", 90, name="Облако")["id"]
self.approve(second)
self.assertEqual("Венок", self.cat.call("item", {"id": first})["item"]["name"])
profile = self.cat.call("profile", {"uid": UID})
self.assertEqual([second, first], [i["id"] for i in profile["items"]])
self.assertEqual("someone", profile["name"])
self.assertEqual(1, profile["totals"]["frame"]["count"])
with self.assertRaises(ValueError):
self.cat.call("item", {"id": "0" * 24})
with self.assertRaises(ValueError):
self.cat.call("profile", {"uid": "12"})
def test_a_rejected_or_pending_item_is_never_shown_or_installed(self):
art = self.publish()["id"]
with self.assertRaises(ValueError):
self.cat.call("install", {"installation": INSTALL_B, "id": art})
self.registry.art_decide(art, "rejected", "ai: nudity")
self.assertEqual([], self.listing()["items"])
self.assertEqual({"items": []}, {"items": self.cat.call("profile", {"uid": UID})["items"]})
def test_install_counts_once_per_installation(self):
art = self.publish()["id"]
self.approve(art)
self.assertEqual(1, self.cat.call("install", {"installation": INSTALL_A, "id": art})["installs"])
self.assertEqual(1, self.cat.call("install", {"installation": INSTALL_A, "id": art})["installs"])
self.assertEqual(2, self.cat.call("install", {"installation": INSTALL_B, "id": art})["installs"])
self.assertEqual(2, self.listing()["items"][0]["installs"])
with self.assertRaises(ValueError):
self.cat.call("install", {"installation": "short", "id": art})
def test_mine_shows_every_state_and_the_reason_of_a_refusal(self):
art = self.publish()["id"]
self.registry.art_decide(art, "rejected", "ai: nudity")
mine = self.cat.call("mine", {"uid": UID, "installation": INSTALL_A})["items"]
self.assertEqual(("rejected", "ai: nudity", True), (mine[0]["status"], mine[0]["reason"], mine[0]["listed"]))
with self.assertRaises(ValueError):
self.cat.call("mine", {"uid": UID, "installation": INSTALL_B})
with self.assertRaises(ValueError):
self.cat.call("mine", {"uid": OTHER, "installation": INSTALL_A})
def test_the_report_of_a_rejection_goes_to_its_author_only(self):
art = self.publish()["id"]
with self.assertRaises(ValueError): # not rejected, no report
self.cat.call("report", {"uid": UID, "installation": INSTALL_A, "id": art})
self.registry.art_decide(art, "rejected", "ai: плохо")
self.registry.art_report_save(art, b"%PDF-test")
reply = self.cat.call("report", {"uid": UID, "installation": INSTALL_A, "id": art})
self.assertEqual(b"%PDF-test", base64.b64decode(reply["pdf"]))
with self.assertRaises(ValueError): # another installation of the same account is not the author
self.cat.call("report", {"uid": UID, "installation": INSTALL_B, "id": art})
self.who, self.inst = OTHER, INSTALL_B
self.registry.shape(OTHER, INSTALL_B, "flower", "peer")
with self.assertRaises(ValueError):
self.cat.call("report", {"uid": OTHER, "installation": INSTALL_B, "id": art})
def test_unpublish_is_the_authors_alone(self):
art = self.publish()["id"]
self.approve(art)
self.who, self.inst = OTHER, INSTALL_B
self.registry.shape(OTHER, INSTALL_B, "flower", "peer")
with self.assertRaises(ValueError):
self.cat.call("unpublish", {"uid": OTHER, "installation": INSTALL_B, "id": art})
self.assertEqual(1, len(self.listing()["items"]))
self.cat.call("unpublish", {"uid": UID, "installation": INSTALL_A, "id": art})
self.assertEqual([], self.listing()["items"])
def test_a_blocked_author_vanishes(self):
self.approve(self.publish()["id"])
with self.registry.connect() as db:
db.execute("INSERT INTO blocked(uid) VALUES(?)", (UID,))
self.assertEqual([], self.listing()["items"])
def test_listing_an_old_private_picture_judges_it_again(self):
private = self.registry.frame_upload(UID, INSTALL_A, "someone", "Своя", picture(33))
art = private["id"][2:]
self.approve(art)
self.who, self.inst = UID, INSTALL_A
again = self.cat.call("publish", {"uid": UID, "installation": INSTALL_A, "kind": "frame", "name": "Новое",
"description": "d", "png": picture(33)})
self.assertEqual(art, again["id"])
self.assertEqual("new", again["status"])
self.assertEqual([], self.listing()["items"])
def test_bad_publish_requests(self):
self.who, self.inst = UID, INSTALL_A
for payload in ({"kind": "sticker"}, {"kind": "frame", "png": "###"}, {"kind": "frame", "uid": "1"}):
with self.assertRaises(ValueError):
self.cat.call("publish", dict({"uid": UID, "installation": INSTALL_A, "unique_id": "someone", "name": "x"}, **payload))
def test_anyone_may_wear_a_catalog_frame_but_not_a_pending_one(self):
art = self.publish("frame", 3)["id"]
self.who, self.inst = OTHER, INSTALL_B
self.registry.shape(OTHER, INSTALL_B, "flower", "peer")
with self.assertRaises(ValueError):
self.registry.avatar_frame(OTHER, INSTALL_B, "u." + art)
self.approve(art)
self.assertTrue(self.registry.avatar_frame(OTHER, INSTALL_B, "u." + art)["published"])
self.assertEqual({OTHER: "u." + art}, self.registry.lookup([OTHER])["avatar_frames"])
def test_bubbles_are_chosen_and_seen_by_others_once_approved(self):
art = self.publish("bubble", 77)["id"]
self.cat.call("bubble_choose", {"uid": UID, "installation": INSTALL_A, "id": art, "unique_id": "someone"})
self.assertEqual({}, self.registry.lookup([UID])["bubbles"])
self.approve(art)
self.assertEqual({UID: art}, self.registry.lookup([UID, OTHER])["bubbles"])
self.who, self.inst = OTHER, INSTALL_B
self.registry.shape(OTHER, INSTALL_B, "flower", "peer")
self.assertTrue(self.cat.call("bubble_choose", {"uid": OTHER, "installation": INSTALL_B, "id": art})["published"])
self.assertEqual({UID: art, OTHER: art}, self.registry.lookup([UID, OTHER])["bubbles"])
self.cat.call("bubble_choose", {"uid": OTHER, "installation": INSTALL_B, "id": "none"})
self.assertEqual({UID: art}, self.registry.lookup([UID, OTHER])["bubbles"])
def test_a_frame_cannot_be_chosen_as_a_bubble_or_the_reverse(self):
frame = self.publish("frame", 8)["id"]
with self.assertRaises(ValueError):
self.cat.call("bubble_choose", {"uid": UID, "installation": INSTALL_A, "id": frame, "unique_id": "someone"})
bubble = self.publish("bubble", 9)["id"]
with self.assertRaises(ValueError):
self.registry.avatar_frame(UID, INSTALL_A, "u." + bubble)
class LibraryTest(unittest.TestCase):
"""The "+" of the store: a library per account, filled by the store and read by the mod."""
setUp = CatalogTest.setUp
publish = CatalogTest.publish
approve = CatalogTest.approve
def test_prove_binds_an_installation_once(self):
self.who, self.inst = UID, INSTALL_A
self.registry.fetcher = lambda unique_id: (UID, "")
self.assertEqual("code_missing", self.cat.call("prove", {"uid": UID, "installation": INSTALL_A, "unique_id": "someone"})["reason"])
self.registry.fetcher = lambda unique_id: (UID, self.registry.code(UID, sha(INSTALL_A)))
self.assertTrue(self.cat.call("prove", {"uid": UID, "installation": INSTALL_A, "unique_id": "someone"})["published"])
self.registry.fetcher = lambda unique_id: (_ for _ in ()).throw(AssertionError("the profile is read only once"))
self.assertTrue(self.cat.call("prove", {"uid": UID, "installation": INSTALL_A})["published"])
def test_add_list_remove(self):
art = self.publish("frame", 12)["id"]
self.approve(art)
self.who, self.inst = OTHER, INSTALL_B
self.registry.fetcher = lambda unique_id: (OTHER, self.registry.code(OTHER, sha(INSTALL_B)))
self.cat.call("prove", {"uid": OTHER, "installation": INSTALL_B, "unique_id": "peer"})
self.assertEqual({"items": []}, self.cat.call("library", {"uid": OTHER, "installation": INSTALL_B}))
self.assertEqual(1, self.cat.call("library_add", {"uid": OTHER, "installation": INSTALL_B, "id": art})["installs"])
self.assertEqual(1, self.cat.call("library_add", {"uid": OTHER, "installation": INSTALL_B, "id": art})["installs"])
items = self.cat.call("library", {"uid": OTHER, "installation": INSTALL_B})["items"]
self.assertEqual([art], [i["id"] for i in items])
self.cat.call("library_remove", {"uid": OTHER, "installation": INSTALL_B, "id": art})
self.assertEqual([], self.cat.call("library", {"uid": OTHER, "installation": INSTALL_B})["items"])
def test_the_library_needs_the_proven_installation_and_an_approved_item(self):
art = self.publish("frame", 13)["id"]
with self.assertRaises(ValueError):
self.cat.call("library_add", {"uid": UID, "installation": INSTALL_A, "id": art}) # not approved yet
self.approve(art)
with self.assertRaises(ValueError):
self.cat.call("library_add", {"uid": UID, "installation": INSTALL_B, "id": art}) # another installation
with self.assertRaises(ValueError):
self.cat.call("library", {"uid": UID, "installation": INSTALL_B})
def test_an_item_taken_off_the_catalog_leaves_the_library(self):
art = self.publish("bubble", 14)["id"]
self.approve(art)
self.cat.call("library_add", {"uid": UID, "installation": INSTALL_A, "id": art})
self.cat.call("unpublish", {"uid": UID, "installation": INSTALL_A, "id": art})
self.assertEqual([], self.cat.call("library", {"uid": UID, "installation": INSTALL_A})["items"])
if __name__ == "__main__":
unittest.main()