Files
jobboard/tests/test_web.py
T
WolfandClaude Fable 5.1 c4f2bcc0c9 Viewing as leads the Roles rail; settings get a rail, agent cards, one Sources section at a time
A search agent is a persona, and every like or pass belongs to the one
being viewed. The rail now opens on Viewing as: a full-width chooser,
the name alone with one agent, a note with none, remembered in the URL
and localStorage. The vote buttons, the reader's affinity control, the
feed, the tag counts and the reader fetch all carry that agent; before,
votes sent none and every one landed on the default agent. Best first
and Newest first are back as the segmented control under Order,
independent of the agent. The rail selects no longer clip descenders:
Chrome ignores line-height on a select, so the fixed box is gone in
favour of padding and a min-height.

Settings render inside a sticky rail (Search agents; Sources with Job
boards, Company boards, Runs, Messages; Account), a strip under the
header on a phone, so Runs is one click away however long the company
list gets. Sources shows one section at a time, chosen by the fragment,
so the old redirects still land. Search agents are cards, newest first:
name, an On/Off switch with its word, the intent in full, the weights,
Edit in place, New agent on top, and a link to Roles as that agent.
Switching one on rescores it.

replace_scores writes nothing for an agent deleted while score was
running, instead of failing the run on the foreign key.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-28 15:05:55 +02:00

968 lines
45 KiB
Python

