"""End-to-end tests for pagination on the home and posts listing pages. Tests that pagination controls appear when there are more posts than the page size, that navigation between pages works, and that boundary controls are correctly disabled on the first and last pages. """ from __future__ import annotations import pytest from playwright.sync_api import Page from tests.e2e.pages import HomePage def _seed_posts(page: Page, base_url: str, count: int) -> None: """Create published posts via form POST to ensure pagination exists. Args: page: Authenticated Playwright page. base_url: Application base URL. count: Number of posts to create. """ for idx in range(count): response = page.request.post( f"{base_url}/web/posts/new", form={ "title": f"Pagination Seed {idx:03d}", "content": f"Content for seed post {idx}.", "tags": "pagination", "action": "publish", }, ) assert response.ok, f"Failed to create post {idx}: {response.status}" @pytest.mark.e2e def test_pagination_navigation_across_pages( user_page: Page, base_url: str, ) -> None: """Test pagination navigation between pages. Steps: 1. Ensure enough posts exist for pagination. 2. Open home page and verify page 1 boundary state. 3. Click "Next" and verify page advances. 4. Click "Previous" and verify back on page 1. Args: user_page: Playwright page authenticated as regular user. base_url: Application base URL. """ home = HomePage(user_page, base_url) home.open() if not home.can_go_next(): _seed_posts(user_page, base_url, 12) home.open() assert home.get_current_page() == 1 assert not home.can_go_prev() assert home.can_go_next() assert home.count_posts() <= 10 with user_page.expect_navigation(wait_until="networkidle"): home.go_to_next_page() assert home.get_current_page() == 2 assert home.can_go_prev() assert home.count_posts() <= 10 with user_page.expect_navigation(wait_until="networkidle"): home.go_to_prev_page() assert home.get_current_page() == 1 assert not home.can_go_prev() @pytest.mark.e2e def test_pagination_boundary_on_last_page( user_page: Page, base_url: str, ) -> None: """Test that the last page has "Next" disabled and "Previous" enabled. Steps: 1. Ensure enough posts exist for pagination. 2. Navigate through all pages to the last one. 3. Verify boundary controls on the last page. Args: user_page: Playwright page authenticated as regular user. base_url: Application base URL. """ home = HomePage(user_page, base_url) home.open() if not home.can_go_next(): _seed_posts(user_page, base_url, 12) home.open() assert home.can_go_next() while home.can_go_next(): with user_page.expect_navigation(wait_until="networkidle"): home.go_to_next_page() assert home.can_go_prev() assert not home.can_go_next() assert home.count_posts() <= 10