from io import BytesIO from PIL import Image from pigway_frame_flow.models import DisplayRequest def test_history_image_survives_cloud_cleanup(client, service): data = BytesIO() Image.new("RGB", (40, 60), "blue").save(data, "JPEG") asset = service.assets.put(data.getvalue(), "example.jpg", "images") request = DisplayRequest(content={"type": "image", "asset_id": asset["asset_id"]}) op = service.submit( "display", request.model_dump(mode="json"), "living-room", "admin", "fixture" ) service.process_one() service.sync() result = service.operation(op)["result"] image_id = result["image_id"] # Simulate an older receipt without the new name snapshot, then cloud cleanup. service.db.execute( "UPDATE operations SET result=? WHERE id=?", ( '{"image_id":%d,"rendered_asset_id":"%s"}' % (image_id, asset["asset_id"]), op, ), ) service.db.execute("DELETE FROM image_sources WHERE device_id=?", ("living-room",)) deletion = service.submit( "delete", {"image_id": image_id, "confirm_current": True}, "living-room", "admin", "delete-fixture", ) service.update(deletion, "COMPLETED", {"image_id": image_id}) receipt = client.get("/api/v1/operations/" + deletion).json()["result"] assert receipt["image_name"] == "example.jpg" assert client.get(receipt["preview_url"]).status_code == 200 service.assets.path(asset["asset_id"]).unlink() service.db.execute("DELETE FROM assets WHERE id=?", (asset["asset_id"],)) receipt = client.get("/api/v1/operations/" + deletion).json()["result"] assert receipt["image_name"] == "example.jpg" assert receipt["preview_url"] is None def test_history_missing_local_image_does_not_break_list(client, service): op = service.submit("delete", {"image_id": 999}, "living-room", "admin", "fixture") service.update( op, "FAILED", {"image_id": 999}, { "code": "DEVICE_OFFLINE", "message": "Device offline", "request_id": "fixture", }, ) receipt = client.get("/api/v1/operations/" + op).json() assert receipt["result"]["image_id"] == 999 assert receipt["result"]["preview_url"] is None assert receipt["error"]["code"] == "DEVICE_OFFLINE"