Files
jobboard/tests/test_pages.py
T

147 lines
6.3 KiB
Python

"""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.models import HAND_REASONS
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_the_rail_opens_on_viewing_as_then_filters_then_order(client: TestClient) -> None:
rail = client.get("/").text.split('<aside id="rail"', 1)[1].split("</aside>", 1)[0]
for part in ('id="agent"', 'id="agent-none"', 'href="/settings/agents"'):
assert part in rail, part
headings = [rail.index(h) for h in ("Viewing as", "<h3>Filters</h3>", "<h3>Order</h3>",
"<h3>Tags")]
assert headings == sorted(headings)
# The agent is not an order any more: the segmented control is the order.
assert rail.index('id="sort"') > rail.index("<h3>Order</h3>")
assert rail.count('id="agent"') == 1
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_watchlist_is_the_third_tab_and_a_shell(client: TestClient) -> None:
page = client.get("/watchlist")
assert page.status_code == 200
assert 'href="/watchlist"\n class="active" aria-current="page">Watchlist' in page.text
assert 'id="watchlist-list"' in page.text and "/static/watchlist.js" in page.text
nav = page.text[page.text.index("<nav>"):page.text.index("</nav>")]
tabs = [t for t in ("Roles", "Applications", "Watchlist") if f">{t}</a>" in nav]
assert tabs == ["Roles", "Applications", "Watchlist"]
assert nav.count('aria-current="page"') == 1
def test_the_watchlist_has_new_company_and_the_form_it_opens(client: TestClient) -> None:
page = client.get("/watchlist").text
assert '<button type="button" id="w-add">New company</button>' in page
assert 'id="watchlist-answer"' in page
template = page.split('<template id="company-form-template">', 1)[1].split("</template>", 1)[0]
for part in ('id="company-form"', 'id="cf-name" required', 'type="url" id="cf-url"',
'id="cf-reason"', 'id="cf-notes"', 'id="cf-cancel"', 'type="submit"'):
assert part in template, part
# The reason's options are the store's reasons, since a row's badge reads
# its words off them and a PATCH sends their values.
options = template.split('<select id="cf-reason">', 1)[1].split("</select>", 1)[0]
values = [v.split('"', 1)[0] for v in options.split('<option value="')[1:]]
assert sorted(values) == sorted(HAND_REASONS)
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
STATIC = Path(__file__).resolve().parents[1] / "app" / "static"
def test_no_script_links_to_a_page_that_does_not_exist() -> None:
"""`/messages` was a 404: the notice log lives on Settings, Sources."""
for script in STATIC.glob("*.js"):
assert '"/messages"' not in script.read_text("utf-8"), script.name
def test_the_reader_excerpt_falls_back_in_the_servers_order() -> None:
"""`excerptOf` cuts the same text `_EXCERPT_SOURCE` does: summary, then
description, then teaser, or a teaser-only role's row blanks after Fetch."""
text = (STATIC / "roles.js").read_text("utf-8")
assert "role.summary || role.description || role.teaser" in text
def test_the_period_is_called_posted_as_the_date_it_counts_back_over(
client: TestClient) -> None:
"""`days` keeps `posted_at` within the window, the date a row shows as its
age, so the select is not called First seen."""
rail = client.get("/").text.split('<aside id="rail"', 1)[1].split("</aside>", 1)[0]
assert '<label class="rail-field" for="period">Posted</label>' in rail
assert "First seen" not in rail
template = (STATIC.parent / "templates" / "roles.html").read_text("utf-8")
assert "docs/architecture.md" not in template and "Agent match" not in template