From dafca5435e04f655e5626386e8d96c37be75543b Mon Sep 17 00:00:00 2001 From: Wolf Date: Wed, 30 Sep 2026 10:03:41 +0200 Subject: [PATCH] Show and set each board's reach on the Companies list, list the readers' declarations on Sources, and document filtering at the source A company row says what a pull asks its board for; its editor sets the board's own reach (the agents', all roles, or countries and remote) through /api/sources/{id}/reach. /api/readers lists each reader's listing, text, filters, places and robots decision, folded under the Follow form. Co-Authored-By: Claude Opus 5.5 --- app/jobs/__init__.py | 6 ++- app/jobs/reach.py | 31 ++++++++++++ app/static/companies.js | 64 +++++++++++++++++++++++- app/static/sources.js | 44 +++++++++++++++++ app/web/api.py | 72 ++++++++++++++++++++++++++- plugins/README.md | 12 ++++- tests/test_web.py | 5 +- tests/test_web_reach.py | 107 ++++++++++++++++++++++++++++++++++++++++ 8 files changed, 333 insertions(+), 8 deletions(-) create mode 100644 tests/test_web_reach.py diff --git a/app/jobs/__init__.py b/app/jobs/__init__.py index 3f5b182..35ed510 100644 --- a/app/jobs/__init__.py +++ b/app/jobs/__init__.py @@ -12,13 +12,15 @@ task in `pipeline.TASKS` and the default schedule in `pipeline.SCHEDULES`. sources.py what a pull reads, changed by hand: boards and searches added, switched on or off discover.py which ATS a company's careers site runs on, from its pages + reach.py which countries a pull asks each big board for, from the agents """ from __future__ import annotations from typing import Any -from app.jobs import cascade, discover, embed, extract, pull, score, sources, stages, summarise +from app.jobs import (cascade, discover, embed, extract, pull, reach, score, sources, stages, + summarise) from app.jobs.pull import search_target, seed_searches from app.jobs.score import seed_agents @@ -38,4 +40,4 @@ def bootstrap(config_path: str = "") -> tuple[Any, Any]: __all__ = ["BOOTSTRAP", "bootstrap", "cascade", "discover", "embed", "extract", "pull", - "score", "search_target", "seed_agents", "seed_searches", "sources", "stages", "summarise"] + "reach", "score", "search_target", "seed_agents", "seed_searches", "sources", "stages", "summarise"] diff --git a/app/jobs/reach.py b/app/jobs/reach.py index e494b96..4b09be6 100644 --- a/app/jobs/reach.py +++ b/app/jobs/reach.py @@ -36,6 +36,8 @@ from typing import Any from app import places from app.models import Agent, Gate, Subscription, company_key from app.storage import Store +from plugins import readers +from plugins.readers import _runner #: A board that listed fewer roles than this when last read whole is read #: whole. @@ -233,3 +235,32 @@ def reaches(store: Store, subs: Iterable[Subscription]) -> Mapping[str, dict[str subs = list(subs) found = plan(store, subs) return {s.target: r for s in subs if (r := found.for_subscription(s)) is not None} + + +#: Why a board is read whole, as the Companies list says it. +_WHOLE = {"small": f"under {SMALL} roles", "unknown": "not read whole yet", + "override": "set here", "favourite": "favourite, too many countries to ask", + "agents": "an agent takes any place"} + + +def line(found: Plan, sub: Subscription) -> str: + """What a pull asks `sub`'s board for, once its reader has narrowed the + reach, in a few words: "DK, SE, remote · agents", "all roles · under 300 + roles", "all roles · Greenhouse cannot filter".""" + module = readers.HOSTS.get(sub.target.partition(":")[0]) + if module is None: + return "" + given, why = found.why(sub) + ask = _runner.ask_for(module, sub.params, given.params()) + if _runner.filtered(ask): + words = sorted(ask.countries) if len(ask.countries) <= 4 \ + else [f"{len(ask.countries)} countries"] + if ask.remote: + words.append("remote") + return f"{', '.join(words)} · {'set here' if why == 'override' else why}" + if why in ("agents", "favourite") and not given.everywhere: + if not module.READS.filters.declared(): + return f"all roles · {module.LABEL} cannot filter" + if why == "agents": + return f"all roles · {module.LABEL} cannot ask for these" + return f"all roles · {_WHOLE[why]}" diff --git a/app/static/companies.js b/app/static/companies.js index 6791537..966da97 100644 --- a/app/static/companies.js +++ b/app/static/companies.js @@ -17,6 +17,11 @@ * into the Follow form's `#b-host`, and a slug. Saving posts to * `/api/companies/{key}/board`, which reads the board once before keeping * it; Detach stops reading it and keeps the company. + * + * A row with a board says what a pull asks it for ("reads DK, SE · agents", + * `reach` from app/jobs/reach.py), and its editor has a second line that + * sets the board's own reach: the agents', all roles, or its own countries + * and remote, posted to `/api/sources/{id}/reach`. */ (function () { "use strict"; @@ -90,6 +95,7 @@ sub.appendChild(link); } sub.append(` · ${count(company.open_roles, "open role")}`); + if (company.reach) sub.append(` · reads ${company.reach}`); if (company.failing_since) { // The board has failed on every pull since then; after 21 days its // roles close. @@ -199,6 +205,58 @@ return form; } + /** The open row's reach editor, for a row with a board: the agents' reach, + all roles, or the board's own countries and remote. */ + function reachEditor(company, rerender) { + if (company.subscription_id == null) return null; + const own = company.reach_params || {}; + const form = document.createElement("form"); + form.className = "board-edit"; + form.autocomplete = "off"; + const mode = document.createElement("select"); + mode.setAttribute("aria-label", `What ${company.name}'s board is asked for`); + for (const [value, label] of [["agents", "Agents' reach"], ["all", "All roles"], + ["own", "These countries"]]) { + mode.appendChild(new Option(label, value)); + } + mode.value = own.reach === "all" ? "all" : (own.countries || own.remote != null ? "own" : "agents"); + const codes = document.createElement("input"); + codes.type = "text"; + codes.spellcheck = false; + codes.placeholder = "DK SE"; + codes.value = (own.countries || []).join(" "); + codes.setAttribute("aria-label", "Country codes"); + const remote = document.createElement("select"); + remote.setAttribute("aria-label", "Remote roles"); + remote.append(new Option("and remote", "yes"), new Option("no remote", "no")); + remote.value = own.remote ? "yes" : "no"; + const save = document.createElement("button"); + save.type = "submit"; + save.textContent = "Set reach"; + const shown = () => { codes.hidden = remote.hidden = mode.value !== "own"; }; + mode.addEventListener("change", shown); + shown(); + form.append(mode, codes, remote, save); + form.addEventListener("submit", async (event) => { + event.preventDefault(); + save.disabled = true; + try { + const result = await api(`/api/sources/${company.subscription_id}/reach`, { + method: "POST", + body: { mode: mode.value, countries: codes.value.split(/[\s,]+/).filter(Boolean), + remote: remote.value === "yes" }, + }); + window.toast.info(`${company.name}: reads ${result.reach} from the next pull.`); + editing = null; + await loadCompanies(agentId()); + rerender(); + } catch (err) { + save.disabled = false; + } + }); + return form; + } + /** The board switch, or nothing for a company with no board: there is no subscription to switch. */ function onSwitch(company, rerender) { @@ -274,7 +332,11 @@ row.appendChild(affinityControl(company.key, company.affinity, async () => render(), agent)); } row.appendChild(subLine(company, render)); - if (editing === company.key) row.appendChild(boardEditor(company, render)); + if (editing === company.key) { + row.appendChild(boardEditor(company, render)); + const reach = reachEditor(company, render); + if (reach) row.appendChild(reach); + } body.appendChild(row); } const open = rows.reduce((sum, c) => sum + c.open_roles, 0); diff --git a/app/static/sources.js b/app/static/sources.js index f2b484d..c986b8b 100644 --- a/app/static/sources.js +++ b/app/static/sources.js @@ -15,6 +15,11 @@ * * It is also the page's first script, so it shows the one section of the * four the URL fragment names and marks it in the settings rail. + * + * Under the Follow form of Company boards, a fold lists what each reader + * declares (`/api/readers`): how its list arrives, where the text is, what + * it filters at the source, how it states places, and the robots.txt + * decision taken when it was built. */ (function () { "use strict"; @@ -277,6 +282,44 @@ } } + // ----------------------------------------------------------------- readers + + /** "country one, remote ids", or "none". */ + function filters(reader) { + const shown = Object.entries(reader.filters).map(([name, how]) => `${name} ${how}`); + return shown.length ? shown.join(", ") : "none"; + } + + function renderReaders(readers) { + const fold = document.createElement("details"); + fold.className = "prune"; + fold.id = "readers"; + const summary = document.createElement("summary"); + summary.textContent = `What each reader does (${readers.length})`; + const list = document.createElement("div"); + list.className = "srows"; + for (const reader of readers) { + const row = document.createElement("div"); + row.className = "srow"; + const name = document.createElement("span"); + name.className = "name"; + const label = document.createElement("span"); + label.textContent = reader.label; + const scope = document.createElement("span"); + scope.className = "badge"; + scope.textContent = reader.scope === "search" ? "job board" : "company board"; + name.append(label, scope); + const sub = document.createElement("span"); + sub.className = "sub"; + sub.textContent = [`list ${reader.listing}`, `text ${reader.text}`, + `filters ${filters(reader)}`, `places ${reader.places}`, reader.robots].join(" · "); + row.append(name, sub); + list.appendChild(row); + } + fold.append(summary, list); + $("#board-form").after(fold); + } + // ------------------------------------------------------ one section shown // Sources shows one section: the one the fragment names or contains, or the @@ -317,6 +360,7 @@ document.addEventListener("DOMContentLoaded", async () => { if (!$("#searches-list")) return; + if ($("#board-form")) api("/api/readers").then(renderReaders, () => {}); try { await load(); } catch (err) { diff --git a/app/web/api.py b/app/web/api.py index 4afc9fd..0e2ccbf 100644 --- a/app/web/api.py +++ b/app/web/api.py @@ -471,6 +471,10 @@ class CompanyOut(BaseModel): from_search: bool = False hand_reason: str = "" by_hand: bool = False + #: The board's own reach in its params (`jobs.reach.OVERRIDE_KEYS`), and + #: what a pull asks its board for (`jobs.reach.line`). + reach_params: dict[str, Any] = {} + reach: str = "" def _company_boards(store: Store) -> dict[str, tuple[int, str, bool, str]]: @@ -493,10 +497,13 @@ def _company_outs(store: Store, agent: int | None) -> list[CompanyOut]: ever_boarded = {company_key(s.company) for s in store.subscriptions(source="ats")} counts = store.open_role_counts() failing = jobs.pull.failing_boards(store) + subs = {s.id: s for s in store.subscriptions(source="ats")} + plan = jobs.reach.plan(store, subs.values()) out = [] for c in store.companies(agent): sub_id, host, on, target = boards.get(c.key, (None, "", None, "")) since, reason = failing.get(target, ("", "")) + sub = subs.get(sub_id) if sub_id is not None else None from_search = jobs.sources.known_from_search_only(c, ever_boarded) out.append(CompanyOut(key=c.key, name=c.name, notes=c.notes, board=host, subscription_id=sub_id, on=on, affinity=c.affinity, @@ -506,7 +513,10 @@ def _company_outs(store: Store, agent: int | None) -> list[CompanyOut]: watch=c.watch, careers_opened_at=c.careers_opened_at, failing_since=since, failing_reason=reason, from_search=from_search, hand_reason=c.hand_reason, - by_hand=c.key not in boards and not from_search)) + by_hand=c.key not in boards and not from_search, + reach_params={k: v for k, v in (sub.params if sub else {}).items() + if k in jobs.reach.OVERRIDE_KEYS}, + reach=jobs.reach.line(plan, sub) if sub else "")) return out @@ -800,6 +810,66 @@ def switch_source(sub_id: int, body: SwitchBody, store: StoreDep, return {"source": _one_source(store, sub_id), "closed": closed} +class ReachBody(BaseModel): + #: `agents`: the enabled agents' reach; `all`: every role; `own`: the + #: `countries` (ISO codes) and remote given here. + mode: Literal["agents", "all", "own"] + countries: list[str] = [] + remote: bool = False + + +@router.post("/sources/{sub_id}/reach") +def set_reach(sub_id: int, body: ReachBody, store: StoreDep) -> dict[str, Any]: + """A board's own reach, which wins over the agents' (see + `app/jobs/reach.py`), or none. The next pull reads the board so; a + narrower reach closes the roles outside it then, except those with a + vote or an application.""" + sub = store.subscription(sub_id) + if sub is None or sub.source != "ats": + raise HTTPException(404, f"no such board: {sub_id}") + codes = sorted({c.strip().upper() for c in body.countries if c.strip()}) + if unknown := [c for c in codes if not places.known(f"country:{c}") or c == "EU"]: + raise HTTPException(422, f"Not a country code: {', '.join(unknown)}.") + if body.mode == "own" and not (codes or body.remote): + raise HTTPException(422, "Name a country or remote, or read all roles.") + params = {k: v for k, v in sub.params.items() if k not in jobs.reach.OVERRIDE_KEYS} + if body.mode == "all": + params["reach"] = "all" + elif body.mode == "own": + params |= {"countries": codes, "remote": body.remote} + sub = store.update_subscription(sub_id, params=params) + return {"source": _one_source(store, sub_id), + "reach": jobs.reach.line(jobs.reach.plan(store, [sub]), sub)} + + +class ReaderOut(BaseModel): + """What a reader declares (`plugins/readers/_contract.py`), for the + Sources page: `filters` holds only the dimensions it filters.""" + + name: str + label: str + scope: str + listing: str + text: str + filters: dict[str, str] + places: str + robots: str + + +@router.get("/readers") +def list_readers() -> list[ReaderOut]: + """Every reader that declares `READS`, boards first, by label.""" + from plugins import readers + + found = [m for m in readers.READERS.values() if isinstance(getattr(m, "READS", None), + readers.Reads)] + return [ReaderOut(name=m.NAME, label=m.LABEL, scope=m.READS.scope, + listing=m.READS.listing, text=m.READS.text, + filters=m.READS.filters.declared(), places=m.READS.places, + robots=m.READS.robots) + for m in sorted(found, key=lambda m: (m.READS.scope != "board", m.LABEL.lower()))] + + class SearchBody(BaseModel): provider: str name: str = "" diff --git a/plugins/README.md b/plugins/README.md index 50951c6..cf69506 100644 --- a/plugins/README.md +++ b/plugins/README.md @@ -69,9 +69,11 @@ Numbers are from the live database after the pull of 2026-09-29. For the "list, | robots.txt | read before the list for the 14 hosts that name a page: Workday, SuccessFactors, Oracle, Phenom, Eightfold, Jibe, Gem, BambooHR, Pinpoint, Deel, Jobylon, Rippling, HiBob, HR Manager; kept for one day per board | A disallow fails the board with "robots.txt disallows it" and counts toward the 21 days. An unreachable file (5xx, 429, timeout) fails the board for this pull and is asked again next pull. A 4xx means no rules, which allows. `Crawl-delay` is not honoured. The eight API hosts (Greenhouse, Lever, Ashby, Workable, SmartRecruiters, Recruitee, Personio, Teamtailor) and the search providers read no robots.txt. | | 429 | first page: the board fails; a later page: the read stops, keeps what it read and is incomplete; a per-role read: the per-role reads of that board stop until the next pull | Nothing is closed on a list cut short by a 429. A search that fails is skipped for the pull. | +How far a board is read: a pull asks each board over 300 roles for the reach of the search agents that are switched on (`app/jobs/reach.py`): the union of their `any` gates on country, town (as its country) and remote, or every role when one agent can take a role from anywhere. A company starred as favourite is asked for its agent's `require` reach, Europe today; a host that asks one country a request reads a favourite whole instead of 40 times. A board's own reach, set on the Companies list, wins. A complete filtered read closes the roles outside the reach, except those with a vote or an application. + What no source does: -- Filter by country or place on the host's side. Every board is read whole; only the search providers take a region. +- Filter on the host's side for a host other than SmartRecruiters, SuccessFactors, Workday, Oracle, Phenom and Eightfold. Greenhouse, Ashby, Recruitee and the rest are read whole. - Read more than one board per company. DFDS is read from its Danish Teamtailor board only. - Use a key taken from a site's script. Maersk, New Work and Welcome to the Jungle are on the Watchlist for that reason. - Read a page that needs a browser to run its script. @@ -223,6 +225,7 @@ What no source does: | Remote | `location.remote`, true or false (122 remote) | | Pay | none: the list states no pay | | Found by discovery | yes, from a `jobs.`, `careers.` or `api.smartrecruiters.com` link; a guessed name is probed | +| Filter at the source | `?country=dk`, one country a request; remote is ignored by the API, so a remote role outside the reach is not read. Bosch DK: 2 of 4,830 (2026-09-30). Which countries is the agents' reach, see below | | Limitations | 10,000 roles per board (100 pages). The identifier is read without case, so a lowercased slug works. An unknown identifier answers an empty list, not an error: a mistyped slug reads as a complete board with no roles and would close a company's roles. | The reader's "Fetch the posting" button shows for these roles, since their description is empty; it reads one posting page on demand and does not feed `embed` or `extract`. Whether it reads a SmartRecruiters page cleanly was not checked. @@ -307,6 +310,7 @@ The reader's "Fetch the posting" button shows for these roles, since their descr | Remote | none | | Pay | none | | Found by discovery | yes, from a link to a role, `/go/` page or search on the site's own host; a link to the SAP back end names the host only; not probed. A site on the company's own domain whose pages link only the back end is set by hand | +| Filter at the source | `optionsFacetsDD_country=DK`, one country a request; no remote facet. Vestas DK: 64 of 668 (2026-09-30). Which countries is the agents' reach, see below | | Limitations | Only the default locale's roles are listed: a role that exists only in another locale (Carlsberg's French brand, 37 of 105) is not read. Only `/search/` and `/job/` are read, never `/services/` or the `successfactors.eu` back end, so a company with only the back end (Bang & Olufsen) cannot be read. 100 pages per board, so 1,000 to 10,000 roles depending on the page size. A third layout or a bot wall that answers 200 fails the board with "not a SuccessFactors search page". Read from this machine only; not yet from the deployed server. | ### Workday @@ -323,6 +327,7 @@ The reader's "Fetch the posting" button shows for these roles, since their descr | Remote | `remoteType`, where the tenant sends it: remote, hybrid, onsite (585 with a value) | | Pay | none: no pay field; pay appears only inside the text | | Found by discovery | yes, from a `myworkdayjobs.com` link with a site; not probed | +| Filter at the source | the tenant's own facets in `appliedFacets`, ids read by `facets` once a day: the facet whose values are countries, and any value that reads as remote. Two facets intersect, so remote is a series of its own. LEGO DK: 97 of 455 (2026-09-30). A tenant with no country facet is read whole. Which countries is the agents' reach, see below | | Limitations | 2,000 roles per board (100 pages of 20). A tenant has several sites and each is its own board. robots.txt paths are matched without case, as Workday serves any case. Every tenant is behind Cloudflare and was read from this machine only. | ### Oracle Recruiting Cloud @@ -339,6 +344,7 @@ The reader's "Fetch the posting" button shows for these roles, since their descr | Remote | `WorkplaceTypeCode`: remote, hybrid, onsite (741 with a value) | | Pay | none: the resource states no pay | | Found by discovery | yes, from a candidate site or role link on the pod, or a REST call naming `siteNumber`; not probed | +| Filter at the source | `selectedLocationsFacet` with the pod's geography ids, and `selectedWorkplaceTypesFacet=ORA_REMOTE` as a series of its own. `locationsFacet` lists the ten biggest countries only, so a country missing from a full list is read unfiltered: fa-evmr (Nokia) has neither DK nor SE listed and is read whole. Which countries is the agents' reach, see below | | Limitations | The host is the tenant's Fusion pod, not the company's domain (`jobs.nokia.com` redirects to it). Links are built for locale `en`. The department fields were empty on every role of the four boards. | ### Phenom @@ -355,6 +361,7 @@ The reader's "Fetch the posting" button shows for these roles, since their descr | Remote | per role only, from the page's `checkRemote` (120 with a value) | | Pay | none | | Found by discovery | yes, from a `///search-results` link; a link to Phenom itself names the host only; not probed | +| Filter at the source | `selected_fields.country`, a list of English names in one request; no remote. Philips NL and DE: 111 of 873 (2026-09-30). Which countries is the agents' reach, see below | | Limitations | The locale is built as `_`, the spelling of the three sites read; another site may spell it differently. 10,000 roles per board. A role page without a description fails the read and is retried on a later pull. | ### Eightfold @@ -371,7 +378,8 @@ The reader's "Fetch the posting" button shows for these roles, since their descr | Remote | `workLocationOption`: onsite, hybrid, remote (628 with a value) | | Pay | none | | Found by discovery | yes, from an `eightfold.ai` link that carries `domain=`; a bare `eightfold.ai` link names the host only; not probed | -| Limitations | With 10 per request and 100 pages, a board over 1,000 roles is never read to the end; Infineon (1,289) and Microsoft (about 2,300) are not attached for that reason. The search takes a location filter, but a target has nowhere to state one yet (planned in wave 2). Verified on three tenants only. | +| Filter at the source | `location=`, one country a request; no remote. Ericsson SE: 154 of 618 (2026-09-30). Which countries is the agents' reach, see below | +| Limitations | With 10 per request and 100 pages, a board over 1,000 roles is never read to the end; unless the country filter brings it under the cap: Infineon (1,289, 163 in DK and SE) and Microsoft (about 2,300, 60) become readable that way. Verified on three tenants only. | ### Gem diff --git a/tests/test_web.py b/tests/test_web.py index 4d62051..6b2d1f4 100644 --- a/tests/test_web.py +++ b/tests/test_web.py @@ -537,7 +537,8 @@ def test_companies_lists_board_and_open_role_count( "ats": "greenhouse", "ats_slug": "", "careers_url": "", "country": "DK", "city": "", "domain": "", "watch": None, "careers_opened_at": "", "failing_since": "", "failing_reason": "", - "from_search": False, "hand_reason": "", "by_hand": False} + "from_search": False, "hand_reason": "", "by_hand": False, + "reach_params": {}, "reach": "all roles · not read whole yet"} [other] = [c for c in res.json() if c["key"] == "other"] assert (other["board"], other["subscription_id"], other["on"], other["ats"]) == ( "", None, None, "") @@ -1386,7 +1387,7 @@ def test_a_company_is_added_by_hand_with_a_reason_and_a_link( "ats": "", "ats_slug": "", "careers_url": "https://revolut.example/careers", "country": "", "city": "", "watch": None, "careers_opened_at": "", "failing_since": "", "failing_reason": "", "from_search": False, - "hand_reason": "added", "by_hand": True} + "hand_reason": "added", "by_hand": True, "reach_params": {}, "reach": ""} assert store.company("revolut").hand_reason == "added" # type: ignore[union-attr] other = client.post("/api/companies", json={"name": "Meta", "hand_reason": "blocked"}) diff --git a/tests/test_web_reach.py b/tests/test_web_reach.py new file mode 100644 index 0000000..b3353e4 --- /dev/null +++ b/tests/test_web_reach.py @@ -0,0 +1,107 @@ +"""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": "all"}) + assert res.json()["reach"] == "all roles · set here" + assert store.subscription(bosch).params == {"note": "kept", "reach": "all"} # type: ignore[union-attr] + + 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), +]) +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": "all"}).status_code == 404 + assert client.post("/api/sources/999/reach", json={"mode": "all"}).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"}, "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