"""The reach of a pull (`app.jobs.reach`): from the enabled agents' gates and All roles tags to each board's `reach`, and through the pull's batches to the runner's `Ask`. No network: the runner's listing is stubbed.""" from __future__ import annotations import sys from pathlib import Path from typing import ClassVar import pytest ROOT = Path(__file__).resolve().parents[1] if str(ROOT) not in sys.path: sys.path.insert(0, str(ROOT)) from pipeline import Tally # noqa: E402 from app.jobs import pull, reach # noqa: E402 from app.models import Gate # noqa: E402 from app.storage import Store # noqa: E402 from plugins.readers import Ask, _runner # noqa: E402 EUROPE = ["AT", "BE", "BG", "HR", "CY", "CZ", "DK", "EE", "FI", "FR", "DE", "GR", "HU", "IE", "IT", "LV", "LT", "LU", "MT", "NL", "PL", "PT", "RO", "SK", "SI", "ES", "SE", "IS", "LI", "NO", "GB", "CH", "EU"] class FakeConfig: sources: ClassVar[dict[str, object]] = {} searches: ClassVar[list[object]] = [] @pytest.fixture def store(tmp_path: Path) -> Store: return Store(tmp_path / "roledesk.db") def _wolf(store: Store, **extra: object) -> int: """The live agent 2 of 2026-09-30: Europe required, Denmark, Sweden, remote or a town in Skåne or Denmark.""" gates = [Gate(field="country", values=EUROPE, mode="require"), Gate(field="country", values=["DK", "SE"], mode="any"), Gate(field="remote", values=["remote"], mode="any"), Gate(field="place", values=["Malmö", "Lund", "copenhagen", "odense"], mode="any"), Gate(field="remote", values=["hybrid"], mode="prefer", weight=0.05)] return store.save_agent("Wolf", gates=gates, **extra).id # type: ignore[arg-type] def _board(store: Store, target: str, company: str, size: int | None = 1000, params: dict[str, object] | None = None) -> None: store.add_subscription("ats", target, label=company, company=company, board=target.partition(":")[0], params=params or {}) store.add_company(company) if size is not None: store.state[f"ats:size:{target}"] = str(size) # ------------------------------------------------------------------ gates def test_the_base_reach_is_the_union_of_the_any_gates_with_towns_as_countries( store: Store) -> None: _wolf(store) store.save_agent("Berlin", gates=[Gate(field="place", values=["Berlin"], mode="any")]) plan = reach.plan(store) assert plan.base == reach.Reach(frozenset({"DK", "SE", "DE"}), remote=True) assert plan.base.params() == {"countries": ["DE", "DK", "SE"], "remote": True} @pytest.mark.parametrize("gates", [ [], [Gate(field="country", values=["DK"], mode="any"), Gate(field="function", values=["engineering"], mode="any")], [Gate(field="remote", values=["hybrid"], mode="any")], [Gate(field="place", values=["Nowhereville"], mode="any")], ], ids=["no any gates", "a function in the group", "hybrid anywhere", "an unknown town"]) def test_an_agent_that_can_take_a_role_from_anywhere_makes_the_reach_everywhere( store: Store, gates: list[Gate]) -> None: _wolf(store) store.save_agent("Other", gates=gates) assert reach.plan(store).base == reach.EVERYWHERE def test_an_agent_switched_off_does_not_widen_the_reach(store: Store) -> None: _wolf(store) store.save_agent("Off", gates=[], enabled=False) assert reach.plan(store).base.countries == {"DK", "SE"} def test_with_no_agent_switched_on_every_board_is_read_whole(store: Store) -> None: _wolf(store, enabled=False) assert reach.plan(store).base == reach.EVERYWHERE def test_the_require_reach_intersects_and_expands_europe(store: Store) -> None: gaz = store.gazetteer() agent = store.agent(_wolf(store)) assert agent is not None europe = reach.require_reach(agent, gaz) assert {"DK", "SE", "DE", "FR", "GB", "NO", "CH"} <= europe.countries assert "US" not in europe.countries and "EU" not in europe.countries narrower = agent.model_copy(update={"gates": [ *agent.gates, Gate(field="country", values=["DK", "US"], mode="require")]}) assert reach.require_reach(narrower, gaz) == reach.Reach(frozenset({"DK"})) plain = agent.model_copy(update={"gates": []}) assert reach.require_reach(plain, gaz) == reach.EVERYWHERE # ------------------------------------------------------------- per board def test_a_company_tagged_all_roles_is_read_across_the_require_reach(store: Store) -> None: _wolf(store) _board(store, "workday:mistral.wd3/Jobs", "Mistral AI") _board(store, "workday:acme.wd3/Jobs", "Acme") store.set_all_countries("mistral-ai", True) plan = reach.plan(store) subs = {s.target.split(".")[0]: s for s in store.subscriptions(source="ats")} wide, why = plan.why(subs["workday:mistral"]) # Europe, and the base's remote, which a tag never reads less than. assert why == "countries" and {"DE", "FR", "DK"} <= wide.countries and wide.remote # The gate lists Europe's countries one by one; the line names the continent. assert reach.line(plan, subs["workday:mistral"]) == "all countries (Europe) · all roles" assert plan.why(subs["workday:acme"]) == (plan.base, "agents") def test_a_favourite_does_not_widen_the_fetch(store: Store) -> None: wolf = _wolf(store) _board(store, "smartrecruiters:mistral", "Mistral AI") store.set_affinity(wolf, "mistral-ai", 2) plan = reach.plan(store) [sub] = store.subscriptions(source="ats") assert plan.why(sub) == (plan.base, "agents") @pytest.mark.parametrize("host", ["smartrecruiters", "successfactors", "eightfold"]) def test_a_tag_too_wide_for_a_one_host_reads_the_board_whole(store: Store, host: str) -> None: _wolf(store) _board(store, f"{host}:mistral", "Mistral AI") store.set_all_countries("mistral-ai", True) [sub] = store.subscriptions(source="ats") assert reach.line(reach.plan(store), sub) == "too many countries to ask: read whole" def test_a_tag_asks_phenom_for_europe_in_one_request(store: Store) -> None: _wolf(store) _board(store, "phenom:mistral", "Mistral AI") store.set_all_countries("mistral-ai", True) [sub] = store.subscriptions(source="ats") found = reach.plan(store) assert reach.line(found, sub) == "all countries (Europe) · remote not asked on Phenom · all roles" def test_an_agent_without_a_required_place_makes_the_tag_everywhere(store: Store) -> None: _wolf(store) store.save_agent("Anywhere", gates=[Gate(field="country", values=["US"], mode="any")]) _board(store, "workday:mistral.wd3/Jobs", "Mistral AI") store.set_all_countries("mistral-ai", True) plan = reach.plan(store) [sub] = store.subscriptions(source="ats") assert plan.why(sub) == (reach.EVERYWHERE, "countries") assert plan.for_subscription(sub) is None assert reach.line(plan, sub) == "all countries (everywhere): read whole" def test_a_boards_own_reach_wins_over_the_tag(store: Store) -> None: _wolf(store) _board(store, "workday:mistral.wd3/Jobs", "Mistral AI", params={"countries": ["FR"]}) store.set_all_countries("mistral-ai", True) [sub] = store.subscriptions(source="ats") assert reach.line(reach.plan(store), sub) == "set here: FR · all roles" @pytest.mark.parametrize("size, params, why", [ (299, {}, "small"), (None, {}, "unknown"), (5000, {"countries": ["DE"]}, "override"), (300, {}, "agents"), ]) def test_small_unknown_and_overridden_boards_get_no_reach( store: Store, size: int | None, params: dict[str, object], why: str) -> None: _wolf(store) _board(store, "workday:acme.wd3/Jobs", "Acme", size, params) [sub] = store.subscriptions(source="ats") found, said = reach.plan(store).why(sub) assert said == why assert (found.params() is None) == (why != "agents") def test_a_board_has_only_the_size_it_was_given(store: Store) -> None: _board(store, "workday:acme.wd3/Jobs", "Acme", size=None) [sub] = store.subscriptions(source="ats") # A board read whole before sizes were kept is not sized by its open roles. store.state["ats:ok:workday:acme.wd3/Jobs"] = "run-0" assert reach.sizes(store, [sub]) == {} store.state["ats:size:workday:acme.wd3/Jobs"] = "12" assert reach.sizes(store, [sub]) == {"workday:acme.wd3/Jobs": 12} def test_describe_is_short() -> None: assert reach.Reach(frozenset({"SE", "DK"}), True).describe() == "DK, SE, remote" assert reach.Reach(frozenset(EUROPE[:10])).describe() == "10 countries" assert reach.EVERYWHERE.describe() == "everywhere" # ---------------------------------------------------------------- the pull def _row(job_id: str) -> dict[str, object]: return {"source_id": job_id, "title": f"Engineer {job_id}", "url": f"https://x.example/{job_id}", "places": [{"text": "Copenhagen", "remote": None}], "posted": None, "description": "", "remote": None, "salary_text": ""} def _pull(store: Store) -> list[dict[str, object]]: batches = pull._boards(FakeConfig(), store) # type: ignore[arg-type] results = [pull.run_batch(FakeConfig(), store, **batch) # type: ignore[arg-type] for batch in batches] pull.close_boards(FakeConfig(), store, Tally(results=tuple(results))) # type: ignore[arg-type] return batches def test_the_pull_hands_each_board_its_reach_and_the_runner_narrows_it( store: Store, monkeypatch: pytest.MonkeyPatch) -> None: _wolf(store) _board(store, "smartrecruiters:BoschGroup", "Bosch") _board(store, "smartrecruiters:tiny", "Tiny", size=12) asked: dict[str, Ask] = {} def fake(module: object, slug: str, get: object, ask: Ask, **kwargs: object ) -> _runner.Listing: asked[slug] = ask return _runner.Listing([_row("1")], True, ["1"], _runner.filtered(ask)) # type: ignore[list-item] monkeypatch.setattr(_runner, "read_listing", fake) [batch] = _pull(store) assert batch["reach"] == {"smartrecruiters:BoschGroup": {"countries": ["DK", "SE"], "remote": True}} # SmartRecruiters filters countries one a request and cannot ask for remote. assert asked["BoschGroup"] == Ask(countries=("DK", "SE")) assert asked["tiny"] == Ask() assert store.state["ats:filtered:smartrecruiters:BoschGroup"] == "1" # A pull rewrites no size: the small board read whole keeps its 12. assert store.state["ats:size:smartrecruiters:tiny"] == "12" assert store.state["ats:size:smartrecruiters:BoschGroup"] == "1000" def test_the_pull_reads_a_tagged_company_with_the_wide_reach( store: Store, monkeypatch: pytest.MonkeyPatch) -> None: _wolf(store) _board(store, "phenom:jobs.mistral.ai/global/en", "Mistral AI") _board(store, "smartrecruiters:bfl", "Black Forest Labs") store.set_all_countries("mistral-ai", True) store.set_all_countries("black-forest-labs", True) asked: dict[str, Ask] = {} def fake(module: object, slug: str, get: object, ask: Ask, **kwargs: object ) -> _runner.Listing: asked[slug] = ask return _runner.Listing([_row("1")], True, ["1"], _runner.filtered(ask)) # type: ignore[list-item] monkeypatch.setattr(_runner, "read_listing", fake) [batch] = _pull(store) phenom = "phenom:jobs.mistral.ai/global/en" assert set(batch["reach"]) == {phenom, "smartrecruiters:bfl"} europe = set(batch["reach"][phenom]["countries"]) assert {"DE", "FR", "DK"} <= europe assert set(asked["jobs.mistral.ai/global/en"].countries) == europe # Europe is more series than SmartRecruiters is asked one by one: whole. assert asked["bfl"] == Ask() assert not store.state.get("ats:filtered:smartrecruiters:bfl") def test_a_batch_queued_before_the_reach_existed_still_runs( store: Store, monkeypatch: pytest.MonkeyPatch) -> None: _board(store, "smartrecruiters:acme", "Acme") monkeypatch.setattr(_runner, "read_listing", lambda *a, **k: _runner.Listing( [_row("1")], True, ["1"])) # type: ignore[list-item] result = pull.run_batch(FakeConfig(), store, # type: ignore[arg-type] targets=["smartrecruiters:acme"], run="r") assert result["roles"] == 1