Skip to content

Self-hosted homelab infrastructure visualizer — interactive network diagram with live status monitoring

48.4 KB
Raw Blame
"""API endpoint tests for /api/v1/zigbee/*."""

from __future__ import annotations

from unittest.mock import AsyncMock, patch

import pytest
from httpx import AsyncClient

from app.services.import_jobs import reset_jobs

# ---------------------------------------------------------------------------
# Fixtures
# ---------------------------------------------------------------------------


@pytest.fixture(autouse=True)
def _clear_import_jobs() -> None:
    """The canvas-import job registry is process-global; isolate each test."""
    reset_jobs()


async def _start_canvas_import(
    client: AsyncClient, headers: dict, body: dict
) -> str:
    """POST /zigbee/import and return the job id.

    The route is fire-and-poll: it answers 202 immediately and the fetch runs as
    a Starlette background task, which the ASGI transport completes before this
    await returns — so the job is already settled by the time we poll.
    """
    res = await client.post("/api/v1/zigbee/import", json=body, headers=headers)
    assert res.status_code == 202, res.text
    job_id: str = res.json()["job_id"]
    assert res.json()["status"] == "running"
    return job_id

# ---------------------------------------------------------------------------
# /api/v1/zigbee/test-connection
# ---------------------------------------------------------------------------

@pytest.mark.asyncio
async def test_test_connection_success(client: AsyncClient, headers: dict) -> None:
    with patch("app.api.routes.zigbee.test_mqtt_connection") as mock_conn:
        mock_conn.return_value = True
        res = await client.post(
            "/api/v1/zigbee/test-connection",
            json={"mqtt_host": "localhost", "mqtt_port": 1883},
            headers=headers,
        )
    assert res.status_code == 200
    data = res.json()
    assert data["connected"] is True
    assert "success" in data["message"].lower()


@pytest.mark.asyncio
async def test_test_connection_failure(client: AsyncClient, headers: dict) -> None:
    with patch("app.api.routes.zigbee.test_mqtt_connection") as mock_conn:
        mock_conn.side_effect = ConnectionError("Connection refused")
        res = await client.post(
            "/api/v1/zigbee/test-connection",
            json={"mqtt_host": "bad-host", "mqtt_port": 1883},
            headers=headers,
        )
    assert res.status_code == 200
    data = res.json()
    assert data["connected"] is False
    assert "refused" in data["message"].lower()


@pytest.mark.asyncio
async def test_test_connection_requires_auth(client: AsyncClient) -> None:
    res = await client.post(
        "/api/v1/zigbee/test-connection",
        json={"mqtt_host": "localhost", "mqtt_port": 1883},
    )
    assert res.status_code == 401


@pytest.mark.asyncio
async def test_test_connection_invalid_port(client: AsyncClient, headers: dict) -> None:
    res = await client.post(
        "/api/v1/zigbee/test-connection",
        json={"mqtt_host": "localhost", "mqtt_port": 99999},
        headers=headers,
    )
    assert res.status_code == 422  # pydantic validation error


# ---------------------------------------------------------------------------
# /api/v1/zigbee/import
# ---------------------------------------------------------------------------

_SAMPLE_NODES = [
    {
        "id": "0x00000000",
        "label": "Coordinator",
        "type": "zigbee_coordinator",
        "ieee_address": "0x00000000",
        "friendly_name": "Coordinator",
        "device_type": "Coordinator",
        "model": None,
        "vendor": None,
        "lqi": None,
        "parent_id": None,
    },
    {
        "id": "0x00000001",
        "label": "router_1",
        "type": "zigbee_router",
        "ieee_address": "0x00000001",
        "friendly_name": "router_1",
        "device_type": "Router",
        "model": "CC2530",
        "vendor": "Texas Instruments",
        "lqi": 230,
        "parent_id": "0x00000000",
    },
]

_SAMPLE_EDGES = [
    {"source": "0x00000000", "target": "0x00000001"},
]


@pytest.mark.asyncio
async def test_import_success(client: AsyncClient, headers: dict) -> None:
    with patch("app.api.routes.zigbee.fetch_networkmap") as mock_fetch:
        mock_fetch.return_value = (_SAMPLE_NODES, _SAMPLE_EDGES)
        job_id = await _start_canvas_import(
            client,
            headers,
            {"mqtt_host": "localhost", "mqtt_port": 1883, "base_topic": "zigbee2mqtt"},
        )

    res = await client.get(f"/api/v1/zigbee/import/{job_id}", headers=headers)
    assert res.status_code == 200
    body = res.json()
    assert body["status"] == "done"
    assert body["job_id"] == job_id
    data = body["result"]
    assert data["device_count"] == 2
    assert len(data["nodes"]) == 2
    assert len(data["edges"]) == 1
    coordinator = next(n for n in data["nodes"] if n["type"] == "zigbee_coordinator")
    assert coordinator["ieee_address"] == "0x00000000"


@pytest.mark.asyncio
async def test_import_post_does_not_block_on_the_fetch(
    client: AsyncClient, headers: dict
) -> None:
    """The POST answers 202 without a payload — that is what keeps a slow mesh
    from outliving a reverse proxy's read timeout (issue #380)."""
    with patch("app.api.routes.zigbee.fetch_networkmap") as mock_fetch:
        mock_fetch.return_value = (_SAMPLE_NODES, _SAMPLE_EDGES)
        res = await client.post(
            "/api/v1/zigbee/import",
            json={"mqtt_host": "localhost", "mqtt_port": 1883},
            headers=headers,
        )
    assert res.status_code == 202
    assert set(res.json()) == {"job_id", "status"}


@pytest.mark.asyncio
async def test_import_job_unknown_id_returns_404(
    client: AsyncClient, headers: dict
) -> None:
    res = await client.get("/api/v1/zigbee/import/no-such-job", headers=headers)
    assert res.status_code == 404


@pytest.mark.asyncio
async def test_import_job_requires_auth(client: AsyncClient, headers: dict) -> None:
    with patch("app.api.routes.zigbee.fetch_networkmap") as mock_fetch:
        mock_fetch.return_value = ([], [])
        job_id = await _start_canvas_import(
            client, headers, {"mqtt_host": "localhost", "mqtt_port": 1883}
        )
    res = await client.get(f"/api/v1/zigbee/import/{job_id}")
    assert res.status_code == 401


