"""UX-02: live-browser verification of PRNT-03 Alpine IP->port auto-derivation."""
from __future__ import annotations

import pytest


def test_port_autofill(page, live_server: str) -> None:
    """Typing an IP address into the printer form auto-populates port_name.

    Evidence for UX-02: tests/e2e/test_port_autofill.py
    Closes: PRNT-03 Alpine.js port auto-derivation requirement.
    """
    # /printers renders a full page (extends base.html) with Alpine.js loaded
    # and the printer form embedded via {% include "partials/printer_form.html" %}
    page.goto(f"{live_server}/printers", wait_until="domcontentloaded")

    # Wait for Alpine to initialise (x-data hydration on the form wrapper)
    page.wait_for_selector("input[name='ip_address']")

    # Fill triggers the 'input' event that Alpine @input listens to
    page.fill("input[name='ip_address']", "192.168.1.100")

    # Alpine @input reacts synchronously; wait_for_function keeps the test stable
    page.wait_for_function(
        "document.querySelector(\"input[name='port_name']\").value === 'IP_192_168_1_100'",
        timeout=2000,
    )

    assert page.input_value("input[name='port_name']") == "IP_192_168_1_100"
