"""A company's two fetch overrides (see "The agents set the reach of a pull" and "Fetch terms" in `docs/decisions.md`): All countries reads its big boards with the agents' wide reach, All roles with no role-type narrowing. The four combinations, the reach line, migration 24 and a merge. No network.""" from __future__ import annotations import json import sqlite3 import sys from pathlib import Path 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 reach # noqa: E402 from app.models import Gate # noqa: E402 from app.storage import Store # noqa: E402 from plugins.readers import Ask, _runner, phenom # noqa: E402 TERMS = ["engineer", "product", "data"] PHILIPS = "phenom:www.careers.philips.com/global/en" @pytest.fixture def store(tmp_path: Path) -> Store: found = Store(tmp_path / "roledesk.db") # Agent 2 of 2026-09-30: Europe required; Denmark, Sweden or remote. found.save_agent("Wolf", fetch_terms=TERMS, gates=[ Gate(field="place", values=["Europe"], mode="require"), Gate(field="country", values=["DK", "SE"], mode="any"), Gate(field="remote", values=["remote"], mode="any")]) return found def _board(store: Store, target: str, company: str, size: int = 5000) -> None: store.add_subscription("ats", target, label=company, company=company, board=target.partition(":")[0]) store.add_company(company) store.state[f"ats:size:{target}"] = str(size) def _read(store: Store, target: str) -> tuple[Ask, str]: """What the next pull asks the board for, and the Companies list's line.""" plan = reach.plan(store) sub = next(s for s in store.subscriptions(source="ats") if s.target == target) return _runner.ask_for(phenom, sub.params, plan.for_subscription(sub)), reach.line(plan, sub) @pytest.mark.parametrize("countries, roles, line", [ (False, False, "follows agents: DK, SE · remote not asked on Phenom · all roles"), (False, True, "follows agents: DK, SE · remote not asked on Phenom · all roles"), (True, False, "all countries (Europe) · remote not asked on Phenom · follows agents' terms: data, engineer, product"), (True, True, "all countries (Europe) · remote not asked on Phenom · all roles"), ], ids=["neither", "all roles alone", "all countries alone", "both"]) def test_the_four_combinations(store: Store, countries: bool, roles: bool, line: str) -> None: _board(store, PHILIPS, "Philips") store.set_all_countries("philips", countries) store.set_all_roles("philips", roles) ask, said = _read(store, PHILIPS) assert said == line # Phenom asks for countries, many in one request, and not for remote. if countries: assert {"DE", "FR", "DK", "SE"} <= set(ask.countries) and len(ask.countries) > 30 else: assert ask.countries == ("DK", "SE") # Terms go only with the wide reach, and never to a company set to All roles. assert ask.terms == (("data", "engineer", "product") if countries and not roles else ()) def test_all_roles_alone_changes_nothing_today(store: Store) -> None: _board(store, PHILIPS, "Philips") before = _read(store, PHILIPS) store.set_all_roles("philips", True) assert _read(store, PHILIPS) == before @pytest.mark.parametrize("target, size, line", [ (PHILIPS, 120, "under 300 roles: read whole"), ("greenhouse:acme", 5000, "Greenhouse cannot filter: read whole"), ], ids=["small", "greenhouse"]) def test_a_board_read_whole_says_why_whatever_the_overrides( store: Store, target: str, size: int, line: str) -> None: _board(store, target, "Acme", size) for countries, roles in ((False, False), (True, False), (True, True)): store.set_all_countries("acme", countries) store.set_all_roles("acme", roles) assert _read(store, target)[1] == line def test_a_merge_keeps_both_overrides(store: Store) -> None: store.add_company("Mistral") store.add_company("Mistral AI") store.set_all_countries("mistral", True) store.set_all_roles("mistral-ai", True) kept = store.merge_companies("mistral-ai", "mistral") assert (kept.all_countries, kept.all_roles) == (1, 1) # ------------------------------------------------------------ migration 24 def test_migration_24_splits_the_tag_and_moves_terms_off(tmp_path: Path) -> None: path = tmp_path / "old.db" store = Store(path) for name in ("Mistral AI", "Acme", "Plain"): store.add_company(name) store.add_subscription("ats", "workday:acme", label="Acme", company="Acme", board="workday", params={"terms": False, "note": "kept"}) store.add_subscription("ats", "workday:plain", label="Plain", company="Plain", board="workday", params={"countries": ["DK"]}) # A board whose company has no row: its params are cleaned, no flag lands. store.add_subscription("ats", "workday:ghost", label="Ghost", company="Ghost", board="workday", params={"terms": False}) with sqlite3.connect(path) as conn: conn.execute("ALTER TABLE companies DROP COLUMN all_countries") # Before 24, all_roles was the All roles tag, the wide reach. conn.execute("UPDATE companies SET all_roles=1 WHERE key='mistral-ai'") conn.execute("PRAGMA user_version = 23") migrated = Store(path) assert migrated.migrations_applied == [24, 25, 26, 27, 28, 29] flags = {c.key: (c.all_countries, c.all_roles) for c in migrated.companies()} assert flags == {"mistral-ai": (1, 1), "acme": (0, 1), "plain": (0, 0)} params = {s.target: s.params for s in migrated.subscriptions(source="ats")} assert params == {"workday:acme": {"note": "kept"}, "workday:plain": {"countries": ["DK"]}, "workday:ghost": {}} def test_migration_24_runs_twice_without_harm(tmp_path: Path) -> None: from app.storage.schema import _split_all_roles path = tmp_path / "db.db" store = Store(path) store.add_company("Acme") store.set_all_roles("acme", True) with sqlite3.connect(path) as conn: _split_all_roles(conn) row = conn.execute("SELECT all_countries, all_roles FROM companies").fetchone() assert json.dumps(list(row)) == "[0, 1]" def test_the_wide_reach_is_named_as_the_gates_name_it(store: Store) -> None: gaz = store.gazetteer() europe = Gate(field="country", values=["AT", "DK", "SE", "FR", "EU"], mode="require") nordics = Gate(field="country", values=["DK", "SE"], mode="require") anywhere = Gate(field="country", values=["DK"], mode="any") agents = [a.model_copy(update={"gates": [g]}) for a, g in ((store.agents()[0], europe), (store.agents()[0], nordics))] # Countries one by one are named by their continent; a short list as written. assert reach.wide_names(agents[:1], gaz) == "Europe" assert reach.wide_names(agents[1:], gaz) == "DK, SE" assert reach.wide_names([agents[0].model_copy(update={"gates": [anywhere]})], gaz) == "everywhere"