@pytest.mark.asyncio
async def test_import_with_credentials(client: AsyncClient, headers: dict) -> None:
    with patch("app.api.routes.zigbee.fetch_networkmap") as mock_fetch:
        mock_fetch.return_value = ([], [])
        await _start_canvas_import(
            client,
            headers,
            {
                "mqtt_host": "localhost",
                "mqtt_port": 1883,
                "mqtt_username": "admin",
                "mqtt_password": "secret",
                "base_topic": "z2m",
            },
        )
    mock_fetch.assert_called_once_with(
        mqtt_host="localhost",
        mqtt_port=1883,
        base_topic="z2m",
        username="admin",
        password="secret",
        tls=False,
        tls_insecure=False,
        include_mesh_links=False,
    )


@pytest.mark.asyncio
async def test_import_connection_error_returns_502(client: AsyncClient, headers: dict) -> None:
    with patch("app.api.routes.zigbee.fetch_networkmap") as mock_fetch:
        mock_fetch.side_effect = ConnectionError("broker unreachable")
        job_id = await _start_canvas_import(
            client, headers, {"mqtt_host": "bad-host", "mqtt_port": 1883}
        )
    res = await client.get(f"/api/v1/zigbee/import/{job_id}", headers=headers)
    assert res.status_code == 502
    assert "broker unreachable" in res.json()["detail"]


@pytest.mark.asyncio
async def test_import_timeout_returns_504(client: AsyncClient, headers: dict) -> None:
    with patch("app.api.routes.zigbee.fetch_networkmap") as mock_fetch:
        mock_fetch.side_effect = TimeoutError("timed out")
        job_id = await _start_canvas_import(
            client, headers, {"mqtt_host": "localhost", "mqtt_port": 1883}
        )
    res = await client.get(f"/api/v1/zigbee/import/{job_id}", headers=headers)
    assert res.status_code == 504
    assert "timed out" in res.json()["detail"]


@pytest.mark.asyncio
async def test_import_malformed_payload_returns_422(client: AsyncClient, headers: dict) -> None:
    with patch("app.api.routes.zigbee.fetch_networkmap") as mock_fetch:
        mock_fetch.side_effect = ValueError("malformed response")
        job_id = await _start_canvas_import(
            client, headers, {"mqtt_host": "localhost", "mqtt_port": 1883}
        )
    res = await client.get(f"/api/v1/zigbee/import/{job_id}", headers=headers)
    assert res.status_code == 422


@pytest.mark.asyncio
async def test_import_requires_auth(client: AsyncClient) -> None:
    res = await client.post(
        "/api/v1/zigbee/import",
        json={"mqtt_host": "localhost", "mqtt_port": 1883},
    )
    assert res.status_code == 401


@pytest.mark.asyncio
async def test_import_empty_network(client: AsyncClient, headers: dict) -> None:
    """An empty Zigbee network (coordinator only) is a valid response."""
    with patch("app.api.routes.zigbee.fetch_networkmap") as mock_fetch:
        mock_fetch.return_value = ([], [])
        job_id = await _start_canvas_import(
            client, headers, {"mqtt_host": "localhost", "mqtt_port": 1883}
        )
    res = await client.get(f"/api/v1/zigbee/import/{job_id}", headers=headers)
    assert res.status_code == 200
    data = res.json()["result"]
    assert data["device_count"] == 0
    assert data["nodes"] == []
    assert data["edges"] == []


@pytest.mark.asyncio
async def test_import_missing_mqtt_host(client: AsyncClient, headers: dict) -> None:
    res = await client.post(
        "/api/v1/zigbee/import",
        json={"mqtt_port": 1883},
        headers=headers,
    )
    assert res.status_code == 422


@pytest.mark.asyncio
async def test_import_with_tls_passes_flags(client: AsyncClient, headers: dict) -> None:
    with patch("app.api.routes.zigbee.fetch_networkmap") as mock_fetch:
        mock_fetch.return_value = ([], [])
        await _start_canvas_import(
            client,
            headers,
            {"mqtt_host": "broker.example.com", "mqtt_port": 8883, "mqtt_tls": True},
        )
    kwargs = mock_fetch.call_args.kwargs
    assert kwargs["tls"] is True
    assert kwargs["tls_insecure"] is False


@pytest.mark.asyncio
async def test_import_tls_insecure_requires_tls(client: AsyncClient, headers: dict) -> None:
    res = await client.post(
        "/api/v1/zigbee/import",
        json={
            "mqtt_host": "broker.example.com",
            "mqtt_port": 1883,
            "mqtt_tls": False,
            "mqtt_tls_insecure": True,
        },
        headers=headers,
    )
    assert res.status_code == 422


# ---------------------------------------------------------------------------
# /api/v1/zigbee/import-pending
# ---------------------------------------------------------------------------

_PENDING_NODES = [
    {
        "id": "0xCOORD",
        "label": "Coordinator",
        "type": "zigbee_coordinator",
        "ieee_address": "0xCOORD",
        "friendly_name": "Coordinator",
        "device_type": "Coordinator",
        "model": None,
        "vendor": None,
        "lqi": None,
        "parent_id": None,
    },
    {
        "id": "0xR1",
        "label": "router_1",
        "type": "zigbee_router",
        "ieee_address": "0xR1",
        "friendly_name": "router_1",
        "device_type": "Router",
        "model": "CC2530",
        "vendor": "TI",
        "lqi": 220,
        "parent_id": "0xCOORD",
    },
    {
        "id": "0xE1",
        "label": "bulb_kitchen",
        "type": "zigbee_enddevice",
        "ieee_address": "0xE1",
        "friendly_name": "bulb_kitchen",
        "device_type": "EndDevice",
        "model": "TRADFRI",
        "vendor": "IKEA",
        "lqi": 180,
        "parent_id": "0xR1",
    },
]

_PENDING_EDGES = [
    {"source": "0xCOORD", "target": "0xR1"},
    {"source": "0xR1", "target": "0xE1"},
]


