Serve Best CV match, Closing soon, the language flag and the deadline in the feed, and suggest an agent's languages from its CV
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9960799b96
commit
14076e988d
5 files changed
+313
-34
No files matched your search
@@ -0,0 +1,178 @@
|
||||
"""The CV match in the feed and the API: Best CV match, Closing soon, the
|
||||
new row and role fields, the agent's CV fields over `/api/agents`, and the
|
||||
languages suggested from a CV. Nothing here reaches a model."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from collections.abc import Iterator
|
||||
from datetime import UTC, datetime
|
||||
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 CvLanguage, Prompt
|
||||
from app.storage import Store
|
||||
from desk.models import Item
|
||||
from desk.tools import agent
|
||||
from prepped import upsert
|
||||
|
||||
|
||||
def posting(source_id: str, **raw: object) -> Item:
|
||||
item = Item(source_id=source_id, location=f"https://boards.example/{source_id}",
|
||||
title=f"Head of Data {source_id}", body="Lead the data team.",
|
||||
published_at=datetime(2026, 9, 20, tzinfo=UTC), raw=dict(raw))
|
||||
item.source = "search"
|
||||
return item
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def app(tmp_path: Path) -> FastAPI:
|
||||
return create_app(Config(server=ServerConfig(host="127.0.0.1", port=0),
|
||||
database=tmp_path / "roledesk.db"))
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def store(app: FastAPI) -> Store:
|
||||
found: Store = app.state.store
|
||||
found.clock = lambda: datetime(2026, 10, 3, 9, 0, tzinfo=UTC)
|
||||
return found
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client(app: FastAPI) -> Iterator[TestClient]:
|
||||
with TestClient(app) as c:
|
||||
yield c
|
||||
|
||||
|
||||
def setup(store: Store) -> tuple[Any, dict[str, str]]:
|
||||
"""Four roles scored a > b > c > d for an agent with a CV and languages;
|
||||
c is judged 90 and b 60, a and d not at all; d is gated."""
|
||||
owner = store.save_agent("Data", prompts=[Prompt(name="Lead", text="Head of data")],
|
||||
cv_text="VP Data.",
|
||||
cv_languages=[CvLanguage(language="da", level="native"),
|
||||
CvLanguage(language="de", level="professional")])
|
||||
uid = {}
|
||||
for sid, deadline in (("a", "2026-10-03"), ("b", "2026-10-07"), ("c", "2026-10-08"),
|
||||
("d", "2026-10-04")):
|
||||
upsert(store, posting(sid, applicationDeadline=deadline))
|
||||
uid[sid] = posting(sid).uid
|
||||
store.replace_scores(owner.id, [(uid["a"], 0.9, ""), (uid["b"], 0.8, ""),
|
||||
(uid["c"], 0.7, ""), (uid["d"], 0.6, "gated")])
|
||||
store.set_judgement(owner.id, uid["c"], "k", 90, 90, 90, ["Leads data."], ["Small team."])
|
||||
store.set_judgement(owner.id, uid["b"], "k", 60, 60, 60, ["Data."], ["Junior."])
|
||||
with store.transaction() as conn:
|
||||
conn.execute("UPDATE roles SET languages_required = ? WHERE uid = ?", (json.dumps(
|
||||
[{"language": "sv", "level": "fluent", "kind": "required"}]), uid["a"]))
|
||||
conn.execute("UPDATE roles SET languages_required = ? WHERE uid = ?", (json.dumps(
|
||||
[{"language": "de", "level": "fluent", "kind": "required"}]), uid["b"]))
|
||||
conn.execute("UPDATE roles SET languages_required = ? WHERE uid = ?", (json.dumps(
|
||||
[{"language": "da", "level": "", "kind": "plus"}]), uid["c"]))
|
||||
return owner, uid
|
||||
|
||||
|
||||
def test_best_cv_match_puts_the_judged_first_by_cv_score(
|
||||
client: TestClient, store: Store) -> None:
|
||||
owner, uid = setup(store)
|
||||
page = client.get("/api/roles", params={"agent": owner.id, "order": "cv"}).json()
|
||||
assert [r["uid"] for r in page["items"]] == [uid["c"], uid["b"], uid["a"]]
|
||||
assert [r["cv_score"] for r in page["items"]] == [90.0, 60.0, None]
|
||||
assert page["total"] == 3
|
||||
scored = client.get("/api/roles", params={"agent": owner.id}).json()["items"]
|
||||
assert [r["uid"] for r in scored] == [uid["a"], uid["b"], uid["c"]]
|
||||
assert scored[1]["cv_score"] == 60.0, "the total rides on every order"
|
||||
assert client.get("/api/roles", params={"agent": owner.id,
|
||||
"order": "best"}).status_code == 422
|
||||
|
||||
|
||||
def test_a_row_carries_the_deadline_and_the_language_flag(
|
||||
client: TestClient, store: Store) -> None:
|
||||
owner, uid = setup(store)
|
||||
rows = {r["uid"]: r for r in
|
||||
client.get("/api/roles", params={"agent": owner.id}).json()["items"]}
|
||||
assert rows[uid["a"]]["deadline"] == "2026-10-03"
|
||||
assert (rows[uid["a"]]["lang_flag"], rows[uid["a"]]["lang_note"]) == \
|
||||
("missing", "requires Swedish (fluent); yours: none")
|
||||
assert (rows[uid["b"]]["lang_flag"], rows[uid["b"]]["lang_note"]) == \
|
||||
("below", "requires German (fluent); yours: professional")
|
||||
assert (rows[uid["c"]]["lang_flag"], rows[uid["c"]]["lang_note"]) == \
|
||||
("plus", "Danish is a plus; yours: native")
|
||||
|
||||
|
||||
def test_closing_soon_keeps_today_to_four_days_on(client: TestClient, store: Store) -> None:
|
||||
owner, uid = setup(store)
|
||||
params = {"agent": owner.id, "order": "newest", "closing": 1}
|
||||
page = client.get("/api/roles", params=params).json()
|
||||
assert sorted(r["uid"] for r in page["items"]) == sorted([uid["a"], uid["b"], uid["d"]])
|
||||
assert page["total"] == 3
|
||||
facets = client.get("/api/roles/facets", params=params).json()
|
||||
assert sum(f["count"] for f in facets["pay"]) == 3
|
||||
|
||||
|
||||
def test_the_role_carries_its_judgement_and_languages(
|
||||
client: TestClient, store: Store) -> None:
|
||||
owner, uid = setup(store)
|
||||
role = client.get(f"/api/roles/{uid['c']}", params={"agent": owner.id}).json()
|
||||
judgement = role["judgement"]
|
||||
assert (judgement["skills"], judgement["cv_score"]) == (90, 90.0)
|
||||
assert judgement["strengths"] == ["Leads data."] and judgement["gaps"] == ["Small team."]
|
||||
assert judgement["judged_at"]
|
||||
assert role["cv_score"] == 90.0 and role["lang_flag"] == "plus"
|
||||
assert role["languages_required"] == [{"language": "da", "level": "", "kind": "plus"}]
|
||||
assert role["deadline"] == "2026-10-08"
|
||||
plain = client.get(f"/api/roles/{uid['a']}", params={"agent": owner.id}).json()
|
||||
assert plain["judgement"] is None and plain["cv_score"] is None
|
||||
|
||||
|
||||
def test_an_agent_without_languages_flags_nothing(client: TestClient, store: Store) -> None:
|
||||
owner, uid = setup(store)
|
||||
store.save_agent("Data", prompts=owner.prompts, agent_id=owner.id, cv_languages=[])
|
||||
rows = client.get("/api/roles", params={"agent": owner.id}).json()["items"]
|
||||
assert {r["lang_flag"] for r in rows} == {""}
|
||||
|
||||
|
||||
def test_the_cv_fields_round_trip_and_are_kept_when_left_out(client: TestClient) -> None:
|
||||
body = {"name": "Data", "cv_text": "VP Data.", "avoid_text": "Reporting.",
|
||||
"must_text": "Permanent.",
|
||||
"cv_languages": [{"language": "da", "level": "native"},
|
||||
{"language": "EN", "level": "fluent"}]}
|
||||
made = client.post("/api/agents", json=body).json()
|
||||
assert made["cv_languages"] == [{"language": "da", "level": "native"},
|
||||
{"language": "en", "level": "fluent"}]
|
||||
kept = client.post("/api/agents", json={"id": made["id"], "name": "Data"}).json()
|
||||
assert (kept["cv_text"], kept["avoid_text"], kept["must_text"]) == \
|
||||
("VP Data.", "Reporting.", "Permanent.")
|
||||
assert len(kept["cv_languages"]) == 2
|
||||
nulls = client.post("/api/agents", json={"id": made["id"], "name": "Data",
|
||||
"cv_text": None, "cv_languages": None}).json()
|
||||
assert nulls["cv_text"] == "VP Data." and len(nulls["cv_languages"]) == 2
|
||||
bad = client.post("/api/agents", json={"name": "Bad", "cv_languages": [
|
||||
{"language": "da", "level": "good"}]})
|
||||
assert bad.status_code == 422
|
||||
|
||||
|
||||
def test_the_languages_of_a_cv_are_suggested_and_nothing_stored(
|
||||
client: TestClient, store: Store, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
monkeypatch.setattr(agent, "backend", lambda settings: "api")
|
||||
monkeypatch.setattr(agent, "ask_json", lambda settings, **kwargs: agent.Reply(
|
||||
data={"languages": [{"language": "da", "level": "native"}]}, backend="api"))
|
||||
res = client.post("/api/agents/cv-languages", json={"cv_text": "Dansk modersmål."})
|
||||
assert res.status_code == 200
|
||||
assert res.json() == {"languages": [{"language": "da", "level": "native"}]}
|
||||
assert store.agents() == []
|
||||
assert client.post("/api/agents/cv-languages", json={"cv_text": " "}).status_code == 422
|
||||
|
||||
def fails(settings: Any, **kwargs: Any) -> agent.Reply:
|
||||
raise agent.AgentError("timed out")
|
||||
|
||||
monkeypatch.setattr(agent, "ask_json", fails)
|
||||
assert client.post("/api/agents/cv-languages", json={"cv_text": "CV"}).status_code == 502
|
||||
|
||||
monkeypatch.setattr(agent, "backend", lambda settings: "")
|
||||
res = client.post("/api/agents/cv-languages", json={"cv_text": "CV"})
|
||||
assert res.status_code == 503 and "No model is reachable" in res.json()["detail"]
|
||||
Reference in new issue
Block a user