Files
jobboard/tests/test_pages.py
T

120 lines
5.1 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