import sys import time from pathlib import Path from client import Client, check from image_probe import jpeg_size BASE_URL = "http://localhost:8080" STATE_FILE = Path("/tmp/lab5_grade3_attachment.txt") def prepare(): c = Client(BASE_URL) alice, bob, _, chat = c.bootstrap() attachment, original = c.attachment(alice, chat) attachment_id = attachment["id"] current = c.call( "GET", f"/api/v1/attachments/{attachment_id}", actor=bob, schema="Attachment", )["json"] check(current["status"] == "queued", "expected queued while worker is stopped") message = c.message( alice, chat, "Проверка очереди", attachments=[attachment_id], )["json"] check( message["attachment_ids"] == [attachment_id], "message did not retain attachment", ) STATE_FILE.write_text( "\n".join([attachment_id, bob["token"], original.hex()]), encoding="utf-8", ) print("PASS: HTTP 202, queued, message linked") print("Attachment:", attachment_id) print("Now restart API and RabbitMQ, then start worker") def verify(): attachment_id, token, original_hex = STATE_FILE.read_text( encoding="utf-8" ).splitlines() c = Client(BASE_URL) actor = {"token": token} deadline = time.monotonic() + 90 while time.monotonic() < deadline: attachment = c.call( "GET", f"/api/v1/attachments/{attachment_id}", actor=actor, schema="Attachment", )["json"] check(attachment["status"] != "failed", "processing failed") if attachment["status"] == "ready": break time.sleep(0.5) else: raise AssertionError("attachment did not become ready") original = c.call( "GET", f"/api/v1/attachments/{attachment_id}/content", actor=actor, ) check(original["bytes"] == bytes.fromhex(original_hex), "original changed") thumbnail = c.call( "GET", f"/api/v1/attachments/{attachment_id}/content?variant=thumbnail", actor=actor, ) check( thumbnail["headers"].get_content_type() == "image/jpeg", "thumbnail is not JPEG", ) check( jpeg_size(thumbnail["bytes"]) == (256, 160), "incorrect thumbnail dimensions", ) print("PASS: restart recovery, ready, original, JPEG thumbnail 256x160") if __name__ == "__main__": if len(sys.argv) != 2 or sys.argv[1] not in {"prepare", "verify"}: raise SystemExit("Usage: python tests/queue_grade3.py prepare|verify") {"prepare": prepare, "verify": verify}[sys.argv[1]]()