"""An emoji on a comment, Slack's way.

Anyone who can read a comment can react to it; the same emoji again takes it
back; only the offered set is accepted; a removed comment carries none; and
nobody is notified, because acknowledging is not news.

Cast (tests/test_domain.py): rian owner · mara admin · cleo client · nate a
member elsewhere.
"""

from tests.conftest import as_user
from tests.test_domain import add_screen_option, make_project


def _setup(client, kit):
    iid = make_project(client, kit)
    _sid, oid = add_screen_option(client, iid)
    as_user(client, "cleo")
    r = client.post(f"/api/options/{oid}/pins",
                    json={"x_percent": 5, "y_percent": 5, "body_md": "Bigger logo?"})
    assert r.status_code == 200, r.text
    pin = r.json()["pin"]
    return oid, pin["comments"][0]["id"]


def _reactions(client, oid):
    return client.get(f"/api/options/{oid}/pins").json()["pins"][0]["comments"][0]["reactions"]


def test_a_reaction_is_added_shared_and_taken_back(client, kit):
    oid, cid = _setup(client, kit)
    as_user(client, "mara")
    assert client.post(f"/api/comments/{cid}/reactions", json={"emoji": "👍"}).status_code == 200
    as_user(client, "cleo")
    client.post(f"/api/comments/{cid}/reactions", json={"emoji": "👍"})
    client.post(f"/api/comments/{cid}/reactions", json={"emoji": "🎉"})
    assert _reactions(client, oid) == [{"emoji": "👍", "users": ["mara", "cleo"]},
                                       {"emoji": "🎉", "users": ["cleo"]}]
    client.post(f"/api/comments/{cid}/reactions", json={"emoji": "👍"})
    assert _reactions(client, oid) == [{"emoji": "👍", "users": ["mara"]},
                                       {"emoji": "🎉", "users": ["cleo"]}]


def test_only_the_offered_emoji(client, kit):
    _oid, cid = _setup(client, kit)
    r = client.post(f"/api/comments/{cid}/reactions", json={"emoji": "💩"})
    assert r.status_code == 422


def test_outsiders_cannot_react_and_nobody_is_told(client, kit):
    oid, cid = _setup(client, kit)
    as_user(client, "rian")
    before = len(client.get("/api/notifications").json()["notifications"])
    as_user(client, "nate")
    assert client.post(f"/api/comments/{cid}/reactions",
                       json={"emoji": "👍"}).status_code == 404
    as_user(client, "mara")
    client.post(f"/api/comments/{cid}/reactions", json={"emoji": "👍"})
    as_user(client, "cleo")
    assert client.get("/api/notifications").json()["notifications"] == []
    as_user(client, "rian")
    assert len(client.get("/api/notifications").json()["notifications"]) == before


def test_a_removed_comment_carries_none(client, kit):
    oid, cid = _setup(client, kit)
    client.post(f"/api/comments/{cid}/reactions", json={"emoji": "👀"})
    client.delete(f"/api/comments/{cid}")
    assert _reactions(client, oid) == []
    assert client.post(f"/api/comments/{cid}/reactions",
                       json={"emoji": "👀"}).status_code == 404
