"""Who can be @mentioned: the project's people, and nobody else.

The list a composer offers must be exactly the set a mention would reach,
or a name in the list is a notification that never arrives.
"""

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


def _people(client, iid, q=""):
    r = client.get(f"/api/projects/{iid}/people", params={"q": q})
    assert r.status_code == 200, r.text
    return r.json()["people"]


def test_the_list_is_the_project_audience(client, kit):
    iid = make_project(client, kit)
    add_screen_option(client, iid)
    names = {p["username"] for p in _people(client, iid)}
    assert "rian" in names            # the owner is always reachable
    assert "cleo" in names            # the client on this project
    assert all(p["name"] for p in _people(client, iid))


def test_the_search_matches_the_start_of_a_username_or_name(client, kit):
    iid = make_project(client, kit)
    add_screen_option(client, iid)
    assert [p["username"] for p in _people(client, iid, "cl")] == ["cleo"]
    assert _people(client, iid, "zzz") == []


def test_a_client_can_search_too_but_an_outsider_cannot(client, kit):
    iid = make_project(client, kit)
    add_screen_option(client, iid)
    as_user(client, "cleo")
    assert {p["username"] for p in _people(client, iid)} >= {"rian", "cleo"}
    as_user(client, "nobody")
    assert client.get(f"/api/projects/{iid}/people").status_code == 404
