Files
LihonT/tests/test_pair_pet.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

144 lines
6.4 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)
ALICE, BOB, STRANGER = "6812345678901234", "6899999999999999", "6877777777777777"
INST = {ALICE: "a" * 64, BOB: "b" * 64, STRANGER: "c" * 64}
def sha(value):
return hashlib.sha256(value.encode("ascii")).hexdigest()
def drawing(shade, size=(300, 200)):
out = io.BytesIO()
Image.new("RGBA", size, (shade, 120, 60, 200)).save(out, "PNG")
return base64.b64encode(out.getvalue()).decode("ascii")
class PairPetTest(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.addCleanup(self.tmp.cleanup)
self.now = 1_790_000_000
self.who = ALICE
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(INST[self.who]))))
for uid in (ALICE, BOB):
self.who = uid
self.assertTrue(self.registry.shape(uid, INST[uid], "flower", "someone")["published"])
def upload(self, uid, peer, shade):
self.who = uid
return self.registry.pet_upload(uid, INST[uid], "someone", peer, "Серийчик", drawing(shade))
def look(self, uid, peer, installation=None):
return self.registry.pet_lookup(uid, installation or INST[uid], peer)
def approve(self, art_id):
self.registry.art_decide(art_id, "approved", "owner")
def test_a_new_picture_waits_for_approval_and_then_both_of_them_see_it(self):
art = self.upload(ALICE, BOB, 10)
self.assertEqual(("new", True), (art["status"], art["published"]))
self.assertEqual({"id": None, "pending": True, "history": []}, self.look(ALICE, BOB))
self.approve(art["id"])
self.assertEqual({"id": art["id"], "pending": False, "history": []}, self.look(ALICE, BOB))
self.assertEqual(art["id"], self.look(BOB, ALICE)["id"]) # the same picture for the other one
self.assertEqual({"id": None, "pending": False, "history": []}, self.look(STRANGER, BOB, INST[STRANGER])) # a stranger sees nothing
self.assertIsNone(self.look(ALICE, BOB, "d" * 64)["id"]) # nor does an installation that did not prove the account
def test_the_picture_is_kept_whole(self):
art = self.upload(ALICE, BOB, 10)
data = self.registry.art_bytes(art["id"], approved_only=False)
self.assertEqual((300, 200), Image.open(io.BytesIO(data)).size) # not cropped to a square
self.assertEqual("RGBA", Image.open(io.BytesIO(data)).mode)
def test_either_of_them_changes_it_and_the_old_one_stays_until_the_new_one_is_approved(self):
first = self.upload(ALICE, BOB, 10)
self.approve(first["id"])
self.now += 3601
second = self.upload(BOB, ALICE, 40) # the other one changes it
shown = self.look(ALICE, BOB)
self.assertEqual((first["id"], True), (shown["id"], shown["pending"])) # the old one is still shown
self.approve(second["id"])
shown = self.look(ALICE, BOB)
self.assertEqual((second["id"], False, [first["id"]]), (shown["id"], shown["pending"], shown["history"]))
def test_a_rejected_picture_never_replaces_the_shown_one(self):
first = self.upload(ALICE, BOB, 10)
self.approve(first["id"])
self.now += 3601
second = self.upload(ALICE, BOB, 40)
self.registry.art_decide(second["id"], "rejected", "ai: no")
self.assertEqual(first["id"], self.look(BOB, ALICE)["id"])
def test_the_pair_changes_its_picture_once_an_hour_and_a_refusal_costs_no_review(self):
self.upload(ALICE, BOB, 10)
before = self.registry.art_counts()
with self.assertRaises(ValueError):
self.upload(BOB, ALICE, 40)
self.assertEqual(before, self.registry.art_counts()) # nothing new was taken for the owner to judge
self.now += 3601
self.assertTrue(self.upload(BOB, ALICE, 40)["published"])
def test_the_same_picture_twice_is_one_entry(self):
first = self.upload(ALICE, BOB, 10)
again = self.upload(ALICE, BOB, 10)
self.assertEqual(first["id"], again["id"])
with self.registry.connect() as db:
self.assertEqual(1, db.execute("SELECT COUNT(*) FROM pair_pet").fetchone()[0])
def test_an_earlier_picture_can_be_brought_back(self):
first = self.upload(ALICE, BOB, 10)
self.approve(first["id"])
self.now += 3601
second = self.upload(ALICE, BOB, 40)
self.approve(second["id"])
self.now += 3601
self.registry.pet_revert(BOB, INST[BOB], ALICE, first["id"])
self.assertEqual(first["id"], self.look(ALICE, BOB)["id"])
with self.assertRaises(ValueError): # once an hour, the same as any change
self.registry.pet_revert(ALICE, INST[ALICE], BOB, second["id"])
self.now += 3601
with self.assertRaises(ValueError): # a picture that was never theirs
self.registry.pet_revert(ALICE, INST[ALICE], BOB, "0" * 24)
with self.assertRaises(ValueError): # no proof, no say
self.registry.pet_revert(STRANGER, INST[STRANGER], BOB, first["id"])
def test_either_side_clears_it(self):
art = self.upload(ALICE, BOB, 10)
self.approve(art["id"])
self.registry.pet_clear(BOB, INST[BOB], ALICE)
self.assertEqual({"id": None, "pending": False, "history": []}, self.look(ALICE, BOB))
with self.assertRaises(ValueError):
self.registry.pet_clear(STRANGER, INST[STRANGER], ALICE)
def test_a_banned_person_hides_the_picture(self):
art = self.upload(ALICE, BOB, 10)
self.approve(art["id"])
with self.registry.connect() as db:
db.execute("INSERT INTO blocked(uid, until) VALUES(?, 0)", (BOB,))
self.assertIsNone(self.look(ALICE, BOB)["id"])
with self.assertRaises(ValueError):
self.upload(BOB, ALICE, 90)
def test_oneself_is_not_a_pair(self):
with self.assertRaises(ValueError):
self.registry.pet_upload(ALICE, INST[ALICE], "someone", ALICE, "x", drawing(5))
if __name__ == "__main__":
unittest.main()