"""The tabs and the JSON API they call, against a temp database and FastAPI's TestClient. No network — nothing here touches an ATS host or the queue's own worker beyond starting and stopping it with the app.""" from __future__ import annotations from collections.abc import Iterator from datetime import UTC, datetime, timedelta from pathlib import Path import pytest from fastapi import FastAPI from fastapi.testclient import TestClient from app.config import Config, ServerConfig from app.main import create_app from app.storage import Store from app.web import MOVED, SETTINGS, TABS from auth import AuthConfig from desk.models import Item class Clock: """A controllable `Store.clock`, for tests that need upserts to land at distinct, known moments — see `test_storage.py`'s twin of this.""" def __init__(self) -> None: self.at = datetime(2026, 9, 23, 9, 0, tzinfo=UTC) def __call__(self) -> datetime: return self.at def tick(self, hours: float = 1) -> None: self.at += timedelta(hours=hours) def posting(source_id: str = "1", **raw: object) -> Item: item = Item(source_id=source_id, location=f"https://boards.example/acme/{source_id}", title="Senior Backend Engineer", body="Build things.", published_at=datetime(2026, 9, 20, tzinfo=UTC), raw=dict(raw)) item.source = "ats" return item @pytest.fixture def app(tmp_path: Path) -> FastAPI: """The database in `tmp_path` and the config passed in, so a test never touches `roledesk.db` and two tests never share a jobs table.""" 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 # ------------------------------------------------------------------- roles def test_list_roles_filters_by_query_place_and_function( client: TestClient, store: Store) -> None: store.upsert_role(posting("1", company="Acme", place="Berlin", function="engineering")) store.upsert_role(posting("2", company="Acme", place="Munich", function="sales")) store.upsert_role(posting("3", company="Other", place="Berlin", function="engineering")) res = client.get("/api/roles", params={"q": "engineer", "place": "Berlin"}) assert res.status_code == 200 assert {r["source_id"] for r in res.json()} == {"1", "3"} res = client.get("/api/roles", params={"function": "sales"}) assert [r["source_id"] for r in res.json()] == ["2"] res = client.get("/api/roles", params={"company": "other"}) assert [r["source_id"] for r in res.json()] == ["3"] def test_list_roles_limit_is_bounded(client: TestClient, store: Store) -> None: for i in range(3): store.upsert_role(posting(str(i))) assert len(client.get("/api/roles", params={"limit": 1}).json()) == 1 assert client.get("/api/roles", params={"limit": 0}).status_code == 422 assert client.get("/api/roles", params={"limit": 1001}).status_code == 422 def test_muted_roles_are_hidden_unless_show_muted(client: TestClient, store: Store) -> None: store.add_company("Acme", key="acme") store.upsert_role(posting("1", company="Acme")) store.upsert_role(posting("2", company="Other")) assert {r["source_id"] for r in client.get("/api/roles").json()} == {"1", "2"} assert client.post("/api/companies/acme/affinity", json={"affinity": -1}).status_code == 200 assert {r["source_id"] for r in client.get("/api/roles").json()} == {"2"} assert {r["source_id"] for r in client.get("/api/roles", params={"show_muted": "true"}).json()} == {"1", "2"} def test_muted_is_excluded_before_the_limit_not_after( client: TestClient, store: Store) -> None: """A regression case: filtering a muted company out of an already `LIMIT`-ed page can leave nothing to show, if that company happens to be the newest thing in the database — exactly what a company with hundreds of roles, all freshly (re-)pulled at once, produces.""" clock = Clock() store.clock = clock store.add_company("Acme", key="acme", affinity=-1) store.upsert_role(posting("other", company="Other")) clock.tick() for i in range(3): store.upsert_role(posting(f"a{i}", company="Acme")) clock.tick() res = client.get("/api/roles", params={"limit": 1}) assert [r["source_id"] for r in res.json()] == ["other"] # --------------------------------------------------------------- companies def test_companies_lists_board_and_open_role_count( client: TestClient, store: Store) -> None: sub = store.add_subscription("ats", "greenhouse:acme", company="Acme", board="greenhouse") store.add_company("Acme", key="acme") store.upsert_role(posting("1", company="Acme"), sub) res = client.get("/api/companies") assert res.status_code == 200 [row] = [c for c in res.json() if c["key"] == "acme"] assert row == {"key": "acme", "name": "Acme", "board": "greenhouse", "affinity": 0, "open_roles": 1} def test_set_affinity_validates_and_updates(client: TestClient, store: Store) -> None: store.add_company("Acme", key="acme") res = client.post("/api/companies/acme/affinity", json={"affinity": 2}) assert res.status_code == 200 assert res.json()["affinity"] == 2 assert store.company("acme").affinity == 2 # type: ignore[union-attr] assert client.post("/api/companies/acme/affinity", json={"affinity": 9}).status_code == 422 assert client.post("/api/companies/nope/affinity", json={"affinity": 1}).status_code == 404 # ----------------------------------------------------------- applications def test_save_creates_an_application_with_a_saved_event( client: TestClient, store: Store) -> None: store.upsert_role(posting("1", company="Acme", place="Berlin")) uid = posting("1").uid res = client.post("/api/applications", json={"role_uid": uid, "status": "saved"}) assert res.status_code == 201 body = res.json() assert body["role_uid"] == uid assert body["status"] == "saved" assert body["company_key"] == "acme" assert body["next_action"] == "Read the posting and decide" assert body["next_date"] is not None events = store.application_events(body["id"]) assert [e.status for e in events] == ["saved"] # Saving the same role again returns the existing row rather than a # second one. again = client.post("/api/applications", json={"role_uid": uid, "status": "saved"}) assert again.json()["id"] == body["id"] listed = client.get("/api/applications").json() assert [a["id"] for a in listed] == [body["id"]] assert listed[0]["role_title"] == "Senior Backend Engineer" def test_save_an_unknown_role_is_refused(client: TestClient) -> None: res = client.post("/api/applications", json={"role_uid": "nope", "status": "saved"}) assert res.status_code == 404 def test_set_status_validates_and_writes_an_event( client: TestClient, store: Store) -> None: store.upsert_role(posting("1", company="Acme")) uid = posting("1").uid saved = client.post("/api/applications", json={"role_uid": uid}).json() res = client.post(f"/api/applications/{saved['id']}/status", json={"status": "applied"}) assert res.status_code == 200 assert res.json()["status"] == "applied" assert [e.status for e in store.application_events(saved["id"])] == ["saved", "applied"] assert client.post(f"/api/applications/{saved['id']}/status", json={"status": "ghosted"}).status_code == 422 assert client.post("/api/applications/nope/status", json={"status": "applied"}).status_code == 404 # -------------------------------------------------------------------- tabs def test_every_page_renders_with_the_whole_nav(client: TestClient) -> None: assert [t.label for t in TABS] == ["Roles", "Applications"] assert [t.label for t in SETTINGS] == ["Ranking", "Searches", "Companies", "Pulls", "Messages", "Account"] assert all(t.path.startswith("/settings/") for t in SETTINGS) for tab in (*TABS, *SETTINGS): res = client.get(tab.path) assert res.status_code == 200, f"{tab.path} answered {res.status_code}" assert "roledesk" in res.text for other in TABS: assert f">{other.label}" in res.text assert 'href="/settings" class="icon' in res.text assert '' in res.text def test_the_cog_and_one_sub_tab_are_current_on_a_settings_page(client: TestClient) -> None: page = client.get("/settings/pulls").text assert 'class="icon active"' in page for tab in SETTINGS: assert f">{tab.label}" in page assert page.count('aria-current="page"') == 1 current = page.index('aria-current="page"') assert page.rindex('href="', 0, current) == page.index('href="/settings/pulls"') assert 'class="icon active"' not in client.get("/").text def test_settings_is_ranking(client: TestClient) -> None: res = client.get("/settings", follow_redirects=False) assert res.status_code == 302 and res.headers["location"] == "/settings/ranking" @pytest.mark.parametrize(("old", "new"), sorted(MOVED.items())) def test_an_old_tab_moves_permanently_and_keeps_its_query( client: TestClient, old: str, new: str) -> None: res = client.get(old, follow_redirects=False) assert res.status_code == 301 and res.headers["location"] == new res = client.get(f"{old}?agent=3&q=a+b", follow_redirects=False) assert res.status_code == 301 and res.headers["location"] == f"{new}?agent=3&q=a+b" assert client.get(old).status_code == 200 def test_each_page_loads_only_its_own_script(client: TestClient) -> None: scripts = {"roles.js", "applications.js", "ranking.js", "companies.js", "sources.js", "jobs.js", "messages.js"} for path, container, script in [ ("/", 'id="roles-list"', "roles.js"), ("/applications", 'id="applications-list"', "applications.js"), ("/settings/ranking", 'id="ranking-form"', "ranking.js"), ("/settings/searches", 'id="searches-list"', "sources.js"), ("/settings/companies", 'id="companies-list"', "companies.js"), ("/settings/pulls", 'id="kinds"', "jobs.js"), ("/settings/messages", 'id="notices"', "messages.js")]: page = client.get(path).text assert container in page, path assert [s for s in scripts if f"/static/{s}" in page] == [script], path # toasts.js before app.js before the page's own script: `api()` needs # `window.toast`, and every page script needs `api()` and the helpers. assert page.index("toasts.js") < page.index("app.js") < page.index(f"/static/{script}") assert client.get(f"/static/{script}").status_code == 200 assert "ROLEDESK_STATUSES" in client.get("/applications").text def test_the_ranking_page_edits_one_ranking(client: TestClient) -> None: page = client.get("/settings/ranking").text assert 'id="a-intent"' in page and " None: page = client.get("/settings/searches").text assert 'id="searches-list"' in page and 'id="boards-list"' in page assert 'value="1" selected>Region Hovedstaden' in page # from plugins.search def test_the_modules_bring_their_own_elements(client: TestClient) -> None: pulls = client.get("/settings/pulls").text assert pulls.index("") < pulls.index("") assert "" in client.get("/settings/account").text def test_the_install_files_are_served(client: TestClient) -> None: manifest = client.get("/static/manifest.json").json() assert manifest["name"] == "roledesk" and manifest["display"] == "standalone" for icon in manifest["icons"]: assert client.get(icon["src"]).status_code == 200 assert client.get("/static/apple-touch-icon.png").status_code == 200 def test_pull_is_declared_every_six_hours(client: TestClient) -> None: rows = {r["name"]: r for r in client.get("/api/schedules").json()} assert rows["pull"]["kind"] == "pull" assert rows["pull"]["cadence"] == "every 6h" assert rows["pull"]["enabled"] is True and rows["pull"]["unknown"] is False def test_the_jobs_tab_offers_pull_once_and_no_batch(client: TestClient) -> None: """A subset rather than the list: `pipeline/tests` registers kinds of its own into the same global `JOBS` when the whole suite runs.""" kinds = [k["kind"] for k in client.get("/api/jobs/kinds").json()] assert {"embed", "extract", "pull", "score"} <= set(kinds) assert kinds.count("pull") == 1 assert not {"pull_batch", "extract_batch", "advance"} & set(kinds) def test_an_error_is_one_sentence(client: TestClient) -> None: """`api()` toasts `detail` as it is, so a 422 is a sentence, not a list.""" res = client.post("/api/agents", json={"name": "Bad", "gates": [{"field": "salary", "values": [1]}]}) assert res.status_code == 422 assert "gates.0.field" in res.json()["detail"] missing = client.get("/nothing-here") assert missing.status_code == 404 and "text/html" in missing.headers["content-type"] def test_health_is_open_and_reads_the_store(client: TestClient) -> None: assert client.get("/health").json() == {"ok": True, "open_roles": 0} def test_a_locked_app_shuts_every_tab_and_the_api( tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setenv("APP_PASSWORD", "open-sesame-please") config = Config(server=ServerConfig(host="127.0.0.1", port=0), database=tmp_path / "locked.db", auth=AuthConfig(enabled="always")) with TestClient(create_app(config), follow_redirects=False) as locked: for tab in (*TABS, *SETTINGS): assert locked.get(tab.path).status_code == 303 assert locked.get("/api/roles").status_code == 401 assert locked.get("/health").status_code == 200 signed_in = locked.post("/login", json={"password": "open-sesame-please"}) assert signed_in.status_code == 200 assert locked.get("/api/roles").status_code == 200 # ------------------------------------------------------------------ agents def test_agents_are_created_updated_listed_and_deleted(client: TestClient) -> None: body = {"name": "Berlin", "intent": "python backend", "gates": [{"field": "place", "values": ["Berlin"], "mode": "require"}]} created = client.post("/api/agents", json=body) assert created.status_code == 200 agent = created.json() assert agent["weights"] == {"fit": 0.6, "recency": 0.2, "affinity": 0.2} assert agent["gates"][0]["weight"] == 1.0 updated = client.post("/api/agents", json={**body, "id": agent["id"], "enabled": False, "weights": {"fit": 1, "recency": 0, "affinity": 0}}) assert updated.json()["enabled"] is False and updated.json()["weights"]["fit"] == 1 assert [a["name"] for a in client.get("/api/agents").json()] == ["Berlin"] assert client.post("/api/agents", json=body).status_code == 409 assert client.delete(f"/api/agents/{agent['id']}").status_code == 204 assert client.get("/api/agents").json() == [] assert client.delete(f"/api/agents/{agent['id']}").status_code == 404 def test_a_gate_on_an_unknown_field_is_refused(client: TestClient) -> None: body = {"name": "Bad", "gates": [{"field": "salary", "values": [1]}]} assert client.post("/api/agents", json=body).status_code == 422 def test_roles_by_agent_are_ordered_by_score_and_hide_the_gated( client: TestClient, store: Store) -> None: for sid in ("low", "high", "gated"): store.upsert_role(posting(sid)) uids = {r.source_id: r.uid for r in store.roles()} agent = store.save_agent("A") store.replace_scores(agent.id, [(uids["low"], 0.2, ""), (uids["high"], 0.8, ""), (uids["gated"], 0.0, "place unknown")]) shown = client.get(f"/api/roles?agent={agent.id}").json() assert [r["source_id"] for r in shown] == ["high", "low"] assert shown[0]["score"] == 0.8 everything = client.get(f"/api/roles?agent={agent.id}&gated=1").json() assert everything[-1]["gated_reason"] == "place unknown" assert client.get("/api/roles").json()[0]["score"] is None assert client.get("/api/roles?agent=999").status_code == 404 # ----------------------------------------------------------------- sources def _fake_search(monkeypatch: pytest.MonkeyPatch, rows: int) -> None: import plugins.search as search def run(target: str, params: dict[str, object], keys: dict[str, object] | None = None) -> tuple[list[dict[str, object]], bool]: return [{"title": f"Engineer {n}", "company": "Acme", "place": "København", "url": f"https://x.example/{n}"} for n in range(rows)], True monkeypatch.setattr(search, "run_search", run) def test_sources_lists_searches_and_boards_with_their_countries( client: TestClient, store: Store) -> None: board = store.add_subscription("ats", "greenhouse:acme", label="Acme", company="Acme", board="greenhouse") store.upsert_role(posting("1", company="Acme", place="Copenhagen"), board) store.upsert_role(posting("2", company="Acme", place="Boston, MA"), board) store.upsert_role(posting("3", company="Acme", place="Aarhus"), board) store.add_subscription("search", "jobindex:cph", label="Copenhagen", board="jobindex", params={"q": "backend", "area": 1}) res = client.get("/api/sources").json() [row] = res["boards"] assert (row["label"], row["on"], row["open_roles"]) == ("Acme", True, 3) assert row["countries"] == [["DK", 2], ["US", 1]] assert [s["label"] for s in res["searches"]] == ["Copenhagen"] 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: board = store.add_subscription("ats", "greenhouse:acme", label="Acme", company="Acme", board="greenhouse") store.upsert_role(posting("1", company="Acme"), board) off = client.post(f"/api/sources/{board.id}/switch", json={"on": False}).json() assert off["closed"] == 1 and off["source"]["on"] is False assert store.subscriptions(source="ats", fetchable=True) == [] assert store.roles() == [] on = client.post(f"/api/sources/{board.id}/switch", json={"on": True}).json() assert on["closed"] == 0 and on["source"]["on"] is True 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: _fake_search(monkeypatch, 3) body = {"provider": "jobindex", "name": "Copenhagen backend", "q": "backend", "area": 1} tried = client.post("/api/searches/try", json=body).json() assert tried["found"] == 3 and tried["complete"] is True assert tried["sample"][0] == {"title": "Engineer 0", "company": "Acme", "place": "København", "url": "https://x.example/0"} assert store.subscriptions(source="search") == [] saved = client.post("/api/searches", json=body).json() assert saved["source"]["target"] == "jobindex:copenhagen-backend" assert saved["source"]["params"] == {"q": "backend", "area": 1} updated = client.post("/api/searches", json={**body, "q": "python", "pages": 3}).json() assert updated["source"]["id"] == saved["source"]["id"] assert updated["source"]["params"] == {"q": "python", "area": 1, "pages": 3} sub_id = saved["source"]["id"] assert client.delete(f"/api/searches/{sub_id}").json() == {"closed": 0} assert client.get("/api/sources").json()["searches"] == [] again = client.post("/api/searches", json=body).json() # the same row, back on assert again["source"]["id"] == sub_id and again["source"]["on"] is True def test_a_search_that_finds_nothing_or_is_malformed_is_refused( client: TestClient, store: Store, monkeypatch: pytest.MonkeyPatch) -> None: _fake_search(monkeypatch, 0) res = client.post("/api/searches", json={"provider": "jobindex", "name": "X", "q": "x"}) assert res.status_code == 422 and "found nothing" in res.json()["detail"] res = client.post("/api/searches", json={"provider": "indeed", "name": "X", "q": "x"}) assert res.status_code == 422 res = client.post("/api/searches/try", json={"provider": "jobindex", "q": " "}) assert res.status_code == 422 and "query" in res.json()["detail"] assert store.subscriptions(source="search") == [] def test_a_board_is_followed_only_if_it_answers( client: TestClient, store: Store, monkeypatch: pytest.MonkeyPatch) -> None: import plugins.ats as ats monkeypatch.setattr(ats, "fetch_board", lambda target: [{"source_id": "1"}]) res = client.post("/api/boards", json={"board": "ashby:acme", "name": "Acme"}) assert res.status_code == 201 assert res.json()["found"] == 1 and res.json()["source"]["target"] == "ashby:acme" assert store.company("acme") is not None again = client.post("/api/boards", json={"board": "ashby:acme", "name": "Acme"}) assert again.status_code == 422 and "already followed" in again.json()["detail"] bad = client.post("/api/boards", json={"board": "acme", "name": "Other"}) assert bad.status_code == 422 and "host:slug" in bad.json()["detail"] def test_prune_previews_then_switches_off(client: TestClient, store: Store) -> None: us = store.add_subscription("ats", "greenhouse:us", label="US", company="US", board="greenhouse") dk = store.add_subscription("ats", "greenhouse:dk", label="DK", company="DK", board="greenhouse") store.upsert_role(posting("1", company="US", place="Boston, MA"), us) store.upsert_role(posting("2", company="DK", place="Copenhagen"), dk) preview = client.get("/api/prune", params={"region": "DK,EU"}).json() assert [b["label"] for b in preview["boards"]] == ["US"] assert len(store.subscriptions(source="ats", fetchable=True)) == 2 done = client.post("/api/prune", json={"region": "DK,EU"}).json() assert (done["pruned"], done["roles_closed"], done["boards_left"]) == (1, 1, 1) assert client.get("/api/prune", params={"region": " "}).status_code == 422 # ------------------------------------------------- roles: text and summary def test_the_posting_text_is_fetched_once( client: TestClient, store: Store, monkeypatch: pytest.MonkeyPatch) -> None: from desk.tools import scrape from desk.tools.article import ArticleContent store.upsert_role(posting("1")) uid = posting("1").uid asked: list[str] = [] def gone(url: str) -> scrape.Scraped: asked.append(url) return scrape.Scraped(None, "not_found", "http", ()) monkeypatch.setattr(scrape, "scrape", gone) assert client.post("/api/roles/nope/text").status_code == 404 res = client.post(f"/api/roles/{uid}/text") assert res.status_code == 422 and "gone" in res.json()["detail"] assert asked == ["https://boards.example/acme/1"] def found(url: str) -> scrape.Scraped: content = ArticleContent(format="plain", content=" The whole posting.\n", source_url=url, method="test") return scrape.Scraped(content, "", "http", ()) monkeypatch.setattr(scrape, "scrape", found) res = client.post(f"/api/roles/{uid}/text") assert res.status_code == 200 body = res.json() assert body["uid"] == uid and body["article_text"] == "The whole posting." assert body["article_at"] assert client.post(f"/api/roles/{uid}/text").status_code == 409 def test_summarise_queues_one_role_and_the_job_writes_it( app: FastAPI, store: Store, monkeypatch: pytest.MonkeyPatch) -> None: """No lifespan, so no worker: the job waits in the table until drained.""" from typing import Any from desk.tools import agent client = TestClient(app) store.upsert_role(posting("1")) store.upsert_role(posting("2")) uid, bare = posting("1").uid, posting("2").uid with store.transaction() as conn: conn.execute("UPDATE roles SET description = '' WHERE uid = ?", (bare,)) monkeypatch.setattr(agent, "backend", lambda settings: "") assert client.post("/api/roles/nope/summarise").status_code == 404 res = client.post(f"/api/roles/{uid}/summarise") assert res.status_code == 422 and "model" in res.json()["detail"] asked: list[Any] = [] def ask_json(settings: Any, **kwargs: Any) -> agent.Reply: items = kwargs["question"]["summaries"] asked.append([i["uid"] for i in items]) return agent.Reply(data={"summaries": [ {"uid": i["uid"], "summary": "Backend role in Berlin, Python."} for i in items]}, backend="api") monkeypatch.setattr(agent, "backend", lambda settings: "api") monkeypatch.setattr(agent, "ask_json", ask_json) res = client.post(f"/api/roles/{bare}/summarise") assert res.status_code == 422 and "no text" in res.json()["detail"] res = client.post(f"/api/roles/{uid}/summarise") assert res.status_code == 202 job = res.json() assert job["kind"] == "summarise_batch" and job["payload"] == {"uids": [uid]} assert job["state"] == "queued" assert client.post(f"/api/roles/{uid}/summarise").json()["id"] == job["id"] app.state.queue.drain(timeout=30) assert asked == [[uid]] assert store.role(uid).summary == "Backend role in Berlin, Python." # type: ignore[union-attr] assert client.post(f"/api/roles/{uid}/summarise").status_code == 409