Files
jobboard/tests/test_sources.py
T

340 lines
15 KiB
Python

"""A company's board, set by hand, by discovery or by the seed: `set_board`
and the three ways in that go through it."""
from __future__ import annotations
import json
import sys
from datetime import UTC, datetime
from pathlib import Path
from typing import Any
import pytest
ROOT = Path(__file__).resolve().parents[1]
if str(ROOT) not in sys.path:
sys.path.insert(0, str(ROOT))
import plugins.ats as ats # noqa: E402
from desk.models import Item # noqa: E402
from app.cli import main # noqa: E402
from app.jobs import sources # noqa: E402
from app.jobs.discover import Discovery # noqa: E402
from app.storage import Store # noqa: E402
@pytest.fixture
def store(tmp_path: Path) -> Store:
return Store(tmp_path / "t.db")
class Boards:
"""A fake `fetch_board`: `listed` maps a target to how many roles it
lists; any other target does not answer. Remembers what was fetched."""
def __init__(self, **listed: int) -> None:
self.listed = {k.replace("_", ":", 1): v for k, v in listed.items()}
self.fetched: list[str] = []
def __call__(self, target: str) -> tuple[list[dict[str, Any]], bool]:
self.fetched.append(target)
if target not in self.listed:
raise OSError("connection refused")
return [{"source_id": str(i)} for i in range(self.listed[target])], True
@pytest.fixture
def boards(monkeypatch: pytest.MonkeyPatch) -> Boards:
fake = Boards(greenhouse_acme=3, ashby_acme=2, lever_quiet=0, lever_acme=1)
monkeypatch.setattr(ats, "fetch_board", fake)
return fake
def posting(source_id: str) -> Item:
item = Item(source_id=source_id, location=f"https://boards.example/acme/{source_id}",
title="Backend Engineer", body="",
published_at=datetime(2026, 9, 20, tzinfo=UTC), raw={})
item.source = "ats"
return item
# --------------------------------------------------------------- set_board
def test_set_board_creates_a_company_with_its_board(store: Store, boards: Boards) -> None:
sub, roles, closed = sources.set_board(store, "acme", "greenhouse:acme", name="Acme",
domain="acme.example")
assert (sub.target, roles, closed) == ("greenhouse:acme", 3, 0)
acme = store.company("acme")
assert acme is not None
assert (acme.name, acme.ats, acme.ats_slug, acme.domain, acme.careers_url) == (
"Acme", "greenhouse", "acme", "acme.example", "https://job-boards.greenhouse.io/acme")
def test_set_board_attaches_a_board_to_a_row_without_one(store: Store, boards: Boards) -> None:
store.add_company("Acme", domain="acme.example", ats="teamtailor", ats_slug="acme",
careers_url="https://acme.example/careers", notes="met them")
sub, roles, closed = sources.set_board(store, "acme", "ashby:acme", domain="other.example")
assert (sub.target, sub.company, roles, closed) == ("ashby:acme", "Acme", 2, 0)
acme = store.company("acme")
assert acme is not None
# The board's host and slug replace what discovery found; the rest of the
# row, domain included, is not overwritten.
assert (acme.ats, acme.ats_slug, acme.domain, acme.notes, acme.careers_url) == (
"ashby", "acme", "acme.example", "met them", "https://acme.example/careers")
def test_set_board_replaces_a_board_and_closes_its_open_roles(
store: Store, boards: Boards) -> None:
old, _, _ = sources.set_board(store, "acme", "greenhouse:acme", name="Acme")
store.upsert_role(posting("1"), old)
store.upsert_role(posting("2"), old)
new, roles, closed = sources.set_board(store, "acme", "lever:acme")
assert (new.id, new.target, roles, closed) == (old.id, "lever:acme", 1, 2)
assert store.roles() == []
acme = store.company("acme")
assert acme is not None
# careers_url was the old board's page, so it follows the board.
assert (acme.ats, acme.careers_url) == ("lever", "https://jobs.lever.co/acme")
def test_a_board_listing_nothing_is_kept(store: Store, boards: Boards) -> None:
sub, roles, _ = sources.set_board(store, "", "lever:quiet", name="Quiet")
assert (sub.target, roles) == ("lever:quiet", 0)
assert store.company("quiet") is not None
def test_a_board_that_does_not_answer_or_is_unknown_is_refused(
store: Store, boards: Boards) -> None:
with pytest.raises(ValueError, match="did not answer"):
sources.set_board(store, "", "greenhouse:gone", name="Gone")
with pytest.raises(ValueError, match="host:slug"):
sources.set_board(store, "", "workday:gone", name="Gone")
with pytest.raises(ValueError, match="needs a name"):
sources.set_board(store, "nobody", "greenhouse:acme")
with pytest.raises(ValueError, match="is not the company"):
sources.set_board(store, "acme", "greenhouse:acme", name="Other")
assert store.companies() == [] and store.subscriptions() == []
assert boards.fetched == ["greenhouse:gone"] # the others refused before it
def test_a_board_another_company_has_leaves_nothing_written(
store: Store, boards: Boards) -> None:
sources.set_board(store, "", "greenhouse:acme", name="Acme")
with pytest.raises(ValueError, match="already has"):
sources.add_board(store, "greenhouse:acme", "Beta")
assert store.company("beta") is None
def test_detach_board_closes_its_roles_and_keeps_the_row(store: Store, boards: Boards) -> None:
sub, _, _ = sources.set_board(store, "", "greenhouse:acme", name="Acme")
store.upsert_role(posting("1"), sub)
assert sources.detach_board(store, "acme") == 1
acme = store.company("acme")
assert acme is not None and (acme.ats, acme.ats_slug) == ("greenhouse", "acme")
assert store.company_boards() == {}
with pytest.raises(ValueError, match="no board"):
sources.detach_board(store, "acme")
with pytest.raises(KeyError):
sources.detach_board(store, "nope")
# ----------------------------------------------------------- add_discovered
def test_add_discovered_sets_or_replaces_the_board_of_an_existing_company(
store: Store, boards: Boards) -> None:
store.add_company("Acme", ats="teamtailor")
key, roles = sources.add_discovered(store, "Acme", "acme.example",
Discovery(board="greenhouse:acme", ats="greenhouse"))
assert (key, roles) == ("acme", 3)
key, roles = sources.add_discovered(store, "Acme", "acme.example",
Discovery(board="ashby:acme", ats="ashby"))
assert roles == 2
assert store.company_subscription("acme").target == "ashby:acme" # type: ignore[union-attr]
assert store.company("acme").domain == "acme.example" # type: ignore[union-attr]
def test_add_discovered_records_an_unreadable_host_with_its_slug(
store: Store, boards: Boards) -> None:
found = Discovery(ats="teamtailor", ats_slug="acme",
careers_url="https://acme.example/careers")
assert sources.add_discovered(store, "Acme", "acme.example", found) == ("acme", 0)
store.update_company("acme", notes="keep")
newer = Discovery(ats="workday", ats_slug="acme-inc",
careers_url="https://acme.example/jobs")
assert sources.add_discovered(store, "Acme", "acme.example", newer) == ("acme", 0)
acme = store.company("acme")
assert acme is not None
assert (acme.ats, acme.ats_slug, acme.careers_url, acme.notes) == (
"workday", "acme-inc", "https://acme.example/jobs", "keep")
def test_add_discovered_keeps_a_board_when_nothing_readable_was_found(
store: Store, boards: Boards) -> None:
sources.set_board(store, "", "greenhouse:acme", name="Acme")
with pytest.raises(ValueError, match="nothing changed"):
sources.add_discovered(store, "Acme", "acme.example", Discovery(ats="workday"))
acme = store.company("acme")
assert acme is not None and acme.ats == "greenhouse"
# --------------------------------------------------------------- the seed
def _seed(tmp_path: Path, *entries: dict[str, str]) -> Path:
path = tmp_path / "companies.json"
path.write_text(json.dumps({"companies": list(entries)}))
return path
def test_a_seed_rerun_attaches_a_board_the_seed_gained(
store: Store, boards: Boards, tmp_path: Path) -> None:
first = _seed(tmp_path,
{"name": "Acme", "domain": "acme.example", "ats": "teamtailor",
"careers_url": "https://acme.example/careers"},
{"name": "Beta", "board": "lever:beta"})
assert sources.seed_companies(store, first) == {
"added": 2, "boards": 1, "skipped": 0, "total": 2, "problems": []}
agent = store.save_agent("A")
store.set_affinity(agent.id, "acme", 2)
store.update_company("acme", notes="met them", status="contacted")
# The next seed found Acme's board, and names a new board for Beta.
later = _seed(tmp_path,
{"name": "Acme", "domain": "acme.example", "board": "ashby:acme"},
{"name": "Beta", "board": "lever:beta-new"})
assert sources.seed_companies(store, later) == {
"added": 0, "boards": 1, "skipped": 1, "total": 2, "problems": []}
assert store.company_subscription("acme").target == "ashby:acme" # type: ignore[union-attr]
acme = store.company("acme", agent.id)
assert acme is not None
assert (acme.affinity, acme.notes, acme.status, acme.ats) == (
2, "met them", "contacted", "ashby")
# A board a row already has is never replaced by the seed.
assert store.company_subscription("beta").target == "lever:beta" # type: ignore[union-attr]
# The seed's boards are not fetched: they were read when it was written.
assert boards.fetched == []
def test_a_seed_rerun_leaves_a_detached_board_detached(
store: Store, boards: Boards, tmp_path: Path) -> None:
seed = _seed(tmp_path, {"name": "Acme", "board": "greenhouse:acme"})
sources.seed_companies(store, seed)
sources.detach_board(store, "acme")
assert sources.seed_companies(store, seed)["skipped"] == 1
assert store.company_boards() == {}
def test_a_seed_board_that_clashes_still_lands_the_company(
store: Store, boards: Boards, tmp_path: Path) -> None:
seed = _seed(tmp_path, {"name": "Acme", "board": "greenhouse:acme"},
{"name": "Acme Two", "board": "greenhouse:acme", "country": "DK"})
result = sources.seed_companies(store, seed)
assert (result["added"], result["boards"]) == (2, 1)
[problem] = result["problems"]
assert problem.startswith("acme-two: another board already has 'greenhouse:acme'")
two = store.company("acme-two")
assert two is not None
assert (two.ats, two.ats_slug, two.country) == ("greenhouse", "acme", "DK")
assert store.company_subscription("acme-two") is None
# -------------------------------------------------------------------- CLI
def _cli(tmp_path: Path, *argv: str) -> int:
config = tmp_path / "config.yaml"
if not config.exists():
config.write_text(f"database: {tmp_path / 't.db'}\n")
return main(["-c", str(config), "companies", *argv])
def test_cli_set_board_attaches_and_replaces(
store: Store, boards: Boards, tmp_path: Path,
capsys: pytest.CaptureFixture[str]) -> None:
store.add_company("Acme", ats="teamtailor")
assert _cli(tmp_path, "set-board", "acme", "greenhouse:acme") == 0
assert json.loads(capsys.readouterr().out) == {
"key": "acme", "board": "greenhouse:acme", "roles": 3, "closed": 0}
assert _cli(tmp_path, "set-board", "acme", "lever:acme") == 0
assert json.loads(capsys.readouterr().out)["board"] == "lever:acme"
assert _cli(tmp_path, "set-board", "nobody", "lever:acme") == 1
assert "no company 'nobody'" in capsys.readouterr().err
assert _cli(tmp_path, "detach", "acme") == 0
assert json.loads(capsys.readouterr().out) == {"key": "acme", "closed": 0}
def test_cli_discover_add_updates_an_existing_company(
store: Store, boards: Boards, tmp_path: Path, monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str]) -> None:
from app.jobs import discover
store.add_company("Acme", ats="teamtailor")
monkeypatch.setattr(discover, "discover",
lambda name, domain: Discovery(board="ashby:acme", ats="ashby",
ats_slug="acme"))
assert _cli(tmp_path, "discover", "Acme", "acme.example", "--add") == 0
out = json.loads(capsys.readouterr().out)
assert (out["key"], out["board"], out["roles"]) == ("acme", "ashby:acme", 2)
assert store.company_subscription("acme").target == "ashby:acme" # type: ignore[union-attr]
def test_cli_seed_prints_counts_and_problems(
store: Store, boards: Boards, tmp_path: Path,
capsys: pytest.CaptureFixture[str]) -> None:
seed = _seed(tmp_path, {"name": "Odd", "board": "nosuch:odd"})
assert _cli(tmp_path, "seed", "--path", str(seed)) == 0
captured = capsys.readouterr()
assert json.loads(captured.out) == {"added": 1, "boards": 0, "skipped": 0, "total": 1}
assert "companies seed: odd: 'nosuch:odd' is not a known board" in captured.err
def test_replacing_or_detaching_a_board_forgets_that_it_was_failing(
store: Store, boards: Boards) -> None:
from app.jobs.pull import FAILING, failing_boards
sources.set_board(store, "acme", "greenhouse:acme", name="Acme")
store.state[FAILING + "greenhouse:acme"] = '{"since": "2026-09-01T00:00:00+00:00", "reason": "404 Not Found"}'
assert "greenhouse:acme" in failing_boards(store)
sources.set_board(store, "acme", "ashby:acme")
assert failing_boards(store) == {}
store.state[FAILING + "ashby:acme"] = '{"since": "2026-09-01T00:00:00+00:00", "reason": "timeout"}'
sources.detach_board(store, "acme")
assert failing_boards(store) == {}
def test_replacing_or_detaching_a_board_forgets_its_robots_and_detail_records(
store: Store, boards: Boards) -> None:
kept = ("ats:robots:", "ats:detailed:", "ats:detailfailed:")
sources.set_board(store, "acme", "greenhouse:acme", name="Acme")
for key in kept:
store.state[key + "greenhouse:acme"] = "[]"
store.state["ats:robots:lever:other"] = "{}"
sources.set_board(store, "acme", "ashby:acme")
assert not any(key + "greenhouse:acme" in store.state for key in kept)
assert "ats:robots:lever:other" in store.state
for key in kept:
store.state[key + "ashby:acme"] = "[]"
sources.detach_board(store, "acme")
assert not any(key + "ashby:acme" in store.state for key in kept)
def test_search_params_keeps_only_what_the_provider_reads() -> None:
assert sources.search_params("jobindex", " python ", area=1, region="CaRE_1nn_cSU") == {
"q": "python", "area": 1}
assert sources.search_params("platsbanken", "data", area=1, country="de") == {
"q": "data", "region": ""}
assert sources.search_params("platsbanken", "data", region=" CaRE_1nn_cSU ", pages=3) == {
"q": "data", "region": "CaRE_1nn_cSU", "pages": 3}
def test_search_params_refuses_a_region_that_is_no_swedish_lan() -> None:
with pytest.raises(ValueError, match="region"):
sources.search_params("platsbanken", "data", region="zYpL_aGk_QY8") # Midland
with pytest.raises(ValueError, match="provider"):
sources.search_params("indeed", "data")