Merge main into wp-review-contract

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
WolfandClaude Fable 5.1 committed 2026-09-30 15:57:46 +02:00
commit fe2a1b0347
36 files changed
+1741 -596

No files matched your search

+171
View File
@@ -0,0 +1,171 @@
"""What the JSON API and the config decide, that the reviews of 2026-09-30
found wrong: a board's reach stored as ISO codes, an empty seniority range,
Fetch text answering as the viewing agent, the model a stage will use, and a
starter search on any reader. No network."""
from __future__ import annotations
from collections.abc import Iterator
from datetime import UTC, datetime
from pathlib import Path
import pytest
from fastapi import FastAPI
from fastapi.testclient import TestClient
from app import config as config_mod
from app.config import Config, ConfigError, ServerConfig
from app.main import create_app
from app.storage import Store
from desk.models import Item
from prepped import upsert
ROOT = Path(__file__).resolve().parents[1]
@pytest.fixture
def app(tmp_path: Path) -> FastAPI:
return create_app(Config(server=ServerConfig(host="127.0.0.1", port=0),
database=tmp_path / "roledesk.db"))
@pytest.fixture
def store(app: FastAPI) -> Store:
return app.state.store
@pytest.fixture
def client(app: FastAPI) -> Iterator[TestClient]:
with TestClient(app) as c:
yield c
def _role(store: Store, source_id: str = "1", title: str = "Senior Backend Engineer") -> str:
item = Item(source_id=source_id, location=f"https://boards.example/acme/{source_id}",
title=title, body="A teaser.", published_at=datetime(2026, 9, 20, tzinfo=UTC),
raw={})
item.source = "ats"
upsert(store, item)
return item.uid
# ---------------------------------------------------------- a board's reach
@pytest.fixture
def board(store: Store) -> int:
return store.add_subscription("ats", "greenhouse:acme", label="Acme", company="Acme",
board="greenhouse").id
def test_the_reach_countries_are_stored_as_iso_codes(
client: TestClient, store: Store, board: int) -> None:
res = client.post(f"/api/sources/{board}/reach",
json={"mode": "own", "countries": ["UK", "Germany", "dk"]})
assert res.status_code == 200, res.text
sub = store.subscription(board)
assert sub is not None and sub.params["countries"] == ["DE", "DK", "GB"]
def test_a_reach_country_that_is_none_is_refused(
client: TestClient, store: Store, board: int) -> None:
res = client.post(f"/api/sources/{board}/reach",
json={"mode": "own", "countries": ["Atlantis"]})
assert res.status_code == 422 and "Atlantis" in res.text
sub = store.subscription(board)
assert sub is not None and "countries" not in sub.params
def test_a_company_row_names_its_attached_board_in_full(
client: TestClient, store: Store, board: int) -> None:
"""The Replace-board confirm compares the typed `host:slug` with this,
not with `board` (the host) or `ats_slug` (the careers site's)."""
store.add_company("Acme")
row = next(c for c in client.get("/api/companies").json() if c["key"] == "acme")
assert (row["board"], row["target"]) == ("greenhouse", "greenhouse:acme")
# ------------------------------------------------------ the seniority range
@pytest.mark.parametrize("path", ["/api/roles", "/api/roles/facets"])
@pytest.mark.parametrize("query", ["level_from=senior&level_to=junior",
"level_from=exec&track=ic"])
def test_a_seniority_range_with_no_level_is_refused_not_ignored(
client: TestClient, store: Store, path: str, query: str) -> None:
_role(store)
res = client.get(f"{path}?{query}")
assert res.status_code == 422 and "no level" in res.text
def test_a_seniority_range_with_levels_still_filters(client: TestClient, store: Store) -> None:
_role(store)
assert client.get("/api/roles?level_from=junior&level_to=senior").status_code == 200
assert client.get("/api/roles/facets?track=ic").status_code == 200
# --------------------------------------------------------------- Fetch text
def test_fetch_text_answers_with_the_viewing_agents_vote(
client: TestClient, store: Store, monkeypatch: pytest.MonkeyPatch) -> None:
from app.jobs import posting
agent = store.save_agent("Wolf", intent="backend")
uid = _role(store)
store.set_vote(agent.id, uid, 1)
monkeypatch.setattr(posting, "fetch_posting",
lambda s, u: s.set_article_text(u, "The whole posting."))
res = client.post(f"/api/roles/{uid}/text?agent={agent.id}")
assert res.status_code == 200, res.text
assert (res.json()["vote"], res.json()["article_text"]) == (1, "The whole posting.")
# The default agent when none is named, as the other role routes read it.
assert client.post(f"/api/roles/{uid}/text").json()["vote"] == 1
assert client.post(f"/api/roles/{uid}/text?agent=999").status_code == 404
# ------------------------------------------------------------------- stages
def test_a_stage_shows_the_model_that_will_answer(tmp_path: Path) -> None:
config = Config(database=tmp_path / "x.db")
app = create_app(config)
with TestClient(app) as client:
by_name = {s["name"]: s for s in client.get("/api/stages").json()}
assert by_name["extract"]["model"] == config.extract.model
config.agent.model = "claude-opus-4-1"
by_name = {s["name"]: s for s in client.get("/api/stages").json()}
assert by_name["extract"]["model"] == by_name["summarise"]["model"] == "claude-opus-4-1"
assert by_name["enrich"]["model"] == ""
# ------------------------------------------------------------------- config
def test_a_platsbanken_search_loads(tmp_path: Path) -> None:
path = tmp_path / "config.yaml"
path.write_text("searches:\n - {name: Sverige, provider: platsbanken, params: {q: python}}\n",
encoding="utf-8")
assert config_mod.load(path).searches[0].provider == "platsbanken"
def test_a_search_provider_that_is_no_reader_names_the_readers(tmp_path: Path) -> None:
path = tmp_path / "config.yaml"
path.write_text("searches:\n - {name: X, provider: monster}\n", encoding="utf-8")
with pytest.raises(ConfigError, match="jobindex, adzuna, platsbanken"):
config_mod.load(path)
def test_the_example_config_holds_every_default() -> None:
"""Each value in `config.example.yaml` is the default, so a copy changes
nothing; a key added there with another value, or a key the code does not
read, fails here."""
loaded = config_mod.load(ROOT / "config.example.yaml").model_dump(exclude={"path"})
assert loaded == Config().model_dump(exclude={"path"})
def test_the_example_config_names_every_key_of_the_sections_it_documents() -> None:
import yaml
raw = yaml.safe_load((ROOT / "config.example.yaml").read_text("utf-8"))
for name, allowed_missing in (("agent", set()), ("extract", set()), ("enrich", set()),
("embed", set()), ("workers", set()),
("summarise", {"on_ingest"})): # accepted, ignored
fields = set(type(getattr(Config(), name)).model_fields)
# `credentials_database` is filled from `database`, never read from the file.
fields -= {"credentials_database"}
assert fields - set(raw[name]) == allowed_missing, name
+16
View File
@@ -117,3 +117,19 @@ def test_the_read_page_of_its_own_is_gone(client: TestClient) -> None:
other = client.get("/applications").text
assert 'id="rail-toggle"' not in other and 'id="search"' not in other
assert "<main>" in other
STATIC = Path(__file__).resolve().parents[1] / "app" / "static"
def test_no_script_links_to_a_page_that_does_not_exist() -> None:
"""`/messages` was a 404: the notice log lives on Settings, Sources."""
for script in STATIC.glob("*.js"):
assert '"/messages"' not in script.read_text("utf-8"), script.name
def test_the_reader_excerpt_falls_back_in_the_servers_order() -> None:
"""`excerptOf` cuts the same text `_EXCERPT_SOURCE` does: summary, then
description, then teaser, or a teaser-only role's row blanks after Fetch."""
text = (STATIC / "roles.js").read_text("utf-8")
assert "role.summary || role.description || role.teaser" in text
+231 -6
View File
@@ -7,6 +7,7 @@ from __future__ import annotations
import json
import sys
import threading
import types
from concurrent.futures import ThreadPoolExecutor
from datetime import UTC, datetime, timedelta
from pathlib import Path
@@ -21,8 +22,8 @@ if str(ROOT) not in sys.path:
from desk.models import Item # noqa: E402
from pipeline import Cancelled, RunContext, Tally # noqa: E402
from app.models import Application # noqa: E402
from plugins import readers # noqa: E402
from plugins.readers import _runner # noqa: E402
from plugins import ats, readers # noqa: E402
from plugins.readers import Filters, Reads, _runner # noqa: E402
from plugins.ats import ATSSource # noqa: E402
from app import prep # noqa: E402
from app.jobs import cascade, pull # noqa: E402
@@ -142,7 +143,7 @@ def test_close_boards_closes_what_a_completed_board_no_longer_lists(
result = pull.close_boards(FakeConfig(), store, FakeTally()) # type: ignore[arg-type]
assert result == {"boards": 1, "searches": 0, "failed": 1, "incomplete": 0,
assert result == {"boards": 1, "searches": 0, "unanswered": 1, "incomplete": 0,
"roles": 1, "closed": 1}
assert {r.source_id for r in store.roles()} == {"greenhouse:acme:1", "greenhouse:other:9"}
@@ -163,7 +164,7 @@ def test_close_boards_never_closes_a_board_stamped_by_an_earlier_run(
result = pull.close_boards(FakeConfig(), store, FakeTally()) # type: ignore[arg-type]
assert result == {"boards": 0, "searches": 0, "failed": 1, "incomplete": 0,
assert result == {"boards": 0, "searches": 0, "unanswered": 1, "incomplete": 0,
"roles": 0, "closed": 0}
assert {r.source_id for r in store.roles()} == {"greenhouse:acme:1"}
@@ -256,7 +257,7 @@ def test_a_failing_board_keeps_its_roles_before_stale_days(
_answer(monkeypatch, ValueError("gone"))
result = _pull(acme)
assert _open(acme) == {"workable:acme:1"}
assert (result["failed"], result["closed"]) == (1, 0)
assert (result["unanswered"], result["closed"]) == (1, 0)
since, reason = pull.failing_boards(acme)["workable:acme"]
assert since and reason == "ValueError: gone"
@@ -486,7 +487,7 @@ def test_run_a_settle_does_not_close_on_run_b_ids(
b_result = pull.run_batch(FakeConfig(), acme, **b) # type: ignore[arg-type]
result = pull.close_boards(FakeConfig(), acme, Tally(results=(a_result,))) # type: ignore[arg-type]
assert (result["failed"], result["closed"]) == (1, 0)
assert (result["unanswered"], result["closed"]) == (1, 0)
assert _open(acme) == {"workable:acme:1", "workable:acme:2"}
assert pull.running(acme) == b["run"]
@@ -507,3 +508,227 @@ def test_the_settle_reads_its_marker_off_its_batches_payloads(acme: Store) -> No
context = RunContext(waited_for=(failed,))
result = pull.close_boards(FakeConfig(), acme, Tally(), context=context) # type: ignore[arg-type]
assert result["boards"] == 1 and pull.running(acme) == ""
# -------------------------------------------------------- losses, guarded
def _days_ago(days: float) -> str:
return (datetime.now(UTC) - timedelta(days=days)).isoformat(timespec="seconds")
def _set(store: Store, column: str, value: str, *source_ids: str) -> None:
with store.transaction() as conn:
conn.executemany(f"UPDATE roles SET {column} = ? WHERE source_id = ?",
[(value, i) for i in source_ids])
@pytest.fixture
def big(store: Store) -> Store:
store.add_subscription("ats", "greenhouse:big", label="Big", company="Big",
board="greenhouse")
return store
def test_a_whole_read_of_nothing_after_forty_keeps_the_roles_and_fails(
big: Store, monkeypatch: pytest.MonkeyPatch) -> None:
_answer(monkeypatch, ([_row(str(n)) for n in range(40)], True))
_pull(big)
_answer(monkeypatch, ([], True))
result = _pull(big)
assert len(_open(big)) == 40
assert (result["unanswered"], result["closed"]) == (1, 0)
assert "under 10% of the 40" in pull.failing_boards(big)["greenhouse:big"][1]
def test_a_whole_read_of_nothing_after_five_closes_the_board(
big: Store, monkeypatch: pytest.MonkeyPatch) -> None:
_answer(monkeypatch, ([_row(str(n)) for n in range(5)], True))
_pull(big)
_answer(monkeypatch, ([], True))
assert _pull(big)["closed"] == 5
assert _open(big) == set()
def test_a_suspect_answer_is_accepted_after_stale_days(
big: Store, monkeypatch: pytest.MonkeyPatch) -> None:
_answer(monkeypatch, ([_row(str(n)) for n in range(40)], True))
_pull(big)
_answer(monkeypatch, ([_row("1")], True))
_pull(big)
assert len(_open(big)) == 40
record = json.loads(big.state[f"{pull.FAILING}greenhouse:big"])
big.state[f"{pull.FAILING}greenhouse:big"] = json.dumps(
record | {"since": _days_ago(pull.STALE_DAYS + 1)})
result = _pull(big)
assert _open(big) == {"greenhouse:big:1"}
assert (result["unanswered"], result["closed"]) == (0, 39)
assert pull.failing_boards(big) == {}
assert json.loads(big.state["ats:count:greenhouse:big"])["count"] == 1
def _remote_host(monkeypatch: pytest.MonkeyPatch, remote: str) -> list[str]:
"""A whole-listing host `fake` that filters by country and, per `remote`,
on remote; its feed lists the ids in the returned list."""
listed: list[str] = []
module: object = types.ModuleType("fake")
module.NAME, module.LABEL, module.PATTERNS = "fake", "Fake", () # type: ignore[attr-defined]
module.slug_ok = lambda slug: slug.isalnum() # type: ignore[attr-defined]
module.allowed = lambda slug: lambda h: h == "jobs.fake.example" # type: ignore[attr-defined]
module.careers_url = lambda slug: f"https://jobs.fake.example/{slug}" # type: ignore[attr-defined]
module.READS = Reads( # type: ignore[attr-defined]
scope="board", listing="whole", text="listing", complete="whole", places="one",
robots="test", filters=Filters(country="many", remote=remote)) # type: ignore[arg-type]
module.feed_url = lambda slug, ask: f"https://jobs.fake.example/{slug}" # type: ignore[attr-defined]
module.parse = lambda body, slug: [_row(i) for i in json.loads(body)] # type: ignore[attr-defined]
monkeypatch.setitem(readers.HOSTS, "fake", module)
monkeypatch.setattr(ats, "_get", lambda client, url, host, slug, **kw: json.dumps(
listed).encode())
return listed
def test_a_filtered_read_that_could_not_ask_remote_keeps_remote_roles_until_stale(
store: Store, monkeypatch: pytest.MonkeyPatch) -> None:
listed = _remote_host(monkeypatch, remote="none")
store.add_subscription("ats", "fake:acme", label="Acme", company="Acme", board="fake")
reach = {"fake:acme": {"countries": ["DK"], "remote": True}}
def pull_once() -> dict[str, object]:
run = pull.claim_run(store)
result = pull.run_batch(FakeConfig(), store, targets=["fake:acme"], run=run, # type: ignore[arg-type]
reach=reach)
return pull.close_boards(FakeConfig(), store, Tally(results=(result,))) # type: ignore[arg-type]
listed[:] = ["1", "2", "3", "4"]
pull_once()
_set(store, "remote", "remote", "fake:acme:3", "fake:acme:4")
listed[:] = ["1"]
assert pull_once()["closed"] == 1
assert _open(store) == {"fake:acme:1", "fake:acme:3", "fake:acme:4"}
assert store.state["ats:remote_unasked:fake:acme"] == "1"
_set(store, "last_seen", _days_ago(pull.STALE_DAYS + 1), "fake:acme:4")
assert pull_once()["closed"] == 1
assert _open(store) == {"fake:acme:1", "fake:acme:3"}
def test_a_narrowed_read_closes_what_it_has_not_listed_for_stale_days(
acme: Store, monkeypatch: pytest.MonkeyPatch) -> None:
_answer(monkeypatch, ([_row(i) for i in "123"], True))
_pull(acme)
def fake(*args: object, **kwargs: object) -> _runner.Listing:
return _runner.Listing([_row("1")], True, ["1"], narrowed=True)
monkeypatch.setattr(_runner, "read_listing", fake)
assert _pull(acme)["closed"] == 0
_set(acme, "last_seen", _days_ago(pull.STALE_DAYS + 1), "workable:acme:3")
assert _pull(acme)["closed"] == 1
assert _open(acme) == {"workable:acme:1", "workable:acme:2"}
def test_a_target_that_raises_after_its_read_is_recorded_and_the_batch_goes_on(
store: Store, monkeypatch: pytest.MonkeyPatch) -> None:
targets = [f"greenhouse:{slug}" for slug in ("a", "b", "c")]
for target in targets:
slug = target.partition(":")[2]
store.add_subscription("ats", target, label=slug, company=slug, board="greenhouse")
def fake_fetch(self: ATSSource) -> object:
[(target, _)] = self.targets()
yield _item("greenhouse", target.partition(":")[2], "1")
self.state[f"ats:ok:{target}"] = self.options["run"]
real = Store.upsert_role
def upsert(self: Store, item: Item, sub: object = None) -> bool:
if item.source_id.startswith("greenhouse:b:"):
raise ValueError("a row the store refuses")
return real(self, item, sub) # type: ignore[arg-type]
monkeypatch.setattr(ATSSource, "fetch", fake_fetch)
monkeypatch.setattr(Store, "upsert_role", upsert)
result = pull.run_batch(FakeConfig(), store, targets=targets, run="r") # type: ignore[arg-type]
assert result["roles"] == 2
assert _open(store) == {"greenhouse:a:1", "greenhouse:c:1"}
assert "ats:ok:greenhouse:b" not in store.state
since, reason = pull.failing_boards(store)["greenhouse:b"]
assert since and reason == "ValueError: a row the store refuses"
assert set(pull.failing_boards(store)) == {"greenhouse:b"}
def test_a_board_whose_rows_do_not_store_keeps_its_failing_record(
acme: Store, monkeypatch: pytest.MonkeyPatch) -> None:
since = _days_ago(2)
acme.state[f"{pull.FAILING}workable:acme"] = json.dumps(
{"since": since, "reason": "404 Not Found"})
_answer(monkeypatch, ([_row("1")], True))
def refuse(self: Store, item: Item, sub: object = None) -> bool:
raise ValueError("no")
monkeypatch.setattr(Store, "upsert_role", refuse)
_pull(acme)
assert pull.failing_boards(acme)["workable:acme"] == (since, "ValueError: no")
assert "ats:ok:workable:acme" not in acme.state
# ------------------------------------------------------- the settle speaks
def test_a_settle_names_the_unanswered_board_and_the_one_that_closed_many(
store: Store) -> None:
sub = store.add_subscription("ats", "greenhouse:big", label="Big", company="Big",
board="greenhouse")
store.add_subscription("ats", "greenhouse:gone", label="Gone", company="Gone",
board="greenhouse")
for n in range(80):
store.upsert_role(_item("greenhouse", "big", str(n), company="Big"), sub)
run = pull.claim_run(store)
store.state["ats:ok:greenhouse:big"] = run
store.state["ats:complete:greenhouse:big"] = "1"
store.state["ats:ids:greenhouse:big"] = json.dumps(
[f"greenhouse:big:{n}" for n in range(20)])
store.state[f"{pull.FAILING}greenhouse:gone"] = json.dumps(
{"since": _days_ago(1), "reason": "404 Not Found"})
said: list[object] = []
result = pull.close_boards(FakeConfig(), store, Tally(results=({"roles": 20, "run": run},)), # type: ignore[arg-type]
context=RunContext(on_notice=said.append))
assert (result["unanswered"], result["closed"]) == (1, 60)
[notice] = said
assert notice.code == "pull.settled" # type: ignore[attr-defined]
assert "greenhouse:gone (404 Not Found)" in notice.text # type: ignore[attr-defined]
assert "greenhouse:big 60 of 80" in notice.text # type: ignore[attr-defined]
assert "closed 60 roles" in notice.text # type: ignore[attr-defined]
def test_a_clean_settle_says_nothing(acme: Store, monkeypatch: pytest.MonkeyPatch) -> None:
_answer(monkeypatch, ([_row(i) for i in "12"], True))
said: list[object] = []
results = [pull.run_batch(FakeConfig(), acme, **batch) # type: ignore[arg-type]
for batch in pull._boards(FakeConfig(), acme)] # type: ignore[arg-type]
pull.close_boards(FakeConfig(), acme, Tally(results=tuple(results)), # type: ignore[arg-type]
context=RunContext(on_notice=said.append))
assert said == []
def test_a_finder_that_raises_releases_the_pull_claim(
acme: Store, monkeypatch: pytest.MonkeyPatch) -> None:
def broken(store: Store, subs: object) -> dict[str, object]:
raise RuntimeError("reach broke")
monkeypatch.setattr(pull, "reaches", broken)
# The worker runs the chain's finder once the job is done; here at once.
context = RunContext(on_task=lambda task: task.finder(FakeConfig(), acme))
with pytest.raises(RuntimeError, match="reach broke"):
cascade.run_pull(FakeConfig(), acme, context=context)
assert pull.running(acme) == ""
# And the job itself lets go when the chain cannot be built.
def no_chain(run: str) -> object:
raise RuntimeError("no chain")
monkeypatch.setattr(cascade, "pull_chain", no_chain)
with pytest.raises(RuntimeError, match="no chain"):
cascade.run_pull(FakeConfig(), acme, context=RunContext(on_task=lambda task: True))
assert pull.running(acme) == ""
+132 -2
View File
@@ -26,7 +26,7 @@ if str(ROOT) not in sys.path:
from plugins import ats, readers # noqa: E402
from plugins.readers import _runner # noqa: E402
from plugins.readers import lever # noqa: E402
from plugins.readers._contract import Ask # noqa: E402
from plugins.readers._contract import Ask, Filters, Reads # noqa: E402
FIXTURES = ROOT / "tests" / "fixtures" / "ats"
@@ -253,7 +253,9 @@ def _fake_host(*, detail: Callable[..., Any] | None = None) -> types.ModuleType:
module.slug_ok = lambda slug: slug.isalnum()
module.allowed = lambda slug: lambda h: h == "jobs.fake.example"
module.careers_url = lambda slug: f"https://jobs.fake.example/{slug}"
module.feed_url = lambda slug: f"https://jobs.fake.example/{slug}/list"
module.READS = Reads(scope="board", listing="whole", complete="whole", places="one",
text="detail" if detail is not None else "listing", robots="test")
module.feed_url = lambda slug, ask: f"https://jobs.fake.example/{slug}/list"
module.parse = lambda body, slug: [
{"source_id": i, "title": f"Role {i}", "location": "", "department": "",
"url": f"https://jobs.fake.example/{slug}/{i}", "posted": None,
@@ -590,3 +592,131 @@ def test_the_eu_api_is_refused_for_a_us_board_and_the_reverse() -> None:
with _client(handler) as client, pytest.raises(ats._Refused):
ats._get(client, "https://api.eu.lever.co/v0/postings/x", "lever", "eu/x")
# ------------------------------------------- the guard, asks, remote unasked
def _listing_host(**reads: Any) -> Any:
"""A declared whole-listing board `fake` whose feed lists `module.ids`,
whatever is asked; `reads` override its declaration."""
module: Any = types.ModuleType("fake")
module.NAME, module.LABEL, module.PATTERNS = "fake", "Fake", ()
module.slug_ok = lambda slug: slug.isalnum()
module.allowed = lambda slug: lambda h: h == "jobs.fake.example"
module.careers_url = lambda slug: f"https://jobs.fake.example/{slug}"
module.READS = Reads(**({"scope": "board", "listing": "whole", "text": "listing",
"complete": "whole", "places": "one", "robots": "test"} | reads))
module.ids = []
module.feed_url = lambda slug, ask: f"https://jobs.fake.example/{slug}/list"
module.parse = lambda body, slug: [
{"source_id": i, "title": f"Role {i}", "url": f"https://jobs.fake.example/{i}",
"places": [{"text": "Berlin", "remote": None}]} for i in json.loads(body)]
return module
def _listed(module: Any) -> Callable[..., bytes]:
return lambda url, **kwargs: json.dumps(module.ids).encode()
def _read_and_stamp(state: dict[str, str], module: Any,
limits: _runner.Limits | None = None) -> _runner.Listing:
listing = _runner.read(state, "ats", "fake:acme", module, "acme", _listed(module),
limits=limits)
_runner.stamp(state, "ats", "fake:acme", "r", listing, listing.ids)
return listing
def test_an_unfiltered_whole_read_of_nothing_after_forty_is_suspect() -> None:
"""A Greenhouse-style board, trusted, unfiltered: a changed answer that
parses to no rows is refused rather than read as a board that closed."""
module = _listing_host()
state: dict[str, str] = {}
module.ids = [str(n) for n in range(40)]
_read_and_stamp(state, module)
module.ids = []
with pytest.raises(_runner.Suspect, match="listed 0 roles, under 10% of the 40"):
_runner.read(state, "ats", "fake:acme", module, "acme", _listed(module))
def test_a_whole_read_under_suspect_min_is_not_held() -> None:
module = _listing_host()
state: dict[str, str] = {}
module.ids = [str(n) for n in range(5)]
_read_and_stamp(state, module)
module.ids = []
assert _read_and_stamp(state, module).ids == []
assert json.loads(state["ats:count:fake:acme"])["count"] == 0
def test_the_same_suspect_answer_is_accepted_after_stale_days() -> None:
module = _listing_host()
state: dict[str, str] = {}
module.ids = [str(n) for n in range(40)]
_read_and_stamp(state, module)
module.ids = ["1"]
failing = "ats:failing:fake:acme"
recent = (datetime.now(UTC) - timedelta(days=20)).isoformat(timespec="seconds")
state[failing] = json.dumps({"since": recent, "reason": "Suspect: listed 1 roles"})
limits = _runner.Limits(page_delay=0, stale_days=21)
with pytest.raises(_runner.Suspect):
_runner.read(state, "ats", "fake:acme", module, "acme", _listed(module), limits=limits)
old = (datetime.now(UTC) - timedelta(days=22)).isoformat(timespec="seconds")
state[failing] = json.dumps({"since": old, "reason": "Suspect: listed 1 roles"})
# Outside a pull (no `stale_days`) the guard never gives way.
with pytest.raises(_runner.Suspect):
_runner.read(state, "ats", "fake:acme", module, "acme", _listed(module))
assert _read_and_stamp(state, module, limits).ids == ["1"]
assert json.loads(state["ats:count:fake:acme"])["count"] == 1
def test_keep_size_resets_the_guards_count_unless_told_not_to() -> None:
state = {"ats:count:fake:acme": json.dumps({"asks": "[]", "count": 40})}
ats.keep_size(state, "fake:acme", 40, reset=False)
assert "ats:count:fake:acme" in state
ats.keep_size(state, "fake:acme", 3)
assert "ats:count:fake:acme" not in state
assert state["ats:size:fake:acme"] == "3"
def test_a_filter_that_maps_to_no_host_id_asks_nothing_and_is_incomplete(
monkeypatch: pytest.MonkeyPatch) -> None:
module = _listing_host(filters=Filters(country="ids"))
module.facets = lambda get, slug: {"country": {}}
module.ids = ["1", "2"]
state: dict[str, str] = {}
listing = _runner.read(state, "ats", "fake:acme", module, "acme", _listed(module),
Ask(countries=("DK",)))
assert (listing.rows, listing.ids, listing.complete, listing.filtered) == (
[], [], False, True)
# Through the plugin: stamped, but not complete, so the settle closes nothing.
monkeypatch.setitem(readers.HOSTS, "fake", module)
monkeypatch.setattr(ats, "_get", lambda client, url, host, slug, **kw: b'["1"]')
source = ats.ATSSource({"boards": [{"url": "fake:acme", "label": "Acme",
"params": {"countries": ["DK"]}}], "run": "r"})
assert list(source.fetch()) == []
assert (source.state["ats:ok:fake:acme"], source.state["ats:complete:fake:acme"]) == (
"r", "")
def test_a_filtered_read_that_cannot_ask_remote_says_so() -> None:
reach = {"countries": ["DK"], "remote": True}
blind = _listing_host(filters=Filters(country="many"))
ask = _runner.ask_for(blind, None, reach)
assert (ask.countries, ask.remote) == (("DK",), None)
assert _runner.remote_wanted(blind, None, reach)
listing = _runner.read_listing(blind, "acme", _listed(blind), ask,
remote=_runner.remote_wanted(blind, None, reach))
assert listing.filtered and listing.remote_unasked
state: dict[str, str] = {}
_runner.stamp(state, "ats", "fake:acme", "r", listing, listing.ids)
assert state["ats:remote_unasked:fake:acme"] == "1"
# A host that filters on remote asks it as a series of its own.
sees = _listing_host(filters=Filters(country="many", remote="many"))
ask = _runner.ask_for(sees, None, reach)
listing = _runner.read_listing(sees, "acme", _listed(sees), ask, remote=True)
assert listing.filtered and not listing.remote_unasked
# A params' remote of False asks nothing and wants nothing.
assert not _runner.remote_wanted(blind, {"remote": False}, reach)
+7 -17
View File
@@ -103,8 +103,6 @@ def test_ask_for_narrows_the_params_to_the_declared_filters() -> None:
both = _whole(filters=Filters(country="many", remote="many"))
ask = _runner.ask_for(both, {"countries": ["DE"]}, {"countries": ["DK"], "remote": True})
assert (ask.countries, ask.remote) == (("DE",), True)
# A module without READS reads the whole board.
assert _runner.ask_for(types.ModuleType("old"), {"countries": ["DK"]}) == Ask()
def test_a_searchs_params_go_whole_into_the_ask_with_its_key(
@@ -139,11 +137,12 @@ def test_ids_filters_are_mapped_through_the_facets() -> None:
ask = Ask(countries=("DK", "XX"))
[mapped] = _runner.series(module, ask, {"country": {"DK": "h-1", "SE": "h-2"}})
assert mapped.countries == ("h-1",)
# None known: the host has nothing in reach, and the read is empty.
# None known: nothing is asked, which is no evidence, so the read is
# incomplete and closes nothing.
assert _runner.series(module, Ask(countries=("XX",)), {"country": {"DK": "h-1"}}) == []
listing = _runner.read_listing(module, "acme", Web(), Ask(countries=("XX",)),
facets={"country": {}})
assert (listing.rows, listing.complete, listing.filtered) == ([], True, True)
assert (listing.rows, listing.complete, listing.filtered) == ([], False, True)
def test_facets_are_asked_once_a_day_and_only_for_an_ids_filter() -> None:
@@ -189,17 +188,6 @@ def test_a_body_over_the_cap_is_read_again_at_the_fallback_feed() -> None:
_runner.read_listing(module, "acme", web)
def test_a_module_without_reads_gets_the_older_signatures() -> None:
module: Any = types.ModuleType("old")
module.NAME = "old"
module.feed_url = lambda slug: f"https://jobs.fake.example/{slug}/list"
module.parse = lambda body, slug: [_row(i) for i in json.loads(body)]
listing = _runner.read_listing(module, "acme",
Web({"https://jobs.fake.example/acme/list": b'["1"]'}),
Ask(countries=("DK",)))
assert [r["source_id"] for r in listing.rows] == ["1"] and listing.complete
def test_a_paged_board_with_a_total_is_complete_when_it_read_as_many() -> None:
pages = [["1", "2"], ["3"]]
listing = _runner.read_listing(_paged(pages, total=3), "acme", _pages_web(pages),
@@ -333,9 +321,11 @@ def test_a_suspect_read_far_under_the_last_count_fails() -> None:
web.answers["https://jobs.fake.example/acme/list"] = b'["1"]'
with pytest.raises(_runner.Suspect, match="under 10% of the 20"):
_runner.read(state, "ats", "t", module, "acme", web)
# A reader whose empty answer is trusted reads it as it is.
# A reader whose empty answer is trusted is held too, from a count of
# `SUSPECT_MIN`.
module.READS = module.READS.model_copy(update={"empty": "trusted"})
assert _runner.read(state, "ats", "t", module, "acme", web).ids == ["1"]
with pytest.raises(_runner.Suspect):
_runner.read(state, "ats", "t", module, "acme", web)
def test_the_guard_compares_only_reads_that_asked_the_same() -> None:
+50
View File
@@ -365,6 +365,56 @@ def test_failed_gates_reads_evidence_only() -> None:
"none of remote remote, seniority senior"]
def test_an_exclude_gate_on_title_gates_whole_words_and_names_the_value(
store: Store, config: Config, fake: FakeEmbedder) -> None:
"""The sales and data engineering titles the owner does not want: a
phrase matches whole words of the folded title, so "Data Engineering
Manager" and "Forward Deployed Engineer" stay, and the reason names the
phrase that matched."""
ae = role(store, "1", "Enterprise Account Executive, UK")
de = role(store, "2", "Senior MLOps/Data Engineer")
manager = role(store, "3", "Data Engineering Manager")
fde = role(store, "4", "Forward Deployed Engineer")
se = role(store, "5", "Sales Engineer (SE)")
agent = store.save_agent("No sales", "", [
Gate(field="title", values=["Account Executive", "data engineer"], mode="exclude")])
run(config, store, score.run_score)
scores = store.scores(agent.id)
assert scores[ae] == (0.0, "title Account Executive")
assert scores[de] == (0.0, "title data engineer")
assert all(scores[uid][1] == "" and scores[uid][0] > 0 for uid in (manager, fde, se))
def test_a_favourite_is_gated_by_an_exclude_gate_it_matches(
store: Store, config: Config, fake: FakeEmbedder) -> None:
"""A star skips the `any` group, not the titles the agent leaves out."""
store.add_company("Fave")
director = role(store, "1", "Account Director, Nordics", company="Fave")
engineer = role(store, "2", "Forward Deployed Engineer", company="Fave")
agent = store.save_agent("No sales", "", [
Gate(field="title", values=["account director"], mode="exclude")])
store.set_affinity(agent.id, "fave", 2)
run(config, store, score.run_score)
scores = store.scores(agent.id)
assert scores[director] == (0.0, "title account director")
assert scores[engineer][1] == ""
def test_failed_gates_names_the_value_an_exclude_gate_matched() -> None:
gates = [Gate(field="country", values=["DK"]),
Gate(field="title", values=["account executive", "rvp"], mode="exclude")]
def cand(*states: int) -> Candidate:
return Candidate(uid="u", posted_at=NOW.isoformat(), affinity=2, gates=list(states))
assert score.failed_gates(cand(1, 0), gates) == []
assert score.failed_gates(cand(1, -1), gates) == []
assert score.failed_gates(cand(1, 2), gates) == ["title rvp"]
assert score.failed_gates(cand(0, 1), gates) == ["country not DK", "title account executive"]
def test_a_prefer_gate_adds_its_weight_to_the_score(
store: Store, config: Config, fake: FakeEmbedder) -> None:
remote = role(store, "1", remote="remote")
+53
View File
@@ -371,3 +371,56 @@ def test_a_search_switched_off_mid_pull_ends_the_pull_closed(
assert store.roles() == []
# And a batch that starts after the switch reads nothing.
assert pull.run_batch(FakeConfig(), store, **batches[0])["roles"] == 0 # type: ignore[arg-type]
# ------------------------------------------------- failures and the guard
def _broken_provider(monkeypatch: pytest.MonkeyPatch) -> None:
"""The provider `fake` of `_fake_provider`, answering with an error."""
_fake_provider(monkeypatch, [], complete=True)
def page(get: Any, slug: str, cursor: Any, ask: Ask) -> Any:
raise ValueError("hits renamed")
readers.PROVIDERS["fake"].page = page
def test_a_search_whose_provider_raises_is_recorded_failing(
store: Store, monkeypatch: pytest.MonkeyPatch) -> None:
store.add_subscription("search", "fake:cph", label="Copenhagen", board="fake",
params={"q": "backend"})
_fake_provider(monkeypatch, ["h1"], complete=True)
_pull(store)
_broken_provider(monkeypatch)
result = _pull(store)
assert result["unanswered"] == 1
since, reason = pull.failing_boards(store)["fake:cph"]
assert since and reason == "ValueError: hits renamed"
assert {r.source_id for r in store.roles()} == {"fake:h1"}
# Failing for `STALE_DAYS` closes its roles, as a board's.
old = (datetime.now(UTC) - timedelta(days=pull.STALE_DAYS + 1)).isoformat(
timespec="seconds")
store.state["search:failing:fake:cph"] = json.dumps({"since": old, "reason": reason})
_pull(store)
assert store.roles() == []
# Its next answer removes the record and reopens what it lists.
_fake_provider(monkeypatch, ["h1"], complete=True)
_pull(store)
assert pull.failing_boards(store) == {}
assert {r.source_id for r in store.roles()} == {"fake:h1"}
def test_a_search_that_lists_nothing_after_forty_is_suspect(
store: Store, monkeypatch: pytest.MonkeyPatch) -> None:
store.add_subscription("search", "fake:cph", label="Copenhagen", board="fake",
params={"q": "backend"})
forty = [f"h{n}" for n in range(40)]
_fake_provider(monkeypatch, forty, complete=True)
_pull(store)
_fake_provider(monkeypatch, [], complete=True)
result = _pull(store)
assert (result["unanswered"], result["closed"]) == (1, 0)
assert len(store.roles()) == 40
assert "under 10% of the 40" in pull.failing_boards(store)["fake:cph"][1]
+3 -1
View File
@@ -597,6 +597,7 @@ def test_companies_lists_board_and_open_role_count(
assert res.status_code == 200
[row] = [c for c in res.json() if c["key"] == "acme"]
assert row == {"key": "acme", "name": "Acme", "notes": "", "board": "greenhouse",
"target": "greenhouse:acme",
"subscription_id": sub.id, "on": True, "affinity": 0, "open_roles": 1,
"ats": "greenhouse", "ats_slug": "", "careers_url": "",
"country": "DK", "city": "", "domain": "", "watch": None,
@@ -1449,7 +1450,8 @@ def test_a_company_is_added_by_hand_with_a_reason_and_a_link(
assert res.status_code == 201, res.text
assert res.json() == {
"key": "revolut", "name": "Revolut Ltd", "domain": "", "notes": "wants a login",
"board": "", "subscription_id": None, "on": None, "affinity": 0, "open_roles": 0,
"board": "", "target": "", "subscription_id": None, "on": None, "affinity": 0,
"open_roles": 0,
"ats": "", "ats_slug": "", "careers_url": "https://revolut.example/careers",
"country": "", "city": "", "watch": None, "careers_opened_at": "",
"failing_since": "", "failing_reason": "", "from_search": False,