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

92 lines
3.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import io
import json
import sys
import unittest
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent.parent / "services" / "members"))
from PIL import Image # noqa: E402
import report # noqa: E402
def png(width, height):
out = io.BytesIO()
Image.new("RGBA", (width, height), (200, 100, 150, 255)).save(out, "PNG")
return out.getvalue()
def reply(data):
return {"choices": [{"message": {"content": "<think>x</think>" + json.dumps(data)}}]}
class DetailCheckTest(unittest.TestCase):
def test_boxes_of_a_wide_picture_are_scaled_by_the_longer_side(self):
data = {"verdict": "reject", "reason": "r", "reason_ru": "р", "regions": [{"label": "a", "label_ru": "б", "box": [0, 100, 500, 400]}]}
result = report.detail_check(png(500, 250), post=lambda body: reply(data))
x1, y1, x2, y2 = result["regions"][0]["box"]
self.assertAlmostEqual(0.0, x1)
self.assertAlmostEqual(0.2, y1) # 100/1000 * 500 / 250
self.assertAlmostEqual(0.5, x2) # 500/1000 of the longer side (= the width here)
self.assertAlmostEqual(0.8, y2)
def test_a_broken_reply_is_not_checked_and_a_nonsense_box_is_dropped(self):
self.assertIsNone(report.detail_check(png(10, 10), post=lambda body: {"choices": []})["ok"])
data = {"verdict": "reject", "reason": "r", "regions": [{"box": [1, 2]}, {"box": [10, 10, 11, 11]}, "x"]}
self.assertEqual([], report.detail_check(png(10, 10), post=lambda body: reply(data))["regions"])
def test_pdf_is_made_with_and_without_regions(self):
for regions in ([], [{"label": "a", "label_ru": "б", "box": [0.1, 0.1, 0.5, 0.5]}]):
detail = {"ok": False, "reason": "Bad", "reason_ru": "Плохо", "regions": regions, "model": "m"}
pdf = report.build_pdf(png(512, 246), detail, name="n", kind="bubble", art_id="a" * 24)
self.assertTrue(pdf.startswith(b"%PDF"))
self.assertIn("t.me/cheb0chik", report.CONTACT)
if __name__ == "__main__":
unittest.main()
class ChainTest(unittest.TestCase):
def setUp(self):
import os
self.env = {k: os.environ.get(k) for k in ("GROQ_KEY", "GEMINI_KEY", "GEMINI_MODEL")}
os.environ["GROQ_KEY"], os.environ["GEMINI_KEY"] = "g", "m"
os.environ.pop("GEMINI_MODEL", None)
self.moderation = report.moderation
self.moderation._cooldown.clear()
self.saved = self.moderation._post_to
def tearDown(self):
import os
self.moderation._post_to = self.saved
self.moderation._cooldown.clear()
for k, v in self.env.items():
os.environ.pop(k, None)
if v is not None:
os.environ[k] = v
def test_a_provider_out_of_quota_hands_over_to_the_next_and_is_left_alone(self):
import urllib.error
calls = []
def fake(base_url, key, body, timeout=30.0):
calls.append(key)
if key == "g":
raise urllib.error.HTTPError(base_url, 429, "quota", {}, None)
return {"choices": [{"message": {"content": "{}"}}]}
self.moderation._post_to = fake
body = {"messages": [{"role": "user", "content": [{"type": "image_url", "image_url": {"url": "x"}}]}]}
first = self.moderation.chain_post(body)
self.assertEqual("gemini", first["_provider"])
self.assertEqual("gemini-3.5-flash-lite", first["_model"])
self.moderation.chain_post(body)
self.assertEqual(["g", "m", "m"], calls) # Groq is not asked again while it cools down
def test_without_any_key_the_check_is_not_made(self):
import os
os.environ.pop("GROQ_KEY"), os.environ.pop("GEMINI_KEY")
self.assertIsNone(report.detail_check(png(10, 10))["ok"])