Roles is newsdesk's feed: rail, feed and reader, with read state and tags
The Roles tab takes newsdesk's shell, ported from its index.html and
app.css rather than rewritten: a filter rail (Show read, Best first or
Newest first, first seen, tag chips with counts, the old filters behind a
disclosure), the feed in newsdesk's row layout (score gutter, title,
company, place, badges, age, tags, the summary as excerpt, judge buttons)
and the reader as a third column that replaces the list on a phone. The
router pushes /roles/{uid}, which the server also renders with the reader
open, so the separate read page and role.js go. j/k, u/n/d, S, Enter,
Escape, o, /, f and c work as in newsdesk. The phone rules come with their
comments.
Two things the row lacked: roles.read_at, stamped when the reader opens a
role or a vote is cast, behind include_read on /api/roles; and role_tags,
written by summarise alongside the summary and served with counts by
/api/filters, filtered in SQL before LIMIT. Migration 5.
PyCharm run configurations in .run/ for serve, pull, seed and pytest.
core.autocrlf=input is set locally so a Windows-side checkout commits LF.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
1 parent
515d2250fc
commit
a9290a6926
23 files changed
+2252
-715
No files matched your search
@@ -0,0 +1,80 @@
|
||||
"""The Roles page's shell: the feed and a role's reader are one page, so
|
||||
`/roles/{uid}` renders what `/` renders with that role open. The script
|
||||
itself is exercised in a browser, not here; this holds what the server
|
||||
decides."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Iterator
|
||||
from datetime import UTC, datetime
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from fastapi import FastAPI
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from app.config import Config, ServerConfig
|
||||
from app.main import create_app
|
||||
from app.storage import Store
|
||||
from desk.models import Item
|
||||
|
||||
|
||||
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:
|
||||
return create_app(Config(server=ServerConfig(host="127.0.0.1", port=0),
|
||||
database=tmp_path / "roledesk.db"))
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def store(app: FastAPI) -> Store:
|
||||
return app.state.store
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client(app: FastAPI) -> Iterator[TestClient]:
|
||||
with TestClient(app) as c:
|
||||
yield c
|
||||
|
||||
|
||||
def test_the_roles_page_is_the_rail_the_feed_and_a_shut_reader(client: TestClient) -> None:
|
||||
page = client.get("/").text
|
||||
for part in ('id="rail"', 'id="list"', 'id="roles-list"', 'id="reader"',
|
||||
'id="rail-toggle"', 'id="search"', 'id="sort"', 'id="period"', 'id="tags"'):
|
||||
assert part in page, part
|
||||
assert '<article id="reader" aria-label="Role" hidden>' in page
|
||||
assert "data-reader" not in page.split("<header", 1)[0]
|
||||
assert "<title>Roles — roledesk</title>" in page
|
||||
assert "/static/roles.js" in page and "/static/judge.js" in page
|
||||
|
||||
|
||||
def test_a_role_address_is_the_same_page_with_its_reader_open(
|
||||
client: TestClient, store: Store) -> None:
|
||||
store.upsert_role(posting("1", company="Acme"))
|
||||
uid = posting("1").uid
|
||||
|
||||
page = client.get(f"/roles/{uid}")
|
||||
assert page.status_code == 200
|
||||
assert 'id="roles-list"' in page.text and "/static/roles.js" in page.text
|
||||
assert '<body data-reader="open">' in page.text
|
||||
assert '<article id="reader" aria-label="Role">' in page.text
|
||||
assert "<title>Senior Backend Engineer — roledesk</title>" in page.text
|
||||
# Roles stays the lit tab, though the path is not its own.
|
||||
assert 'href="/"\n class="active" aria-current="page">Roles' in page.text
|
||||
|
||||
missing = client.get("/roles/nope")
|
||||
assert missing.status_code == 404 and "text/html" in missing.headers["content-type"]
|
||||
|
||||
|
||||
def test_the_read_page_of_its_own_is_gone(client: TestClient) -> None:
|
||||
assert client.get("/static/role.js").status_code == 404
|
||||
other = client.get("/applications").text
|
||||
assert 'id="rail-toggle"' not in other and 'id="search"' not in other
|
||||
assert "<main>" in other
|
||||
+126
-2
@@ -86,19 +86,38 @@ def test_a_v1_file_migrates_to_the_shape_a_new_file_has(tmp_path: Path) -> None:
|
||||
Store(old)
|
||||
with sqlite3.connect(old) as conn:
|
||||
for column in ("permit", "languages", "extract_tries", "vote", "voted_at",
|
||||
"summary", "summarised_at", "article_text", "article_at"):
|
||||
"summary", "summarised_at", "article_text", "article_at",
|
||||
"read_at"):
|
||||
conn.execute(f"ALTER TABLE roles DROP COLUMN {column}")
|
||||
for column in ("intent_key", "intent_vec"):
|
||||
conn.execute(f"ALTER TABLE agents DROP COLUMN {column}")
|
||||
conn.execute("DROP TABLE vectors")
|
||||
conn.execute("DROP TABLE role_tags")
|
||||
conn.execute("PRAGMA user_version = 1")
|
||||
|
||||
assert Store(old).migrations_applied == list(range(2, LATEST + 1))
|
||||
# ALTER appends, so the order differs; every read is by column name.
|
||||
for table in ("roles", "agents", "vectors"):
|
||||
for table in ("roles", "agents", "vectors", "role_tags"):
|
||||
assert sorted(_columns(old, table)) == sorted(_columns(fresh, table))
|
||||
|
||||
|
||||
def test_a_v4_file_migrates_read_state_and_tags(tmp_path: Path) -> None:
|
||||
fresh, old = tmp_path / "fresh.db", tmp_path / "old.db"
|
||||
Store(fresh)
|
||||
Store(old)
|
||||
with sqlite3.connect(old) as conn:
|
||||
conn.execute("ALTER TABLE roles DROP COLUMN read_at")
|
||||
conn.execute("DROP TABLE role_tags")
|
||||
conn.execute("PRAGMA user_version = 4")
|
||||
|
||||
assert Store(old).migrations_applied == [5]
|
||||
assert sorted(_columns(old, "roles")) == sorted(_columns(fresh, "roles"))
|
||||
with sqlite3.connect(old) as conn:
|
||||
tables = {r[0] for r in conn.execute(
|
||||
"SELECT name FROM sqlite_master WHERE type='table'")}
|
||||
assert "role_tags" in tables
|
||||
|
||||
|
||||
def test_state_is_a_mapping(store: Store) -> None:
|
||||
store.state["ats:last"] = "2026-09-23"
|
||||
assert store.state["ats:last"] == "2026-09-23"
|
||||
@@ -244,6 +263,97 @@ def test_a_vote_is_stamped_cleared_and_filtered(store: Store, clock: Clock) -> N
|
||||
store.set_vote("nope", 1)
|
||||
|
||||
|
||||
def test_read_state_is_set_cleared_and_a_vote_marks_read(
|
||||
store: Store, clock: Clock) -> None:
|
||||
store.upsert_role(posting("1"))
|
||||
uid = posting("1").uid
|
||||
role = store.role(uid)
|
||||
assert role is not None and role.read_at is None
|
||||
|
||||
read = store.set_read(uid)
|
||||
assert read.read_at == clock().isoformat(timespec="seconds")
|
||||
clock.tick()
|
||||
again = store.set_read(uid)
|
||||
assert again.read_at == read.read_at, "reading twice keeps the first stamp"
|
||||
|
||||
cleared = store.set_read(uid, False)
|
||||
assert cleared.read_at is None
|
||||
|
||||
voted = store.set_vote(uid, 1)
|
||||
assert voted.read_at is not None, "voting is reading"
|
||||
stamp = voted.read_at
|
||||
unvoted = store.set_vote(uid, None)
|
||||
assert unvoted.read_at == stamp, "clearing a vote is not un-reading"
|
||||
|
||||
with pytest.raises(KeyError):
|
||||
store.set_read("nope")
|
||||
|
||||
|
||||
def test_roles_include_read_excludes_a_read_role_before_the_limit(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_read(uid["1"])
|
||||
|
||||
assert {r.source_id for r in store.roles()} == {"1", "2"}
|
||||
assert {r.source_id for r in store.roles(include_read=False)} == {"2"}
|
||||
assert store.count_roles(include_read=False) == 1
|
||||
|
||||
|
||||
def test_set_tags_normalises_dedupes_and_caps(store: Store) -> None:
|
||||
store.upsert_role(posting("1"))
|
||||
uid = posting("1").uid
|
||||
long_tag = "x" * 50
|
||||
|
||||
tags = store.set_tags(uid, [" Backend Engineer ", "python", "PYTHON", "", " ",
|
||||
long_tag, *[f"t{i}" for i in range(15)]])
|
||||
|
||||
assert tags == sorted(tags)
|
||||
assert len(tags) == 12 # capped at _MAX_TAGS
|
||||
assert len(set(tags)) == 12 # no repeats
|
||||
assert all(len(t) <= 40 for t in tags) # cut to _MAX_TAG_LEN
|
||||
assert "backend engineer" in tags and "python" in tags
|
||||
role = store.role(uid)
|
||||
assert role is not None and role.tags == tags # loaded sorted, with the role
|
||||
|
||||
with pytest.raises(KeyError):
|
||||
store.set_tags("nope", ["x"])
|
||||
|
||||
|
||||
def test_roles_filters_by_tags_any_and_all(store: Store) -> None:
|
||||
store.upsert_role(posting("1"))
|
||||
store.upsert_role(posting("2"))
|
||||
store.upsert_role(posting("3"))
|
||||
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"])
|
||||
store.set_tags(uid["3"], ["go"])
|
||||
|
||||
assert {r.source_id for r in store.roles(tags=["python"])} == {"1", "2"}
|
||||
assert {r.source_id for r in store.roles(tags=["python", "go"])} == {"1", "2", "3"}
|
||||
assert {r.source_id for r in
|
||||
store.roles(tags=["python", "backend"], tag_mode="all")} == {"1"}
|
||||
assert store.count_roles(tags=["python"]) == 2
|
||||
assert store.count_roles(tags=["python", "backend"], tag_mode="all") == 1
|
||||
with pytest.raises(ValueError, match="tag_mode"):
|
||||
store.roles(tags=["python"], tag_mode="maybe")
|
||||
|
||||
|
||||
def test_tag_counts_orders_by_count_then_tag_and_respects_muted(store: Store) -> None:
|
||||
store.add_company("Hidden", key="hidden", affinity=-1)
|
||||
store.upsert_role(posting("1", company="Acme"))
|
||||
store.upsert_role(posting("2", company="Acme"))
|
||||
store.upsert_role(posting("3", company="Hidden"))
|
||||
uid = {r.source_id: r.uid for r in store.roles(hide_muted=False)}
|
||||
store.set_tags(uid["1"], ["python", "backend"])
|
||||
store.set_tags(uid["2"], ["python"])
|
||||
store.set_tags(uid["3"], ["python", "backend"])
|
||||
store.set_read(uid["1"])
|
||||
|
||||
assert store.tag_counts() == [("python", 2, 1), ("backend", 1, 0)]
|
||||
assert store.tag_counts(show_muted=True) == [("python", 3, 2), ("backend", 2, 1)]
|
||||
|
||||
|
||||
def test_a_vote_moves_the_served_score_and_the_order(store: Store) -> None:
|
||||
for sid in ("a", "b", "c"):
|
||||
store.upsert_role(posting(sid))
|
||||
@@ -389,6 +499,20 @@ def test_a_summary_is_written_once(store: Store) -> None:
|
||||
assert store.set_summary("nope", "x") is False
|
||||
|
||||
|
||||
def test_a_summarys_tags_land_with_it_and_not_on_a_rejected_late_answer(
|
||||
store: Store) -> None:
|
||||
store.upsert_role(posting())
|
||||
uid = posting().uid
|
||||
assert store.set_summary(uid, "First.", ["Python", "backend"]) is True
|
||||
role = store.role(uid)
|
||||
assert role is not None and role.tags == ["backend", "python"]
|
||||
|
||||
# A second write is rejected (already summarised), and does not touch tags.
|
||||
assert store.set_summary(uid, "Second.", ["go"]) is False
|
||||
role = store.role(uid)
|
||||
assert role is not None and role.tags == ["backend", "python"]
|
||||
|
||||
|
||||
def test_summarise_waits_on_the_top_of_each_enabled_agent(store: Store) -> None:
|
||||
"""Two agents each contribute their own top one; a closed role, a hidden
|
||||
company's role and a gated score are not in anybody's top."""
|
||||
|
||||
+19
-2
@@ -43,8 +43,9 @@ class Model:
|
||||
return agent.Reply(data={"summaries": self.reply(items)}, backend="api")
|
||||
|
||||
|
||||
def written(items: list[dict[str, Any]]) -> list[dict[str, str]]:
|
||||
return [{"uid": i["uid"], "summary": f"A backend role at Acme, {i['uid']}."}
|
||||
def written(items: list[dict[str, Any]]) -> list[dict[str, Any]]:
|
||||
return [{"uid": i["uid"], "summary": f"A backend role at Acme, {i['uid']}.",
|
||||
"tags": ["Backend Engineer", "python", "fintech"]}
|
||||
for i in items]
|
||||
|
||||
|
||||
@@ -89,12 +90,28 @@ def test_a_batch_writes_the_summary_and_stamps_summarised_at(
|
||||
assert role is not None
|
||||
assert role.summary == f"A backend role at Acme, {uid}."
|
||||
assert role.summarised_at is not None
|
||||
assert role.tags == ["backend engineer", "fintech", "python"]
|
||||
assert result["summarised"] == 1 and result["pending"] == 0
|
||||
[[item]] = model.asked
|
||||
assert item == {"uid": uid, "title": "Backend Engineer 1", "company": "Acme",
|
||||
"place": "Copenhagen", "text": "Python and Go on Kubernetes. Pays 700k DKK."}
|
||||
|
||||
|
||||
def test_the_replys_tags_are_normalised_and_stored(
|
||||
store: Store, config: Config, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
[uid] = ranked(store, "1")
|
||||
stub(monkeypatch, lambda items: [
|
||||
{"uid": items[0]["uid"], "summary": "A backend role, long enough to store.",
|
||||
"tags": [" Backend Engineer ", "PYTHON", "python", "", " "]}])
|
||||
|
||||
modeltask.run(summarise.TASK, config, store)
|
||||
|
||||
role = store.role(uid)
|
||||
assert role is not None
|
||||
# Collapsed whitespace, lowercased, deduplicated, empties dropped.
|
||||
assert role.tags == ["backend engineer", "python"]
|
||||
|
||||
|
||||
def test_the_fetched_page_is_read_before_the_description_and_capped(
|
||||
store: Store, config: Config, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
[uid] = ranked(store, "1")
|
||||
|
||||
+65
-2
@@ -199,6 +199,70 @@ def test_a_vote_moves_a_role_in_the_ranking_at_once(
|
||||
assert ranked() == [("a", 1.0), ("c", 0.66), ("b", 0.6)] # capped at 1
|
||||
|
||||
|
||||
def test_read_state_is_set_and_cleared_over_the_api(client: TestClient, store: Store) -> None:
|
||||
store.upsert_role(posting("1"))
|
||||
uid = posting("1").uid
|
||||
|
||||
res = client.post(f"/api/roles/{uid}/read")
|
||||
assert res.status_code == 200
|
||||
assert res.json()["read_at"] is not None
|
||||
|
||||
res = client.delete(f"/api/roles/{uid}/read")
|
||||
assert res.status_code == 200
|
||||
assert res.json()["read_at"] is None
|
||||
|
||||
assert client.post("/api/roles/nope/read").status_code == 404
|
||||
assert client.delete("/api/roles/nope/read").status_code == 404
|
||||
|
||||
|
||||
def test_list_roles_filters_by_include_read(client: TestClient, store: Store) -> None:
|
||||
store.upsert_role(posting("1"))
|
||||
store.upsert_role(posting("2"))
|
||||
uid = posting("1").uid
|
||||
client.post(f"/api/roles/{uid}/read")
|
||||
|
||||
all_ = client.get("/api/roles").json()
|
||||
assert {r["source_id"] for r in all_["items"]} == {"1", "2"}
|
||||
|
||||
unread = client.get("/api/roles", params={"include_read": "false"}).json()
|
||||
assert {r["source_id"] for r in unread["items"]} == {"2"}
|
||||
assert unread["total"] == 1
|
||||
|
||||
|
||||
def test_list_roles_filters_by_tags_any_and_all(client: TestClient, store: Store) -> None:
|
||||
store.upsert_role(posting("1"))
|
||||
store.upsert_role(posting("2"))
|
||||
uid = {r.source_id: r.uid for r in store.roles()}
|
||||
store.set_tags(uid["1"], ["python", "backend"])
|
||||
store.set_tags(uid["2"], ["python", "frontend"])
|
||||
|
||||
res = client.get("/api/roles", params={"tags": ["python"]})
|
||||
assert {r["source_id"] for r in res.json()["items"]} == {"1", "2"}
|
||||
|
||||
both = client.get("/api/roles", params={"tags": ["python", "backend"],
|
||||
"tag_mode": "all"})
|
||||
assert {r["source_id"] for r in both.json()["items"]} == {"1"}
|
||||
[item] = both.json()["items"]
|
||||
assert item["tags"] == ["backend", "python"]
|
||||
|
||||
|
||||
def test_filters_lists_tag_counts_and_respects_show_muted(
|
||||
client: TestClient, store: Store) -> None:
|
||||
store.add_company("Hidden", key="hidden", affinity=-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
|
||||
@@ -213,8 +277,7 @@ def test_one_role_and_its_read_page(client: TestClient, store: Store) -> None:
|
||||
|
||||
page = client.get(f"/roles/{uid}")
|
||||
assert page.status_code == 200
|
||||
assert 'id="role"' in page.text and "/static/role.js" in page.text
|
||||
assert "/static/roles.js" not in page.text
|
||||
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"]
|
||||
|
||||
|
||||
Reference in new issue
Block a user