From e9a4a0908e5bf0ff1cb19efd181bb2f1cf6a5a4e Mon Sep 17 00:00:00 2001 From: jacobyoby Date: Tue, 8 Sep 2026 13:50:15 -0700 Subject: [PATCH] fix(drawio): emit tri-state node tooltips --- NetDiagram-PS/NetDiagram-PS.psm1 | 37 +++++++++++++++++++--------- tests/NetDiagram.Tests.ps1 | 42 +++++++++++++++++++++++++++++++- 2 files changed, 66 insertions(+), 13 deletions(-) diff --git a/NetDiagram-PS/NetDiagram-PS.psm1 b/NetDiagram-PS/NetDiagram-PS.psm1 index e84ea15..44f97ef 100644 --- a/NetDiagram-PS/NetDiagram-PS.psm1 +++ b/NetDiagram-PS/NetDiagram-PS.psm1 @@ -872,21 +872,29 @@ function Export-DrawIO { default { @{ shape='rectangle'; fillColor='#e1e1e1'; strokeColor='#999999' } } } - # Override fill color for reachability status + # Status is tri-state and controls the status color consistently. if ($node.Reachable -eq $true) { + $status = 'Reachable' $shapeConfig.fillColor = '#d5e8d4' $shapeConfig.strokeColor = '#82b366' - } elseif ($node.Reachable -eq $false) { + } + elseif ($node.Reachable -eq $false) { + $status = 'Unreachable' $shapeConfig.fillColor = '#f8cecc' $shapeConfig.strokeColor = '#b85450' } + else { + $status = 'Unknown' + $shapeConfig.fillColor = '#f5f5f5' + $shapeConfig.strokeColor = '#666666' + } # Node label with better formatting (escape entire label for XML) $labelText = $node.Hostname if ($node.IP -ne $node.Hostname) { $labelText += "`n$($node.IP)" } - $label = [System.Security.SecurityElement]::Escape($labelText) + $label = ([System.Security.SecurityElement]::Escape($labelText)) -replace "`n", ' ' # Build enhanced style with shadow and rounded corners $nodeStyle = "shape=$($shapeConfig.shape);rounded=1;whiteSpace=wrap;html=1;align=center;verticalAlign=top;" @@ -901,16 +909,21 @@ function Export-DrawIO { "Vendor: $($node.Vendor)", "OS: $($node.OS)", "Layer: $($node.Layer)", - "Status: $(if ($node.Reachable) { 'Reachable' } else { 'Unreachable' })" + "Status: $status" ) - $tooltip = [System.Security.SecurityElement]::Escape(($tooltipParts -join "`n")) - - $null = $xml.AppendLine(" ") - $null = $xml.AppendLine(" ") - $null = $xml.AppendLine(' ') - - # Add custom metadata as UserObject (best practice) - # Note: In production, this would replace the mxCell with UserObject, but keeping it simple for MVP + $tooltip = ([System.Security.SecurityElement]::Escape(($tooltipParts -join "`n"))) -replace "`n", ' ' + $ipAttribute = [System.Security.SecurityElement]::Escape([string]$node.IP) + $hostnameAttribute = [System.Security.SecurityElement]::Escape([string]$node.Hostname) + $roleAttribute = [System.Security.SecurityElement]::Escape([string]$node.Role) + $vendorAttribute = [System.Security.SecurityElement]::Escape([string]$node.Vendor) + $osAttribute = [System.Security.SecurityElement]::Escape([string]$node.OS) + $layerAttribute = [System.Security.SecurityElement]::Escape([string]$node.Layer) + + $null = $xml.AppendLine(" ") + $null = $xml.AppendLine(" ") + $null = $xml.AppendLine(" ") + $null = $xml.AppendLine(' ') + $null = $xml.AppendLine(' ') } } diff --git a/tests/NetDiagram.Tests.ps1 b/tests/NetDiagram.Tests.ps1 index e321402..189ab87 100644 --- a/tests/NetDiagram.Tests.ps1 +++ b/tests/NetDiagram.Tests.ps1 @@ -554,7 +554,7 @@ Describe 'Export-DrawIO duplicate-IP handling (#8 regression)' { $topology | Export-DrawIO -OutFile $drawioPath -WarningAction SilentlyContinue $xml = [xml](Get-Content -Path $drawioPath -Raw) - $ids = @($xml.SelectNodes('//mxCell') | ForEach-Object { $_.id }) + $ids = @($xml.SelectNodes('//*[@id]') | ForEach-Object { $_.id }) # All mxCell ids must be unique ($ids | Sort-Object -Unique).Count | Should -Be $ids.Count @@ -564,6 +564,46 @@ Describe 'Export-DrawIO duplicate-IP handling (#8 regression)' { } } +Describe 'Export-DrawIO tooltip and reachability metadata (#10 regression)' { + It 'Emits draw.io UserObjects with consistent metadata for all three states' { + $topology = [pscustomobject]@{ + Nodes = @( + [pscustomobject]@{ IP = '192.168.1.1'; Hostname = 'router'; Role = 'router'; Vendor = 'Cisco'; OS = 'IOS'; Layer = 'Core'; Reachable = $true } + [pscustomobject]@{ IP = '192.168.1.2'; Hostname = 'switch'; Role = 'switch'; Vendor = 'Cisco'; OS = 'NX-OS'; Layer = 'Access'; Reachable = $false } + [pscustomobject]@{ IP = '192.168.1.3'; Hostname = 'untested-router'; Role = 'router'; Vendor = 'Unknown'; OS = 'Unknown'; Layer = 'Core'; Reachable = $null } + [pscustomobject]@{ IP = '192.168.1.4'; Hostname = 'untested-switch'; Role = 'switch'; Vendor = 'Unknown'; OS = 'Unknown'; Layer = 'Access'; Reachable = $null } + ) + Edges = @( + [pscustomobject]@{ SourceIP = '192.168.1.1'; TargetIP = '192.168.1.3'; Label = 'Test'; Confidence = 'L3-Inferred' } + ) + Subnets = @() + } + $drawioPath = Join-Path $script:TestDataPath 'status-tooltips.drawio' + + $topology | Export-DrawIO -OutFile $drawioPath + $xml = [xml](Get-Content -Path $drawioPath -Raw) + $objects = @($xml.SelectNodes('//UserObject')) + + $objects | Should -HaveCount 4 + @($objects.status) | Should -Contain 'Reachable' + @($objects.status) | Should -Contain 'Unreachable' + @($objects.status) | Should -Contain 'Unknown' + + $unknown = @($objects | Where-Object { $_.status -eq 'Unknown' }) + $unknown | Should -HaveCount 2 + @($unknown.role) | Should -Contain 'router' + @($unknown.role) | Should -Contain 'switch' + foreach ($node in $unknown) { + $node.tooltip | Should -Match 'Status: Unknown' + $node.mxCell.style | Should -Match 'fillColor=#f5f5f5' + } + + $edge = $xml.SelectSingleNode('//mxCell[@edge="1"]') + @($objects.id) | Should -Contain $edge.source + @($objects.id) | Should -Contain $edge.target + } +} + Describe 'Export-DrawIO subnet container parenting (#9 regression)' { It 'Parents nodes into the matching subnet container instead of the root' { # Test inventory nodes live in 192.168.1.0/24 and there is a matching subnet.