File size: 2,547 Bytes
c72a1e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""
Playwright E2E tests for StudyAgent.

Requires:
  - Server running: cd ~/Projekte/StudyAgent && .venv/bin/uvicorn main:app --reload
  - Browsers installed: playwright install chromium
"""
import pytest

pw = pytest.importorskip("playwright")
from playwright.sync_api import Page, expect


def _add_subject(page: Page, name: str) -> None:
    """Fill and submit the add-subject form via the visible input field."""
    name_input = page.locator('input[name="name"]')
    name_input.fill(name)
    page.locator('button[type="submit"]:has-text("Fach hinzufuegen")').click()
    # Wait for htmx to swap the new card into the DOM
    page.wait_for_timeout(500)


def test_dashboard_loads(page: Page, base_url: str) -> None:
    """Dashboard returns 200 and renders the subjects section heading."""
    response = page.goto(base_url)
    assert response is not None and response.status == 200

    # The section heading is always present on the page
    expect(page.locator("text=Aktive Faecher")).to_be_visible()


def test_add_subject_flow(page: Page, base_url: str) -> None:
    """Adding a subject shows it on the page as a subject card."""
    page.goto(base_url)

    _add_subject(page, "Mathematik")

    # The subject card renders the name inside .subject-name
    expect(page.locator(".subject-name", has_text="Mathematik")).to_be_visible()


def test_wip_limit_enforced(page: Page, base_url: str) -> None:
    """A third subject is rejected with the WIP-limit buddy warning."""
    page.goto(base_url)

    # Add first two subjects — the form is rendered while < 2 active subjects
    _add_subject(page, "Fach 1")

    # After first add, reload so the template re-evaluates the condition and
    # renders the form for the second subject
    page.reload()
    _add_subject(page, "Fach 2")

    # Now the form is hidden by the template (WIP=2/2). Post the third subject
    # directly via the API to trigger the server-side rejection message.
    page.evaluate(
        """async () => {
            const body = new URLSearchParams({ name: 'Fach 3', semester: '' });
            const resp = await fetch('/subjects', {
                method: 'POST',
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                body: body.toString(),
            });
            const html = await resp.text();
            document.getElementById('wip-warning').innerHTML = html;
        }"""
    )

    # The rejection message contains "Parkiere"
    expect(page.locator("#wip-warning")).to_contain_text("Parkiere")