Migration 17 adds roles.place_fold and fills seniority from the title where extract has not read the role. Every connection registers place_match, so the Place filter and the place gate match whole folded words: Lund no longer finds Billund. GET /api/roles takes company_keys, places, seniority, seniority_unknown and pay_unknown; new routes suggest companies and places, label place tokens and count the seniority and pay facets. Co-Authored-By: Claude <noreply@anthropic.com>
1647 lines
81 KiB
Python
1647 lines
81 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
|
|
|
|
import json
|
|
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.models import excerpt as models_excerpt
|
|
from app.storage import Store
|
|
from app.web import SETTINGS, TABS
|
|
from app.web.api import _score_after_change
|
|
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"))
|
|
uid = {r.source_id: r.uid for r in store.roles()}
|
|
|
|
res = client.get("/api/roles", params={"q": "engineer", "place": "Berlin"})
|
|
assert res.status_code == 200
|
|
assert {r["uid"] for r in res.json()["items"]} == {uid["1"], uid["3"]}
|
|
|
|
res = client.get("/api/roles", params={"function": "sales"})
|
|
assert [r["uid"] for r in res.json()["items"]] == [uid["2"]]
|
|
|
|
res = client.get("/api/roles", params={"company": "other"})
|
|
assert [r["uid"] for r in res.json()["items"]] == [uid["3"]]
|
|
|
|
|
|
def test_list_roles_filters_by_company_keys_places_and_seniority(
|
|
client: TestClient, store: Store) -> None:
|
|
for sid, company, place, title in (
|
|
("kbh", "Acme", "København K", "Senior Engineer"),
|
|
("malmo", "Acme", "Malmö", "Engineer"),
|
|
("billund", "Other", "Billund, Denmark", "Junior Engineer"),
|
|
("berlin", "Other", "Berlin", "Senior Engineer")):
|
|
item = posting(sid, company=company, place=place)
|
|
item.title = title
|
|
store.upsert_role(item)
|
|
uid = {r.source_id: r.uid for r in store.roles()}
|
|
|
|
def shown(**params: Any) -> set[str]:
|
|
res = client.get("/api/roles", params=params)
|
|
assert res.status_code == 200, res.text
|
|
assert res.json()["total"] == len(res.json()["items"])
|
|
return {sid for sid, u in uid.items() if u in {r["uid"] for r in res.json()["items"]}}
|
|
|
|
assert shown(company_keys=["acme"]) == {"kbh", "malmo"}
|
|
assert shown(company_keys=["acme", "other"]) == set(uid)
|
|
assert shown(places=["country:DK"]) == {"kbh", "billund"}
|
|
assert shown(places=["country:DK", "region:skane"]) == {"kbh", "billund", "malmo"}
|
|
assert shown(places=["place:kobenhavn"]) == {"kbh"}
|
|
assert shown(places=["Lund"]) == set() # not Billund
|
|
assert shown(seniority=["senior"]) == {"kbh", "berlin", "malmo"}
|
|
assert shown(seniority=["senior"], seniority_unknown="false") == {"kbh", "berlin"}
|
|
assert shown(seniority=["senior", "junior"], seniority_unknown="false",
|
|
company_keys=["other"]) == {"billund", "berlin"}
|
|
|
|
res = client.get("/api/roles", params={"places": ["region:atlantis"]})
|
|
assert res.status_code == 422 and "region:atlantis" in res.json()["detail"]
|
|
res = client.get("/api/roles", params={"seniority": ["boss"]})
|
|
assert res.status_code == 422 and "boss" in res.json()["detail"]
|
|
|
|
|
|
def test_role_facets_count_each_facet_under_the_other_filters(
|
|
client: TestClient, store: Store) -> None:
|
|
for sid, title, raw in (("a", "Senior Engineer", {"salary_min": 70000}),
|
|
("b", "Senior Engineer", {}),
|
|
("c", "Engineer", {"salary_text": "EUR 50 per hour"})):
|
|
item = posting(sid, **raw)
|
|
item.title = title
|
|
store.upsert_role(item)
|
|
|
|
res = client.get("/api/roles/facets",
|
|
params={"seniority": ["senior"], "seniority_unknown": "false",
|
|
"pay": "stated"})
|
|
assert res.status_code == 200
|
|
body = res.json()
|
|
levels = {f["value"]: f["count"] for f in body["seniority"]}
|
|
assert (levels["senior"], levels[""], levels["junior"]) == (1, 1, 0) # a and c pay
|
|
assert body["pay"] == [{"value": "normalised", "count": 1}, # a and b are senior
|
|
{"value": "stated", "count": 0}, {"value": "", "count": 1}]
|
|
assert client.get("/api/roles/facets", params={"places": ["country:ZZ"]}).status_code == 422
|
|
|
|
|
|
def test_suggest_companies_folds_the_name_and_leaves_out_the_hidden(
|
|
client: TestClient, store: Store) -> None:
|
|
for sid, company in (("1", "Ørsted"), ("2", "Ørsted"), ("3", "Data Minds"),
|
|
("4", "Big Data Corp"), ("5", "Big Data Corp"), ("6", "Big Data Corp"),
|
|
("7", "Metadata Ltd"), ("8", "Hidden Data")):
|
|
store.upsert_role(posting(sid, company=company))
|
|
agent = store.save_agent("A")
|
|
store.set_affinity(agent.id, "hidden-data", -1)
|
|
store.set_affinity(agent.id, "ørsted", 2)
|
|
|
|
def suggest(q: str, **params: Any) -> list[dict[str, Any]]:
|
|
res = client.get("/api/suggest/companies", params={"q": q, **params})
|
|
assert res.status_code == 200
|
|
return res.json()
|
|
|
|
assert suggest("orsted") == [{"key": "ørsted", "name": "Ørsted", "count": 2,
|
|
"affinity": 2}]
|
|
# A word prefix, not a substring: Metadata is not found; by open roles.
|
|
assert [c["name"] for c in suggest("data")] == ["Big Data Corp", "Data Minds"]
|
|
assert [c["name"] for c in suggest("big da")] == ["Big Data Corp"]
|
|
assert [c["name"] for c in suggest("data", limit=1)] == ["Big Data Corp"]
|
|
assert suggest("") == []
|
|
# Another agent hid nothing.
|
|
other = store.save_agent("B")
|
|
assert "Hidden Data" in [c["name"] for c in suggest("data", agent=other.id)]
|
|
|
|
|
|
def test_suggest_places_and_read_labels_back(client: TestClient, store: Store) -> None:
|
|
for sid, place in (("1", "Malmö"), ("2", "Malmö"), ("3", "Lund"),
|
|
("4", "København K"), ("5", "Kolding")):
|
|
store.upsert_role(posting(sid, place=place))
|
|
|
|
res = client.get("/api/suggest/places", params={"q": "ska"})
|
|
assert res.status_code == 200
|
|
assert res.json()[0] == {"token": "region:skane", "label": "Skåne", "kind": "region",
|
|
"count": 3}
|
|
tokens = [s["token"] for s in client.get("/api/suggest/places",
|
|
params={"q": "kob"}).json()]
|
|
assert "place:kobenhavn" in tokens
|
|
assert client.get("/api/suggest/places", params={"q": ""}).json() == []
|
|
|
|
res = client.get("/api/places/labels",
|
|
params={"tokens": ["country:DK", "region:skane", "place:malmo",
|
|
"region:atlantis"]})
|
|
assert res.json() == [{"token": "country:DK", "label": "Denmark"},
|
|
{"token": "region:skane", "label": "Skåne"},
|
|
{"token": "place:malmo", "label": "Malmö"}]
|
|
|
|
|
|
def test_list_roles_exposes_and_filters_by_pay(client: TestClient, store: Store) -> None:
|
|
"""`salary_text` and `salary_state` (app/models.py) are on every role the
|
|
API serves, and `pay` (`Store.PAY`) filters the feed by it, in SQL before
|
|
`limit` like the other filters."""
|
|
store.upsert_role(posting("normalised", salary_min=70000, salary_max=90000,
|
|
currency="EUR", salary_text="EUR 70,000 to 90,000 per year"))
|
|
store.upsert_role(posting("stated", salary_text="USD 45 to 60 per hour"))
|
|
store.upsert_role(posting("none"))
|
|
uid = {r.source_id: r.uid for r in store.roles()}
|
|
|
|
items = {r["uid"]: r for r in client.get("/api/roles").json()["items"]}
|
|
assert items[uid["normalised"]]["salary_text"] == "EUR 70,000 to 90,000 per year"
|
|
assert items[uid["normalised"]]["salary_state"] == "normalised"
|
|
assert items[uid["stated"]]["salary_state"] == "stated"
|
|
assert items[uid["none"]]["salary_state"] == ""
|
|
|
|
res = client.get("/api/roles", params={"pay": "stated"})
|
|
assert {r["uid"] for r in res.json()["items"]} == {uid["normalised"], uid["stated"]}
|
|
res = client.get("/api/roles", params={"pay": "normalised"})
|
|
assert [r["uid"] for r in res.json()["items"]] == [uid["normalised"]]
|
|
res = client.get("/api/roles", params={"pay": "stated_only"})
|
|
assert [r["uid"] for r in res.json()["items"]] == [uid["stated"]]
|
|
assert client.get("/api/roles", params={"pay": "bogus"}).status_code == 422
|
|
|
|
|
|
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")
|
|
uid = {r.source_id: r.uid for r in store.roles()}
|
|
|
|
def shown(**params: Any) -> set[str]:
|
|
return {r["uid"] for r in client.get("/api/roles", params=params).json()["items"]}
|
|
|
|
assert shown() == {uid["1"], uid["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() == {uid["2"]}
|
|
assert shown(show_muted="true") == {uid["1"], uid["2"]}
|
|
assert shown(agent=agent.id, order="newest") == {uid["2"]}
|
|
# Hidden is per agent: the other agent still sees Acme.
|
|
assert shown(agent=other.id, order="newest") == {uid["1"], uid["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()
|
|
uid = {r.source_id: r.uid for r in store.roles()}
|
|
|
|
res = client.get("/api/roles", params={"limit": 1})
|
|
assert [r["uid"] for r in res.json()["items"]] == [uid["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()
|
|
uid = {r.source_id: r.uid for r in store.roles()}
|
|
|
|
first = client.get("/api/roles", params={"limit": 2}).json()
|
|
assert [r["uid"] for r in first["items"]] == [uid["4"], uid["3"]]
|
|
assert first["total"] == 5
|
|
rest = client.get("/api/roles", params={"limit": 2, "offset": 4}).json()
|
|
assert [r["uid"] for r in rest["items"]] == [uid["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["uid"] for r in page["items"]}
|
|
|
|
assert judged("liked") == {uid["liked"]}
|
|
assert judged("passed") == {uid["passed"]}
|
|
assert judged("unjudged") == {uid["open"]}
|
|
assert judged("") == {uid["liked"], uid["passed"], uid["fine"], uid["open"]} # a pass is not hidden
|
|
assert judged("liked", agent=agent.id, order="newest") == {uid["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") == {uid["open"], uid["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["uid"] for r in page["items"]] == [uid["3"], uid["2"], uid["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["uid"] for r in scored["items"]] == [uid["1"], uid["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()}
|
|
label = {v: k for k, v in uid.items()}
|
|
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 [(label[r["uid"]], 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"))
|
|
ids = {r.source_id: r.uid for r in store.roles()}
|
|
uid = posting("1").uid
|
|
client.post(f"/api/roles/{uid}/read")
|
|
|
|
all_ = client.get("/api/roles").json()
|
|
assert {r["uid"] for r in all_["items"]} == {ids["1"], ids["2"]}
|
|
|
|
unread = client.get("/api/roles", params={"include_read": "false"}).json()
|
|
assert {r["uid"] for r in unread["items"]} == {ids["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["uid"] for r in res.json()["items"]} == {uid["1"], uid["2"]}
|
|
|
|
both = client.get("/api/roles", params={"tags": ["python", "backend"],
|
|
"tag_mode": "all"})
|
|
assert {r["uid"] for r in both.json()["items"]} == {uid["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", ats="greenhouse", country="DK")
|
|
store.upsert_role(posting("1", company="Acme"), sub)
|
|
store.add_company("Other", key="other", domain="other.example") # no board found
|
|
store.add_company("Novo", key="novo", ats="taleo", # followed, not readable
|
|
careers_url="https://novo.example/careers")
|
|
|
|
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", "notes": "", "board": "greenhouse",
|
|
"subscription_id": sub.id, "on": True, "affinity": 0, "open_roles": 1,
|
|
"ats": "greenhouse", "ats_slug": "", "careers_url": "",
|
|
"country": "DK", "city": "", "domain": "", "watch": None,
|
|
"careers_opened_at": "", "failing_since": "", "failing_reason": "",
|
|
"from_search": False, "hand_reason": "", "by_hand": False}
|
|
[other] = [c for c in res.json() if c["key"] == "other"]
|
|
assert (other["board"], other["subscription_id"], other["on"], other["ats"]) == (
|
|
"", None, None, "")
|
|
[novo] = [c for c in res.json() if c["key"] == "novo"]
|
|
assert (novo["board"], novo["subscription_id"], novo["ats"], novo["careers_url"]) == (
|
|
"", None, "taleo", "https://novo.example/careers")
|
|
assert not any(c["from_search"] for c in res.json())
|
|
# Companies with no board shown, unread or not, are the list checked by hand.
|
|
assert {c["key"] for c in res.json() if c["by_hand"]} == {"other", "novo"}
|
|
|
|
|
|
def test_judging_a_company_known_only_from_a_search_creates_its_row(
|
|
client: TestClient, store: Store) -> None:
|
|
"""Jobindex and Adzuna employers have roles and no company row. The first
|
|
affinity change makes a minimal one, named by its roles, which the
|
|
Companies list marks `from_search`, unlike a followed company without a
|
|
board, and a company whose board was detached."""
|
|
store.save_agent("A")
|
|
search = store.add_subscription("search", "jobindex:cph", label="cph", board="jobindex")
|
|
for i, name in enumerate(("Novo Nordisk A/S", "Novo Nordisk A/S", "NOVO NORDISK A/S")):
|
|
item = posting(f"n{i}", company=name)
|
|
item.source = "search"
|
|
store.upsert_role(item, search)
|
|
key = "novo-nordisk"
|
|
assert store.company(key) is None
|
|
|
|
res = client.post(f"/api/companies/{key}/affinity", json={"affinity": -1})
|
|
assert res.status_code == 200, res.text
|
|
assert (res.json()["name"], res.json()["affinity"]) == ("Novo Nordisk A/S", -1)
|
|
assert client.post("/api/companies/nobody/affinity", json={"affinity": 1}).status_code == 404
|
|
|
|
store.add_company("Pleo", key="pleo", domain="pleo.io") # followed, no board found
|
|
sub = store.add_subscription("ats", "lever:gone", company="Gone", board="lever")
|
|
store.add_company("Gone", key="gone")
|
|
store.set_company_board("gone", "") # detached
|
|
assert not store.subscription(sub.id).show # type: ignore[union-attr]
|
|
rows = {c["key"]: c for c in client.get("/api/companies").json()}
|
|
assert rows["gone"]["board"] == ""
|
|
assert rows[key]["from_search"] and rows[key]["open_roles"] == 3
|
|
assert (rows[key]["board"], rows[key]["ats"], rows[key]["careers_url"]) == ("", "", "")
|
|
assert not rows["pleo"]["from_search"] and not rows["gone"]["from_search"]
|
|
|
|
|
|
def test_companies_say_since_when_a_board_fails(client: TestClient, store: Store,
|
|
capsys: pytest.CaptureFixture[str],
|
|
monkeypatch: pytest.MonkeyPatch) -> None:
|
|
"""What the pull recorded, on `/api/companies` and beside `app companies
|
|
check`'s live result."""
|
|
import plugins.ats as ats
|
|
from app.cli import check_companies
|
|
|
|
store.add_subscription("ats", "greenhouse:acme", company="Acme", board="greenhouse")
|
|
store.add_company("Acme", key="acme", ats="greenhouse")
|
|
store.state["ats:failing:greenhouse:acme"] = json.dumps(
|
|
{"since": "2026-09-01T06:00:00+00:00", "reason": "404 Not Found"})
|
|
|
|
[row] = client.get("/api/companies").json()
|
|
assert (row["failing_since"], row["failing_reason"]) == (
|
|
"2026-09-01T06:00:00+00:00", "404 Not Found")
|
|
|
|
monkeypatch.setattr(ats, "fetch_board", lambda target: ([{"source_id": "1"}], True))
|
|
assert check_companies(store) == 0
|
|
assert capsys.readouterr().out.splitlines() == [
|
|
"greenhouse:acme (greenhouse:acme): now 1 roles; "
|
|
"pull: failing since 2026-09-01: 404 Not Found",
|
|
"checked 1 boards, 0 dead, 1 failing in the pull",
|
|
]
|
|
|
|
|
|
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.get(res.json()["score_job"]).kind == "score"
|
|
|
|
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
|
|
|
|
|
|
def test_a_score_asked_for_during_a_run_is_one_follow_up_after_it(queue: Any) -> None:
|
|
"""No worker runs here (no client), so the test moves the jobs itself."""
|
|
first = _score_after_change(queue)
|
|
assert first is not None and first.dedup_key == "score"
|
|
assert _score_after_change(queue).id == first.id # queued: it will read the change
|
|
|
|
assert queue.jobs.claim({"cpu": 1}).id == first.id
|
|
during = {_score_after_change(queue).id for _ in range(10)}
|
|
assert len(during) == 1 and first.id not in during
|
|
follow = queue.jobs.get(during.pop())
|
|
assert (follow.depends_on, follow.after, follow.dedup_key) == (
|
|
[first.id], "done", f"score:after:{first.id}")
|
|
|
|
queue.jobs.finish(first.id, "done")
|
|
assert _score_after_change(queue).id == follow.id # queued, not started: still the one
|
|
|
|
assert queue.jobs.claim({"cpu": 1}).id == follow.id
|
|
after = _score_after_change(queue)
|
|
assert after.id not in (first.id, follow.id) and after.dedup_key == "score"
|
|
|
|
|
|
def test_post_score_answers_with_the_job_to_wait_for(client: TestClient, queue: Any) -> None:
|
|
res = client.post("/api/score")
|
|
assert res.status_code == 200
|
|
assert queue.jobs.get(res.json()["id"]).kind == "score"
|
|
|
|
|
|
# ------------------------------------------------------------- watchlist
|
|
|
|
def test_the_watchlist_rating_is_global_and_leaves_affinity_alone(
|
|
client: TestClient, store: Store) -> None:
|
|
store.add_company("Acme", ats="workday")
|
|
agent = store.save_agent("A")
|
|
store.set_affinity(agent.id, "acme", 1)
|
|
|
|
for watch, stored in ((True, 1), (False, 0), (None, None)):
|
|
res = client.post("/api/companies/acme/watch", json={"watch": watch})
|
|
assert res.status_code == 200 and res.json()["watch"] == stored
|
|
listed = client.get("/api/companies").json()[0]
|
|
assert (listed["watch"], listed["affinity"]) == (stored, 1)
|
|
|
|
|
|
def test_the_careers_page_is_set_cleared_and_must_be_a_web_address(
|
|
client: TestClient, store: Store) -> None:
|
|
store.add_company("Acme")
|
|
|
|
res = client.post("/api/companies/acme/careers", json={"careers_url": "https://a.example/jobs"})
|
|
assert res.status_code == 200 and res.json()["careers_url"] == "https://a.example/jobs"
|
|
for bad in ("javascript:alert(1)", "a.example/jobs", "ftp://a.example"):
|
|
assert client.post("/api/companies/acme/careers",
|
|
json={"careers_url": bad}).status_code == 422
|
|
assert store.company("acme").careers_url == "https://a.example/jobs"
|
|
cleared = client.post("/api/companies/acme/careers", json={"careers_url": ""})
|
|
assert cleared.status_code == 200 and store.company("acme").careers_url == ""
|
|
|
|
|
|
def test_opening_the_careers_page_stamps_the_time(client: TestClient, store: Store) -> None:
|
|
store.add_company("Acme")
|
|
assert client.get("/api/companies").json()[0]["careers_opened_at"] == ""
|
|
|
|
res = client.post("/api/companies/acme/opened")
|
|
|
|
assert res.status_code == 200
|
|
assert res.json()["careers_opened_at"] == store.company("acme").careers_opened_at != ""
|
|
assert client.get("/api/companies").json()[0]["careers_opened_at"] != ""
|
|
|
|
|
|
def test_the_watchlist_endpoints_404_for_an_unknown_company(client: TestClient) -> None:
|
|
assert client.post("/api/companies/nope/watch", json={"watch": True}).status_code == 404
|
|
assert client.post("/api/companies/nope/careers",
|
|
json={"careers_url": "https://a.example"}).status_code == 404
|
|
assert client.post("/api/companies/nope/opened").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", "Watchlist"]
|
|
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_every_section_of_the_sources_page_exists(client: TestClient) -> None:
|
|
"""Each entry of `SECTIONS["/settings/sources"]` is a `<section
|
|
data-section>` on the page, which is what sources.js shows one of, and
|
|
the log at the end of Runs still answers `#messages`."""
|
|
anchors = [anchor for anchor, _ in SECTIONS["/settings/sources"]]
|
|
assert anchors == ["job-boards", "company-boards", "runs"]
|
|
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
|
|
runs = page[page.index('id="runs"'):]
|
|
assert 'id="messages"' in runs
|
|
|
|
|
|
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", ["/agents", "/settings/ranking", "/companies", "/sources",
|
|
"/jobs", "/schedules", "/messages", "/settings/searches",
|
|
"/settings/companies", "/settings/pulls",
|
|
"/settings/messages", "/account"])
|
|
def test_an_old_tab_path_is_gone(client: TestClient, old: str) -> None:
|
|
"""The paths the settings cog moved off of answer 404 now, not a cached
|
|
301: a browser caches a 301 indefinitely, so a path reused later would be
|
|
unreachable were the redirect still there."""
|
|
assert client.get(old).status_code == 404
|
|
|
|
|
|
def test_each_page_loads_only_its_own_script(client: TestClient) -> None:
|
|
"""Sources is four scripts on one page, one per section and one for the
|
|
log in Runs, 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-prompts"' 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
|
|
# What the model reads after a pull: a count and a score floor per stage.
|
|
assert "<legend>What the model reads</legend>" in template
|
|
for limit in ("a-extract-top", "a-extract-min", "a-summarise-top", "a-summarise-min"):
|
|
assert f'id="{limit}"' in template
|
|
assert 'id="a-extract-top" min="0" step="10" value="500"' in page
|
|
assert 'id="a-summarise-min" min="0" max="1" step="0.05" value="0.65"' 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 and the runs, 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"):
|
|
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"')
|
|
assert 'href="/settings/sources#messages"' not in page
|
|
# 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.
|
|
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
|
|
# The host select is plugins.ats's HOSTS, not a list typed into the page.
|
|
from plugins.ats import HOSTS
|
|
for host, label in HOSTS.items():
|
|
assert f'<option value="{host}">{label}</option>' in page
|
|
boards = page[page.index('id="company-boards"'):page.index('id="runs"')].lower()
|
|
assert "hidden" in boards and "mute" not in boards
|
|
# Runs: the pull button, the queue's elements, and the log as its last
|
|
# block. No button per stage, and no cadence in the lede.
|
|
runs = page[page.index('id="runs"'):page.index("</section>", page.index('id="runs"'))]
|
|
assert '<button type="button" id="pull-now" class="secondary">Run the pull now</button>' in runs
|
|
assert 'id="kinds"' not in page and "Run one stage" not in page
|
|
assert "On the schedule below the pull reads every board" in runs
|
|
assert "Every 6 hours" not in runs
|
|
assert '<desk-jobs idle="show">' in runs and 'id="idle"' not in runs
|
|
assert runs.index('<desk-schedules add="off">') < runs.index('<div id="messages">') \
|
|
< runs.index("<h3>Problems</h3>") < runs.index('id="clear-all"') < runs.index('id="notices"')
|
|
|
|
|
|
def test_the_modules_bring_their_own_elements(client: TestClient) -> None:
|
|
pulls = client.get("/settings/sources").text
|
|
assert pulls.index('<desk-jobs idle="show">') < pulls.index('<desk-schedules add="off">')
|
|
# The queue's chip is in the header of every page, and its script loads once.
|
|
for path in ("/", "/settings/sources"):
|
|
page = client.get(path).text
|
|
assert '<desk-queue href="/settings/sources#runs">' in page
|
|
assert page.count('src="/_jobs/jobs.js"') == 1
|
|
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_save_and_score_queues_a_score(client: TestClient, queue: Any) -> None:
|
|
"""The Search agents page's "Save and score" posts `{kind: "score"}`: a
|
|
task on its own, now that no stage has a button of its own."""
|
|
res = client.post("/api/jobs", json={"kind": "score", "payload": {}})
|
|
assert res.status_code == 202
|
|
assert queue.jobs.last("score", states=("queued", "running", "done")) is not None
|
|
|
|
|
|
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_agents_take_and_serve_prompts_and_still_take_an_intent(client: TestClient) -> None:
|
|
prompts = [{"name": "AI and data leadership", "text": "VP, Head or Director of AI"},
|
|
{"name": "Forward deployed", "text": ""}]
|
|
created = client.post("/api/agents", json={"name": "Wolf", "prompts": prompts})
|
|
assert created.status_code == 200
|
|
assert created.json()["prompts"] == prompts and "intent" not in created.json()
|
|
listed = client.get("/api/agents").json()
|
|
assert listed[0]["prompts"] == prompts and "intent" not in listed[0]
|
|
|
|
# Reordered in place; then the body from before prompts, which becomes one.
|
|
agent_id = created.json()["id"]
|
|
swapped = client.post("/api/agents", json={"id": agent_id, "name": "Wolf",
|
|
"prompts": prompts[::-1]})
|
|
assert swapped.json()["prompts"] == prompts[::-1]
|
|
legacy = client.post("/api/agents", json={"id": agent_id, "name": "Wolf",
|
|
"intent": "python backend"})
|
|
assert legacy.json()["prompts"] == [{"name": "Wolf", "text": "python backend"}]
|
|
# `prompts` wins over `intent` when both are sent.
|
|
both = client.post("/api/agents", json={"id": agent_id, "name": "Wolf", "prompts": [],
|
|
"intent": "python backend"})
|
|
assert both.json()["prompts"] == []
|
|
|
|
|
|
@pytest.mark.parametrize("prompts, says", [
|
|
([{"name": "AI", "text": "a"}, {"name": "ai", "text": "b"}], "two prompts are named 'ai'"),
|
|
([{"name": " ", "text": "a"}], "a prompt needs a name"),
|
|
])
|
|
def test_a_duplicate_or_empty_prompt_name_is_a_422_sentence(
|
|
client: TestClient, prompts: list[dict[str, str]], says: str) -> None:
|
|
res = client.post("/api/agents", json={"name": "Bad", "prompts": prompts})
|
|
assert res.status_code == 422 and says in res.json()["detail"]
|
|
assert client.get("/api/agents").json() == []
|
|
|
|
|
|
def test_the_role_endpoint_serves_the_agents_score_and_its_prompt(
|
|
client: TestClient, store: Store) -> None:
|
|
store.upsert_role(posting("1"))
|
|
uid = posting("1").uid
|
|
agent = store.save_agent("A", prompts=[])
|
|
store.replace_scores(agent.id, [(uid, 0.8, "", "Forward deployed")])
|
|
|
|
role = client.get(f"/api/roles/{uid}", params={"agent": agent.id}).json()
|
|
assert (role["score"], role["gated_reason"], role["prompt"]) == \
|
|
(0.8, "", "Forward deployed")
|
|
row = client.get("/api/roles", params={"agent": agent.id}).json()["items"][0]
|
|
assert row["prompt"] == "Forward deployed"
|
|
voted = client.post(f"/api/roles/{uid}/vote", json={"value": 1, "agent": agent.id})
|
|
assert voted.json()["score"] == pytest.approx(0.96)
|
|
other = store.save_agent("B")
|
|
unscored = client.get(f"/api/roles/{uid}", params={"agent": other.id}).json()
|
|
assert (unscored["score"], unscored["prompt"]) == (None, "")
|
|
|
|
|
|
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_an_agents_limits_are_saved_listed_and_checked(client: TestClient) -> None:
|
|
"""How deep the model stages read is the agent's own; a score outside
|
|
0..1 is refused."""
|
|
default = client.post("/api/agents", json={"name": "Wide"}).json()
|
|
assert default["limits"] == {"extract_top": 500, "extract_min": 0.55,
|
|
"summarise_top": 30, "summarise_min": 0.65}
|
|
narrow = {"extract_top": 50, "extract_min": 0.6, "summarise_top": 5, "summarise_min": 0.7}
|
|
saved = client.post("/api/agents", json={"name": "Narrow", "limits": narrow})
|
|
assert saved.status_code == 200 and saved.json()["limits"] == narrow
|
|
assert [a["limits"]["extract_top"] for a in client.get("/api/agents").json()] == [50, 500]
|
|
bad = client.post("/api/agents", json={"name": "Bad", "limits": {"extract_min": 2}})
|
|
assert bad.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["uid"] for r in shown] == [uids["high"], uids["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
|
|
|
|
|
|
def test_list_roles_serves_the_row_projection_and_not_the_big_columns(
|
|
client: TestClient, store: Store) -> None:
|
|
"""`GET /api/roles` is `RoleRow`: the fields a feed row draws, an
|
|
`excerpt` built on the server, and never `description`, `feed_text`,
|
|
`raw` or `article_text` — those are what the reader's own fetch,
|
|
`GET /api/roles/{uid}`, still answers in full."""
|
|
long_description = "Build things. " * 50
|
|
item = posting("1", company="Acme", place="Berlin, Germany", remote="hybrid",
|
|
function="engineering")
|
|
item.body = long_description
|
|
store.upsert_role(item)
|
|
uid = item.uid
|
|
|
|
row = client.get("/api/roles").json()["items"][0]
|
|
assert set(row) == {
|
|
"uid", "company_key", "company", "title", "place", "country", "remote", "function",
|
|
"seniority", "salary_min", "salary_max", "currency", "salary_text", "salary_state",
|
|
"original_url", "posted_at", "score", "gated_reason", "prompt", "vote",
|
|
"voted_at", "read_at", "tags", "excerpt",
|
|
}
|
|
assert row["uid"] == uid
|
|
assert row["company"] == "Acme" and row["place"] == "Berlin, Germany"
|
|
assert row["remote"] == "hybrid" and row["function"] == "engineering"
|
|
assert row["seniority"] == "senior" # "Senior Backend Engineer"
|
|
assert len(row["excerpt"]) <= 200
|
|
assert row["excerpt"] == models_excerpt(long_description)
|
|
|
|
full = client.get(f"/api/roles/{uid}").json()
|
|
assert full["description"] == long_description
|
|
|
|
|
|
def test_list_roles_page_stays_small_even_with_long_postings(
|
|
client: TestClient, store: Store) -> None:
|
|
"""The size bound the projection exists for: a page of rows with a real
|
|
posting's worth of text each (description, feed_text, raw and
|
|
article_text set, as a board's upsert would leave them) stays small,
|
|
because none of the four ever reach the response. Measured against a
|
|
copy of the live database in the report; this is the regression guard."""
|
|
big = "Requirement. " * 500 # ~6.5 KB, decisions.md's own average
|
|
for n in range(60):
|
|
item = posting(str(n), company="Acme")
|
|
item.body = big
|
|
store.upsert_role(item)
|
|
with store.transaction() as conn:
|
|
conn.execute("UPDATE roles SET feed_text = ?, raw = ?, article_text = ?",
|
|
(big, json.dumps({"blob": big}), big))
|
|
|
|
page = client.get("/api/roles", params={"limit": 60})
|
|
assert len(page.json()["items"]) == 60
|
|
# Well under one tenth of what 60 full roles (about 5.5 KB each,
|
|
# decisions.md's own figure) would answer with.
|
|
assert len(page.content) < 100_000
|
|
|
|
|
|
def test_list_roles_excerpt_prefers_the_summary(client: TestClient, store: Store) -> None:
|
|
store.upsert_role(posting("1"))
|
|
uid = posting("1").uid
|
|
store.set_summary(uid, "A short, hand-written summary.")
|
|
row = client.get("/api/roles").json()["items"][0]
|
|
assert row["excerpt"] == "A short, hand-written summary."
|
|
|
|
|
|
# ----------------------------------------------------------------- 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}
|
|
|
|
|
|
#: 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)
|
|
|
|
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() == []
|
|
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, queue: Any, 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") == []
|
|
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"]
|
|
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, queue: Any, monkeypatch: pytest.MonkeyPatch) -> None:
|
|
import plugins.ats as ats
|
|
|
|
monkeypatch.setattr(ats, "fetch_board", lambda target: ([{"source_id": "1"}], True))
|
|
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
|
|
# 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.
|
|
again = client.post("/api/boards", json={"board": "ashby:acme", "name": "Acme"})
|
|
assert again.status_code == 201
|
|
assert again.json()["source"]["id"] == res.json()["source"]["id"]
|
|
bad = client.post("/api/boards", json={"board": "acme", "name": "Other"})
|
|
assert bad.status_code == 422 and "host:slug" in bad.json()["detail"]
|
|
|
|
def down(target: str) -> list[dict[str, str]]:
|
|
raise OSError("connection refused")
|
|
|
|
monkeypatch.setattr(ats, "fetch_board", down)
|
|
dead = client.post("/api/boards", json={"board": "ashby:other", "name": "Other"})
|
|
assert dead.status_code == 422 and "did not answer" in dead.json()["detail"]
|
|
assert store.company("other") is None
|
|
|
|
|
|
def test_a_companys_board_is_set_replaced_and_detached(
|
|
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
|
|
store.add_company("Acme", key="acme", ats="teamtailor", ats_slug="acme")
|
|
|
|
res = client.post("/api/companies/acme/board", json={"board": "greenhouse:acme"})
|
|
assert res.status_code == 200
|
|
body = res.json()
|
|
assert (body["found"], body["closed"], body["source"]["target"]) == (0, 0, "greenhouse:acme")
|
|
sub_id = body["source"]["id"]
|
|
store.upsert_role(posting("1", company="Acme"), store.subscription(sub_id))
|
|
[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
|
|
assert client.delete("/api/companies/nope/board").status_code == 404
|
|
bad = client.post("/api/companies/acme/board", json={"board": "taleo:acme"})
|
|
assert bad.status_code == 422 and "host:slug" in bad.json()["detail"]
|
|
|
|
|
|
def test_a_company_is_added_by_hand_with_a_reason_and_a_link(
|
|
client: TestClient, store: Store) -> None:
|
|
res = client.post("/api/companies", json={
|
|
"name": "Revolut Ltd", "careers_url": " https://revolut.example/careers ",
|
|
"notes": "wants a login"})
|
|
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,
|
|
"ats": "", "ats_slug": "", "careers_url": "https://revolut.example/careers",
|
|
"country": "", "city": "", "watch": None, "careers_opened_at": "",
|
|
"failing_since": "", "failing_reason": "", "from_search": False,
|
|
"hand_reason": "added", "by_hand": True}
|
|
assert store.company("revolut").hand_reason == "added" # type: ignore[union-attr]
|
|
|
|
other = client.post("/api/companies", json={"name": "Meta", "hand_reason": "blocked"})
|
|
assert other.status_code == 201 and other.json()["hand_reason"] == "blocked"
|
|
# The key comes from the name, so a legal suffix or case is the same company.
|
|
assert client.post("/api/companies", json={"name": "REVOLUT"}).status_code == 409
|
|
assert store.company("revolut").notes == "wants a login" # type: ignore[union-attr]
|
|
|
|
|
|
def test_adding_a_company_by_hand_refuses_a_bad_link_reason_or_name(
|
|
client: TestClient, store: Store) -> None:
|
|
for link in ("javascript:alert(1)", "ftp://x.example/jobs", "revolut.example/careers",
|
|
"https://"):
|
|
res = client.post("/api/companies", json={"name": "Revolut", "careers_url": link})
|
|
assert res.status_code == 422, link
|
|
assert client.post("/api/companies", json={"name": "Revolut", "careers_url": ""}
|
|
).status_code == 201
|
|
assert client.post("/api/companies", json={"name": "Meta", "hand_reason": "lazy"}
|
|
).status_code == 422
|
|
assert client.post("/api/companies", json={"name": " "}).status_code == 422
|
|
assert client.post("/api/companies", json={}).status_code == 422
|
|
assert [c.key for c in store.companies()] == ["revolut"]
|
|
|
|
|
|
def test_a_company_from_a_search_only_is_no_longer_one_once_it_has_a_reason(
|
|
client: TestClient, store: Store) -> None:
|
|
store.save_agent("A")
|
|
search = store.add_subscription("search", "jobindex:cph", label="cph", board="jobindex")
|
|
item = posting("1", company="Novo")
|
|
item.source = "search"
|
|
store.upsert_role(item, search)
|
|
client.post("/api/companies/novo/affinity", json={"affinity": 1})
|
|
[row] = client.get("/api/companies").json()
|
|
assert (row["from_search"], row["by_hand"]) == (True, False)
|
|
|
|
res = client.patch("/api/companies/novo", json={"hand_reason": "unread"})
|
|
assert res.status_code == 200
|
|
assert (res.json()["from_search"], res.json()["by_hand"]) == (False, True)
|
|
|
|
|
|
def test_a_company_is_edited_and_only_the_fields_sent_change(
|
|
client: TestClient, store: Store) -> None:
|
|
store.add_company("Acme", key="acme", careers_url="https://acme.example/jobs",
|
|
notes="old", hand_reason="unread")
|
|
res = client.patch("/api/companies/acme", json={"notes": "new", "name": "Acme Labs"})
|
|
assert res.status_code == 200, res.text
|
|
body = res.json()
|
|
assert (body["key"], body["name"], body["notes"], body["careers_url"],
|
|
body["hand_reason"]) == ("acme", "Acme Labs", "new", "https://acme.example/jobs",
|
|
"unread")
|
|
# A link and a reason can be cleared, since "" is a value.
|
|
res = client.patch("/api/companies/acme", json={"careers_url": "", "hand_reason": ""})
|
|
assert (res.json()["careers_url"], res.json()["hand_reason"]) == ("", "")
|
|
assert client.patch("/api/companies/acme", json={}).status_code == 200
|
|
|
|
assert client.patch("/api/companies/acme", json={"careers_url": "javascript:x"}
|
|
).status_code == 422
|
|
assert client.patch("/api/companies/acme", json={"hand_reason": "lazy"}).status_code == 422
|
|
assert client.patch("/api/companies/acme", json={"name": " "}).status_code == 422
|
|
assert client.patch("/api/companies/acme", json={"notes": None}).status_code == 422
|
|
assert client.patch("/api/companies/nope", json={"notes": "x"}).status_code == 404
|
|
assert store.company("acme").name == "Acme Labs" # type: ignore[union-attr]
|
|
|
|
|
|
def test_a_company_with_a_board_keeps_its_name_and_takes_the_other_edits(
|
|
client: TestClient, store: Store, monkeypatch: pytest.MonkeyPatch) -> None:
|
|
import plugins.ats as ats
|
|
|
|
monkeypatch.setattr(ats, "fetch_board", lambda target: ([], True))
|
|
store.add_company("Acme", key="acme")
|
|
assert client.post("/api/companies/acme/board", json={"board": "greenhouse:acme"}
|
|
).status_code == 200
|
|
|
|
res = client.patch("/api/companies/acme", json={"name": "Acme Labs"})
|
|
assert res.status_code == 409 and "detach" in res.json()["detail"]
|
|
assert client.patch("/api/companies/acme", json={"name": "Acme"}).status_code == 200
|
|
assert client.patch("/api/companies/acme", json={"notes": "hi"}).status_code == 200
|
|
# Detached, the subscription is still there and the name stays fixed.
|
|
client.delete("/api/companies/acme/board")
|
|
assert client.patch("/api/companies/acme", json={"name": "Acme Labs"}).status_code == 409
|
|
assert store.company("acme").name == "Acme" # type: ignore[union-attr]
|
|
|
|
|
|
def test_the_watchlists_careers_link_and_the_by_hand_edit_share_one_url_rule(
|
|
client: TestClient, store: Store) -> None:
|
|
store.add_company("Acme", key="acme")
|
|
for bad in ("https://", "javascript:alert(1)"):
|
|
posted = client.post("/api/companies/acme/careers", json={"careers_url": bad})
|
|
patched = client.patch("/api/companies/acme", json={"careers_url": bad})
|
|
assert posted.status_code == patched.status_code == 422
|
|
assert posted.json() == patched.json()
|
|
assert store.company("acme").careers_url == "" # type: ignore[union-attr]
|
|
|
|
|
|
def test_opening_a_by_hand_company_shows_when_it_was_last_looked_at(
|
|
client: TestClient, store: Store) -> None:
|
|
clock = Clock()
|
|
store.clock = clock
|
|
store.add_company("Acme", key="acme", hand_reason="unread")
|
|
assert client.post("/api/companies/acme/opened").status_code == 200
|
|
[row] = client.get("/api/companies").json()
|
|
assert (row["by_hand"], row["careers_opened_at"]) == (True, "2026-09-23T09:00:00+00:00")
|
|
assert client.post("/api/companies/acme/checked").status_code in (404, 405)
|
|
|
|
|
|
def test_a_company_is_deleted_unless_it_is_read(
|
|
client: TestClient, store: Store, monkeypatch: pytest.MonkeyPatch) -> None:
|
|
import plugins.ats as ats
|
|
|
|
monkeypatch.setattr(ats, "fetch_board", lambda target: ([], True))
|
|
agent = store.save_agent("A")
|
|
store.add_company("Meta", key="meta", hand_reason="blocked")
|
|
store.set_affinity(agent.id, "meta", 1)
|
|
res = client.delete("/api/companies/meta")
|
|
assert res.status_code == 204 and res.content == b""
|
|
assert store.company("meta") is None
|
|
assert store.company("meta", agent.id) is None
|
|
assert client.delete("/api/companies/meta").status_code == 404
|
|
assert "meta" not in {c["key"] for c in client.get("/api/companies").json()}
|
|
|
|
# A board shown: refused until it is detached; a detached board is no board.
|
|
store.add_company("Acme", key="acme")
|
|
sub_id = client.post("/api/companies/acme/board", json={"board": "greenhouse:acme"}
|
|
).json()["source"]["id"]
|
|
res = client.delete("/api/companies/acme")
|
|
assert res.status_code == 409 and "greenhouse:acme" in res.json()["detail"]
|
|
store.upsert_role(posting("1", company="Acme"), store.subscription(sub_id))
|
|
client.delete("/api/companies/acme/board")
|
|
assert client.delete("/api/companies/acme").status_code == 204
|
|
|
|
# Open roles hold a company even where no board is on: they would be
|
|
# orphaned from the list.
|
|
store.add_company("Beta", key="beta")
|
|
store.upsert_role(posting("2", company="Beta"))
|
|
res = client.delete("/api/companies/beta")
|
|
assert res.status_code == 409 and "open roles" in res.json()["detail"]
|
|
assert store.company("beta") is not None
|
|
|
|
|
|
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(
|
|
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.
|
|
|
|
`summarise_batch` is `isolate=True` in production, which runs it in a
|
|
child process that imports `desk.tools.agent` fresh and never sees the
|
|
stub below. What is under test here is the route and the queue, not
|
|
isolation itself — that has its own tests in `pipeline/tests` — so the
|
|
job runs in-thread for this one drain.
|
|
"""
|
|
from dataclasses import replace
|
|
|
|
from desk.tools import agent
|
|
from pipeline import JOBS
|
|
|
|
monkeypatch.setitem(JOBS, "summarise_batch",
|
|
replace(JOBS["summarise_batch"], isolate=False, timeout=0.0))
|
|
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
|