@pytest.mark.asyncio
async def test_import_pending_endpoint_creates_zigbee_scan_run(
    client: AsyncClient, headers: dict
) -> None:
    """Endpoint returns a ScanRun (kind=zigbee, status=running) immediately;
    the actual networkmap fetch + pending persist runs in the background."""
    from unittest.mock import AsyncMock

    with patch(
        "app.api.routes.zigbee._background_zigbee_import",
        new_callable=AsyncMock,
    ):
        res = await client.post(
            "/api/v1/zigbee/import-pending",
            json={"mqtt_host": "localhost", "mqtt_port": 1883},
            headers=headers,
        )
    assert res.status_code == 200
    run = res.json()
    assert run["kind"] == "zigbee"
    assert run["status"] == "running"
    assert run["ranges"] == ["localhost:1883"]


@pytest.mark.asyncio
async def test_persist_pending_import_coordinator_goes_to_pending(
    db_session,
) -> None:
    """Coordinator is no longer auto-placed — it lands in pending like the rest."""
    from sqlalchemy import select

    from app.api.routes.zigbee import _persist_pending_import
    from app.db.models import InventoryDevice, Node

    result = await _persist_pending_import(db_session, _PENDING_NODES, _PENDING_EDGES)
    assert result.device_count == 3
    assert result.pending_created == 3          # coordinator included now
    assert result.pending_updated == 0
    assert result.coordinator is None           # not auto-placed
    assert result.coordinator_already_existed is False
    assert result.links_recorded == 2

    # No canvas Node auto-created for the coordinator.
    nodes = (await db_session.execute(select(Node))).scalars().all()
    assert nodes == []
    # Coordinator sits in the pending inventory.
    coord = (
        await db_session.execute(
            select(InventoryDevice).where(InventoryDevice.ieee_address == "0xCOORD")
        )
    ).scalar_one()
    assert coord.status == "pending"
    assert coord.suggested_type == "zigbee_coordinator"
    assert coord.device_subtype == "Coordinator"


@pytest.mark.asyncio
async def test_persist_pending_import_idempotent_updates_existing(
    db_session,
) -> None:
    from app.api.routes.zigbee import _persist_pending_import

    await _persist_pending_import(db_session, _PENDING_NODES, _PENDING_EDGES)

    bumped = [dict(n) for n in _PENDING_NODES]
    bumped[1]["lqi"] = 99
    result = await _persist_pending_import(db_session, bumped, _PENDING_EDGES)

    assert result.pending_created == 0
    assert result.pending_updated == 3          # coordinator upserts too
    assert result.coordinator_already_existed is False
    assert result.links_recorded == 2


@pytest.mark.asyncio
async def test_persist_pending_import_replaces_links(db_session) -> None:
    from sqlalchemy import select

    from app.api.routes.zigbee import _persist_pending_import
    from app.db.models import InventoryDeviceLink

    await _persist_pending_import(db_session, _PENDING_NODES, _PENDING_EDGES)

    new_edges = [{"source": "0xCOORD", "target": "0xR1"}]
    await _persist_pending_import(db_session, _PENDING_NODES[:2], new_edges)

    rows = (await db_session.execute(select(InventoryDeviceLink))).scalars().all()
    assert len(rows) == 1
    assert (rows[0].source_ieee, rows[0].target_ieee) == ("0xCOORD", "0xR1")


@pytest.mark.asyncio
async def test_persist_pending_import_records_link_lqi(db_session) -> None:
    """LQI measures the link, so it is stored on the link row (#496)."""
    from sqlalchemy import select

    from app.api.routes.zigbee import _persist_pending_import
    from app.db.models import InventoryDeviceLink

    edges = [
        {"source": "0xCOORD", "target": "0xR1", "lqi": 220},
        {"source": "0xR1", "target": "0xE1", "lqi": 0},
    ]
    await _persist_pending_import(db_session, _PENDING_NODES, edges)

    rows = (await db_session.execute(select(InventoryDeviceLink))).scalars().all()
    by_pair = {(r.source_ieee, r.target_ieee): r.lqi for r in rows}
    assert by_pair[("0xCOORD", "0xR1")] == 220
    # A dead link reads 0 — it must survive as 0, not collapse to NULL.
    assert by_pair[("0xR1", "0xE1")] == 0


@pytest.mark.asyncio
async def test_persist_pending_import_link_lqi_defaults_to_none(db_session) -> None:
    """A map that reported no LQI leaves the column NULL rather than guessing."""
    from sqlalchemy import select

    from app.api.routes.zigbee import _persist_pending_import
    from app.db.models import InventoryDeviceLink

    await _persist_pending_import(db_session, _PENDING_NODES, _PENDING_EDGES)

    rows = (await db_session.execute(select(InventoryDeviceLink))).scalars().all()
    assert rows
    assert all(r.lqi is None for r in rows)


@pytest.mark.asyncio
async def test_persist_pending_import_tags_mesh_links_apart(db_session) -> None:
    """Mesh links carry their own discovery_source so the canvas can style them."""
    from sqlalchemy import select

    from app.api.routes.zigbee import _persist_pending_import
    from app.db.models import InventoryDeviceLink

    edges = [
        {"source": "0xCOORD", "target": "0xR1", "lqi": 200, "kind": "tree"},
        {"source": "0xR1", "target": "0xE1", "lqi": 150, "kind": "mesh"},
    ]
    await _persist_pending_import(db_session, _PENDING_NODES, edges)

    rows = (await db_session.execute(select(InventoryDeviceLink))).scalars().all()
    by_pair = {(r.source_ieee, r.target_ieee): r.discovery_source for r in rows}
    assert by_pair[("0xCOORD", "0xR1")] == "zigbee"
    assert by_pair[("0xR1", "0xE1")] == "zigbee_mesh"


@pytest.mark.asyncio
async def test_persist_pending_import_wipes_mesh_links_too(db_session) -> None:
    """The wipe must cover both sources, or mesh links pile up import on import."""
    from sqlalchemy import select

    from app.api.routes.zigbee import _persist_pending_import
    from app.db.models import InventoryDeviceLink

    with_mesh = [
        {"source": "0xCOORD", "target": "0xR1", "kind": "tree"},
        {"source": "0xR1", "target": "0xE1", "kind": "mesh"},
    ]
    await _persist_pending_import(db_session, _PENDING_NODES, with_mesh)
    # Re-import with the option off: the stale mesh row must be gone, not kept.
    await _persist_pending_import(
        db_session, _PENDING_NODES, [{"source": "0xCOORD", "target": "0xR1", "kind": "tree"}]
    )

    rows = (await db_session.execute(select(InventoryDeviceLink))).scalars().all()
    assert len(rows) == 1
    assert rows[0].discovery_source == "zigbee"


