Skip to content

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

fix(inventory): stop a renamed service vanishing from the canvas on screen

Pouzor439d3d5829c6
diff --git a/frontend/src/stores/__tests__/canvasStore/deviceFacts.test.ts b/frontend/src/stores/__tests__/canvasStore/deviceFacts.test.ts
index 207eab2..111ee53 100644
--- a/frontend/src/stores/__tests__/canvasStore/deviceFacts.test.ts
+++ b/frontend/src/stores/__tests__/canvasStore/deviceFacts.test.ts
@@ -141,4 +141,23 @@ describe('canvasStore — applyDeviceFacts keeps each node\'s arrangement', () =
useCanvasStore.getState().applyDeviceFacts('d-1', { services: [ssh] })
expect(useCanvasStore.getState().nodes[0].data.services).toEqual([ssh])
})
+
+ it('redraws a renamed service instead of losing it (#468)', () => {
+ // Correcting a scanner guess in the Device Inventory is a label change, not
+ // a different service — the node keeps drawing it, under the new name.
+ useCanvasStore.getState().loadCanvas([drawnAs('n1', [ssh, kuma])], [])
+ const homepage = { ...kuma, service_name: 'Homepage' }
+ useCanvasStore.getState().applyDeviceFacts('d-1', { services: [ssh, homepage] })
+ expect(useCanvasStore.getState().nodes[0].data.services).toEqual([ssh, homepage])
+ })
+
+ it('does not claim a rename it took from the row as a canvas edit', () => {
+ useCanvasStore.getState().loadCanvas([drawnAs('n1', [ssh, kuma])], [])
+ useCanvasStore.getState().applyDeviceFacts('d-1', {
+ services: [ssh, { ...kuma, service_name: 'Homepage' }],
+ })
+ const { nodes, factsBaseline } = useCanvasStore.getState()
+ expect(changedFactFields(nodes[0].data, factsBaseline.n1)).toEqual([])
+ expect(useCanvasStore.getState().hasUnsavedChanges).toBe(false)
+ })
})
diff --git a/frontend/src/utils/__tests__/deviceFacts.test.ts b/frontend/src/utils/__tests__/deviceFacts.test.ts
index 7568df1..030cd6a 100644
--- a/frontend/src/utils/__tests__/deviceFacts.test.ts
+++ b/frontend/src/utils/__tests__/deviceFacts.test.ts
@@ -10,6 +10,7 @@ import {
deviceFactsToNodeData,
factsBaselineOf,
factsBaselines,
+ listArrangedForNode,
} from '@/utils/deviceFacts'
import type { InventoryEntry, NodeData } from '@/types'
import type { Node } from '@xyflow/react'
@@ -95,6 +96,50 @@ describe('factsBaselines', () => {
})
})
+describe('listArrangedForNode', () => {
+ const ssh = { port: 22, protocol: 'tcp' as const, service_name: 'ssh' }
+ const guessed = { port: 8096, protocol: 'tcp' as const, service_name: 'TCP/8096' }
+ const renamed = { ...guessed, service_name: 'Jellyfin' }
+
+ it('follows a rename rather than hiding the service (#468)', () => {
+ // A service is its port, not its name. Keying on the name dropped the entry
+ // the node drew and appended the renamed one hidden, so correcting a
+ // scanner guess in the Device Inventory made it vanish from the canvas.
+ expect(listArrangedForNode([ssh, guessed], [ssh, renamed])).toEqual([ssh, renamed])
+ })
+
+ it('keeps the node\'s place and flag across a rename', () => {
+ const arranged = listArrangedForNode([{ ...guessed, visible: false }, ssh], [ssh, renamed])
+ expect(arranged).toEqual([{ ...renamed, visible: false }, ssh])
+ })
+
+ it('still tells two services on the same port apart by protocol', () => {
+ const udp = { ...guessed, protocol: 'udp' as const }
+ expect(listArrangedForNode([guessed], [guessed, udp])).toEqual([
+ guessed,
+ { ...udp, visible: false },
+ ])
+ })
+
+ it('keys a port-less service by name — nothing else identifies one', () => {
+ const proxied = { protocol: 'tcp' as const, service_name: 'Vaultwarden', host: 'vault.lan' }
+ const other = { protocol: 'tcp' as const, service_name: 'Gitea', host: 'git.lan' }
+ expect(listArrangedForNode([proxied], [proxied, other])).toEqual([
+ proxied,
+ { ...other, visible: false },
+ ])
+ })
+
+ it('is unchanged for properties, which carry their own key', () => {
+ const rack = { key: 'Rack', value: 'A1', icon: null, visible: true }
+ const owner = { key: 'Owner', value: 'me', icon: null, visible: true }
+ expect(listArrangedForNode([{ ...rack, visible: false }], [rack, owner])).toEqual([
+ { ...rack, visible: false },
+ { ...owner, visible: false },
+ ])
+ })
+})
+
describe('deviceFactsToNodeData', () => {
const row = (over: Partial<InventoryEntry> = {}): InventoryEntry => ({
id: 'd-1',
diff --git a/frontend/src/utils/deviceFacts.ts b/frontend/src/utils/deviceFacts.ts
index 4d2a7f4..f7cd174 100644
--- a/frontend/src/utils/deviceFacts.ts
+++ b/frontend/src/utils/deviceFacts.ts
@@ -47,11 +47,25 @@ const encode = (value: unknown): string => JSON.stringify(value ?? null)
* a service or dragging a property into place is not an edit to the device and
* must not be reported as one — otherwise a rearranged canvas would push its
* arrangement onto every other canvas showing the same device.
+ *
+ * A service is identified by its port and protocol, never by its name — the
+ * same identity the backend's `_service_identity_key` uses. The name is a
+ * label: the scanner guesses it, the user corrects it, and keying on it made a
+ * rename look like a different service. `listArrangedForNode` would then drop
+ * the entry the node drew and append the renamed one hidden, so correcting a
+ * service in the Device Inventory made it vanish from every canvas (issue #468).
+ *
+ * A service with no port keeps the three-part form, name included: nothing else
+ * tells two of those apart.
*/
-const keyOf = (item: Record<string, unknown>): string =>
- 'key' in item
- ? String(item.key ?? '').toLowerCase()
- : `${item.port ?? ''}|${item.protocol ?? ''}|${String(item.service_name ?? '').toLowerCase()}`
+const keyOf = (item: Record<string, unknown>): string => {
+ if ('key' in item) return String(item.key ?? '').toLowerCase()
+ const { port, protocol } = item
+ if (port === undefined || port === null || port === '') {
+ return `None|${protocol ?? ''}|${String(item.service_name ?? '').toLowerCase()}`
+ }
+ return `${port}|${protocol ?? ''}`
+}
const encodeFacts = (field: DeviceFactField, value: unknown): string => {
if (field !== 'services' && field !== 'properties') return encode(value)