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

74 lines
3.4 KiB
Python

import hashlib
import importlib.util
import tempfile
import unittest
from pathlib import Path
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()
class PairSpeciesTest(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, STRANGER):
self.who = uid
self.assertTrue(self.registry.shape(uid, INST[uid], "flower", "someone")["published"])
def test_offer_waits_for_the_other_one(self):
self.registry.species_offer(ALICE, INST[ALICE], BOB, "cat")
self.assertEqual(self.registry.species_lookup(ALICE, INST[ALICE], BOB), {"kind": "", "offer": ""}) # the giver sees no offer to answer
self.assertEqual(self.registry.species_lookup(BOB, INST[BOB], ALICE), {"kind": "", "offer": "cat"})
self.registry.species_answer(BOB, INST[BOB], ALICE, True)
for me, other in ((ALICE, BOB), (BOB, ALICE)):
self.assertEqual(self.registry.species_lookup(me, INST[me], other), {"kind": "cat", "offer": ""})
def test_decline_keeps_the_old_animal(self):
self.registry.species_offer(ALICE, INST[ALICE], BOB, "dog")
self.registry.species_answer(BOB, INST[BOB], ALICE, True)
self.registry.species_offer(ALICE, INST[ALICE], BOB, "lion")
self.assertEqual(self.registry.species_answer(BOB, INST[BOB], ALICE, False)["kind"], "dog")
self.assertEqual(self.registry.species_lookup(BOB, INST[BOB], ALICE), {"kind": "dog", "offer": ""})
def test_cannot_accept_your_own_offer(self):
self.registry.species_offer(ALICE, INST[ALICE], BOB, "cat")
with self.assertRaises(ValueError):
self.registry.species_answer(ALICE, INST[ALICE], BOB, True)
def test_a_third_person_sees_nothing(self):
self.registry.species_offer(ALICE, INST[ALICE], BOB, "cat")
self.assertEqual(self.registry.species_lookup(STRANGER, INST[STRANGER], ALICE), {"kind": "", "offer": ""})
with self.assertRaises(ValueError):
self.registry.species_answer(STRANGER, INST[STRANGER], ALICE, True)
def test_unknown_animal_and_wrong_proof(self):
with self.assertRaises(ValueError):
self.registry.species_offer(ALICE, INST[ALICE], BOB, "dragon")
with self.assertRaises(ValueError):
self.registry.species_offer(ALICE, "d" * 64, BOB, "cat")
def test_old_offer_goes_stale(self):
self.registry.species_offer(ALICE, INST[ALICE], BOB, "cat")
self.now += 8 * 86400
self.assertEqual(self.registry.species_lookup(BOB, INST[BOB], ALICE)["offer"], "")
with self.assertRaises(ValueError):
self.registry.species_answer(BOB, INST[BOB], ALICE, True)
if __name__ == "__main__":
unittest.main()