Merge branch 'worktree-agent-a7e6bf7e67aa710c3'
This commit is contained in:
commit
0ea6c10d80
18 files changed
+1038
-178
No files matched your search
+39
-16
@@ -9,9 +9,9 @@ from pathlib import Path
|
||||
import httpx
|
||||
import pytest
|
||||
|
||||
from app.cli import seed_companies
|
||||
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
|
||||
|
||||
|
||||
@@ -103,7 +103,7 @@ def run(site: Site, name: str = "Acme", domain: str = "acme.com") -> Discovery:
|
||||
|
||||
def test_a_board_named_on_the_home_page_ends_discovery_there() -> None:
|
||||
site = Site({"https://acme.com/": '<a href="https://jobs.lever.co/acme">Jobs</a>'})
|
||||
assert run(site) == Discovery(board="lever:acme", ats="lever",
|
||||
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/"]
|
||||
@@ -135,18 +135,42 @@ def test_a_careers_pages_own_link_is_followed_one_level_down() -> None:
|
||||
"https://acme.com/careers/open-positions"]
|
||||
|
||||
|
||||
def test_an_unreadable_host_is_kept_when_nothing_readable_turns_up() -> None:
|
||||
def test_an_unreadable_host_named_by_a_page_is_the_answer_and_nothing_is_probed() -> None:
|
||||
site = Site({
|
||||
"https://acme.com/": "<p>welcome</p>",
|
||||
"https://acme.com/careers": '<a href="https://acme.wd3.myworkdayjobs.com/x">Jobs</a>',
|
||||
})
|
||||
}, boards={("greenhouse", "acme")})
|
||||
found = run(site)
|
||||
assert found == Discovery(ats="workday", careers_url="https://acme.com/careers",
|
||||
assert found == Discovery(ats="workday", ats_slug="acme",
|
||||
careers_url="https://acme.com/careers",
|
||||
evidence="https://acme.wd3.myworkdayjobs.com")
|
||||
assert not found.readable
|
||||
# Every slug was still tried on the hosts with a probe, in case a readable
|
||||
# board exists that the pages do not link.
|
||||
assert ("greenhouse", "acme") in site.probed
|
||||
# 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/": '<script src="https://cdn.teamtailor.com/x.js"></script>'
|
||||
'<a href="/careers">Careers</a>',
|
||||
"https://acme.com/careers": '<a href="https://jobs.lever.co/acme">Jobs</a>',
|
||||
})
|
||||
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/": '<script src="https://cdn.teamtailor.com/x.js"></script>',
|
||||
"https://acme.com/careers": '<a href="https://acme-jobs.teamtailor.com/jobs">Jobs</a>',
|
||||
})
|
||||
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:
|
||||
@@ -181,8 +205,7 @@ 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,
|
||||
capsys: pytest.CaptureFixture[str]) -> None:
|
||||
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",
|
||||
@@ -192,12 +215,13 @@ def test_the_seed_keeps_a_company_the_app_cannot_read(store: Store, tmp_path: Pa
|
||||
"careers_url": "https://www.novonordisk.com/careers.html"},
|
||||
{"name": "Odd", "board": "nosuch:odd"},
|
||||
]}))
|
||||
assert seed_companies(store, str(seed)) == {"added": 3, "readable": 1, "skipped": 0,
|
||||
"total": 3}
|
||||
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.country, pleo.city, pleo.careers_url) == (
|
||||
"ashby", "DK", "Copenhagen", "https://jobs.ashbyhq.com/pleo")
|
||||
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")
|
||||
@@ -206,6 +230,5 @@ def test_the_seed_keeps_a_company_the_app_cannot_read(store: Store, tmp_path: Pa
|
||||
"https://www.novonordisk.com/careers.html")
|
||||
odd = store.company("odd")
|
||||
assert odd is not None and odd.ats == ""
|
||||
assert "not a known board" in capsys.readouterr().err
|
||||
# Re-running adds nothing and edits nothing.
|
||||
assert seed_companies(store, str(seed))["skipped"] == 3
|
||||
assert seed_companies(store, seed)["skipped"] == 3
|
||||
@@ -0,0 +1,289 @@
|
||||
"""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) -> list[dict[str, Any]]:
|
||||
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])]
|
||||
|
||||
|
||||
@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, "", "teamtailor: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
|
||||
+94
-3
@@ -62,7 +62,7 @@ def test_schema_creates_on_an_empty_file(tmp_path: Path) -> None:
|
||||
"application_events", "agents", "scores", "state"} <= tables
|
||||
assert "idx_roles_cluster" in indexes
|
||||
assert store.migrations_applied == []
|
||||
assert LATEST == 8
|
||||
assert LATEST == 9
|
||||
# A second open finds it current and applies nothing.
|
||||
assert Store(path).migrations_applied == []
|
||||
|
||||
@@ -94,7 +94,7 @@ def test_a_v1_file_migrates_to_the_shape_a_new_file_has(tmp_path: Path) -> None:
|
||||
# v1 had affinity on the company; the vote columns v4 added and v6
|
||||
# drops again are not in a fresh file, so there is nothing to put back.
|
||||
conn.execute("ALTER TABLE companies ADD COLUMN affinity INTEGER NOT NULL DEFAULT 0")
|
||||
for column in ("ats", "country", "city"): # v7
|
||||
for column in ("ats", "country", "city", "ats_slug"): # v7, v9
|
||||
conn.execute(f"ALTER TABLE companies DROP COLUMN {column}")
|
||||
for table in ("vectors", "role_tags", "agent_companies", "role_votes"):
|
||||
conn.execute(f"DROP TABLE {table}")
|
||||
@@ -107,6 +107,21 @@ def test_a_v1_file_migrates_to_the_shape_a_new_file_has(tmp_path: Path) -> None:
|
||||
assert sorted(_columns(old, table)) == sorted(_columns(fresh, table))
|
||||
|
||||
|
||||
def test_a_v8_file_gains_ats_slug_and_keeps_its_companies(tmp_path: Path) -> None:
|
||||
path = tmp_path / "v8.db"
|
||||
Store(path).add_company("Acme", ats="teamtailor", country="DK")
|
||||
with sqlite3.connect(path) as conn:
|
||||
conn.execute("ALTER TABLE companies DROP COLUMN ats_slug")
|
||||
conn.execute("PRAGMA user_version = 8")
|
||||
|
||||
migrated = Store(path)
|
||||
|
||||
assert migrated.migrations_applied == [9]
|
||||
acme = migrated.company("acme")
|
||||
assert acme is not None
|
||||
assert (acme.ats, acme.country, acme.ats_slug) == ("teamtailor", "DK", "")
|
||||
|
||||
|
||||
def _downgrade_to_v4(path: Path) -> None:
|
||||
"""A fresh file put back to the v4 shape: votes on the role, affinity on
|
||||
the company, no read state, no tags, no per-agent tables."""
|
||||
@@ -116,7 +131,7 @@ def _downgrade_to_v4(path: Path) -> None:
|
||||
conn.execute("ALTER TABLE roles ADD COLUMN vote INTEGER CHECK (vote IN (-1, 0, 1))")
|
||||
conn.execute("ALTER TABLE roles ADD COLUMN voted_at TEXT")
|
||||
conn.execute("ALTER TABLE companies ADD COLUMN affinity INTEGER NOT NULL DEFAULT 0")
|
||||
for column in ("ats", "country", "city"): # v7
|
||||
for column in ("ats", "country", "city", "ats_slug"): # v7, v9
|
||||
conn.execute(f"ALTER TABLE companies DROP COLUMN {column}")
|
||||
conn.execute("ALTER TABLE agents DROP COLUMN limits") # v8
|
||||
conn.execute("DROP TABLE agent_companies")
|
||||
@@ -642,6 +657,82 @@ def test_open_role_counts_and_company_boards(store: Store) -> None:
|
||||
assert store.company_boards() == {"acme": "greenhouse"}
|
||||
|
||||
|
||||
def test_set_company_board_creates_the_row_and_its_board_together(store: Store) -> None:
|
||||
company, sub, closed = store.set_company_board(
|
||||
"acme", "greenhouse:acme", host="greenhouse", name="Acme",
|
||||
fields={"ats": "greenhouse", "ats_slug": "acme", "country": "DK"})
|
||||
assert (company.name, company.ats, company.ats_slug, company.country) == (
|
||||
"Acme", "greenhouse", "acme", "DK")
|
||||
assert sub is not None and closed == 0
|
||||
assert (sub.target, sub.label, sub.company, sub.board, sub.show) == (
|
||||
"greenhouse:acme", "Acme", "Acme", "greenhouse", 1)
|
||||
assert store.company_subscription("acme") == sub
|
||||
assert store.company_boards() == {"acme": "greenhouse"}
|
||||
|
||||
|
||||
def test_a_board_clash_leaves_no_company_row(store: Store) -> None:
|
||||
"""The subscription insert fails on UNIQUE (source, target), and the
|
||||
company row written a statement earlier goes with it."""
|
||||
store.set_company_board("acme", "greenhouse:acme", host="greenhouse", name="Acme")
|
||||
with pytest.raises(ValueError, match="already has 'greenhouse:acme'"):
|
||||
store.set_company_board("beta", "greenhouse:acme", host="greenhouse", name="Beta")
|
||||
assert store.company("beta") is None
|
||||
assert [s.company for s in store.subscriptions(source="ats")] == ["Acme"]
|
||||
|
||||
|
||||
def test_a_clash_on_replace_leaves_the_old_board_and_its_roles(store: Store) -> None:
|
||||
store.set_company_board("acme", "greenhouse:acme", host="greenhouse", name="Acme")
|
||||
_, beta, _ = store.set_company_board("beta", "lever:beta", host="lever", name="Beta")
|
||||
assert beta is not None
|
||||
store.upsert_role(posting("1", company="Beta"), beta)
|
||||
with pytest.raises(ValueError):
|
||||
store.set_company_board("beta", "greenhouse:acme", host="greenhouse",
|
||||
fields={"ats": "greenhouse"})
|
||||
assert store.company_subscription("beta") == beta
|
||||
assert store.company("beta").ats == "" # type: ignore[union-attr]
|
||||
assert [r.source_id for r in store.roles()] == ["1"] # still open
|
||||
|
||||
|
||||
def test_replacing_a_board_rewrites_it_in_place_and_closes_the_old_roles(
|
||||
store: Store) -> None:
|
||||
_, old, _ = store.set_company_board("acme", "greenhouse:acme", host="greenhouse",
|
||||
name="Acme")
|
||||
assert old is not None
|
||||
store.update_subscription(old.id, fetch=False, target_id="gh-1")
|
||||
store.upsert_role(posting("1", company="Acme"), old)
|
||||
store.upsert_role(posting("2", company="Acme"), old)
|
||||
|
||||
company, new, closed = store.set_company_board("acme", "ashby:acme", host="ashby",
|
||||
fields={"ats": "ashby"})
|
||||
assert closed == 2 and store.roles() == []
|
||||
assert new is not None and new.id == old.id
|
||||
assert (new.target, new.board, new.target_id, new.fetch, new.subscribed) == (
|
||||
"ashby:acme", "ashby", "", 1, 1)
|
||||
assert company.ats == "ashby"
|
||||
# The same board again closes nothing.
|
||||
store.upsert_role(posting("3", company="Acme"), new)
|
||||
assert store.set_company_board("acme", "ashby:acme", host="ashby")[2] == 0
|
||||
assert [r.source_id for r in store.roles()] == ["3"]
|
||||
|
||||
|
||||
def test_detaching_a_board_hides_it_closes_its_roles_and_keeps_the_row(
|
||||
store: Store) -> None:
|
||||
_, sub, _ = store.set_company_board("acme", "greenhouse:acme", host="greenhouse",
|
||||
name="Acme")
|
||||
assert sub is not None
|
||||
store.upsert_role(posting("1", company="Acme"), sub)
|
||||
company, detached, closed = store.set_company_board("acme", "")
|
||||
assert closed == 1 and company.key == "acme"
|
||||
assert detached is not None
|
||||
assert (detached.subscribed, detached.fetch, detached.show) == (0, 0, 0)
|
||||
assert store.company_boards() == {}
|
||||
# The next board reuses the row rather than clashing on its label.
|
||||
_, again, _ = store.set_company_board("acme", "lever:acme", host="lever")
|
||||
assert again is not None and again.id == sub.id and again.show == 1
|
||||
with pytest.raises(KeyError):
|
||||
store.set_company_board("nope", "")
|
||||
|
||||
|
||||
# ----------------------------------------------------------- subscriptions
|
||||
|
||||
def test_subscriptions_crud(store: Store) -> None:
|
||||
|
||||
+53
-4
@@ -386,7 +386,7 @@ def test_companies_lists_board_and_open_role_count(
|
||||
sub = store.add_subscription("ats", "greenhouse:acme", company="Acme", board="greenhouse")
|
||||
store.add_company("Acme", key="acme", ats="greenhouse", country="DK")
|
||||
store.upsert_role(posting("1", company="Acme"), sub)
|
||||
store.add_company("Other", key="other") # known only from a search: no board
|
||||
store.add_company("Other", key="other") # no board found
|
||||
store.add_company("Novo", key="novo", ats="successfactors", # followed, not readable
|
||||
careers_url="https://novo.example/careers")
|
||||
|
||||
@@ -395,7 +395,8 @@ def test_companies_lists_board_and_open_role_count(
|
||||
[row] = [c for c in res.json() if c["key"] == "acme"]
|
||||
assert row == {"key": "acme", "name": "Acme", "board": "greenhouse",
|
||||
"subscription_id": sub.id, "on": True, "affinity": 0, "open_roles": 1,
|
||||
"ats": "greenhouse", "careers_url": "", "country": "DK"}
|
||||
"ats": "greenhouse", "ats_slug": "", "careers_url": "",
|
||||
"country": "DK"}
|
||||
[other] = [c for c in res.json() if c["key"] == "other"]
|
||||
assert (other["board"], other["subscription_id"], other["on"], other["ats"]) == (
|
||||
"", None, None, "")
|
||||
@@ -653,7 +654,10 @@ def test_the_sources_page_has_a_section_per_plugin(client: TestClient) -> None:
|
||||
# Company boards: the follow form, the agent select, the list.
|
||||
assert 'id="companies-list"' in page and 'id="c-agent"' in page
|
||||
assert 'id="b-host"' in page and 'id="b-slug"' in page and 'id="b-name"' in page
|
||||
assert '<option value="greenhouse">' in page and '<option value="personio">' in page
|
||||
# The host select is plugins.ats's HOSTS, not a list typed into the page.
|
||||
from plugins.ats import HOSTS
|
||||
for host, label in HOSTS.items():
|
||||
assert f'<option value="{host}">{label}</option>' in page
|
||||
boards = page[page.index('id="company-boards"'):page.index('id="runs"')].lower()
|
||||
assert "hidden" in boards and "mute" not in boards
|
||||
# Runs: the pull button, the queue's elements, and the log as its last
|
||||
@@ -915,11 +919,56 @@ def test_a_board_is_followed_only_if_it_answers(
|
||||
assert res.json()["found"] == 1 and res.json()["source"]["target"] == "ashby:acme"
|
||||
assert store.company("acme") is not None
|
||||
|
||||
# Following a company that exists goes through `set_board`: the same
|
||||
# board again is the same subscription, not a refusal or a second one.
|
||||
again = client.post("/api/boards", json={"board": "ashby:acme", "name": "Acme"})
|
||||
assert again.status_code == 422 and "already followed" in again.json()["detail"]
|
||||
assert again.status_code == 201
|
||||
assert again.json()["source"]["id"] == res.json()["source"]["id"]
|
||||
bad = client.post("/api/boards", json={"board": "acme", "name": "Other"})
|
||||
assert bad.status_code == 422 and "host:slug" in bad.json()["detail"]
|
||||
|
||||
def down(target: str) -> list[dict[str, str]]:
|
||||
raise OSError("connection refused")
|
||||
|
||||
monkeypatch.setattr(ats, "fetch_board", down)
|
||||
dead = client.post("/api/boards", json={"board": "ashby:other", "name": "Other"})
|
||||
assert dead.status_code == 422 and "did not answer" in dead.json()["detail"]
|
||||
assert store.company("other") is None
|
||||
|
||||
|
||||
def test_a_companys_board_is_set_replaced_and_detached(
|
||||
client: TestClient, store: Store, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
import plugins.ats as ats
|
||||
|
||||
monkeypatch.setattr(ats, "fetch_board", lambda target: []) # between openings
|
||||
store.add_company("Acme", key="acme", ats="teamtailor", ats_slug="acme")
|
||||
|
||||
res = client.post("/api/companies/acme/board", json={"board": "greenhouse:acme"})
|
||||
assert res.status_code == 200
|
||||
body = res.json()
|
||||
assert (body["found"], body["closed"], body["source"]["target"]) == (0, 0, "greenhouse:acme")
|
||||
sub_id = body["source"]["id"]
|
||||
store.upsert_role(posting("1", company="Acme"), store.subscription(sub_id))
|
||||
[row] = client.get("/api/companies").json()
|
||||
assert (row["board"], row["subscription_id"], row["ats"], row["ats_slug"]) == (
|
||||
"greenhouse", sub_id, "greenhouse", "acme")
|
||||
|
||||
res = client.post("/api/companies/acme/board", json={"board": "lever:acme"})
|
||||
assert res.json()["closed"] == 1 and res.json()["source"]["id"] == sub_id
|
||||
|
||||
res = client.delete("/api/companies/acme/board")
|
||||
assert res.status_code == 200 and res.json() == {"closed": 0}
|
||||
[row] = client.get("/api/companies").json()
|
||||
assert (row["board"], row["subscription_id"], row["ats"]) == ("", None, "lever")
|
||||
assert client.get("/api/sources").json()["boards"] == []
|
||||
assert client.delete("/api/companies/acme/board").status_code == 409
|
||||
|
||||
assert client.post("/api/companies/nope/board",
|
||||
json={"board": "lever:acme"}).status_code == 404
|
||||
assert client.delete("/api/companies/nope/board").status_code == 404
|
||||
bad = client.post("/api/companies/acme/board", json={"board": "teamtailor:acme"})
|
||||
assert bad.status_code == 422 and "host:slug" in bad.json()["detail"]
|
||||
|
||||
|
||||
# ------------------------------------------------- roles: text and summary
|
||||
|
||||
|
||||
Reference in new issue
Block a user