"""The limit words of a board (`app/jobs/limits.py`) and what the Companies and Sources pages carry of them.""" from __future__ import annotations import html import json import re import types 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.jobs import limits, reach from app.main import create_app from app.storage import Store from desk.models import Item from plugins.readers import Fields, Filters, Reads from prepped import upsert TARGET = "fake:acme" def fake(**over: Any) -> types.ModuleType: """A reader that declares `READS`, the only thing `words` looks at.""" reads: dict[str, Any] = { "scope": "board", "listing": "whole", "text": "listing", "complete": "whole", "places": "one", "robots": "read: test", "fields": Fields(pay="figures", posted="trusted"), "filters": Filters(country="one", remote="one")} reads.update(over) module = types.ModuleType("fake") module.READS = Reads(**reads) # type: ignore[attr-defined] return module def state(**keys: str) -> dict[str, str]: """`ats::fake:acme` for each keyword, with the markers of a complete read unless given.""" kept = {"ok": "run", "complete": "1"} | keys return {f"ats:{k}:{TARGET}": v for k, v in kept.items()} def words(module: types.ModuleType, kept: dict[str, str], size: int | None = 500, **kw: Any) -> list[str]: return limits.words(module, kept, TARGET, None, size, **kw) def test_a_quiet_board_has_no_words() -> None: assert words(fake(), state()) == [] @pytest.mark.parametrize("keys, word", [ ({"failing": json.dumps({"since": "2026-09-01", "reason": "x"})}, "failing"), ({"complete": ""}, "read incomplete"), ({"swept": "1"}, "swept"), ({"sweep": "{}"}, "swept"), ({"filtered": "1"}, "filtered by reach"), ({"remote_unasked": "1"}, "remote not asked"), ({"narrowed": "1"}, "narrowed by terms"), ]) def test_the_words_of_the_state(keys: dict[str, str], word: str) -> None: found = words(fake(), state(**keys)) assert word in found assert set(found) <= {word, "read incomplete"} def test_a_board_never_read_is_not_incomplete() -> None: """No `ok` marker is a board not read yet, which no word says.""" assert words(fake(), {}) == [] def test_a_missing_or_empty_remote_unasked_is_not_set() -> None: assert "remote not asked" not in words(fake(), state()) assert "remote not asked" not in words(fake(), state(remote_unasked="")) def test_the_line_says_nothing_asked() -> None: assert words(fake(), state(), line="site lists none of DK, SE: nothing asked") == [ "nothing asked"] assert words(fake(), state(), line="follows agents: DK ยท all roles") == [] def test_size_and_ceiling() -> None: paged = fake(listing="paged", page_size=20, ceiling=2000, complete="end") assert words(paged, state(), 0) == ["size 0", "read whole"] assert words(paged, state(), 1999) == [] assert words(paged, state(), 2000) == ["over the ceiling"] assert words(paged, state(), None) == [] assert words(fake(), state(), 5000) == [] # no ceiling declared @pytest.mark.parametrize("text, word", [("detail", "text per role"), ("none", "no text"), ("excerpt", "excerpt only")]) def test_where_the_text_is(text: str, word: str) -> None: assert words(fake(text=text), state()) == [word] def test_what_the_host_states() -> None: assert words(fake(fields=Fields(pay="none", posted="listed")), state()) == [ "no pay", "date not trusted"] assert words(fake(fields=Fields(pay="line", posted="none")), state()) == [ "date not trusted"] def test_roles_without_text_and_text_reads_that_failed() -> None: kept = state(detailfailed=json.dumps(["1", "2", "3"])) assert words(fake(), kept, counts=limits.Texts(9, 4)) == [ "without text: 4", "text failed: 3"] assert words(fake(), state(), counts=limits.Texts(9, 0)) == [] def test_a_board_is_read_whole_under_the_bar_or_when_its_host_cannot_filter() -> None: assert words(fake(), state(), reach.SMALL - 1) == ["read whole"] assert words(fake(), state(), reach.SMALL) == [] assert words(fake(filters=Filters()), state(), 5000) == ["read whole"] def test_the_words_come_in_the_order_of_the_vocabulary() -> None: found = words(fake(text="none", fields=Fields()), state(complete="", filtered="1", narrowed="1", remote_unasked="1"), 0, counts=limits.Texts(2, 2), line="nothing asked") assert found == ["read incomplete", "filtered by reach", "remote not asked", "narrowed by terms", "nothing asked", "size 0", "no text", "no pay", "date not trusted", "without text: 2", "read whole"] phrases = [w.partition(":")[0] for w in found] assert phrases == sorted(phrases, key=list(limits.WORDS).index) def test_a_search_reader_reads_its_own_keys() -> None: module = fake(scope="search", complete="never", listing="paged", page_size=20) assert words(module, {f"search:failing:{TARGET}": "{}"}, None) == ["failing"] assert "failing" not in words(module, state(failing="{}"), None) def test_a_module_that_declares_nothing_gets_the_words_of_its_state() -> None: assert words(types.ModuleType("old"), state(filtered="1"), 0) == [ "filtered by reach", "size 0"] def test_the_filter_offers_every_word_with_its_meaning() -> None: """The template repeats `WORDS`, since no view hands it over; they must agree.""" text = (Path(__file__).parent.parent / "app/templates/settings/sources.html").read_text() group = re.search(r'(.*?)', text, re.S) assert group options = [(html.unescape(v), html.unescape(t)) for v, t in re.findall( r'', group.group(1))] assert options == list(limits.WORDS.items()) # ---------------------------------------------------------- the two endpoints @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 role(source: str, source_id: str, body: str) -> Item: item = Item(source_id=source_id, location=f"https://x.example/{source_id}", title="Senior Backend Engineer", body=body, published_at=datetime(2026, 9, 20, tzinfo=UTC), raw={"company": "Acme"}) item.source = source return item def test_companies_carry_their_limits_and_the_boards_own_open_roles( client: TestClient, store: Store) -> None: board = store.add_subscription("ats", "lever:acme", company="Acme", board="lever") search = store.add_subscription("search", "jobindex:cph", label="cph", board="jobindex") store.add_company("Acme", key="acme", ats="lever") upsert(store, role("ats", "1", "Build things."), board) upsert(store, role("ats", "2", ""), board) upsert(store, role("search", "s1", "Teaser"), search) store.state["ats:ok:lever:acme"] = "run" store.state["ats:complete:lever:acme"] = "" store.state["ats:size:lever:acme"] = "12" [row] = client.get("/api/companies").json() assert (row["open_roles"], row["board_open"]) == (3, 2) # Lever states pay and a trusted date, gives its text in the list and cannot filter. assert row["limits"] == ["read incomplete", "without text: 1", "read whole"] def test_a_company_without_a_board_has_no_limits(client: TestClient, store: Store) -> None: store.add_company("Novo", key="novo", ats="taleo") [row] = client.get("/api/companies").json() assert (row["limits"], row["board_open"]) == ([], None) def test_the_readers_carry_what_they_declare_of_their_limits(client: TestClient) -> None: found = {r["name"]: r for r in client.get("/api/readers").json()} workday = found["workday"] assert (workday["complete"], workday["page_size"], workday["ceiling"]) == ( "stated", 20, 2000) assert workday["teaser"] is False and workday["empty"] == "trusted" assert workday["fields"]["pay"] == "none" assert set(workday["fields"]) == {"pay", "posted", "remote", "country", "seniority", "employment"} assert found["oracle"]["teaser"] is True