@pytest.mark.asyncio
async def test_importing_a_second_network_keeps_the_first_networks_links(db_session) -> None:
    """Regression: the wipe covered every zigbee link, so a second Zigbee2MQTT
    instance imported after the first erased the first one's map."""
    from sqlalchemy import select

    from app.api.routes.zigbee import _persist_pending_import
    from app.db.models import InventoryDeviceLink

    first_edges = [
        {"source": "0xCOORD", "target": "0xR1", "kind": "tree"},
        {"source": "0xR1", "target": "0xE1", "kind": "mesh"},
    ]
    await _persist_pending_import(db_session, _PENDING_NODES, first_edges)

    other = [
        {**_PENDING_NODES[0], "id": "0xCOORD2", "ieee_address": "0xCOORD2"},
        {**_PENDING_NODES[1], "id": "0xR9", "ieee_address": "0xR9"},
    ]
    await _persist_pending_import(
        db_session, other, [{"source": "0xCOORD2", "target": "0xR9", "kind": "tree"}]
    )

    rows = (await db_session.execute(select(InventoryDeviceLink))).scalars().all()
    assert {(r.source_ieee, r.target_ieee, r.discovery_source) for r in rows} == {
        ("0xCOORD", "0xR1", "zigbee"),
        ("0xR1", "0xE1", "zigbee_mesh"),
        ("0xCOORD2", "0xR9", "zigbee"),
    }


@pytest.mark.asyncio
async def test_persist_pending_import_sets_coordinator_pending_fields(db_session) -> None:
    """Coordinator lands in pending carrying its vendor/model metadata."""
    from sqlalchemy import select

    from app.api.routes.zigbee import _persist_pending_import
    from app.db.models import InventoryDevice

    nodes_with_meta = [dict(n) for n in _PENDING_NODES]
    nodes_with_meta[0]["vendor"] = "TI"
    nodes_with_meta[0]["model"] = "CC2652"

    await _persist_pending_import(db_session, nodes_with_meta, _PENDING_EDGES)

    coord = (
        await db_session.execute(
            select(InventoryDevice).where(InventoryDevice.ieee_address == "0xCOORD")
        )
    ).scalar_one()
    assert coord.vendor == "TI"
    assert coord.model == "CC2652"
    assert coord.suggested_type == "zigbee_coordinator"


@pytest.mark.asyncio
async def test_persist_pending_import_backfills_inventory_for_approved_node(
    db_session,
) -> None:
    """A device on canvas but missing its inventory row gets one backfilled
    (status="approved"), so it shows in the inventory list. Node props still
    refresh with the latest Vendor/Model/LQI.
    """
    from sqlalchemy import select

    from app.api.routes.zigbee import _persist_pending_import
    from app.db.models import InventoryDevice, Node

    # Simulate: router was approved earlier → exists as a canvas Node, but with
    # no matching device_inventory row (e.g. a legacy auto-placed device).
    device = InventoryDevice(
        ieee_address="0xR1",
        friendly_name="router_1",
        suggested_type="zigbee_router",
        status="approved",
        status_live="online",
        check_method="none",
        properties=[],
    )
    db_session.add(device)
    await db_session.flush()
    approved = Node(label="router_1", type="zigbee_router", device_id=device.id)
    db_session.add(approved)
    await db_session.commit()

    bumped = [dict(n) for n in _PENDING_NODES]
    bumped[1]["lqi"] = 250  # new LQI from re-import
    await _persist_pending_import(db_session, bumped, _PENDING_EDGES)

    # An inventory row is backfilled as "approved" (it is on a canvas).
    inv = (
        await db_session.execute(
            select(InventoryDevice).where(InventoryDevice.ieee_address == "0xR1")
        )
    ).scalar_one()
    assert inv.status == "approved"
    assert inv.suggested_type == "zigbee_router"
    assert inv.device_subtype == "Router"

    # Node properties got refreshed.
    refreshed = (
        await db_session.execute(
            select(InventoryDevice).where(InventoryDevice.ieee_address == "0xR1")
        )
    ).scalar_one()
    keys = {p["key"]: p["value"] for p in refreshed.properties}
    assert keys == {"IEEE": "0xR1", "Vendor": "TI", "Model": "CC2530", "LQI": "250"}
    # Brand-new props on an existing Node start hidden.
    assert all(p["visible"] is False for p in refreshed.properties)


@pytest.mark.asyncio
async def test_persist_pending_import_preserves_hidden_inventory_for_approved_node(
    db_session,
) -> None:
    """If the on-canvas device already has a hidden inventory row, re-import
    refreshes its metadata but must NOT flip it back to approved/visible."""
    from sqlalchemy import select

    from app.api.routes.zigbee import _persist_pending_import
    from app.db.models import InventoryDevice, Node

    # One row, hidden by the user, and a canvas still draws it.
    drawn = InventoryDevice(
        ieee_address="0xR1", friendly_name="router_1", suggested_type="zigbee_router",
        device_subtype="Router", status="hidden", status_live="online",
        check_method="none", discovery_source="zigbee",
    )
    db_session.add(drawn)
    await db_session.flush()
    db_session.add(Node(label="router_1", type="zigbee_router", device_id=drawn.id))
    await db_session.commit()

    await _persist_pending_import(db_session, _PENDING_NODES, _PENDING_EDGES)

    inv = (
        await db_session.execute(
            select(InventoryDevice).where(InventoryDevice.ieee_address == "0xR1")
        )
    ).scalar_one()
    assert inv.status == "hidden"  # stays hidden


