"""Open the projects page in a browser against an in-process server."""

from __future__ import annotations

import subprocess
from pathlib import Path

import pytest

from integration.harness import running_server

sync_api = pytest.importorskip("playwright.sync_api")


@pytest.mark.playwright
def test_projects_page_loads_without_auth(tmp_path: Path) -> None:
    projects = tmp_path / "projects"
    repo = projects / "demo"
    repo.mkdir(parents=True)
    subprocess.run(["git", "init", "-b", "main", str(repo)], check=True, capture_output=True)

    with (
        running_server(tmp_path=tmp_path, projects=projects, auth=False) as base,
        sync_api.sync_playwright() as playwright,
    ):
        browser = playwright.chromium.launch(headless=True)
        try:
            page = browser.new_page()
            page.goto(base, wait_until="domcontentloaded")
            sync_api.expect(page).to_have_title("PyGitWeb - Projects")
            sync_api.expect(page.get_by_role("heading", name="Projects")).to_be_visible()
            sync_api.expect(page.get_by_role("link", name="demo", exact=True)).to_be_visible()
        finally:
            browser.close()