"""E2E tests for the comment submission flow.""" from __future__ import annotations import pytest from playwright.sync_api import Page, expect ARTICLE_SLUG = "nightly-playwright-journey" def _go_to_article(page: Page, base_url: str) -> None: page.goto(f"{base_url}/articles/{ARTICLE_SLUG}/", wait_until="networkidle") def _submit_comment(page: Page, *, name: str = "E2E Tester", email: str = "e2e@example.com", body: str) -> None: """Fill and submit the main (non-reply) comment form.""" form = page.locator("form[data-comment-form]") form.locator('input[name="author_name"]').fill(name) form.locator('input[name="author_email"]').fill(email) form.locator('textarea[name="body"]').fill(body) form.get_by_role("button", name="Post comment").click() @pytest.mark.e2e def test_valid_comment_shows_moderation_message(page: Page, base_url: str) -> None: """Successful comment submission must show the awaiting-moderation message.""" _go_to_article(page, base_url) _submit_comment(page, body="This is a test comment from Playwright.") # HTMX swaps the form container inline — wait for the moderation message expect(page.get_by_text("awaiting moderation")).to_be_visible(timeout=10_000) @pytest.mark.e2e def test_valid_comment_not_immediately_visible(page: Page, base_url: str) -> None: """Submitted comment must NOT appear in the comments list before moderation.""" _go_to_article(page, base_url) unique_body = "Unique unmoderated comment body xq7z" _submit_comment(page, body=unique_body) # Wait for HTMX response to settle expect(page.get_by_text("awaiting moderation")).to_be_visible(timeout=10_000) expect(page.get_by_text(unique_body)).not_to_be_visible() @pytest.mark.e2e def test_empty_body_shows_form_errors(page: Page, base_url: str) -> None: _go_to_article(page, base_url) _submit_comment(page, body=" ") # whitespace-only body page.wait_for_load_state("networkidle") expect(page.locator('[aria-label="Comment form errors"]')).to_be_visible(timeout=10_000) assert "commented=1" not in page.url @pytest.mark.e2e def test_missing_name_shows_form_errors(page: Page, base_url: str) -> None: _go_to_article(page, base_url) form = page.locator("form[data-comment-form]") form.locator('input[name="author_name"]').fill("") form.locator('input[name="author_email"]').fill("e2e@example.com") form.locator('textarea[name="body"]').fill("Comment without a name.") form.get_by_role("button", name="Post comment").click() page.wait_for_load_state("networkidle") assert "commented=1" not in page.url @pytest.mark.e2e def test_reply_form_visible_on_approved_comment(page: Page, base_url: str) -> None: """An approved seeded comment must display a reply form.""" _go_to_article(page, base_url) # The seeded approved comment should be visible (as author name) expect(page.get_by_text("E2E Approved Commenter", exact=True)).to_be_visible() # And a Reply toggle for it expect(page.locator("summary").filter(has_text="Reply")).to_be_visible() @pytest.mark.e2e def test_reply_submission_shows_moderation_message(page: Page, base_url: str) -> None: """Submitting a reply to an approved comment should show moderation message.""" _go_to_article(page, base_url) # Click the Reply toggle (summary element) page.locator("summary").filter(has_text="Reply").first.click() # The reply form should now be visible post_reply_btn = page.get_by_test_id("post-reply-btn").first expect(post_reply_btn).to_be_visible() # Fill the form fields # Use a locator that finds the container for this reply form (the details element) reply_container = page.locator("details").filter(has=post_reply_btn).first reply_container.locator('input[name="author_name"]').fill("E2E Replier") reply_container.locator('input[name="author_email"]').fill("replier@example.com") reply_container.locator('textarea[name="body"]').fill("This is a test reply.") post_reply_btn.click() # HTMX swaps the reply form container inline expect(page.get_by_text("awaiting moderation")).to_be_visible(timeout=10_000) @pytest.mark.e2e def test_comments_section_absent_when_disabled(page: Page, base_url: str) -> None: """Article with comments_enabled=False must not show the comments section.""" response = page.goto(f"{base_url}/articles/e2e-no-comments/", wait_until="networkidle") assert response is not None and response.status == 200, ( f"Expected 200 for e2e-no-comments article, got {response and response.status}" ) expect(page.get_by_role("heading", level=1)).to_have_text("No Comments Article") expect(page.get_by_role("heading", name="Comments", exact=True)).to_have_count(0) expect(page.get_by_role("button", name="Post comment")).to_have_count(0)