@pytest.mark.asyncio
async def test_persist_pending_import_revives_orphaned_approved_device(
    db_session,
) -> None:
    """Regression for #167: approve → delete node → re-import must re-list device.

    When a device was approved (InventoryDevice.status="approved") and its canvas
    Node was later deleted, the orphaned "approved" row must be reset to
    "pending" on re-import so it shows up in the Pending list again — instead of
    being silently swallowed (re-import reports "found" but Pending stays empty).
    """
    from sqlalchemy import select

    from app.api.routes.zigbee import _persist_pending_import
    from app.db.models import InventoryDevice

    # Simulate prior approve: a InventoryDevice marked approved, but NO matching
    # Node exists (the user deleted the canvas node afterwards).
    orphan = InventoryDevice(
        ieee_address="0xR1",
        friendly_name="router_1",
        hostname="router_1",
        suggested_type="zigbee_router",
        device_subtype="Router",
        model="CC2530",
        vendor="TI",
        lqi=220,
        status="approved",
        discovery_source="zigbee",
    )
    db_session.add(orphan)
    await db_session.commit()

    result = await _persist_pending_import(db_session, _PENDING_NODES, _PENDING_EDGES)

    # No new row created for 0xR1 — the existing one was updated/revived.
    revived = (
        await db_session.execute(
            select(InventoryDevice).where(InventoryDevice.ieee_address == "0xR1")
        )
    ).scalar_one()
    assert revived.status == "pending"
    # Coordinator + end device 0xE1 are brand new → created; router was revived.
    assert result.pending_created == 2
    assert result.pending_updated == 1

    # It is now visible to the Pending list (status filter == "pending").
    listed = (
        await db_session.execute(
            select(InventoryDevice).where(InventoryDevice.status == "pending")
        )
    ).scalars().all()
    assert {p.ieee_address for p in listed} == {"0xCOORD", "0xR1", "0xE1"}


@pytest.mark.asyncio
async def test_persist_pending_import_keeps_hidden_hidden_on_reimport(
    db_session,
) -> None:
    """A user-hidden device must stay hidden on re-import (not revived like #167)."""
    from sqlalchemy import select

    from app.api.routes.zigbee import _persist_pending_import
    from app.db.models import InventoryDevice

    hidden = InventoryDevice(
        ieee_address="0xR1",
        friendly_name="router_1",
        suggested_type="zigbee_router",
        device_subtype="Router",
        status="hidden",
        discovery_source="zigbee",
    )
    db_session.add(hidden)
    await db_session.commit()

    await _persist_pending_import(db_session, _PENDING_NODES, _PENDING_EDGES)

    still_hidden = (
        await db_session.execute(
            select(InventoryDevice).where(InventoryDevice.ieee_address == "0xR1")
        )
    ).scalar_one()
    assert still_hidden.status == "hidden"


@pytest.mark.asyncio
async def test_persist_pending_import_preserves_user_visibility(db_session) -> None:
    """If user has already made props visible, re-import must not flip them back."""
    from sqlalchemy import select

    from app.api.routes.zigbee import _persist_pending_import
    from app.db.models import InventoryDevice, Node

    device = InventoryDevice(
        ieee_address="0xR1",
        friendly_name="router_1",
        suggested_type="zigbee_router",
        status="approved",
        status_live="online",
        check_method="none",
        properties=[
            {"key": "IEEE", "value": "0xR1", "icon": None, "visible": True},
            {"key": "Vendor", "value": "TI", "icon": None, "visible": True},
            {"key": "Custom", "value": "kept", "icon": None, "visible": True},
        ],
    )
    db_session.add(device)
    await db_session.flush()
    approved = Node(label="router_1", type="zigbee_router", device_id=device.id)
    db_session.add(approved)
    await db_session.commit()

    bumped = [dict(n) for n in _PENDING_NODES]
    bumped[1]["lqi"] = 99
    bumped[1]["model"] = "CC2530"
    await _persist_pending_import(db_session, bumped, _PENDING_EDGES)

    refreshed = (
        await db_session.execute(
            select(InventoryDevice).where(InventoryDevice.ieee_address == "0xR1")
        )
    ).scalar_one()
    by_key = {p["key"]: p for p in refreshed.properties}
    # Existing keys keep their visibility (True).
    assert by_key["IEEE"]["visible"] is True
    assert by_key["Vendor"]["visible"] is True
    # New key arrives hidden.
    assert by_key["Model"]["visible"] is False
    assert by_key["LQI"]["visible"] is False
    assert by_key["LQI"]["value"] == "99"
    # Non-zigbee user-added prop is preserved untouched.
    assert by_key["Custom"]["value"] == "kept"
    assert by_key["Custom"]["visible"] is True


@pytest.mark.asyncio
async def test_persist_pending_import_refreshes_approved_coordinator_node(
    db_session,
) -> None:
    """An already-approved coordinator Node gets its props refreshed on
    re-import, and a backfilled inventory row (approved) so it shows in the
    inventory list — this is the legacy auto-placed coordinator scenario."""
    from sqlalchemy import select

    from app.api.routes.zigbee import _persist_pending_import
    from app.db.models import InventoryDevice, Node

    # Legacy: coordinator auto-placed as a canvas Node, no device_inventory row.
    drawn = InventoryDevice(
        ieee_address="0xCOORD", friendly_name="Coordinator",
        suggested_type="zigbee_coordinator", status="approved", status_live="online",
        check_method="none",
    )
    db_session.add(drawn)
    await db_session.flush()
    db_session.add(Node(label="Coordinator", type="zigbee_coordinator", device_id=drawn.id))
    await db_session.commit()

    bumped = [dict(n) for n in _PENDING_NODES]
    bumped[0]["vendor"] = "TI"
    bumped[0]["model"] = "CC2652"
    await _persist_pending_import(db_session, bumped, _PENDING_EDGES)

    coord = (
        await db_session.execute(
            select(InventoryDevice).where(InventoryDevice.ieee_address == "0xCOORD")
        )
    ).scalar_one()
    keys = {p["key"]: p["value"] for p in coord.properties}
    assert keys["Vendor"] == "TI"
    assert keys["Model"] == "CC2652"
    by_key = {p["key"]: p for p in coord.properties}
    assert by_key["Vendor"]["visible"] is False
    # Inventory row backfilled as approved → now visible in the inventory list.
    inv = (
        await db_session.execute(
            select(InventoryDevice).where(InventoryDevice.ieee_address == "0xCOORD")
        )
    ).scalar_one()
    assert inv.status == "approved"
    assert inv.suggested_type == "zigbee_coordinator"


