"""The reach of a pull (`app.jobs.reach`): from the enabled agents' gates and favourites 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_favourite_is_read_across_the_require_reach(store: Store) -> None: wolf = _wolf(store) _board(store, "smartrecruiters:mistral", "Mistral AI") _board(store, "smartrecruiters:acme", "Acme") store.set_affinity(wolf, "mistral-ai", 2) plan = reach.plan(store) subs = {s.target: s for s in store.subscriptions(source="ats")} favourite, why = plan.why(subs["smartrecruiters:mistral"]) assert why == "favourite" and {"DE", "FR", "DK"} <= favourite.countries assert plan.why(subs["smartrecruiters:acme"]) == (plan.base, "agents") def test_a_company_starred_by_one_agent_keeps_the_others_reach(store: Store) -> None: wolf = _wolf(store) store.save_agent("Anywhere", gates=[Gate(field="country", values=["US"], mode="any")]) _board(store, "smartrecruiters:mistral", "Mistral AI") store.set_affinity(wolf, "mistral-ai", 2) favourite = reach.plan(store).favourites["mistral-ai"] assert "US" in favourite.countries and "FR" in favourite.countries @pytest.mark.parametrize("size, params, why", [ (299, {}, "small"), (None, {}, "unknown"), (5000, {"reach": "all"}, "override"), (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_read_before_sizes_were_kept_is_sized_by_its_open_roles(store: Store) -> None: _board(store, "workday:acme.wd3/Jobs", "Acme", size=None) [sub] = store.subscriptions(source="ats") store.state["ats:ok:workday:acme.wd3/Jobs"] = "run-0" assert reach.sizes(store, [sub]) == {"workday:acme.wd3/Jobs": 0} # A filtered board's open roles are not its size. store.state["ats:filtered:workday:acme.wd3/Jobs"] = "1" assert reach.sizes(store, [sub]) == {} 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" # The small board, read whole, keeps its size; the filtered one keeps its last. assert store.state["ats:size:smartrecruiters:tiny"] == "1" assert store.state["ats:size:smartrecruiters:BoschGroup"] == "1000" 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