Self-hosted homelab infrastructure visualizer — interactive network diagram with live status monitoring
TypeScript diagramhomelabhomelab-infrastructure-observabilitynetworkproxmox-veself-hostedzigbeezwavezwave-js
fix: remap removed handles to a surviving side (#570)
monsterlagaead15881316
diff --git a/backend/app/api/routes/nodes.py b/backend/app/api/routes/nodes.pyindex c47f165..ac0407a 100644--- a/backend/app/api/routes/nodes.py+++ b/backend/app/api/routes/nodes.py@@ -40,12 +40,20 @@ async def _remap_shrunk_handles(db: AsyncSession, node: Node, sent: dict[str, AnThe canvas store does the same remap client-side (canvasStore.updateNode), sothe UI never hits this; an API client — the MCP write tools — lowers a handlecount without it, and React Flow silently drops an edge whose handle no longer- exists. Removed handles fall back to the side's slot-0 ID, or to 'bottom' when- the side went to zero and has no slot 0 left.+ exists. Removed handles fall back to the side's slot-0 ID, or to a surviving+ side (preferring bottom) when the side went to zero. Reject an update that+ would leave an attached edge with no connection point.Call before the new counts are written to the node."""- fallbacks: dict[str, str] = {}+ counts: dict[str, int] = {}+ for side in SIDES:+ field = handle_count_field(side)+ count = sent.get(field, getattr(node, field, None))+ counts[side] = count if isinstance(count, int) else side_default(side)+ fallback_side = next((side for side in ('bottom', 'top', 'left', 'right') if counts[side] > 0), None)++ fallbacks: dict[str, str | None] = {}for side in SIDES:field = handle_count_field(side)new_count = sent.get(field)@@ -55,15 +63,27 @@ async def _remap_shrunk_handles(db: AsyncSession, node: Node, sent: dict[str, Anold_count = raw_old if isinstance(raw_old, int) else side_default(side)if new_count >= old_count:continue- fallback = 'bottom' if new_count == 0 else handle_id(side, 0)+ fallback = fallback_side if new_count == 0 else handle_id(side, 0)for hid in removed_handle_ids(side, old_count, new_count):fallbacks[hid] = fallback+ # API-created edges can retain the invisible target handle's suffix.+ fallbacks[f"{hid}-t"] = f"{fallback}-t" if fallback is not None else Noneif not fallbacks:returnresult = await db.execute(select(Edge).where(or_(Edge.source == node.id, Edge.target == node.id)))- for edge in result.scalars().all():+ edges = result.scalars().all()+ if fallback_side is None and any(+ (edge.source == node.id and edge.source_handle in fallbacks)+ or (edge.target == node.id and edge.target_handle in fallbacks)+ for edge in edges+ ):+ raise HTTPException(+ status_code=status.HTTP_422_UNPROCESSABLE_CONTENT,+ detail="Cannot remove all connection points from a node with attached edges",+ )+ for edge in edges:if edge.source == node.id and edge.source_handle in fallbacks:edge.source_handle = fallbacks[edge.source_handle]if edge.target == node.id and edge.target_handle in fallbacks:diff --git a/backend/tests/test_nodes.py b/backend/tests/test_nodes.pyindex acdea17..c6716ee 100644--- a/backend/tests/test_nodes.py+++ b/backend/tests/test_nodes.py@@ -1,3 +1,4 @@+import pytestfrom httpx import AsyncClient@@ -635,7 +636,177 @@ async def test_a_side_dropped_to_zero_falls_back_to_bottom(client: AsyncClient,assert (await _edge_by_id(client, headers, edge_id))["source_handle"] == "bottom"-async def test_the_target_end_is_remapped_too(client: AsyncClient, headers: dict):[email protected](("endpoint", "suffix"), [("source", ""), ("target", ""), ("target", "-t")])+ ("counts", "update", "handles", "expected"),+ [+ (+ {"top_handles": 3, "bottom_handles": 0, "right_handles": 2},+ {"top_handles": 0},+ ["top", "top-3"],+ "right",+ ),+ (+ {"top_handles": 3, "bottom_handles": 2, "right_handles": 2},+ {"top_handles": 0, "bottom_handles": 0},+ ["top-3", "bottom-2"],+ "right",+ ),+ (+ {"top_handles": 3, "bottom_handles": 0},+ {"top_handles": 0, "left_handles": 2},+ ["top-3"],+ "left",+ ),+ (+ {"bottom_handles": 3},+ {"bottom_handles": 0},+ ["bottom", "bottom-3"],+ "top",+ ),+ ],+ ids=["right-only", "simultaneous-shrink", "new-side", "default-top"],+)+async def test_zeroed_sides_remap_to_a_surviving_side(+ client: AsyncClient, headers: dict, endpoint: str, suffix: str,+ counts: dict, update: dict, handles: list[str], expected: str+):+ # Regression for #445: bottom is not a fallback if the final counts don't+ # draw it. All counts in the PATCH must be considered together.+ node = await client.post(+ "/api/v1/nodes", json={"type": "switch", "label": "sw1", **counts}, headers=headers+ )+ assert node.status_code == 201+ node_id = node.json()["id"]+ other = await client.post("/api/v1/nodes", json={"type": "nas", "label": "nas1"}, headers=headers)+ other_id = other.json()["id"]+ other_endpoint = "target" if endpoint == "source" else "source"+ edge_ids = []+ for handle in handles:+ edge = await client.post(+ "/api/v1/edges",+ json={endpoint: node_id, other_endpoint: other_id, f"{endpoint}_handle": f"{handle}{suffix}",+ f"{other_endpoint}_handle": "top"},+ headers=headers,+ )+ assert edge.status_code == 201+ edge_ids.append(edge.json()["id"])++ res = await client.patch(f"/api/v1/nodes/{node_id}", json=update, headers=headers)++ assert res.status_code == 200+ assert res.json()[f"{expected}_handles"] > 0+ for edge_id in edge_ids:+ edge = await _edge_by_id(client, headers, edge_id)+ assert edge[f"{endpoint}_handle"] == f"{expected}{suffix}"+ assert edge[f"{other_endpoint}_handle"] == "top"++[email protected](("endpoint", "suffix"), [("source", ""), ("target", ""), ("target", "-t")])+async def test_removing_all_handles_rejects_attached_edges_without_changes(+ client: AsyncClient, headers: dict, endpoint: str, suffix: str+):+ node = await client.post(+ "/api/v1/nodes",+ json={"type": "switch", "label": "sw1", "top_handles": 3, "bottom_handles": 2,+ "port_labels": {"top-3": "uplink"}},+ headers=headers,+ )+ node_id = node.json()["id"]+ other = await client.post("/api/v1/nodes", json={"type": "nas", "label": "nas1"}, headers=headers)+ other_endpoint = "target" if endpoint == "source" else "source"+ edges = []+ for handle in ("top-3", "bottom-2"):+ edge = await client.post(+ "/api/v1/edges",+ json={endpoint: node_id, other_endpoint: other.json()["id"], f"{endpoint}_handle": f"{handle}{suffix}",+ f"{other_endpoint}_handle": "top"},+ headers=headers,+ )+ assert edge.status_code == 201+ edges.append(edge.json())++ res = await client.patch(+ f"/api/v1/nodes/{node_id}",+ json={"top_handles": 0, "bottom_handles": 0, "label": "renamed"},+ headers=headers,+ )++ assert res.status_code == 422+ assert "connection point" in res.json()["detail"]+ assert (await client.get(f"/api/v1/nodes/{node_id}", headers=headers)).json() == node.json()+ for edge in edges:+ assert await _edge_by_id(client, headers, edge["id"]) == edge+++async def test_auto_assigned_target_remaps_to_a_surviving_side(client: AsyncClient, headers: dict):+ source = await client.post(+ "/api/v1/nodes", json={"type": "router", "label": "r1", "pos_y": 0}, headers=headers+ )+ target = await client.post(+ "/api/v1/nodes",+ json={"type": "switch", "label": "sw1", "pos_y": 300,+ "top_handles": 3, "bottom_handles": 0, "right_handles": 2},+ headers=headers,+ )+ edge = await client.post(+ "/api/v1/edges", json={"source": source.json()["id"], "target": target.json()["id"]}, headers=headers+ )+ assert edge.status_code == 201+ assert edge.json()["target_handle"] == "top-t"++ res = await client.patch(+ f"/api/v1/nodes/{target.json()['id']}", json={"top_handles": 0}, headers=headers+ )++ assert res.status_code == 200+ updated_edge = await _edge_by_id(client, headers, edge.json()["id"])+ assert updated_edge["target_handle"] == "right-t"+ assert updated_edge["source_handle"] == edge.json()["source_handle"]+++async def test_removing_all_handles_rejects_auto_assigned_target_without_changes(client: AsyncClient, headers: dict):+ source = await client.post(+ "/api/v1/nodes", json={"type": "router", "label": "r1", "pos_y": 0}, headers=headers+ )+ target = await client.post(+ "/api/v1/nodes",+ json={"type": "switch", "label": "sw1", "pos_y": 300, "port_labels": {"top": "uplink"}},+ headers=headers,+ )+ edge = await client.post(+ "/api/v1/edges", json={"source": source.json()["id"], "target": target.json()["id"]}, headers=headers+ )+ assert edge.status_code == 201+ assert edge.json()["target_handle"] == "top-t"++ res = await client.patch(+ f"/api/v1/nodes/{target.json()['id']}",+ json={"top_handles": 0, "bottom_handles": 0, "label": "renamed"},+ headers=headers,+ )++ assert res.status_code == 422+ assert "connection point" in res.json()["detail"]+ assert (await client.get(f"/api/v1/nodes/{target.json()['id']}", headers=headers)).json() == target.json()+ assert await _edge_by_id(client, headers, edge.json()["id"]) == edge.json()+++async def test_removing_all_handles_without_attached_edges_is_allowed(client: AsyncClient, headers: dict):+ node = await client.post("/api/v1/nodes", json={"type": "switch", "label": "sw1"}, headers=headers)+ node_id = node.json()["id"]++ res = await client.patch(+ f"/api/v1/nodes/{node_id}", json={"top_handles": 0, "bottom_handles": 0}, headers=headers+ )++ assert res.status_code == 200+ assert res.json()["top_handles"] == 0+ assert res.json()["bottom_handles"] == 0++[email protected]("suffix", ["", "-t"])+async def test_the_target_end_is_remapped_too(client: AsyncClient, headers: dict, suffix: str):src = (await client.post("/api/v1/nodes", json={"type": "router", "label": "r1"}, headers=headers)).json()["id"]tgt = (await client.post(@@ -645,7 +816,7 @@ async def test_the_target_end_is_remapped_too(client: AsyncClient, headers: dictedge_id = (await client.post("/api/v1/edges",- json={"source": src, "target": tgt, "source_handle": "bottom", "target_handle": "top-3"},+ json={"source": src, "target": tgt, "source_handle": "bottom", "target_handle": f"top-3{suffix}"},headers=headers,)).json()["id"]@@ -653,7 +824,7 @@ async def test_the_target_end_is_remapped_too(client: AsyncClient, headers: dictawait client.patch(f"/api/v1/nodes/{tgt}", json={"top_handles": 1}, headers=headers)edge = await _edge_by_id(client, headers, edge_id)- assert edge["target_handle"] == "top"+ assert edge["target_handle"] == f"top{suffix}"assert edge["source_handle"] == "bottom"