@pytest.mark.asyncio
async def test_persist_pending_import_device_on_multiple_canvases(
    db_session,
) -> None:
    """Regression: a device approved onto TWO designs (one Node each) must not
    crash re-import with MultipleResultsFound — props refresh on both nodes."""
    from sqlalchemy import select

    from app.api.routes.zigbee import _persist_pending_import
    from app.db.models import Design, InventoryDevice, Node

    d1 = Design(name="d1")
    d2 = Design(name="d2")
    drawn = InventoryDevice(
        ieee_address="0xR1", friendly_name="router_1", suggested_type="zigbee_router",
        status="approved", status_live="online", check_method="none",
    )
    db_session.add_all([d1, d2, drawn])
    await db_session.flush()
    for d in (d1, d2):
        db_session.add(Node(
            label="router_1", type="zigbee_router", device_id=drawn.id, design_id=d.id,
        ))
    await db_session.commit()

    bumped = [dict(n) for n in _PENDING_NODES]
    bumped[1]["lqi"] = 240
    # Must not raise.
    await _persist_pending_import(db_session, bumped, _PENDING_EDGES)

    nodes = (
        await db_session.execute(select(Node).where(Node.device_id == drawn.id))
    ).scalars().all()
    assert len(nodes) == 2  # both canvas placements preserved
    # One row behind them, so one refresh serves both canvases.
    await db_session.refresh(drawn)
    for n in [drawn]:
        lqi = {p["key"]: p["value"] for p in n.properties}.get("LQI")
        assert lqi == "240"  # refreshed on every canvas


@pytest.mark.asyncio
async def test_import_pending_requires_auth(client: AsyncClient) -> None:
    res = await client.post(
        "/api/v1/zigbee/import-pending",
        json={"mqtt_host": "localhost", "mqtt_port": 1883},
    )
    assert res.status_code == 401


@pytest.mark.asyncio
async def test_test_connection_with_tls(client: AsyncClient, headers: dict) -> None:
    with patch("app.api.routes.zigbee.test_mqtt_connection") as mock_conn:
        mock_conn.return_value = True
        res = await client.post(
            "/api/v1/zigbee/test-connection",
            json={
                "mqtt_host": "broker.example.com",
                "mqtt_port": 8883,
                "mqtt_tls": True,
                "mqtt_tls_insecure": True,
            },
            headers=headers,
        )
    assert res.status_code == 200
    kwargs = mock_conn.call_args.kwargs
    assert kwargs["tls"] is True
    assert kwargs["tls_insecure"] is True


# ---------------------------------------------------------------------------
# /api/v1/zigbee/config + /sync-now (auto-sync)
# ---------------------------------------------------------------------------

from app.core.config import settings  # noqa: E402


@pytest.fixture
def _restore_zigbee_env():
    """Snapshot + restore the env-only Zigbee connection/activation settings."""
    keys = (
        "zigbee_mqtt_host", "zigbee_mqtt_port", "zigbee_mqtt_username",
        "zigbee_mqtt_password", "zigbee_base_topic", "zigbee_sync_enabled",
        "zigbee_sync_interval",
    )
    saved = {k: getattr(settings, k) for k in keys}
    yield
    for k, v in saved.items():
        setattr(settings, k, v)


@pytest.mark.asyncio
async def test_config_omits_credentials(
    client: AsyncClient, headers: dict, _restore_zigbee_env
) -> None:
    settings.zigbee_mqtt_host = "broker"
    settings.zigbee_mqtt_username = "user"
    settings.zigbee_mqtt_password = "supersecret"
    res = await client.get("/api/v1/zigbee/config", headers=headers)
    assert res.status_code == 200
    assert "supersecret" not in res.text
    assert "user" not in res.text
    assert res.json()["host_configured"] is True


@pytest.mark.asyncio
async def test_config_requires_auth(client: AsyncClient) -> None:
    res = await client.get("/api/v1/zigbee/config")
    assert res.status_code == 401


@pytest.mark.asyncio
async def test_enable_sync_without_host_rejected(
    client: AsyncClient, headers: dict, _restore_zigbee_env
) -> None:
    settings.zigbee_mqtt_host = ""
    res = await client.post(
        "/api/v1/zigbee/config",
        json={"sync_enabled": True, "sync_interval": 600},
        headers=headers,
    )
    assert res.status_code == 400


@pytest.mark.asyncio
async def test_save_config_persists_only_sync_fields(
    client: AsyncClient, headers: dict, _restore_zigbee_env
) -> None:
    settings.zigbee_mqtt_host = "broker"
    saved: dict = {}
    with patch.object(type(settings), "save_overrides", lambda self: saved.update(
        host=self.zigbee_mqtt_host, enabled=self.zigbee_sync_enabled,
        interval=self.zigbee_sync_interval,
    )), patch("app.api.routes.zigbee.set_zigbee_sync_enabled"), \
            patch("app.api.routes.zigbee.reschedule_zigbee_sync"):
        res = await client.post(
            "/api/v1/zigbee/config",
            json={"mqtt_host": "attacker", "sync_enabled": True, "sync_interval": 900},
            headers=headers,
        )
    assert res.status_code == 200
    assert settings.zigbee_mqtt_host == "broker"  # body host ignored
    assert saved == {"host": "broker", "enabled": True, "interval": 900}



@pytest.mark.asyncio
async def test_save_config_persists_mesh_links_opt_in(
    client: AsyncClient, headers: dict, _restore_zigbee_env, monkeypatch
) -> None:
    settings.zigbee_mqtt_host = "broker"
    monkeypatch.setattr(settings, "zigbee_sync_include_mesh_links", False)
    with patch.object(type(settings), "save_overrides", lambda self: None), \
            patch("app.api.routes.zigbee.set_zigbee_sync_enabled"), \
            patch("app.api.routes.zigbee.reschedule_zigbee_sync"):
        res = await client.post(
            "/api/v1/zigbee/config",
            json={"sync_enabled": True, "sync_interval": 900, "include_mesh_links": True},
            headers=headers,
        )
    assert res.status_code == 200
    assert res.json()["include_mesh_links"] is True
    assert settings.zigbee_sync_include_mesh_links is True
    res = await client.get("/api/v1/zigbee/config", headers=headers)
    assert res.json()["include_mesh_links"] is True


