local-in-30s / tests /test_api_bridge.py
Jackylau666's picture
Deploy Local in 30s hackathon app
674c130 verified
Raw
History Blame Contribute Delete
3.17 kB
import base64
import unittest
from io import BytesIO
from PIL import Image
def _tiny_png_b64() -> str:
image = Image.new("RGB", (1, 1), (255, 128, 0))
buf = BytesIO()
image.save(buf, format="PNG")
return base64.b64encode(buf.getvalue()).decode("ascii")
class ApiBridgeTest(unittest.TestCase):
def test_decode_payload_returns_frontend_culture_decode_shape(self):
from api_bridge import decode_payload_to_culture_decode
captured = {}
def fake_run_decode(image):
captured["size"] = image.size
return "raw model text"
def fake_parse_sections(raw):
assert raw == "raw model text"
return {
"title": "Test Shrine",
"location": "Kyoto, Japan",
"category": "Architecture / Religion",
"flag": "JP",
"features": ["gate", "path", "shrine", "vermilion"],
"tourists": "A red gate path.",
"locals": "A local meaning.",
"avoid_bullets": ["Do not block.", "Do not climb.", "Ask first."],
"matters": "It matters culturally.",
}
def fake_sections_to_decode(sections, uploaded_b64=None):
assert uploaded_b64 == _tiny_png_b64()
return {
"id": "abc123",
"title": sections["title"],
"location": sections["location"],
"category": sections["category"],
"flag": sections["flag"],
"_uploaded_b64": uploaded_b64,
"scan": {
"subject": sections["tourists"],
"features": sections["features"],
"contextPrompt": "A traveler is looking at Test Shrine.",
},
"layers": {
"l1": {"key": "l1", "title": "What tourists see", "label": "L1", "tone": "Objective", "text": sections["tourists"], "priority": "standard"},
"l2": {"key": "l2", "title": "What locals know", "label": "L2", "tone": "Revealing", "text": sections["locals"], "priority": "highlight"},
"l3": {"key": "l3", "title": "What not to do", "label": "L3", "tone": "Protective", "bullets": sections["avoid_bullets"], "priority": "standard"},
"l4": {"key": "l4", "title": "Why it matters", "label": "L4", "tone": "Cultural depth", "text": sections["matters"], "priority": "highlight"},
},
}
result = decode_payload_to_culture_decode(
{"image": _tiny_png_b64(), "mime": "image/png"},
run_decode=fake_run_decode,
parse_sections=fake_parse_sections,
sections_to_decode=fake_sections_to_decode,
)
self.assertEqual(captured["size"], (1, 1))
self.assertEqual(result["id"], "abc123")
self.assertTrue(result["image"].startswith("data:image/png;base64,"))
self.assertNotIn("_uploaded_b64", result)
self.assertEqual(result["layers"]["l3"]["bullets"], ["Do not block.", "Do not climb.", "Ask first."])
if __name__ == "__main__":
unittest.main()