The boilerplate's rule: the deploy kit's Dockerfile, the APP_CONFIG, APP_PASSWORD and APP_DATA variables and its commands all assume a package called app, so a project renames its display title and nothing else. The console script is `app`, and the launch entry runs `uv run app serve --port 8790`. The domain noun stays `role`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
252 lines
10 KiB
Python
252 lines
10 KiB
Python
"""The JSON API the page calls, against a temp database and FastAPI's
|
|
TestClient. No network — nothing here touches an ATS host or the queue's own
|
|
worker beyond starting and stopping it with the app."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from collections.abc import Iterator
|
|
from datetime import UTC, datetime, timedelta
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
from fastapi import FastAPI
|
|
from fastapi.testclient import TestClient
|
|
|
|
from desk.models import Item
|
|
from app.storage import Store
|
|
from app.web.app import create_app
|
|
|
|
|
|
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:
|
|
db = tmp_path / "roledesk.db"
|
|
config_path = tmp_path / "roles.yaml"
|
|
config_path.write_text(f"database: {db.as_posix()}\n", encoding="utf-8")
|
|
return create_app(str(config_path))
|
|
|
|
|
|
@pytest.fixture
|
|
def store(app: FastAPI) -> Store:
|
|
return app.state.store
|
|
|
|
|
|
@pytest.fixture
|
|
def client(app: FastAPI) -> Iterator[TestClient]:
|
|
with TestClient(app) as c:
|
|
yield c
|
|
|
|
|
|
# ------------------------------------------------------------------- roles
|
|
|
|
def test_list_roles_filters_by_query_place_and_function(
|
|
client: TestClient, store: Store) -> None:
|
|
store.upsert_role(posting("1", company="Acme", place="Berlin", function="engineering"))
|
|
store.upsert_role(posting("2", company="Acme", place="Munich", function="sales"))
|
|
store.upsert_role(posting("3", company="Other", place="Berlin", function="engineering"))
|
|
|
|
res = client.get("/api/roles", params={"q": "engineer", "place": "Berlin"})
|
|
assert res.status_code == 200
|
|
assert {r["source_id"] for r in res.json()} == {"1", "3"}
|
|
|
|
res = client.get("/api/roles", params={"function": "sales"})
|
|
assert [r["source_id"] for r in res.json()] == ["2"]
|
|
|
|
res = client.get("/api/roles", params={"company": "other"})
|
|
assert [r["source_id"] for r in res.json()] == ["3"]
|
|
|
|
|
|
def test_list_roles_limit_is_bounded(client: TestClient, store: Store) -> None:
|
|
for i in range(3):
|
|
store.upsert_role(posting(str(i)))
|
|
assert len(client.get("/api/roles", params={"limit": 1}).json()) == 1
|
|
assert client.get("/api/roles", params={"limit": 0}).status_code == 422
|
|
assert client.get("/api/roles", params={"limit": 1001}).status_code == 422
|
|
|
|
|
|
def test_muted_roles_are_hidden_unless_show_muted(client: TestClient, store: Store) -> None:
|
|
store.add_company("Acme", key="acme")
|
|
store.upsert_role(posting("1", company="Acme"))
|
|
store.upsert_role(posting("2", company="Other"))
|
|
|
|
assert {r["source_id"] for r in client.get("/api/roles").json()} == {"1", "2"}
|
|
|
|
assert client.post("/api/companies/acme/affinity", json={"affinity": -1}).status_code == 200
|
|
assert {r["source_id"] for r in client.get("/api/roles").json()} == {"2"}
|
|
assert {r["source_id"] for r in
|
|
client.get("/api/roles", params={"show_muted": "true"}).json()} == {"1", "2"}
|
|
|
|
|
|
def test_muted_is_excluded_before_the_limit_not_after(
|
|
client: TestClient, store: Store) -> None:
|
|
"""A regression case: filtering a muted company out of an already
|
|
`LIMIT`-ed page can leave nothing to show, if that company happens to be
|
|
the newest thing in the database — exactly what a company with hundreds
|
|
of roles, all freshly (re-)pulled at once, produces."""
|
|
clock = Clock()
|
|
store.clock = clock
|
|
store.add_company("Acme", key="acme", affinity=-1)
|
|
store.upsert_role(posting("other", company="Other"))
|
|
clock.tick()
|
|
for i in range(3):
|
|
store.upsert_role(posting(f"a{i}", company="Acme"))
|
|
clock.tick()
|
|
|
|
res = client.get("/api/roles", params={"limit": 1})
|
|
assert [r["source_id"] for r in res.json()] == ["other"]
|
|
|
|
|
|
# --------------------------------------------------------------- companies
|
|
|
|
def test_companies_lists_board_and_open_role_count(
|
|
client: TestClient, store: Store) -> None:
|
|
sub = store.add_subscription("ats", "greenhouse:acme", company="Acme", board="greenhouse")
|
|
store.add_company("Acme", key="acme")
|
|
store.upsert_role(posting("1", company="Acme"), sub)
|
|
|
|
res = client.get("/api/companies")
|
|
assert res.status_code == 200
|
|
[row] = [c for c in res.json() if c["key"] == "acme"]
|
|
assert row == {"key": "acme", "name": "Acme", "board": "greenhouse",
|
|
"affinity": 0, "open_roles": 1}
|
|
|
|
|
|
def test_set_affinity_validates_and_updates(client: TestClient, store: Store) -> None:
|
|
store.add_company("Acme", key="acme")
|
|
|
|
res = client.post("/api/companies/acme/affinity", json={"affinity": 2})
|
|
assert res.status_code == 200
|
|
assert res.json()["affinity"] == 2
|
|
assert store.company("acme").affinity == 2 # type: ignore[union-attr]
|
|
|
|
assert client.post("/api/companies/acme/affinity", json={"affinity": 9}).status_code == 422
|
|
assert client.post("/api/companies/nope/affinity", json={"affinity": 1}).status_code == 404
|
|
|
|
|
|
# ----------------------------------------------------------- applications
|
|
|
|
def test_save_creates_an_application_with_a_saved_event(
|
|
client: TestClient, store: Store) -> None:
|
|
store.upsert_role(posting("1", company="Acme", place="Berlin"))
|
|
uid = posting("1").uid
|
|
|
|
res = client.post("/api/applications", json={"role_uid": uid, "status": "saved"})
|
|
assert res.status_code == 201
|
|
body = res.json()
|
|
assert body["role_uid"] == uid
|
|
assert body["status"] == "saved"
|
|
assert body["company_key"] == "acme"
|
|
assert body["next_action"] == "Read the posting and decide"
|
|
assert body["next_date"] is not None
|
|
|
|
events = store.application_events(body["id"])
|
|
assert [e.status for e in events] == ["saved"]
|
|
|
|
# Saving the same role again returns the existing row rather than a
|
|
# second one.
|
|
again = client.post("/api/applications", json={"role_uid": uid, "status": "saved"})
|
|
assert again.json()["id"] == body["id"]
|
|
|
|
listed = client.get("/api/applications").json()
|
|
assert [a["id"] for a in listed] == [body["id"]]
|
|
assert listed[0]["role_title"] == "Senior Backend Engineer"
|
|
|
|
|
|
def test_save_an_unknown_role_is_refused(client: TestClient) -> None:
|
|
res = client.post("/api/applications", json={"role_uid": "nope", "status": "saved"})
|
|
assert res.status_code == 404
|
|
|
|
|
|
def test_set_status_validates_and_writes_an_event(
|
|
client: TestClient, store: Store) -> None:
|
|
store.upsert_role(posting("1", company="Acme"))
|
|
uid = posting("1").uid
|
|
saved = client.post("/api/applications", json={"role_uid": uid}).json()
|
|
|
|
res = client.post(f"/api/applications/{saved['id']}/status", json={"status": "applied"})
|
|
assert res.status_code == 200
|
|
assert res.json()["status"] == "applied"
|
|
assert [e.status for e in store.application_events(saved["id"])] == ["saved", "applied"]
|
|
|
|
assert client.post(f"/api/applications/{saved['id']}/status",
|
|
json={"status": "ghosted"}).status_code == 422
|
|
assert client.post("/api/applications/nope/status",
|
|
json={"status": "applied"}).status_code == 404
|
|
|
|
|
|
# --------------------------------------------------------------------- page
|
|
|
|
def test_index_renders_the_shell(client: TestClient) -> None:
|
|
res = client.get("/")
|
|
assert res.status_code == 200
|
|
assert "roledesk" in res.text
|
|
assert 'id="roles-list"' in res.text
|
|
assert "<desk-jobs>" in res.text
|
|
|
|
|
|
# ------------------------------------------------------------------ agents
|
|
|
|
def test_agents_are_created_updated_listed_and_deleted(client: TestClient) -> None:
|
|
body = {"name": "Berlin", "intent": "python backend",
|
|
"gates": [{"field": "place", "values": ["Berlin"], "mode": "require"}]}
|
|
created = client.post("/api/agents", json=body)
|
|
assert created.status_code == 200
|
|
agent = created.json()
|
|
assert agent["weights"] == {"fit": 0.6, "recency": 0.2, "affinity": 0.2}
|
|
assert agent["gates"][0]["weight"] == 1.0
|
|
|
|
updated = client.post("/api/agents", json={**body, "id": agent["id"], "enabled": False,
|
|
"weights": {"fit": 1, "recency": 0,
|
|
"affinity": 0}})
|
|
assert updated.json()["enabled"] is False and updated.json()["weights"]["fit"] == 1
|
|
|
|
assert [a["name"] for a in client.get("/api/agents").json()] == ["Berlin"]
|
|
assert client.post("/api/agents", json=body).status_code == 409
|
|
assert client.delete(f"/api/agents/{agent['id']}").status_code == 204
|
|
assert client.get("/api/agents").json() == []
|
|
assert client.delete(f"/api/agents/{agent['id']}").status_code == 404
|
|
|
|
|
|
def test_a_gate_on_an_unknown_field_is_refused(client: TestClient) -> None:
|
|
body = {"name": "Bad", "gates": [{"field": "salary", "values": [1]}]}
|
|
assert client.post("/api/agents", json=body).status_code == 422
|
|
|
|
|
|
def test_roles_by_agent_are_ordered_by_score_and_hide_the_gated(
|
|
client: TestClient, store: Store) -> None:
|
|
for sid in ("low", "high", "gated"):
|
|
store.upsert_role(posting(sid))
|
|
uids = {r.source_id: r.uid for r in store.roles()}
|
|
agent = store.save_agent("A")
|
|
store.replace_scores(agent.id, [(uids["low"], 0.2, ""), (uids["high"], 0.8, ""),
|
|
(uids["gated"], 0.0, "place unknown")])
|
|
|
|
shown = client.get(f"/api/roles?agent={agent.id}").json()
|
|
assert [r["source_id"] for r in shown] == ["high", "low"]
|
|
assert shown[0]["score"] == 0.8
|
|
everything = client.get(f"/api/roles?agent={agent.id}&gated=1").json()
|
|
assert everything[-1]["gated_reason"] == "place unknown"
|
|
assert client.get("/api/roles").json()[0]["score"] is None
|
|
assert client.get("/api/roles?agent=999").status_code == 404
|