"""Board discovery: which ATS a careers site names, and in what order the site's pages are read.""" from __future__ import annotations import json from pathlib import Path import httpx import pytest from app.jobs import discover as mod from app.jobs.discover import Discovery, boards_in, careers_links, discover, slug_guesses from app.jobs.sources import seed_companies from app.storage import Store # ------------------------------------------------------------------ parsing def test_boards_in_names_readable_hosts_with_their_slug() -> None: page = """ Open roles One role Workable SR Recruitee Personio """ assert [(h, s) for h, s, _url in boards_in(page)] == [ ("greenhouse", "pleo"), ("lever", "spotify"), ("ashby", "deepl"), ("workable", "acme"), ("smartrecruiters", "bosch"), ("recruitee", "acme"), ("personio", "acme-gmbh")] def test_boards_in_keeps_hosts_the_app_cannot_read() -> None: page = """ Workday SF TT """ assert boards_in(page) == [ ("workday", "novo", "https://novo.wd3.myworkdayjobs.com"), ("successfactors", "", "performancemanager.successfactors.eu"), ("teamtailor", "acme", "https://acme.teamtailor.com")] def test_boards_in_dedupes_and_skips_the_api_paths_own_words() -> None: page = ('a' 'b') assert [(h, s) for h, s, _ in boards_in(page)] == [("greenhouse", "acme")] assert boards_in("nothing here") == [] def test_careers_links_keeps_on_site_links_that_read_as_careers() -> None: html = """ About Careers We are hiring Partner jobs Karriere Mail Careers again """ assert careers_links(html, "https://www.acme.com/") == [ "https://www.acme.com/careers", "https://jobs.acme.com/", "https://www.acme.com/en/karriere"] def test_slug_guesses_come_from_the_key_and_the_domain() -> None: assert slug_guesses("Trade Republic Bank GmbH", "www.traderepublic.com") == [ "trade-republic-bank", "traderepublicbank", "traderepublic"] assert slug_guesses("Pleo", "pleo.io") == ["pleo"] # ------------------------------------------------------------------- discover class Site: """A fake web: `pages` maps a URL to `(final URL, html)`; anything else is a page that did not answer. Remembers the order of fetches.""" def __init__(self, pages: dict[str, tuple[str, str] | str], boards: set[tuple[str, str]] = frozenset()) -> None: self.pages = {k: (v if isinstance(v, tuple) else (k, v)) for k, v in pages.items()} self.boards = boards self.fetched: list[str] = [] self.probed: list[tuple[str, str]] = [] def fetch(self, client: httpx.Client, url: str) -> tuple[str, str]: self.fetched.append(url) return self.pages.get(url, ("", "")) def probe(self, client: httpx.Client, host: str, slug: str) -> bool: self.probed.append((host, slug)) return (host, slug) in self.boards def run(site: Site, name: str = "Acme", domain: str = "acme.com") -> Discovery: with httpx.Client() as client: return discover(name, domain, fetch=site.fetch, probe_slug=site.probe, client=client) def test_a_board_named_on_the_home_page_ends_discovery_there() -> None: site = Site({"https://acme.com/": 'Jobs'}) assert run(site) == Discovery(board="lever:acme", ats="lever", ats_slug="acme", careers_url="https://acme.com/", evidence="jobs.lever.co/acme") assert site.fetched == ["https://acme.com/"] assert site.probed == [] def test_the_home_pages_own_careers_link_is_read_before_the_usual_paths() -> None: site = Site({ "https://acme.com/": 'Join us', "https://acme.com/company/join-us": ('https://acme.com/company/join-us', 'Open'), "https://acme.com/careers": 'no', }) found = run(site) assert found.board == "recruitee:acme" assert found.careers_url == "https://acme.com/company/join-us" assert site.fetched == ["https://acme.com/", "https://acme.com/company/join-us"] def test_a_careers_pages_own_link_is_followed_one_level_down() -> None: site = Site({ "https://acme.com/": "
no links
", "https://acme.com/careers": 'See open positions', "https://acme.com/careers/open-positions": '', }) assert run(site).board == "ashby:acme" assert site.fetched[:3] == ["https://acme.com/", "https://acme.com/careers", "https://acme.com/careers/open-positions"] def test_an_unreadable_host_named_by_a_page_is_the_answer_and_nothing_is_probed() -> None: site = Site({ "https://acme.com/": "welcome
", "https://acme.com/careers": 'Jobs', }, boards={("greenhouse", "acme")}) found = run(site) assert found == Discovery(ats="workday", ats_slug="acme", careers_url="https://acme.com/careers", evidence="https://acme.wd3.myworkdayjobs.com") assert not found.readable # The page named the host: a probe could only find another tenant's # board of the same name, like the greenhouse:acme this fake web has. assert site.probed == [] def test_a_readable_board_on_a_later_page_still_beats_an_unreadable_host() -> None: site = Site({ "https://acme.com/": '' 'Careers', "https://acme.com/careers": 'Jobs', }) assert run(site).board == "lever:acme" assert site.probed == [] def test_an_unreadable_hosts_slug_is_kept_when_a_later_link_names_it() -> None: """The CDN names teamtailor before any link does; the slug comes from the first link that carries one, the acme of acme.teamtailor.com.""" site = Site({ "https://acme.com/": '', "https://acme.com/careers": 'Jobs', }) found = run(site) assert (found.ats, found.ats_slug, found.board) == ("teamtailor", "acme-jobs", "") assert found.evidence == "https://acme-jobs.teamtailor.com" assert found.careers_url == "https://acme.com/" def test_slug_probes_find_a_board_the_pages_never_link() -> None: site = Site({"https://acme.com/": "a script draws the jobs
", "https://acme.com/jobs": ""}, boards={("ashby", "acme")}) found = run(site) assert found.board == "ashby:acme" assert found.careers_url == "https://acme.com/jobs" assert found.evidence == "https://api.ashbyhq.com/posting-api/job-board/acme" def test_a_site_that_never_answers_gives_an_empty_discovery() -> None: site = Site({}) assert run(site) == Discovery() assert len(site.fetched) <= mod.MAX_PAGES + 1 def test_a_domain_is_folded_to_its_site() -> None: site = Site({"https://acme.com/": 'Jobs'}) with httpx.Client() as client: found = discover("Acme", "https://www.acme.com/some/page", fetch=site.fetch, probe_slug=site.probe, client=client) assert found.board == "lever:acme" assert discover("Acme", "", fetch=site.fetch, probe_slug=site.probe) == Discovery() # ---------------------------------------------------------------- the seed @pytest.fixture def store(tmp_path: Path) -> Store: return Store(tmp_path / "t.db") def test_the_seed_keeps_a_company_the_app_cannot_read(store: Store, tmp_path: Path) -> None: seed = tmp_path / "companies.json" seed.write_text(json.dumps({"companies": [ {"name": "Pleo", "domain": "pleo.io", "country": "DK", "city": "Copenhagen", "board": "ashby:pleo"}, {"name": "Novo Nordisk", "domain": "novonordisk.com", "country": "DK", "city": "Bagsværd", "ats": "successfactors", "careers_url": "https://www.novonordisk.com/careers.html"}, {"name": "Odd", "board": "nosuch:odd"}, ]})) result = seed_companies(store, seed) assert result == {"added": 3, "boards": 1, "skipped": 0, "total": 3, "problems": ["odd: 'nosuch:odd' is not a known board; kept without it"]} pleo = store.company("pleo") assert pleo is not None assert (pleo.ats, pleo.ats_slug, pleo.country, pleo.city, pleo.careers_url) == ( "ashby", "pleo", "DK", "Copenhagen", "https://jobs.ashbyhq.com/pleo") [sub] = store.subscriptions(source="ats") assert (sub.target, sub.company) == ("ashby:pleo", "Pleo") novo = store.company("novo-nordisk") assert novo is not None assert (novo.ats, novo.careers_url) == ("successfactors", "https://www.novonordisk.com/careers.html") odd = store.company("odd") assert odd is not None and odd.ats == "" # Re-running adds nothing and edits nothing. assert seed_companies(store, seed)["skipped"] == 3