"""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, "", "taleo: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="taleo", 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 # The host and slug follow what discovery found last; the link, once # there, is kept, since a person may have typed it. assert (acme.ats, acme.ats_slug, acme.careers_url, acme.notes) == ( "taleo", "acme-inc", "https://acme.example/careers", "keep") def test_add_discovered_fills_a_link_only_where_there_is_none( store: Store, boards: Boards) -> None: store.add_company("Acme", ats="teamtailor") found = Discovery(ats="taleo", ats_slug="acme", careers_url="https://acme.example/jobs") sources.add_discovered(store, "Acme", "acme.example", found) assert store.company("acme").careers_url == "https://acme.example/jobs" # type: ignore[union-attr] store.update_company("acme", careers_url="https://typed.example/by-hand") sources.add_discovered(store, "Acme", "acme.example", found) assert store.company("acme").careers_url == "https://typed.example/by-hand" # type: ignore[union-attr] 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="taleo")) 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 def test_a_seed_rerun_fills_an_empty_link_and_reason_and_never_overwrites_one( store: Store, boards: Boards, tmp_path: Path) -> None: store.add_company("Bare", key="bare") store.add_company("Typed", key="typed", careers_url="https://typed.example/by-hand", hand_reason="blocked", notes="mine") seed = _seed(tmp_path, {"name": "Bare", "careers_url": "https://bare.example/jobs", "hand_reason": "unread"}, {"name": "Typed", "careers_url": "https://seed.example/jobs", "hand_reason": "robots"}, {"name": "Fresh", "careers_url": "https://fresh.example/jobs", "hand_reason": "key"}) result = sources.seed_companies(store, seed) assert (result["added"], result["skipped"], result["problems"]) == (1, 2, []) got = {k: (c.careers_url, c.hand_reason, c.notes) for k in ("bare", "typed", "fresh") if (c := store.company(k)) is not None} assert got == { "bare": ("https://bare.example/jobs", "unread", ""), "typed": ("https://typed.example/by-hand", "blocked", "mine"), "fresh": ("https://fresh.example/jobs", "key", "")} # Run again, nothing moves. assert sources.seed_companies(store, seed)["added"] == 0 assert store.company("bare").hand_reason == "unread" # type: ignore[union-attr] def test_a_seed_reason_outside_the_vocabulary_is_a_problem_not_a_row_value( store: Store, boards: Boards, tmp_path: Path) -> None: result = sources.seed_companies( store, _seed(tmp_path, {"name": "Odd", "hand_reason": "lazy"})) assert result["problems"] == ["odd: 'lazy' is not a hand_reason; kept without it"] assert store.company("odd").hand_reason == "" # type: ignore[union-attr] def test_a_seed_link_reaches_a_row_whose_board_clashes( store: Store, boards: Boards, tmp_path: Path) -> None: sources.set_board(store, "acme", "greenhouse:acme", name="Acme") store.add_company("Twin", key="twin") result = sources.seed_companies(store, _seed( tmp_path, {"name": "Twin", "board": "greenhouse:acme", "careers_url": "https://twin.example/jobs"})) assert len(result["problems"]) == 1 assert store.company("twin").careers_url == "https://twin.example/jobs" # type: ignore[union-attr] # ------------------------------------------------------------- by hand def test_a_company_added_by_hand_has_no_board_and_a_checked_link(store: Store) -> None: company = sources.add_by_hand(store, "Revolut Ltd", careers_url=" https://r.example/jobs ", notes="login wall") assert (company.key, company.hand_reason, company.careers_url, company.notes) == ( "revolut", "added", "https://r.example/jobs", "login wall") assert store.company_subscription("revolut") is None with pytest.raises(sources.Conflict, match="on the list already"): sources.add_by_hand(store, "REVOLUT") for link in ("javascript:alert(1)", "r.example/jobs", "https://", "ftp://r.example"): with pytest.raises(ValueError, match="http"): sources.add_by_hand(store, "Other", careers_url=link) with pytest.raises(ValueError, match="hand_reason"): sources.add_by_hand(store, "Other", hand_reason="lazy") with pytest.raises(ValueError, match="needs a name"): sources.add_by_hand(store, " ") assert [c.key for c in store.companies()] == ["revolut"] def test_edit_company_changes_what_it_is_given_and_renames_only_without_a_board( store: Store, boards: Boards) -> None: sources.add_by_hand(store, "Acme", notes="old") edited = sources.edit_company(store, "acme", name="Acme GmbH", hand_reason="gone") assert (edited.key, edited.name, edited.notes, edited.hand_reason) == ( "acme", "Acme GmbH", "old", "gone") with pytest.raises(ValueError, match="needs a name"): sources.edit_company(store, "acme", name=" ") with pytest.raises(ValueError, match="not a field"): sources.edit_company(store, "acme", ats="lever") with pytest.raises(KeyError): sources.edit_company(store, "nobody", notes="x") sources.set_board(store, "acme", "greenhouse:acme") with pytest.raises(sources.Conflict, match="detach"): sources.edit_company(store, "acme", name="Acme Labs") # The name it has, and any other field, are no rename. assert sources.edit_company(store, "acme", name="Acme GmbH", notes="new").notes == "new" sources.detach_board(store, "acme") with pytest.raises(sources.Conflict): sources.edit_company(store, "acme", name="Acme Labs") def test_remove_company_refuses_a_board_shown_and_open_roles( store: Store, boards: Boards) -> None: agent = store.save_agent("A") sources.add_by_hand(store, "Hand") store.set_affinity(agent.id, "hand", 2) sources.remove_company(store, "hand") assert store.company("hand") is None assert store.company("hand", agent.id) is None with pytest.raises(KeyError): sources.remove_company(store, "hand") sub, _found, _closed = sources.set_board(store, "acme", "greenhouse:acme", name="Acme") with pytest.raises(sources.Conflict, match="detach the board"): sources.remove_company(store, "acme") store.upsert_role(posting("1"), sub) sources.detach_board(store, "acme") # closes the role: no board, none open sources.remove_company(store, "acme") assert store.company("acme") is None # The detached subscription stays and is found again by a company of that name. assert store.company_subscription("acme") is not None store.add_company("Open", key="open") other = store.add_subscription("search", "jobindex:x", label="x", board="jobindex") item = posting("2") item.raw = {"company": "Open"} store.upsert_role(item, other) with pytest.raises(ValueError, match="open roles"): sources.remove_company(store, "open") def test_by_hand_lists_the_unboarded_never_opened_first( store: Store, boards: Boards) -> None: sources.add_by_hand(store, "Zeta") sources.add_by_hand(store, "Beta") sources.add_by_hand(store, "Old") sources.add_by_hand(store, "Recent") store.update_company("old", careers_opened_at="2026-08-01T00:00:00+00:00") store.update_company("recent", careers_opened_at="2026-09-20T00:00:00+00:00") sources.set_board(store, "read", "greenhouse:acme", name="Read") # a board: not listed store.add_company("Pleo", key="pleo", ats="teamtailor") # unread, no reason: listed store.add_company("Quiet", key="quiet") # from a search only sources.set_board(store, "gone", "ashby:acme", name="Gone") sources.detach_board(store, "gone") # detached: listed assert [c.key for c in sources.by_hand(store)] == [ "beta", "gone", "pleo", "zeta", "old", "recent"] # -------------------------------------------------------------------- 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_cli_add_with_one_name_lists_a_company_by_hand_and_with_two_follows_a_board( store: Store, boards: Boards, tmp_path: Path, capsys: pytest.CaptureFixture[str]) -> None: assert _cli(tmp_path, "add", "Revolut", "--careers-url", "https://r.example/jobs", "--note", "login wall") == 0 assert json.loads(capsys.readouterr().out) == {"key": "revolut", "hand_reason": "added"} revolut = store.company("revolut") assert revolut is not None assert (revolut.careers_url, revolut.notes, revolut.hand_reason) == ( "https://r.example/jobs", "login wall", "added") assert _cli(tmp_path, "add", "Meta", "--reason", "blocked") == 0 assert store.company("meta").hand_reason == "blocked" # type: ignore[union-attr] capsys.readouterr() assert _cli(tmp_path, "add", "Revolut") == 1 assert "on the list already" in capsys.readouterr().err assert _cli(tmp_path, "add", "Odd", "--careers-url", "javascript:alert(1)") == 1 assert "http" in capsys.readouterr().err with pytest.raises(SystemExit): # argparse refuses the reason _cli(tmp_path, "add", "Odd", "--reason", "lazy") capsys.readouterr() assert store.company("odd") is None # Two words are the board and the company, as before. assert _cli(tmp_path, "add", "greenhouse:acme", "Acme") == 0 assert json.loads(capsys.readouterr().out) == { "key": "acme", "board": "greenhouse:acme", "roles": 3} def test_cli_edit_checked_and_remove_a_company_by_hand( store: Store, boards: Boards, tmp_path: Path, capsys: pytest.CaptureFixture[str]) -> None: store.add_company("Acme", key="acme", notes="keep", hand_reason="unread") assert _cli(tmp_path, "edit", "acme", "--careers-url", "https://acme.example/jobs", "--reason", "robots") == 0 assert json.loads(capsys.readouterr().out) == { "key": "acme", "name": "Acme", "careers_url": "https://acme.example/jobs", "notes": "keep", "hand_reason": "robots"} assert _cli(tmp_path, "edit", "acme", "--note", "") == 0 assert store.company("acme").notes == "" # type: ignore[union-attr] assert _cli(tmp_path, "edit", "acme", "--careers-url", "ftp://x") == 1 assert "http" in capsys.readouterr().err assert _cli(tmp_path, "edit", "nobody", "--note", "x") == 1 assert "no company 'nobody'" in capsys.readouterr().err assert _cli(tmp_path, "checked", "acme") == 0 stamp = json.loads(capsys.readouterr().out)["careers_opened_at"] assert stamp and store.company("acme").careers_opened_at == stamp # type: ignore[union-attr] assert _cli(tmp_path, "checked", "nobody") == 1 capsys.readouterr() sources.set_board(store, "read", "greenhouse:acme", name="Read") assert _cli(tmp_path, "remove", "read") == 1 assert "detach the board first" in capsys.readouterr().err assert _cli(tmp_path, "remove", "acme") == 0 assert store.company("acme") is None assert _cli(tmp_path, "remove", "acme") == 1 def test_cli_by_hand_prints_never_opened_first_then_the_oldest_and_the_watch_rating( store: Store, boards: Boards, tmp_path: Path, capsys: pytest.CaptureFixture[str]) -> None: store.add_company("Recent", careers_url="https://recent.example/jobs", hand_reason="gone") store.add_company("Old", hand_reason="robots") store.add_company("Never", careers_url="https://never.example/jobs", hand_reason="added") store.update_company("recent", careers_opened_at="2026-09-20T08:00:00+00:00", watch=0) store.update_company("old", careers_opened_at="2026-08-01T08:00:00+00:00", watch=1) sources.set_board(store, "read", "greenhouse:acme", name="Read") assert _cli(tmp_path, "by-hand") == 0 lines = capsys.readouterr().out.splitlines() assert [ln.split()[0] for ln in lines[:-1]] == ["Never", "Old", "Recent"] assert lines[0].split() == ["Never", "added", "-", "never", "https://never.example/jobs"] assert lines[1].split() == ["Old", "robots", "yes", "2026-08-01"] assert lines[2].split() == ["Recent", "gone", "no", "2026-09-20", "https://recent.example/jobs"] assert lines[-1] == "3 by hand" 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_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")