Skip to content

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

8.3 KB
Raw Blame
/**
 * A node carries a copy of its inventory row's facts, hydrated on load. Telling
 * "this canvas edited it" apart from "the row moved on since" needs the values
 * the canvas was given — that baseline is what these functions keep.
 */
import { describe, it, expect } from 'vitest'
import {
  DEVICE_FACT_FIELDS,
  changedFactFields,
  deviceFactsToNodeData,
  factsBaselineOf,
  factsBaselines,
  listArrangedForNode,
} from '@/utils/deviceFacts'
import type { InventoryEntry, NodeData } from '@/types'
import type { Node } from '@xyflow/react'

const data = (over: Partial<NodeData> = {}): NodeData => ({
  label: 'NAS',
  type: 'nas',
  status: 'online',
  services: [],
  ip: '10.0.0.5',
  notes: 'in the garage',
  ...over,
})

describe('changedFactFields', () => {
  it('reports nothing when the node still holds what it was given', () => {
    const base = factsBaselineOf(data())
    expect(changedFactFields(data(), base)).toEqual([])
  })

  it('reports only the edited field', () => {
    const base = factsBaselineOf(data())
    expect(changedFactFields(data({ notes: 'moved to the loft' }), base)).toEqual(['notes'])
  })

  it('ignores live status — the checker owns reachability, not a canvas', () => {
    const base = factsBaselineOf(data())
    expect(changedFactFields(data({ status: 'offline' }), base)).toEqual([])
    expect(DEVICE_FACT_FIELDS).not.toContain('status')
  })

  it('compares lists by value, not identity', () => {
    const props = [{ key: 'Rack', value: 'A1', icon: null, visible: true }]
    const base = factsBaselineOf(data({ properties: props }))
    expect(changedFactFields(data({ properties: [...props] }), base)).toEqual([])
    expect(changedFactFields(data({ properties: [] }), base)).toEqual(['properties'])
  })

  it('does not call hiding a service an edit to the device', () => {
    const services = [
      { port: 22, protocol: 'tcp' as const, service_name: 'ssh' },
      { port: 3001, protocol: 'tcp' as const, service_name: 'Uptime Kuma' },
    ]
    const base = factsBaselineOf(data({ services }))
    const hidden = [services[0], { ...services[1], visible: false }]
    // Visibility belongs to this node — pushing it as a device edit would hide
    // the service on every other canvas drawing the same row.
    expect(changedFactFields(data({ services: hidden }), base)).toEqual([])
  })

  it('does not call reordering an edit to the device', () => {
    const props = [
      { key: 'Rack', value: 'A1', icon: null, visible: true },
      { key: 'Owner', value: 'me', icon: null, visible: true },
    ]
    const base = factsBaselineOf(data({ properties: props }))
    expect(changedFactFields(data({ properties: [props[1], props[0]] }), base)).toEqual([])
  })

  it('still reports a real edit to a list item', () => {
    const props = [{ key: 'Rack', value: 'A1', icon: null, visible: true }]
    const base = factsBaselineOf(data({ properties: props }))
    expect(changedFactFields(data({ properties: [{ ...props[0], value: 'B2' }] }), base)).toEqual([
      'properties',
    ])
  })

  it('treats every fact as changed with no baseline — a new node has nothing to revert', () => {
    expect(changedFactFields(data(), undefined)).toEqual([...DEVICE_FACT_FIELDS])
  })
})

describe('factsBaselines', () => {
  it('keys one baseline per node', () => {
    const nodes = [
      { id: 'n1', position: { x: 0, y: 0 }, data: data() },
      { id: 'n2', position: { x: 0, y: 0 }, data: data({ notes: 'rack B' }) },
    ] as Node<NodeData>[]
    const baselines = factsBaselines(nodes)
    expect(Object.keys(baselines)).toEqual(['n1', 'n2'])
    expect(changedFactFields(nodes[1].data, baselines.n2)).toEqual([])
    expect(changedFactFields(nodes[1].data, baselines.n1)).toEqual(['notes'])
  })
})

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' }

  const adminIp = {
    port: 443,
    protocol: 'tcp' as const,
    service_name: 'Pi-hole',
    host: '192.168.1.3',
    path: '/admin',
  }
  const adminDomain = { ...adminIp, host: 'pihole.domain.ch' }

  it('keeps two sites served on one port apart (#503)', () => {
    // A reverse proxy serves both on 443. Keying on the port alone collapsed
    // them, so the node drew one and the canvas saved that shortened list back
    // over the row — deleting the other for good.
    expect(listArrangedForNode([adminIp, adminDomain], [adminIp, adminDomain])).toEqual([
      adminIp,
      adminDomain,
    ])
  })

  it('hides one site without touching the other', () => {
    const arranged = listArrangedForNode(
      [{ ...adminIp, visible: false }, adminDomain],
      [adminIp, adminDomain],
    )
    expect(arranged).toEqual([{ ...adminIp, visible: false }, adminDomain])
  })

  it('follows an edited host rather than hiding the service', () => {
    // The site is part of a service's identity, so editing it in the Device
    // Inventory rewrites the key the node holds — #468 in a new field unless
    // the match falls back to the port.
    const moved = { ...adminIp, host: 'pihole.lan' }
    expect(listArrangedForNode([adminIp], [moved])).toEqual([moved])
  })

  it('brings a second site in hidden rather than mistaking it for an edit', () => {
    const arranged = listArrangedForNode([adminIp], [adminIp, adminDomain])
    expect(arranged).toEqual([adminIp, { ...adminDomain, visible: false }])
  })

  it('reads a path the same way the service URL does', () => {
    // `admin` and `/admin` reach the same URL, so they are one service.
    const typed = { ...adminIp, path: 'admin' }
    expect(listArrangedForNode([typed], [adminIp])).toEqual([adminIp])
  })

  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',
    ip: '10.0.0.5',
    mac: null,
    hostname: 'nas.lan',
    os: null,
    services: [],
    suggested_type: null,
    status: 'approved',
    discovered_at: '2026-01-01T00:00:00Z',
    ...over,
  })

  it('carries the curated facts a node draws', () => {
    const out = deviceFactsToNodeData(row({ label: 'Big NAS', type: 'nas', notes: 'loft' }))
    expect(out.label).toBe('Big NAS')
    expect(out.type).toBe('nas')
    expect(out.notes).toBe('loft')
    expect(out.ip).toBe('10.0.0.5')
  })

  it('leaves the inventory lifecycle out — it is not node data', () => {
    expect(deviceFactsToNodeData(row()).status).toBeUndefined()
  })

  it('falls back through the same name precedence the inventory shows', () => {
    expect(deviceFactsToNodeData(row({ friendly_name: 'Nas box' })).label).toBe('Nas box')
    expect(deviceFactsToNodeData(row()).label).toBe('nas.lan')
  })
})