import json


def all_cells_run(event_str: str, expected_count: int) -> bool:
    """Wait for an event signalling all cells have run.

    `execution_count` should equal number of nonempty cells.
    """
    try:
        event = json.loads(event_str)
        msg_type = event["msg_type"]
        content = event["content"]
        execution_count = content["execution_count"]
        status = content["status"]
    except (TypeError, KeyError):
        return False

    return all(
        (
            msg_type == "execute_reply",
            execution_count == expected_count,
            status == "ok",
        )
    )