Files
jobboard/tests/test_stages.py
T
WolfandClaude Sonnet 471ace703c Switch the extract and summarise stages on the page, over config.yaml
A stored `stage:extract` / `stage:summarise` in the state table overrides
extract.enabled / summarise.enabled; with none, the file applies. One
function, stages.stage_on, decides, and both tasks' finders ask it.
GET/PUT /api/stages and `app stages` show and set it with who would answer;
Runs has a "What the model reads" block, and a search agent's card says when
a stage is off and links to the switch.

Co-Authored-By: Claude Sonnet <noreply@anthropic.com>
2026-09-29 15:54:53 +02:00

271 lines
11 KiB
Python

"""The switch for the two model stages: the stored setting beats config.yaml,
every route to a stage asks the one function, and the API and `app stages`
show and set it. Nothing here reaches a model."""
from __future__ import annotations
from collections.abc import Iterator
from dataclasses import replace
from datetime import UTC, datetime
from pathlib import Path
import pytest
from fastapi import FastAPI
from fastapi.testclient import TestClient
from app import cli
from app.config import Config, ServerConfig
from app.jobs import cascade, stages
from app.main import create_app
from app.storage import Store
from desk.models import Item
from desk.tools import agent
from pipeline import JOBS, TASKS
NAMES = ("extract", "summarise")
def posting(source_id: str) -> Item:
item = Item(source_id=source_id, location=f"https://boards.example/{source_id}",
title=f"Backend Engineer {source_id}", body="Python and Go on Kubernetes.",
published_at=datetime(2026, 9, 20, tzinfo=UTC),
raw={"company": "Acme", "place": "Copenhagen"})
item.source = "ats"
return item
def config(tmp_path: Path, on: bool = False) -> Config:
return Config(database=str(tmp_path / "roledesk.db"),
extract={"enabled": on}, summarise={"enabled": on})
@pytest.fixture
def store(tmp_path: Path) -> Store:
return Store(tmp_path / "roledesk.db")
def ranked(store: Store) -> None:
"""Two roles scored well for one enabled agent: inside both stages' bounds."""
for sid in ("1", "2"):
store.upsert_role(posting(sid))
one = store.save_agent("Backend", "python backend")
store.replace_scores(one.id, [(posting("1").uid, 1.0, ""), (posting("2").uid, 0.9, "")])
def answered_by(monkeypatch: pytest.MonkeyPatch, backend: str) -> None:
monkeypatch.setattr(agent, "backend", lambda settings: backend)
# ---------------------------------------------------------- the one decision
@pytest.mark.parametrize("name", NAMES)
def test_a_stored_setting_beats_config_and_an_absent_one_falls_back(
store: Store, tmp_path: Path, name: str) -> None:
on, off = config(tmp_path, True), config(tmp_path, False)
assert stages.stored(store, name) is None
assert stages.stage_on(on, store, name) is True
assert stages.stage_on(off, store, name) is False
stages.set_stage(store, name, True)
assert store.state[f"stage:{name}"] == "on"
assert stages.stage_on(off, store, name) is True
stages.set_stage(store, name, False)
assert store.state[f"stage:{name}"] == "off"
assert stages.stage_on(on, store, name) is False
del store.state[f"stage:{name}"]
assert stages.stage_on(on, store, name) is True
def test_one_stage_does_not_move_the_other_and_a_junk_value_is_absent(
store: Store, tmp_path: Path) -> None:
off = config(tmp_path, False)
stages.set_stage(store, "extract", True)
assert stages.stage_on(off, store, "summarise") is False
store.state["stage:summarise"] = "maybe"
assert stages.stage_on(off, store, "summarise") is False
with pytest.raises(KeyError):
stages.set_stage(store, "embed", True)
@pytest.mark.parametrize("name", NAMES)
def test_a_pull_runs_a_stage_on_by_the_setting_though_config_says_off(
store: Store, tmp_path: Path, monkeypatch: pytest.MonkeyPatch, name: str) -> None:
ranked(store)
answered_by(monkeypatch, "api")
stage = cascade.task(name, cascade="pull")
assert stage.finder(config(tmp_path, False), store) == []
stages.set_stage(store, name, True)
[batch] = stage.finder(config(tmp_path, False), store)
assert len(batch.payload["uids"]) == 2
@pytest.mark.parametrize("name", NAMES)
def test_a_pull_passes_a_stage_off_by_the_setting_though_config_says_on(
store: Store, tmp_path: Path, monkeypatch: pytest.MonkeyPatch, name: str) -> None:
ranked(store)
answered_by(monkeypatch, "api")
stage = cascade.task(name, cascade="pull")
assert len(stage.finder(config(tmp_path, True), store)) == 1
stages.set_stage(store, name, False)
assert stage.finder(config(tmp_path, True), store) == []
@pytest.mark.parametrize("name", NAMES)
def test_the_task_on_its_own_reads_the_same_setting(
store: Store, tmp_path: Path, monkeypatch: pytest.MonkeyPatch, name: str) -> None:
"""`app job extract` and the `/api/jobs` entry point are the stage off the
cascade: the same finder, the same switch."""
ranked(store)
answered_by(monkeypatch, "api")
alone = TASKS[name]()
assert alone.finder(config(tmp_path, False), store) == []
stages.set_stage(store, name, True)
assert len(alone.finder(config(tmp_path, False), store)) == 1
# ------------------------------------------------------------------- the API
@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 client(app: FastAPI) -> Iterator[TestClient]:
with TestClient(app) as c:
yield c
def test_get_stages_lists_both_with_their_source_model_and_who_answers(
client: TestClient, monkeypatch: pytest.MonkeyPatch) -> None:
answered_by(monkeypatch, "api")
res = client.get("/api/stages")
assert res.status_code == 200
assert res.json() == [
{"name": "extract", "on": False, "source": "config",
"model": "claude-haiku-4-5-20251001", "backend": "api",
"answer": "answered by the Claude API"},
{"name": "summarise", "on": False, "source": "config",
"model": "claude-haiku-4-5-20251001", "backend": "api",
"answer": "answered by the Claude API"},
]
def test_who_answers_is_said_for_the_api_claude_code_and_nobody(
client: TestClient, monkeypatch: pytest.MonkeyPatch) -> None:
answers = {}
for backend in ("api", "claude_code", ""):
answered_by(monkeypatch, backend)
[first, second] = client.get("/api/stages").json()
assert first["backend"] == second["backend"] == backend
answers[backend] = first["answer"]
assert answers["api"] == "answered by the Claude API"
assert answers["claude_code"] == "answered by the Claude Code sign-in on this machine"
assert answers[""] == ("no model is reachable: set ANTHROPIC_API_KEY or "
"CLAUDE_CODE_OAUTH_TOKEN, see docs/llm-backends.md")
def test_put_stores_the_setting_returns_the_same_shape_and_starts_nothing(
client: TestClient, store: Store) -> None:
before = client.get("/api/jobs").json()
res = client.put("/api/stages/summarise", json={"on": True})
assert res.status_code == 200
assert res.json()["name"] == "summarise"
assert res.json()["on"] is True and res.json()["source"] == "stored"
assert store.state["stage:summarise"] == "on"
assert client.get("/api/stages").json()[0]["source"] == "config"
assert client.get("/api/jobs").json() == before
res = client.put("/api/stages/summarise", json={"on": False})
assert res.json()["on"] is False and store.state["stage:summarise"] == "off"
def test_an_unknown_stage_is_404_and_a_bad_body_is_422(client: TestClient) -> None:
assert client.put("/api/stages/embed", json={"on": True}).status_code == 404
assert client.put("/api/stages/extract", json={"on": "sometimes"}).status_code == 422
assert client.put("/api/stages/extract", json={}).status_code == 422
def test_agent_backend_off_is_named_rather_than_blamed_on_missing_keys(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, store: Store) -> None:
off = Config(database=str(tmp_path / "roledesk.db"), agent={"backend": "off"})
[first, _] = stages.describe(off, store)
assert first.backend == "" and "agent.backend" in first.answer
def test_the_summarise_button_works_with_the_stage_off(
app: FastAPI, store: Store, monkeypatch: pytest.MonkeyPatch) -> None:
"""Off by the stored setting over an `on` config: the button queues
`summarise_batch` for one uid, which never asks the switch."""
monkeypatch.setitem(JOBS, "summarise_batch",
replace(JOBS["summarise_batch"], isolate=False, timeout=0.0))
app.state.config.summarise.enabled = True
stages.set_stage(store, "summarise", False)
store.upsert_role(posting("1"))
uid = posting("1").uid
monkeypatch.setattr(agent, "backend", lambda settings: "api")
monkeypatch.setattr(agent, "ask_json", lambda settings, **kw: agent.Reply(
data={"summaries": [{"uid": i["uid"], "summary": "Backend role, Python."}
for i in kw["question"]["summaries"]]}, backend="api"))
res = TestClient(app).post(f"/api/roles/{uid}/summarise")
assert res.status_code == 202
app.state.queue.drain(timeout=30)
assert store.role(uid).summary == "Backend role, Python." # type: ignore[union-attr]
# ----------------------------------------------------------------- the CLI
def test_app_stages_prints_and_sets_through_the_same_function(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str]) -> None:
answered_by(monkeypatch, "claude_code")
path = tmp_path / "config.yaml"
path.write_text(f"database: {tmp_path / 'roledesk.db'}\nextract:\n enabled: true\n")
assert cli.main(["-c", str(path), "stages"]) == 0
out = capsys.readouterr().out.splitlines()
assert out[0].startswith("extract") and " on " in out[0] and "config.yaml" in out[0]
assert out[1].startswith("summarise") and " off " in out[1]
assert "Claude Code sign-in" in out[1]
assert cli.main(["-c", str(path), "stages", "extract", "off"]) == 0
line = capsys.readouterr().out.strip()
assert line.startswith("extract") and " off " in line and "set by the switch" in line
assert Store(tmp_path / "roledesk.db").state["stage:extract"] == "off"
assert cli.main(["-c", str(path), "stages", "summarise", "on"]) == 0
capsys.readouterr()
assert cli.main(["-c", str(path), "stages"]) == 0
out = capsys.readouterr().out.splitlines()
assert " off " in out[0] and " on " in out[1]
def test_app_stages_wants_a_stage_and_a_state_together(
tmp_path: Path, capsys: pytest.CaptureFixture[str]) -> None:
path = tmp_path / "config.yaml"
path.write_text(f"database: {tmp_path / 'roledesk.db'}\n")
assert cli.main(["-c", str(path), "stages", "extract"]) == 1
assert "on or off" in capsys.readouterr().err
with pytest.raises(SystemExit):
cli.main(["-c", str(path), "stages", "embed", "on"])
def test_runs_has_the_switch_block(
client: TestClient) -> None:
page = client.get("/settings/sources").text
assert 'id="stages"' in page and 'id="stages-list"' in page and 'id="stages-answer"' in page
assert "Run the pull now does so at once" in page and "/settings/agents" in page