Files
jobboard/tests/test_web_reach.py
T
WolfandClaude Opus 5.5 0fd45b333a Fetch terms narrow what big boards read with the wide reach send (WP F2)
An agent has fetch_terms (migration 23, API, a chip field in the agent
editor). The terms of a pull are the union over the enabled agents, none
when one of them has none. They go only to wide reads: a company tagged All
roles, or a reach ask_for drops as too many countries for a one host; a
board's {"terms": false} switches them off. SmartRecruiters q,
SuccessFactors q, Workday searchText, Phenom keywords, Eightfold query and
Oracle keyword declare query="one"; the runner makes one series per term
times the country series, drops the places first over MAX_SERIES, then the
terms. A term read is narrowed: it writes no ats:size, closes nothing and
prunes no per-role state, and the 10% guard holds it to its own
fingerprint. The Companies reach line counts the terms.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 11:59:27 +02:00

138 lines
5.8 KiB
Python

"""A board's own reach on the Companies list, and the readers' declarations
on the Sources page, through the JSON API. No network."""
from __future__ import annotations
from collections.abc import Iterator
from pathlib import Path
import pytest
from fastapi import FastAPI
from fastapi.testclient import TestClient
from app.config import Config, ServerConfig
from app.main import create_app
from app.models import Gate
from app.storage import Store
@pytest.fixture
def app(tmp_path: Path) -> FastAPI:
return create_app(Config(server=ServerConfig(host="127.0.0.1", port=0),
database=tmp_path / "roledesk.db"))
@pytest.fixture
def store(app: FastAPI) -> Store:
return app.state.store
@pytest.fixture
def client(app: FastAPI) -> Iterator[TestClient]:
with TestClient(app) as c:
yield c
@pytest.fixture
def bosch(store: Store) -> int:
"""A big SmartRecruiters board and an agent that wants Denmark or remote."""
store.save_agent("Wolf", gates=[Gate(field="country", values=["DK"], mode="any"),
Gate(field="remote", values=["remote"], mode="any")])
sub = store.add_subscription("ats", "smartrecruiters:BoschGroup", label="Bosch",
company="Bosch", board="smartrecruiters",
params={"note": "kept"})
store.add_company("Bosch")
store.state["ats:size:smartrecruiters:BoschGroup"] = "4830"
return sub.id
def _row(client: TestClient) -> dict[str, object]:
return next(c for c in client.get("/api/companies").json() if c["key"] == "bosch")
def test_a_company_row_says_what_its_board_is_asked_for(client: TestClient, bosch: int) -> None:
row = _row(client)
# SmartRecruiters cannot ask for remote, so only the country is asked.
assert (row["reach"], row["reach_params"]) == ("DK · agents", {})
def test_a_boards_own_reach_is_kept_in_its_params_and_wins(
client: TestClient, store: Store, bosch: int) -> None:
res = client.post(f"/api/sources/{bosch}/reach",
json={"mode": "own", "countries": ["de", "se"], "remote": True})
assert res.status_code == 200, res.text
assert res.json()["reach"] == "DE, SE · set here"
sub = store.subscription(bosch)
assert sub is not None
assert sub.params == {"note": "kept", "countries": ["DE", "SE"], "remote": True}
assert _row(client)["reach_params"] == {"countries": ["DE", "SE"], "remote": True}
res = client.post(f"/api/sources/{bosch}/reach", json={"mode": "agents"})
assert res.json()["reach"] == "DK · agents"
assert store.subscription(bosch).params == {"note": "kept"} # type: ignore[union-attr]
@pytest.mark.parametrize("body, status", [
({"mode": "own"}, 422),
({"mode": "own", "countries": ["XX"]}, 422),
({"mode": "own", "countries": ["EU"]}, 422),
({"mode": "nowhere"}, 422),
({"mode": "all"}, 422), # every role is the company's tag now
])
def test_a_reach_that_names_nothing_is_refused(
client: TestClient, bosch: int, body: dict[str, object], status: int) -> None:
assert client.post(f"/api/sources/{bosch}/reach", json=body).status_code == status
def test_a_search_or_an_unknown_board_has_no_reach(client: TestClient, store: Store) -> None:
search = store.add_subscription("search", "jobindex:x", label="X", board="jobindex",
params={"q": "python"})
assert client.post(f"/api/sources/{search.id}/reach",
json={"mode": "agents"}).status_code == 404
assert client.post("/api/sources/999/reach", json={"mode": "agents"}).status_code == 404
#: Every state a job may be in, for a "was one queued at all" check that does
#: not race a background worker draining it.
_ANY_STATE = ("queued", "running", "done", "failed", "cancelled")
def test_the_all_roles_tag_reads_the_wide_reach_and_on_queues_a_pull(
client: TestClient, app: FastAPI, store: Store, bosch: int) -> None:
queue = app.state.queue
res = client.post("/api/companies/bosch/all-roles", json={"on": True})
assert res.status_code == 200, res.text
assert res.json()["all_roles"] == 1
# On reopens roles, so it queues a pull as switching a board on does.
assert queue.jobs.last("pull", states=_ANY_STATE) is not None
row = _row(client)
# Wolf requires no place, so the wide reach is everywhere.
assert (row["all_roles"], row["reach"]) == (True, "all roles · company tag")
# A board's own countries win over the tag.
client.post(f"/api/sources/{bosch}/reach", json={"mode": "own", "countries": ["DE"]})
assert _row(client)["reach"] == "DE · set here"
def test_turning_the_tag_off_queues_nothing(
client: TestClient, app: FastAPI, store: Store, bosch: int) -> None:
store.set_all_roles("bosch", True)
res = client.post("/api/companies/bosch/all-roles", json={"on": False})
assert res.status_code == 200 and res.json()["all_roles"] == 0
assert app.state.queue.jobs.last("pull", states=_ANY_STATE) is None
assert _row(client)["reach"] == "DK · agents"
assert client.post("/api/companies/nobody/all-roles", json={"on": True}).status_code == 404
def test_the_readers_are_listed_with_what_they_declare(client: TestClient) -> None:
found = {r["name"]: r for r in client.get("/api/readers").json()}
assert found["workday"] == {
"name": "workday", "label": "Workday", "scope": "board", "listing": "paged",
"text": "detail", "filters": {"country": "ids", "remote": "ids", "query": "one"},
"places": "many",
"robots": found["workday"]["robots"]}
assert found["workday"]["robots"].startswith("read:")
assert found["greenhouse"]["filters"] == {}
scopes = [r["scope"] for r in client.get("/api/readers").json()]
assert scopes == sorted(scopes) # boards first, then the searches
assert "search" in scopes