@pytest.mark.asyncio
async def test_save_config_mesh_links_defaults_off(
    client: AsyncClient, headers: dict, _restore_zigbee_env, monkeypatch
) -> None:
    settings.zigbee_mqtt_host = "broker"
    monkeypatch.setattr(settings, "zigbee_sync_include_mesh_links", True)
    with patch.object(type(settings), "save_overrides", lambda self: None), \
            patch("app.api.routes.zigbee.set_zigbee_sync_enabled"), \
            patch("app.api.routes.zigbee.reschedule_zigbee_sync"):
        res = await client.post(
            "/api/v1/zigbee/config",
            json={"sync_enabled": False, "sync_interval": 900},
            headers=headers,
        )
    assert res.status_code == 200
    assert settings.zigbee_sync_include_mesh_links is False


@pytest.mark.parametrize("enabled", [True, False])
def test_env_import_request_follows_mesh_links_setting(monkeypatch, enabled: bool) -> None:
    """Auto-sync and /sync-now both build their request here, so the Settings
    toggle reaches the scheduled job and the manual re-sync alike."""
    from app.api.routes.zigbee import env_import_request

    monkeypatch.setattr(settings, "zigbee_mqtt_host", "broker")
    monkeypatch.setattr(settings, "zigbee_sync_include_mesh_links", enabled)
    assert env_import_request().include_mesh_links is enabled

@pytest.mark.asyncio
async def test_sync_now_creates_scan_run(
    client: AsyncClient, headers: dict, _restore_zigbee_env
) -> None:
    settings.zigbee_mqtt_host = "broker"
    with patch("app.api.routes.zigbee._background_zigbee_import", new_callable=AsyncMock):
        res = await client.post("/api/v1/zigbee/sync-now", headers=headers)
    assert res.status_code == 200
    data = res.json()
    assert data["kind"] == "zigbee"
    assert data["status"] == "running"


@pytest.mark.asyncio
async def test_sync_now_rejected_without_host(
    client: AsyncClient, headers: dict, _restore_zigbee_env
) -> None:
    settings.zigbee_mqtt_host = ""
    res = await client.post("/api/v1/zigbee/sync-now", headers=headers)
    assert res.status_code == 400


@pytest.mark.asyncio
async def test_sync_now_requires_auth(client: AsyncClient) -> None:
    res = await client.post("/api/v1/zigbee/sync-now")
    assert res.status_code == 401


@pytest.mark.asyncio
async def test_canvas_import_also_lands_in_device_inventory(
    client: AsyncClient, headers: dict, db_session
) -> None:
    """A canvas import is an import: the devices reach the Device Inventory too."""
    from sqlalchemy import select

    from app.db.models import InventoryDevice

    with patch("app.api.routes.zigbee.fetch_networkmap") as mock_fetch:
        mock_fetch.return_value = (_SAMPLE_NODES, _SAMPLE_EDGES)
        job_id = await _start_canvas_import(
            client, headers, {"mqtt_host": "localhost", "mqtt_port": 1883}
        )

    res = await client.get(f"/api/v1/zigbee/import/{job_id}", headers=headers)
    nodes = res.json()["result"]["nodes"]

    rows = (
        await db_session.execute(
            select(InventoryDevice).where(InventoryDevice.discovery_source == "zigbee")
        )
    ).scalars().all()
    by_ieee = {r.ieee_address: r for r in rows}
    assert set(by_ieee) == {"0x00000000", "0x00000001"}

    # Each node names the row it draws, so the canvas save links to it rather
    # than minting a second, IEEE-less row.
    for node in nodes:
        assert node["device_id"] == by_ieee[node["ieee_address"]].id


@pytest.mark.asyncio
async def test_canvas_import_reuses_the_existing_inventory_row(
    client: AsyncClient, headers: dict, db_session
) -> None:
    from sqlalchemy import select

    from app.db.models import InventoryDevice

    existing = InventoryDevice(
        ieee_address="0x00000001",
        friendly_name="router_1",
        status="pending",
        discovery_source="zigbee",
    )
    db_session.add(existing)
    await db_session.commit()
    existing_id = existing.id

    with patch("app.api.routes.zigbee.fetch_networkmap") as mock_fetch:
        mock_fetch.return_value = (_SAMPLE_NODES, _SAMPLE_EDGES)
        job_id = await _start_canvas_import(
            client, headers, {"mqtt_host": "localhost", "mqtt_port": 1883}
        )

    res = await client.get(f"/api/v1/zigbee/import/{job_id}", headers=headers)
    router = next(
        n for n in res.json()["result"]["nodes"] if n["ieee_address"] == "0x00000001"
    )
    assert router["device_id"] == existing_id
    rows = (
        await db_session.execute(
            select(InventoryDevice).where(InventoryDevice.ieee_address == "0x00000001")
        )
    ).scalars().all()
    assert len(rows) == 1


@pytest.mark.asyncio
async def test_canvas_import_failure_is_reported_on_the_job(
    client: AsyncClient, headers: dict
) -> None:
    """A failed inventory write fails the job rather than returning half a map."""
    with patch("app.api.routes.zigbee.fetch_networkmap") as mock_fetch, patch(
        "app.api.routes.zigbee._persist_pending_import",
        new=AsyncMock(side_effect=ValueError("inventory write failed")),
    ):
        mock_fetch.return_value = (_SAMPLE_NODES, _SAMPLE_EDGES)
        job_id = await _start_canvas_import(
            client, headers, {"mqtt_host": "localhost", "mqtt_port": 1883}
        )

    res = await client.get(f"/api/v1/zigbee/import/{job_id}", headers=headers)
    assert res.status_code == 422
    assert "inventory write failed" in res.json()["detail"]


