Ask before a destructive board or search switch; queue a pull after a reopening one

Switching a company's board or a job board search off, replacing a
company's board and detaching one all close open roles in a tap; each
now asks first, naming the count (a replace also names the old and the
new board), the same as removing a search and deleting an agent
already do.

Switching a target back on, following a board, setting a board and
saving a search used to show nothing until the schedule's next pull,
up to six hours later. Each now queues a pull job right in the API
route, so the CLI and any other caller of the same endpoint behave
like the page; the queued pull's own refusal while one is already
running is not surfaced as an error.

Co-Authored-By: Claude Sonnet <noreply@anthropic.com>
This commit is contained in:
WolfandClaude Sonnet committed 2026-09-29 15:18:16 +02:00
1 parent 92ff2bc069
commit e6be593542
6 files changed
+143 -23

No files matched your search

+58 -5
View File
@@ -870,8 +870,13 @@ def test_sources_lists_searches_and_boards_with_their_countries(
assert res["searches"][0]["params"] == {"q": "backend", "area": 1}
def test_switching_a_board_off_closes_its_roles_and_on_keeps_them_closed_until_a_pull(
client: TestClient, store: Store) -> None:
#: Every non-final state a job may be in, for a "was one queued at all" check
#: that does not race a background worker draining it before the assertion.
_ANY_STATE = ("queued", "running", "done", "failed", "cancelled")
def test_switching_a_board_off_closes_its_roles_and_on_queues_a_pull(
client: TestClient, store: Store, queue: Any) -> None:
board = store.add_subscription("ats", "greenhouse:acme", label="Acme", company="Acme",
board="greenhouse")
store.upsert_role(posting("1", company="Acme"), board)
@@ -880,14 +885,18 @@ def test_switching_a_board_off_closes_its_roles_and_on_keeps_them_closed_until_a
assert off["closed"] == 1 and off["source"]["on"] is False
assert store.subscriptions(source="ats", fetchable=True) == []
assert store.roles() == []
assert queue.jobs.last("pull", states=_ANY_STATE) is None # off reads nothing again
on = client.post(f"/api/sources/{board.id}/switch", json={"on": True}).json()
assert on["closed"] == 0 and on["source"]["on"] is True
# On queues a pull right after, so the reopened board is read by the one
# running or the next one rather than the schedule, up to six hours away.
assert queue.jobs.last("pull", states=_ANY_STATE) is not None
assert client.post("/api/sources/999/switch", json={"on": True}).status_code == 404
def test_a_search_is_tried_saved_updated_and_removed(
client: TestClient, store: Store, monkeypatch: pytest.MonkeyPatch) -> None:
client: TestClient, store: Store, queue: Any, monkeypatch: pytest.MonkeyPatch) -> None:
_fake_search(monkeypatch, 3)
body = {"provider": "jobindex", "name": "Copenhagen backend", "q": "backend", "area": 1}
@@ -896,10 +905,14 @@ def test_a_search_is_tried_saved_updated_and_removed(
assert tried["sample"][0] == {"title": "Engineer 0", "company": "Acme",
"place": "København", "url": "https://x.example/0"}
assert store.subscriptions(source="search") == []
assert queue.jobs.last("pull", states=_ANY_STATE) is None # a mere try reads nothing
saved = client.post("/api/searches", json=body).json()
assert saved["source"]["target"] == "jobindex:copenhagen-backend"
assert saved["source"]["params"] == {"q": "backend", "area": 1}
# Saving queues a pull right after, so its roles are read by the one
# running or the next one rather than the schedule, up to six hours away.
assert queue.jobs.last("pull", states=_ANY_STATE) is not None
updated = client.post("/api/searches", json={**body, "q": "python", "pages": 3}).json()
assert updated["source"]["id"] == saved["source"]["id"]
@@ -925,7 +938,7 @@ def test_a_search_that_finds_nothing_or_is_malformed_is_refused(
def test_a_board_is_followed_only_if_it_answers(
client: TestClient, store: Store, monkeypatch: pytest.MonkeyPatch) -> None:
client: TestClient, store: Store, queue: Any, monkeypatch: pytest.MonkeyPatch) -> None:
import plugins.ats as ats
monkeypatch.setattr(ats, "fetch_board", lambda target: ([{"source_id": "1"}], True))
@@ -933,6 +946,10 @@ def test_a_board_is_followed_only_if_it_answers(
assert res.status_code == 201
assert res.json()["found"] == 1 and res.json()["source"]["target"] == "ashby:acme"
assert store.company("acme") is not None
# Following a board queues a pull right after, so its roles are read by
# the one running or the next one rather than the schedule, up to six
# hours away.
assert queue.jobs.last("pull", states=_ANY_STATE) 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.
@@ -952,7 +969,7 @@ def test_a_board_is_followed_only_if_it_answers(
def test_a_companys_board_is_set_replaced_and_detached(
client: TestClient, store: Store, monkeypatch: pytest.MonkeyPatch) -> None:
client: TestClient, store: Store, queue: Any, monkeypatch: pytest.MonkeyPatch) -> None:
import plugins.ats as ats
monkeypatch.setattr(ats, "fetch_board", lambda target: ([], True)) # between openings
@@ -967,16 +984,24 @@ def test_a_companys_board_is_set_replaced_and_detached(
[row] = client.get("/api/companies").json()
assert (row["board"], row["subscription_id"], row["ats"], row["ats_slug"]) == (
"greenhouse", sub_id, "greenhouse", "acme")
# Setting a board queues a pull right after, so it is read by the one
# running or the next one rather than the schedule, up to six hours away.
assert queue.jobs.last("pull", states=_ANY_STATE) is not None
res = client.post("/api/companies/acme/board", json={"board": "lever:acme"})
assert res.json()["closed"] == 1 and res.json()["source"]["id"] == sub_id
before_detach = queue.jobs.last("pull", states=_ANY_STATE)
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
# Detach reads nothing, so it queues no new pull.
after_detach = queue.jobs.last("pull", states=_ANY_STATE)
assert (before_detach.id if before_detach else None) == (
after_detach.id if after_detach else None)
assert client.post("/api/companies/nope/board",
json={"board": "lever:acme"}).status_code == 404
@@ -985,6 +1010,34 @@ def test_a_companys_board_is_set_replaced_and_detached(
assert bad.status_code == 422 and "host:slug" in bad.json()["detail"]
def test_the_pull_a_source_switch_queues_is_refused_quietly_while_one_runs(
app: FastAPI, store: Store) -> None:
"""The four actions that queue a `pull` (switching a source on, following
or setting a board, saving a search) all go through the same job, whose
own refusal while another pull is in flight is not an error — see
`app.jobs.pull.claim_run` and `app.jobs.cascade.run_pull`. Exercised here
through the switch route as a stand-in for the other three, which queue
the identical `pull` job. No lifespan on this client, so no background
worker races the assertion: the queued job waits for the explicit
`drain`, and a refused pull starts no cascade — no fetch, no embed,
nothing else here needs mocking."""
from app.jobs import pull
client = TestClient(app)
board = store.add_subscription("ats", "greenhouse:acme", label="Acme", company="Acme",
board="greenhouse")
running = pull.claim_run(store)
assert running
res = client.post(f"/api/sources/{board.id}/switch", json={"on": True})
assert res.status_code == 200
app.state.queue.drain(timeout=30)
job = app.state.queue.jobs.last("pull", states=("done",))
assert job is not None
assert job.result == {"skipped": pull.ALREADY_RUNNING, "running": running}
# ------------------------------------------------- roles: text and summary
def test_the_posting_text_is_fetched_once(