"""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
from typing import Any
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 app.web.pages import SECTIONS
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 queue(app: FastAPI) -> Any:
return app.state.queue
@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()["items"]} == {"1", "3"}
res = client.get("/api/roles", params={"function": "sales"})
assert [r["source_id"] for r in res.json()["items"]] == ["2"]
res = client.get("/api/roles", params={"company": "other"})
assert [r["source_id"] for r in res.json()["items"]] == ["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()["items"]) == 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"))
agent = store.save_agent("A")
other = store.save_agent("B")
def shown(**params: Any) -> set[str]:
return {r["source_id"] for r in client.get("/api/roles", params=params).json()["items"]}
assert shown() == {"1", "2"}
# No agent in the body: the default agent's, which is also who a list
# without `agent` hides for.
assert client.post("/api/companies/acme/affinity", json={"affinity": -1}).status_code == 200
assert shown() == {"2"}
assert shown(show_muted="true") == {"1", "2"}
assert shown(agent=agent.id, order="newest") == {"2"}
# Hidden is per agent: the other agent still sees Acme.
assert shown(agent=other.id, order="newest") == {"1", "2"}
assert [c["affinity"] for c in client.get("/api/companies").json()] == [-1]
assert [c["affinity"] for c in
client.get("/api/companies", params={"agent": other.id}).json()] == [0]
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")
store.set_affinity(store.save_agent("A").id, "acme", -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()["items"]] == ["other"]
assert res.json()["total"] == 1
def test_list_roles_pages_by_offset_and_counts_the_total(
client: TestClient, store: Store) -> None:
clock = Clock()
store.clock = clock
for i in range(5):
store.upsert_role(posting(str(i)))
clock.tick()
first = client.get("/api/roles", params={"limit": 2}).json()
assert [r["source_id"] for r in first["items"]] == ["4", "3"]
assert first["total"] == 5
rest = client.get("/api/roles", params={"limit": 2, "offset": 4}).json()
assert [r["source_id"] for r in rest["items"]] == ["0"] and rest["total"] == 5
assert client.get("/api/roles", params={"offset": 9}).json() == {"items": [], "total": 5}
assert client.get("/api/roles", params={"offset": -1}).status_code == 422
assert len(client.get("/api/roles").json()["items"]) == 5 # default 60
def test_a_vote_is_set_cleared_and_filtered_on(client: TestClient, store: Store) -> None:
for sid in ("liked", "passed", "fine", "open"):
store.upsert_role(posting(sid))
uid = {r.source_id: r.uid for r in store.roles()}
agent = store.save_agent("A")
# No `agent` in the body: the default agent's vote.
res = client.post(f"/api/roles/{uid['liked']}/vote", json={"value": 1})
assert res.status_code == 200
assert res.json()["vote"] == 1 and res.json()["voted_at"] is not None
assert store.votes(agent.id) == {uid["liked"]: 1}
client.post(f"/api/roles/{uid['passed']}/vote", json={"value": -1, "agent": agent.id})
client.post(f"/api/roles/{uid['fine']}/vote", json={"value": 0})
def judged(value: str, **params: Any) -> set[str]:
page = client.get("/api/roles", params={"judged": value, **params}).json()
return {r["source_id"] for r in page["items"]}
assert judged("liked") == {"liked"}
assert judged("passed") == {"passed"}
assert judged("unjudged") == {"open"}
assert judged("") == {"liked", "passed", "fine", "open"} # a pass is not hidden
assert judged("liked", agent=agent.id, order="newest") == {"liked"}
assert client.get("/api/roles", params={"judged": "maybe"}).status_code == 422
assert client.get(f"/api/roles/{uid['liked']}").json()["vote"] == 1
assert client.get(f"/api/roles/{uid['liked']}", params={"agent": agent.id}).json()["vote"] == 1
cleared = client.delete(f"/api/roles/{uid['liked']}/vote")
assert cleared.status_code == 200
assert cleared.json()["vote"] is None and cleared.json()["voted_at"] is None
assert judged("unjudged") == {"open", "liked"}
cleared = client.delete(f"/api/roles/{uid['passed']}/vote", params={"agent": agent.id})
assert cleared.status_code == 200 and cleared.json()["vote"] is None
assert client.post(f"/api/roles/{uid['open']}/vote",
json={"value": 2}).status_code == 422
assert client.post("/api/roles/nope/vote", json={"value": 1}).status_code == 404
assert client.delete("/api/roles/nope/vote").status_code == 404
assert client.post(f"/api/roles/{uid['open']}/vote",
json={"value": 1, "agent": 999}).status_code == 404
assert client.delete(f"/api/roles/{uid['open']}/vote", params={"agent": 999}).status_code == 404
def test_votes_are_per_agent_over_the_api(client: TestClient, store: Store) -> None:
store.upsert_role(posting("1"))
uid = posting("1").uid
one = store.save_agent("One")
two = store.save_agent("Two")
assert client.post(f"/api/roles/{uid}/vote", json={"value": 1, "agent": one.id}).json()["vote"] == 1
assert client.post(f"/api/roles/{uid}/vote", json={"value": -1, "agent": two.id}).json()["vote"] == -1
assert client.get(f"/api/roles/{uid}", params={"agent": one.id}).json()["vote"] == 1
assert client.get(f"/api/roles/{uid}", params={"agent": two.id}).json()["vote"] == -1
assert client.get(f"/api/roles/{uid}").json()["vote"] == 1 # the default's
assert client.get(f"/api/roles/{uid}", params={"agent": 999}).status_code == 404
def listed(**params: Any) -> list[int | None]:
return [r["vote"] for r in client.get("/api/roles", params=params).json()["items"]]
assert listed(agent=one.id, order="newest") == [1]
assert listed(agent=two.id, order="newest") == [-1]
assert listed() == [1]
def test_a_vote_or_an_affinity_needs_an_agent_to_belong_to(
client: TestClient, store: Store) -> None:
"""Nothing to vote as: a 422 with a sentence, not a vote that lands
nowhere. Reading is fine, everything is simply unjudged and neutral."""
store.upsert_role(posting("1"))
store.add_company("Acme", key="acme")
uid = posting("1").uid
res = client.post(f"/api/roles/{uid}/vote", json={"value": 1})
assert res.status_code == 422 and "Settings" in res.json()["detail"]
assert client.delete(f"/api/roles/{uid}/vote").status_code == 422
assert client.post("/api/companies/acme/affinity", json={"affinity": 1}).status_code == 422
assert client.get(f"/api/roles/{uid}").json()["vote"] is None
assert client.get("/api/roles").json()["items"][0]["vote"] is None
assert client.get("/api/companies").json()[0]["affinity"] == 0
def test_roles_with_an_agent_can_be_ordered_newest_and_keep_the_votes(
client: TestClient, store: Store) -> None:
clock = Clock()
store.clock = clock
for sid in ("1", "2", "3"):
store.upsert_role(posting(sid))
clock.tick()
uid = {r.source_id: r.uid for r in store.roles()}
agent = store.save_agent("A")
store.replace_scores(agent.id, [(uid["1"], 0.9, ""), (uid["2"], 0.1, "")]) # 3 unscored
store.set_vote(agent.id, uid["1"], 1)
page = client.get("/api/roles", params={"agent": agent.id, "order": "newest"}).json()
assert [r["source_id"] for r in page["items"]] == ["3", "2", "1"]
assert [r["score"] for r in page["items"]] == [None, None, None]
assert [r["vote"] for r in page["items"]] == [None, None, 1]
assert page["total"] == 3
scored = client.get("/api/roles", params={"agent": agent.id, "order": "score"}).json()
assert [r["source_id"] for r in scored["items"]] == ["1", "2"]
assert client.get("/api/roles", params={"agent": agent.id,
"order": "best"}).status_code == 422
def test_a_vote_moves_a_role_in_the_ranking_at_once(
client: TestClient, store: Store) -> None:
for sid in ("a", "b", "c"):
store.upsert_role(posting(sid))
uid = {r.source_id: r.uid for r in store.roles()}
agent = store.save_agent("A")
store.replace_scores(agent.id, [(uid["a"], 0.9, ""), (uid["b"], 0.6, ""),
(uid["c"], 0.55, "")])
def ranked() -> list[tuple[str, float]]:
page = client.get("/api/roles", params={"agent": agent.id}).json()
return [(r["source_id"], round(r["score"], 3)) for r in page["items"]]
assert ranked() == [("a", 0.9), ("b", 0.6), ("c", 0.55)]
client.post(f"/api/roles/{uid['a']}/vote", json={"value": -1})
client.post(f"/api/roles/{uid['c']}/vote", json={"value": 1})
assert ranked() == [("c", 0.66), ("b", 0.6), ("a", 0.45)]
client.post(f"/api/roles/{uid['b']}/vote", json={"value": 0})
client.post(f"/api/roles/{uid['a']}/vote", json={"value": 1})
assert ranked() == [("a", 1.0), ("c", 0.66), ("b", 0.6)] # capped at 1
def test_read_state_is_set_and_cleared_over_the_api(client: TestClient, store: Store) -> None:
store.upsert_role(posting("1"))
uid = posting("1").uid
res = client.post(f"/api/roles/{uid}/read")
assert res.status_code == 200
assert res.json()["read_at"] is not None
res = client.delete(f"/api/roles/{uid}/read")
assert res.status_code == 200
assert res.json()["read_at"] is None
assert client.post("/api/roles/nope/read").status_code == 404
assert client.delete("/api/roles/nope/read").status_code == 404
def test_list_roles_filters_by_include_read(client: TestClient, store: Store) -> None:
store.upsert_role(posting("1"))
store.upsert_role(posting("2"))
uid = posting("1").uid
client.post(f"/api/roles/{uid}/read")
all_ = client.get("/api/roles").json()
assert {r["source_id"] for r in all_["items"]} == {"1", "2"}
unread = client.get("/api/roles", params={"include_read": "false"}).json()
assert {r["source_id"] for r in unread["items"]} == {"2"}
assert unread["total"] == 1
def test_list_roles_filters_by_tags_any_and_all(client: TestClient, store: Store) -> None:
store.upsert_role(posting("1"))
store.upsert_role(posting("2"))
uid = {r.source_id: r.uid for r in store.roles()}
store.set_tags(uid["1"], ["python", "backend"])
store.set_tags(uid["2"], ["python", "frontend"])
res = client.get("/api/roles", params={"tags": ["python"]})
assert {r["source_id"] for r in res.json()["items"]} == {"1", "2"}
both = client.get("/api/roles", params={"tags": ["python", "backend"],
"tag_mode": "all"})
assert {r["source_id"] for r in both.json()["items"]} == {"1"}
[item] = both.json()["items"]
assert item["tags"] == ["backend", "python"]
def test_filters_lists_tag_counts_and_respects_show_muted(
client: TestClient, store: Store) -> None:
store.add_company("Hidden", key="hidden")
agent = store.save_agent("A") # the default agent, whose hidden companies count
store.set_affinity(agent.id, "hidden", -1)
store.upsert_role(posting("1", company="Acme"))
store.upsert_role(posting("2", company="Hidden"))
uid = {r.source_id: r.uid for r in store.roles(hide_muted=False)}
store.set_tags(uid["1"], ["python"])
store.set_tags(uid["2"], ["python"])
res = client.get("/api/filters")
assert res.status_code == 200
assert res.json() == {"tags": [{"tag": "python", "count": 1, "unread": 1}]}
muted = client.get("/api/filters", params={"show_muted": "true"})
assert muted.json() == {"tags": [{"tag": "python", "count": 2, "unread": 2}]}
def test_one_role_and_its_read_page(client: TestClient, store: Store) -> None:
store.upsert_role(posting("1", company="Acme", place="Berlin"))
uid = posting("1").uid
res = client.get(f"/api/roles/{uid}")
assert res.status_code == 200
body = res.json()
assert (body["uid"], body["company"], body["description"]) == (uid, "Acme",
"Build things.")
assert body["vote"] is None and body["summary"] == "" and body["article_text"] == ""
assert client.get("/api/roles/nope").status_code == 404
page = client.get(f"/roles/{uid}")
assert page.status_code == 200
assert '<body data-reader="open">' in page.text and "/static/roles.js" in page.text
missing = client.get("/roles/nope")
assert missing.status_code == 404 and "text/html" in missing.headers["content-type"]
def test_the_roles_page_offers_the_judged_filter_and_hidden_companies(
client: TestClient) -> None:
page = client.get("/").text
assert 'id="f-judged"' in page and "<details" in page
for label in ("Liked", "Unjudged", "Passed", "Show hidden companies"):
assert label in page
assert "Show muted" not in page
# --------------------------------------------------------------- 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)
store.add_company("Other", key="other") # known only from a search: no board
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",
"subscription_id": sub.id, "on": True, "affinity": 0, "open_roles": 1}
[other] = [c for c in res.json() if c["key"] == "other"]
assert (other["board"], other["subscription_id"], other["on"]) == ("", None, None)
def test_set_affinity_validates_updates_and_enqueues_a_score(
client: TestClient, store: Store, queue: Any) -> None:
store.add_company("Acme", key="acme")
agent = store.save_agent("A")
other = store.save_agent("B")
# No `agent` in the body: the default agent's affinity.
res = client.post("/api/companies/acme/affinity", json={"affinity": 2})
assert res.status_code == 200
assert res.json()["affinity"] == 2
assert store.company("acme", agent.id).affinity == 2 # type: ignore[union-attr]
assert store.company("acme", other.id).affinity == 0 # type: ignore[union-attr]
assert queue.jobs.last("score", states=("queued", "running", "done")) is not None
res = client.post("/api/companies/acme/affinity", json={"affinity": -1, "agent": other.id})
assert res.status_code == 200 and res.json()["affinity"] == -1
assert store.company("acme", agent.id).affinity == 2 # type: ignore[union-attr]
assert [c["affinity"] for c in client.get("/api/companies", params={"agent": other.id}).json()] == [-1]
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
assert client.post("/api/companies/acme/affinity",
json={"affinity": 1, "agent": 999}).status_code == 404
assert client.get("/api/companies", params={"agent": 999}).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"
# No placeholder: a next step is the CLI's to set.
assert body["next_action"] == "" and body["next_date"] is 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
def test_unsave_drops_a_saved_application_and_refuses_one_that_moved_on(
client: TestClient, store: Store) -> None:
store.upsert_role(posting("1", company="Acme"))
store.upsert_role(posting("2", company="Acme"))
first = client.post("/api/applications", json={"role_uid": posting("1").uid}).json()
second = client.post("/api/applications", json={"role_uid": posting("2").uid}).json()
assert client.delete(f"/api/applications/{first['id']}").status_code == 204
assert store.application(first["id"]) is None
assert store.application_events(first["id"]) == []
assert client.delete(f"/api/applications/{first['id']}").status_code == 404
client.post(f"/api/applications/{second['id']}/status", json={"status": "applied"})
refused = client.delete(f"/api/applications/{second['id']}")
assert refused.status_code == 409 and "withdrawn" in refused.json()["detail"]
assert store.application(second["id"]) is not None
def test_the_applications_page_has_no_next_column(client: TestClient) -> None:
page = client.get("/applications").text
assert "<th>Next</th>" not in page and "<th>Saved</th>" in page
# -------------------------------------------------------------------- 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] == ["Search agents", "Sources", "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</title>" in res.text
for other in TABS:
assert f">{other.label}</a>" in res.text
assert 'href="/settings" class="icon' in res.text
assert '<link rel="manifest" href="/static/manifest.json">' in res.text
def test_the_cog_and_one_rail_entry_are_current_on_a_settings_page(client: TestClient) -> None:
page = client.get("/settings/sources").text
assert 'class="icon active"' in page
for tab in SETTINGS:
assert f">{tab.label}</a>" 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/sources"')
assert 'class="icon active"' not in client.get("/").text
def test_every_settings_page_has_the_rail_and_not_the_tab_row(client: TestClient) -> None:
"""The rail replaces the `<h1>Settings</h1>` and the sub-tab row: every
page, then the Sources sections indented under Sources, from any page."""
for tab in SETTINGS:
page = client.get(tab.path).text
assert '<aside class="settings-rail"' in page, tab.path
assert 'class="subtabs"' not in page and "<h1>Settings</h1>" not in page
rail = page[page.index('<aside class="settings-rail"'):page.index("</aside>")]
assert [rail.index(f'href="{t.path}"') for t in SETTINGS] == sorted(
rail.index(f'href="{t.path}"') for t in SETTINGS)
for anchor, label in SECTIONS["/settings/sources"]:
link = f'href="/settings/sources#{anchor}" data-section="{anchor}">{label}</a>'
assert rail.index('href="/settings/sources"') < rail.index(link) \
< rail.index('href="/settings/account"')
def test_a_moved_section_is_one_the_sources_page_shows(client: TestClient) -> None:
"""Every old path that lands on a fragment of Sources names a section of
`SECTIONS`, and each is a `<section data-section>` on the page, which is
what sources.js shows one of."""
anchors = [anchor for anchor, _ in SECTIONS["/settings/sources"]]
assert anchors == ["job-boards", "company-boards", "runs", "messages"]
for to in MOVED.values():
path, _, anchor = to.partition("#")
if anchor:
assert path == "/settings/sources" and anchor in anchors, to
page = client.get("/settings/sources").text
assert page.count("data-section>") == len(anchors)
for anchor in anchors:
assert f'<section class="card" id="{anchor}" data-section>' in page
def test_settings_is_search_agents(client: TestClient) -> None:
res = client.get("/settings", follow_redirects=False)
assert res.status_code == 302 and res.headers["location"] == "/settings/agents"
@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:
"""A merged page's old path lands on its section: the query goes before
the anchor, or the browser would take `#runs?q=` as one fragment."""
assert {"/agents", "/settings/ranking", "/companies", "/sources", "/jobs",
"/schedules", "/messages", "/settings/searches", "/settings/companies",
"/settings/pulls", "/settings/messages", "/account"} <= set(MOVED)
path, _, anchor = new.partition("#")
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
assert res.headers["location"] == f"{path}?agent=3&q=a+b" + (f"#{anchor}" if anchor else "")
assert client.get(old).status_code == 200
def test_each_page_loads_only_its_own_script(client: TestClient) -> None:
"""Sources is four sections' scripts on one page, each guarding on its own
root element; every other page is one."""
scripts = {"roles.js", "applications.js", "agents.js", "companies.js", "sources.js",
"jobs.js", "messages.js"}
for path, container, own in [
("/", 'id="roles-list"', ["roles.js"]),
("/applications", 'id="applications-list"', ["applications.js"]),
("/settings/agents", 'id="agents-list"', ["agents.js"]),
("/settings/sources", 'id="searches-list"',
["sources.js", "companies.js", "jobs.js", "messages.js"])]:
page = client.get(path).text
assert container in page, path
assert sorted(s for s in scripts if f"/static/{s}" in page) == sorted(own), path
# toasts.js before app.js before the page's own scripts: `api()` needs
# `window.toast`, and every page script needs `api()` and the helpers.
for script in own:
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
assert client.get("/static/ranking.js").status_code == 404
def test_the_agents_page_lists_and_edits_search_agents(client: TestClient) -> None:
"""A card per agent, filled by agents.js, and the form as a `<template>`
it clones into the card being edited, or a new one on top."""
page = client.get("/settings/agents").text
assert 'id="agents-list"' in page and 'id="agents-count"' in page
assert page.index('id="a-new"') < page.index('id="agents-list"')
template = page[page.index('<template id="agent-form-template">'):page.index("</template>")]
assert 'id="agent-form"' in template and 'id="a-cancel"' in template
assert 'id="a-name"' in page and 'placeholder="My search"' in page
assert 'id="a-intent"' in page and "<details" in page and 'id="a-gates"' in page
for weight in ("a-fit", "a-recency", "a-affinity", "a-taste"):
assert f'id="{weight}"' in page
assert 'id="a-taste" min="0" step="0.05" value="0.2"' in page
assert 'id="a-new"' in page and 'id="a-delete"' in page
assert "Save and score" in page
def test_the_sources_page_has_a_section_per_plugin(client: TestClient) -> None:
"""Job boards, company boards, the runs and the message log, in that
order, each linked from the rail; no jump row any more."""
page = client.get("/settings/sources").text
assert '<aside class="settings-rail"' in page and 'class="jump"' not in page
for anchor in ("job-boards", "company-boards", "runs", "messages"):
assert f'href="/settings/sources#{anchor}"' in page and f'id="{anchor}"' in page
assert page.index('id="job-boards"') < page.index('id="company-boards"') \
< page.index('id="runs"') < page.index('id="messages"')
# Job boards: the Jobindex areas and the Adzuna countries, from plugins.search.
assert 'id="searches-list"' in page and 'id="search-form"' in page
assert 'value="1" selected>Region Hovedstaden' in page
assert "job board" in page.lower() and "saved search" not in page.lower()
# Company boards: the follow form, the agent select, the list, the prune tool.
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
assert 'id="prune-form"' in page and "Switch off boards with no roles in a region" in page
boards = page[page.index('id="company-boards"'):page.index('id="runs"')].lower()
assert "hidden" in boards and "mute" not in boards
# Runs: Pull now, the queue's elements, one stage at a time, the log.
assert 'id="pull-now"' in page and 'id="kinds"' in page
assert 'id="notices"' in page and 'id="clear-all"' in page
def test_the_modules_bring_their_own_elements(client: TestClient) -> None:
pulls = client.get("/settings/sources").text
assert pulls.index("<desk-jobs>") < pulls.index("<desk-schedules>")
assert "<auth-account>" 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, "taste": 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, "taste": 0.5}})
assert updated.json()["enabled"] is False and updated.json()["weights"]["fit"] == 1
assert updated.json()["weights"]["taste"] == 0.5
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_deleting_an_agent_takes_its_votes_and_affinities_with_it(
client: TestClient, store: Store) -> None:
store.upsert_role(posting("1"))
store.add_company("Acme", key="acme")
uid = posting("1").uid
agent = client.post("/api/agents", json={"name": "A"}).json()
kept = client.post("/api/agents", json={"name": "B"}).json()
client.post(f"/api/roles/{uid}/vote", json={"value": 1, "agent": agent["id"]})
client.post(f"/api/roles/{uid}/vote", json={"value": -1, "agent": kept["id"]})
client.post("/api/companies/acme/affinity", json={"affinity": 2, "agent": agent["id"]})
assert client.delete(f"/api/agents/{agent['id']}").status_code == 204
with store.transaction() as conn:
assert conn.execute("SELECT COUNT(*) FROM role_votes").fetchone()[0] == 1
assert conn.execute("SELECT COUNT(*) FROM agent_companies").fetchone()[0] == 0
assert client.get(f"/api/roles/{uid}", params={"agent": kept["id"]}).json()["vote"] == -1
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()["items"]
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()["items"]
assert everything[-1]["gated_reason"] == "place unknown"
assert client.get("/api/roles").json()["items"][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 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