"""The Gem host module against Productboard's saved listing and two saved role
answers. No network."""
from __future__ import annotations
import sys
from datetime import UTC, datetime
from pathlib import Path
from typing import Any
import pytest
ROOT = Path(__file__).resolve().parents[1]
if str(ROOT) not in sys.path:
sys.path.insert(0, str(ROOT))
from app.jobs import discover # noqa: E402
from plugins import ats, readers # noqa: E402
from plugins.readers import Ask, gem # noqa: E402
FIXTURES = ROOT / "tests" / "fixtures" / "ats"
API = "https://jobs.gem.com/api/public/graphql"
def _read(name: str) -> bytes:
return (FIXTURES / name).read_bytes()
def _get_of(answers: dict[str, bytes | Exception], asked: list[dict[str, Any]]) -> Any:
"""A `get` that answers a GraphQL call by its operation name."""
def get(url: str, *, method: str = "GET", payload: Any = None,
headers: dict[str, str] | None = None) -> bytes:
assert url == API and method == "POST"
asked.append(payload | {"headers": headers})
answer = answers[payload["operationName"]]
if isinstance(answer, Exception):
raise answer
return answer
return get
def _listing() -> dict[str, bytes | Exception]:
return {"JobBoardList": _read("gem.json")}
def test_a_listing_becomes_rows() -> None:
asked: list[dict[str, Any]] = []
decoded, results, total, cursor = gem.page(_get_of(_listing(), asked), "productboard", None, Ask())
assert asked == [{"operationName": "JobBoardList", "variables": {"boardId": "productboard"},
"query": gem.LIST_QUERY, "headers": None}]
assert (total, cursor) == (None, None) and len(results) == 11
rows = gem.rows(decoded, "productboard")
assert len(rows) == 11
first, backend = rows[0], rows[2]
assert first["source_id"] == "8080046002" # numeric, from a synced Greenhouse
assert first["title"] == "Staff AI Product Engineer (Prague/Brno/Remote EU)"
assert first["url"] == "https://jobs.gem.com/productboard/8080046002"
assert first["department"] == "Product Engineering" and first["remote"] == "HYBRID"
assert first["location"] == "Prague"
assert (first["posted"], first["description"], first["salary_text"]) == (None, "", "")
assert backend["source_id"] == "am9icG9zdDqWC1V_RM7U6ORvTd1Iu64r" # opaque
assert backend["location"] == "Prague; Remote Europe; Brno"
# Every place is kept, and the host's own flag says which is remote.
assert backend["places"] == [{"text": "Prague", "remote": False},
{"text": "Remote Europe", "remote": True},
{"text": "Brno", "remote": False}]
assert first["employment"] == "FULL_TIME"
assert len({r["source_id"] for r in rows}) == 11
@pytest.mark.parametrize(("kind", "flags", "remote"), [
("IN_OFFICE", [False], "IN_OFFICE"), ("HYBRID", [True], "HYBRID"), ("REMOTE", [False], "REMOTE"),
(None, [True, True], True), (None, [True, False], None), (None, [], None),
("SOMETHING_NEW", [False], "SOMETHING_NEW")])
def test_remote_is_the_location_type_then_the_locations(
kind: str | None, flags: list[bool], remote: bool | str | None) -> None:
posting = {"extId": "1", "title": "Role", "job": {"locationType": kind},
"locations": [{"city": "X", "isRemote": f} for f in flags]}
assert gem.rows([posting], "acme")[0]["remote"] == remote
def test_a_posting_without_a_title_or_an_ext_id_is_left_out() -> None:
assert gem.rows([{"extId": "1", "title": " "}, {"extId": "", "title": "Role"}], "acme") == []
def test_the_board_is_one_post_without_origin_or_referer() -> None:
asked: list[dict[str, Any]] = []
decoded, _results, total, cursor = gem.page(_get_of(_listing(), asked), "productboard",
None, Ask())
(call,) = asked
assert call["variables"] == {"boardId": "productboard"} and call["headers"] is None
assert len(gem.rows(decoded, "productboard")) == 11 and (total, cursor) == (None, None)
def test_an_empty_board_is_no_rows() -> None:
body = b'{"data":{"oatsExternalJobPostings":{"jobPostings":[]}}}'
decoded, results, _total, cursor = gem.page(
_get_of({"JobBoardList": body}, []), "nobody", None, Ask())
assert gem.rows(decoded, "nobody") == [] and results == [] and cursor is None
def test_it_declares_one_page_that_may_be_empty_by_mistake() -> None:
reads = gem.READS
assert (reads.listing, reads.text, reads.places, reads.empty) == (
"paged", "detail", "many", "suspect")
assert "POST /api/public/graphql" in reads.robots
@pytest.mark.parametrize(("body", "message"), [
(b'{"errors":[{"message":"Cannot query field \\"extId\\""}]}', "refused: Cannot query field"),
(b'{"data":{"somethingElse":{}}}', "no oatsExternalJobPostings"),
(b'{"data":null}', "no oatsExternalJobPostings"),
(b'{"data":{"oatsExternalJobPostings":{"postings":[]}}}', "no jobPostings list"),
(b'{"data":{"oatsExternalJobPostings":null}}', "no jobPostings list"),
(b'[]', "not a Gem answer"),
])
def test_an_answer_of_another_shape_fails_with_a_clear_error(body: bytes, message: str) -> None:
with pytest.raises(ValueError, match=message):
gem.page(_get_of({"JobBoardList": body}, []), "productboard", None, Ask())
def test_detail_reads_the_text_and_the_date() -> None:
row = {"source_id": "am9icG9zdDq5-IOakBvxHzWhMPnTzoPp"}
asked: list[dict[str, Any]] = []
found = gem.detail(_get_of({"ExternalJobPosting": _read("gem-detail-2.json")}, asked),
"productboard", row)
assert asked[0]["operationName"] == "ExternalJobPosting"
assert asked[0]["variables"] == {"boardId": "productboard", "extId": row["source_id"]}
assert asked[0]["query"] == gem.DETAIL_QUERY
assert set(found) <= set(ats.DETAIL_FIELDS)
assert found["posted"] == datetime.fromtimestamp(1787574069, tz=UTC)
assert found["description"].startswith("Only the best engineers are being built here.")
assert "<" not in found["description"] and "&" not in found["description"]
def test_detail_of_a_role_that_is_not_there_raises() -> None:
body = b'{"data":{"oatsExternalJobPosting":null}}'
with pytest.raises(ValueError, match="no role gone on acme"):
gem.detail(_get_of({"ExternalJobPosting": body}, []), "acme", {"source_id": "gone"})
def test_detail_reads_the_numeric_role_too() -> None:
found = gem.detail(_get_of({"ExternalJobPosting": _read("gem-detail-1.json")}, []),
"productboard", {"source_id": "8080046002"})
assert found["posted"] == datetime.fromtimestamp(1753349342, tz=UTC)
assert found["description"]
def test_the_slug_the_urls_and_the_allow_list() -> None:
assert gem.KEEP_CASE
for slug in ("productboard", "Productboard", "senndertechnologies-gmbh", "a_b", "1"):
assert gem.slug_ok(slug)
assert gem.careers_url("Productboard") == "https://jobs.gem.com/Productboard"
allow = gem.allowed("productboard")
assert allow("jobs.gem.com") and allow("JOBS.gem.com")
assert not allow("gem.com") and not allow("productboard.gem.com")
assert not allow("jobs.gem.com.evil.example") and not allow("localhost")
assert readers.HOSTS["gem"] is gem and ats.HOSTS["gem"] == "Gem"
@pytest.mark.parametrize("slug", [
"", "a@b", "a#b", "a?b", "..", ".", "a/b", "a%2fb", "a:443", "a b", " a", "a\n", "a\r\n",
"a.b", "127.0.0.1", "[::1]", "localhost.", "a\\b", "a\"b"])
def test_a_hostile_slug_is_refused(slug: str) -> None:
assert not gem.slug_ok(slug)
assert not ats.is_known_target(f"gem:{slug}")
def test_a_local_word_as_a_slug_still_reaches_gems_host_only() -> None:
assert ats.is_known_target("gem:localhost")
allow = gem.allowed("localhost")
assert allow("jobs.gem.com") and not allow("localhost")
def test_discovery_finds_the_board_keeping_its_case() -> None:
page = ('Jobs'
'x'
'y')
assert [(h, s) for h, s, _ in discover.boards_in(page)] == [
("gem", "Productboard"), ("gem", "sennder-gmbh")]