@pytest.mark.asyncio
async def test_approving_a_device_twice_links_the_second_card(
    client: AsyncClient, headers: dict, db_session
) -> None:
    """Regression for #532: a device drawn twice on one canvas gets its mesh
    link on both cards.

    The second card is a confirmed duplicate (force=True). Resolving the link by
    ieee alone could land on the first card, whose edge already exists, so the
    new card was left without its route.
    """
    import uuid

    from sqlalchemy import select

    from app.db.models import Design, Edge, InventoryDevice, InventoryDeviceLink

    design = Design(id=str(uuid.uuid4()), name="Zigbee")
    db_session.add_all([
        design,
        InventoryDevice(id="d-router", ieee_address="0xROUTER", status="pending"),
        InventoryDevice(id="d-sensor", ieee_address="0xSENSOR", status="pending"),
        InventoryDeviceLink(
            id=str(uuid.uuid4()), source_ieee="0xROUTER", target_ieee="0xSENSOR",
            discovery_source="zigbee",
        ),
    ])
    await db_session.commit()

    async def approve(device_id: str, node_type: str, force: bool = False) -> dict:
        res = await client.post(
            f"/api/v1/scan/pending/{device_id}/approve",
            json={"type": node_type, "label": device_id, "design_id": design.id, "force": force},
            headers=headers,
        )
        assert res.status_code == 200, res.text
        body: dict = res.json()
        return body

    router = await approve("d-router", "zigbee_router")
    first = await approve("d-sensor", "zigbee_enddevice")
    second = await approve("d-sensor", "zigbee_enddevice", force=True)

    assert first["edges_created"] == 1
    assert second["edges_created"] == 1
    assert second["edges"][0]["source"] == router["node_id"]
    assert second["edges"][0]["target"] == second["node_id"]
    edges = (await db_session.execute(select(Edge))).scalars().all()
    assert {(e.source, e.target) for e in edges} == {
        (router["node_id"], first["node_id"]),
        (router["node_id"], second["node_id"]),
    }


@pytest.mark.asyncio
async def test_resolve_links_targets_the_card_just_placed(db_session) -> None:
    """Regression for #532, pinned at the helper: with two cards for one device,
    the lookup by ieee is free to return the card that is already linked. The
    card just placed must be the one that gets the edge.
    """
    import uuid

    from sqlalchemy import select

    from app.api.routes.scan import _resolve_pending_links_for_ieee
    from app.db.models import Design, Edge, InventoryDevice, InventoryDeviceLink, Node

    design = Design(id=str(uuid.uuid4()), name="Zigbee")
    db_session.add_all([
        design,
        InventoryDevice(id="d-router", ieee_address="0xROUTER", status="approved"),
        InventoryDevice(id="d-sensor", ieee_address="0xSENSOR", status="approved"),
        InventoryDeviceLink(
            id=str(uuid.uuid4()), source_ieee="0xROUTER", target_ieee="0xSENSOR",
            discovery_source="zigbee",
        ),
    ])
    await db_session.flush()
    router = Node(type="zigbee_router", label="router", device_id="d-router", design_id=design.id)
    placed = Node(type="zigbee_enddevice", label="new", device_id="d-sensor", design_id=design.id)
    # Inserted after the new card, so an unordered lookup by ieee lands on it.
    linked = Node(type="zigbee_enddevice", label="old", device_id="d-sensor", design_id=design.id)
    for node in (router, placed, linked):
        db_session.add(node)
        await db_session.flush()
    db_session.add(Edge(source=router.id, target=linked.id, type="iot", design_id=design.id))
    await db_session.commit()

    created = await _resolve_pending_links_for_ieee(
        db_session, "0xSENSOR", design.id, self_node=placed
    )

    assert [(e["source"], e["target"]) for e in created] == [(router.id, placed.id)]
    edges = (await db_session.execute(select(Edge))).scalars().all()
    assert {(e.source, e.target) for e in edges} == {(router.id, linked.id), (router.id, placed.id)}


@pytest.mark.asyncio
async def test_second_card_is_linked_after_the_canvas_was_saved(
    client: AsyncClient, headers: dict, db_session
) -> None:
    """Regression for #532 as reported: approve, save the canvas, add the device
    again. The save used to detach the cards from their rows, so the second
    approve found no coordinator to link to.
    """
    import uuid

    from app.db.models import Design, InventoryDevice, InventoryDeviceLink

    design = Design(id=str(uuid.uuid4()), name="Zigbee")
    db_session.add_all([
        design,
        InventoryDevice(id="d-coord", ieee_address="0xCOORD", status="pending"),
        InventoryDevice(id="d-lidl", ieee_address="0xLIDL", status="pending"),
        InventoryDeviceLink(
            id=str(uuid.uuid4()), source_ieee="0xCOORD", target_ieee="0xLIDL",
            discovery_source="zigbee",
        ),
    ])
    await db_session.commit()

    async def approve(device_id: str, node_type: str, force: bool = False) -> dict:
        res = await client.post(
            f"/api/v1/scan/pending/{device_id}/approve",
            json={"type": node_type, "label": device_id, "design_id": design.id, "force": force},
            headers=headers,
        )
        assert res.status_code == 200, res.text
        body: dict = res.json()
        return body

    coord = await approve("d-coord", "zigbee_coordinator")
    first = await approve("d-lidl", "zigbee_router")
    # What a client that never learned the device ids sends: no device_id.
    nodes = [
        {"id": coord["node_id"], "type": "zigbee_coordinator", "label": "Coordinator"},
        {"id": first["node_id"], "type": "zigbee_router", "label": "Lidl PC"},
    ]
    edge = first["edges"][0]
    res = await client.post(
        "/api/v1/canvas/save",
        json={
            "design_id": design.id,
            "nodes": nodes,
            "edges": [{"id": edge["id"], "source": edge["source"], "target": edge["target"], "type": "iot"}],
            "viewport": {},
        },
        headers=headers,
    )
    assert res.status_code == 200, res.text

    second = await approve("d-lidl", "zigbee_router", force=True)

    assert second["edges_created"] == 1
    assert (second["edges"][0]["source"], second["edges"][0]["target"]) == (
        coord["node_id"], second["node_id"],
    )


@pytest.mark.asyncio
async def test_persist_pending_import_records_its_source(db_session) -> None:
    """A row the import creates lists it among its sources, and a re-import puts
    it back on a row a canvas save left as ["canvas"] alone (#532) — the UI files
    a device by that list."""
    from sqlalchemy import select

    from app.api.routes.zigbee import _persist_pending_import
    from app.db.models import InventoryDevice

    await _persist_pending_import(db_session, _PENDING_NODES, _PENDING_EDGES)
    rows = (await db_session.execute(select(InventoryDevice))).scalars().all()
    assert rows and all(r.discovery_sources == ["zigbee"] for r in rows)

    for row in rows:
        row.discovery_sources = ["canvas"]
    await db_session.commit()
    await _persist_pending_import(db_session, _PENDING_NODES, _PENDING_EDGES)

    rows = (await db_session.execute(select(InventoryDevice))).scalars().all()
    assert all(r.discovery_sources == ["canvas", "zigbee"] for r in rows)