Skip to content

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

22.5 KB
Raw Blame
import { describe, it, expect, beforeEach } from 'vitest'
import { useCanvasStore } from '@/stores/canvasStore'
import { makeNode } from '@/test/factories'
import { serializeNode, deserializeApiNode, type ApiNode } from '@/utils/canvasSerializer'

function resetStore() {
  useCanvasStore.setState({
    nodes: [],
    edges: [],
    hasUnsavedChanges: false,
    selectedNodeId: null,
    selectedNodeIds: [],
    editingGroupRectId: null,
    editingTextId: null,
    past: [],
    future: [],
    clipboard: { nodes: [], edges: [] },
    serviceStatuses: {},
    floorMap: null,
  })
}

const zone = (id: string, x = 100, y = 100) => ({
  ...makeNode(id, { type: 'groupRect' }),
  type: 'groupRect',
  position: { x, y },
  width: 400,
  height: 300,
})

describe('canvasStore — zone (groupRect) parenting', () => {
  beforeEach(resetStore)

  it('addToZone parents the node and rebases its position on the zone', () => {
    useCanvasStore.setState({
      nodes: [zone('z1'), { ...makeNode('n1'), position: { x: 160, y: 220 } }],
    })
    useCanvasStore.getState().addToZone('z1', 'n1')

    const child = useCanvasStore.getState().nodes.find((n) => n.id === 'n1')!
    expect(child.parentId).toBe('z1')
    expect(child.data.parent_id).toBe('z1')
    expect(child.position).toEqual({ x: 60, y: 120 })
  })

  it('addToZone does not clamp the child with extent, so it can be dragged out', () => {
    useCanvasStore.setState({ nodes: [zone('z1'), makeNode('n1')] })
    useCanvasStore.getState().addToZone('z1', 'n1')
    expect(useCanvasStore.getState().nodes.find((n) => n.id === 'n1')?.extent).toBeUndefined()
  })

  it('addToZone orders the zone before its child (React Flow requirement)', () => {
    useCanvasStore.setState({ nodes: [makeNode('n1'), zone('z1')] })
    useCanvasStore.getState().addToZone('z1', 'n1')
    const ids = useCanvasStore.getState().nodes.map((n) => n.id)
    expect(ids.indexOf('z1')).toBeLessThan(ids.indexOf('n1'))
  })

  it('addToZone is a no-op when the target is not a zone', () => {
    useCanvasStore.setState({ nodes: [makeNode('g1', { type: 'group' }), makeNode('n1')] })
    useCanvasStore.getState().addToZone('g1', 'n1')
    expect(useCanvasStore.getState().nodes.find((n) => n.id === 'n1')?.parentId).toBeUndefined()
  })

  it('addToZone is a no-op when the node is already in that zone', () => {
    useCanvasStore.setState({ nodes: [zone('z1'), makeNode('n1')] })
    useCanvasStore.getState().addToZone('z1', 'n1')
    const first = useCanvasStore.getState().nodes.find((n) => n.id === 'n1')!
    useCanvasStore.getState().addToZone('z1', 'n1')
    expect(useCanvasStore.getState().nodes.find((n) => n.id === 'n1')).toEqual(first)
  })

  it('removeFromGroup releases a zone child back to absolute coords', () => {
    useCanvasStore.setState({
      nodes: [zone('z1'), { ...makeNode('n1'), position: { x: 160, y: 220 } }],
    })
    useCanvasStore.getState().addToZone('z1', 'n1')
    useCanvasStore.getState().removeFromGroup('z1', 'n1')

    const child = useCanvasStore.getState().nodes.find((n) => n.id === 'n1')!
    expect(child.parentId).toBeUndefined()
    expect(child.data.parent_id).toBeUndefined()
    expect(child.position).toEqual({ x: 160, y: 220 })
  })

  it('deleting a zone releases its children instead of deleting them', () => {
    useCanvasStore.setState({
      nodes: [zone('z1'), { ...makeNode('n1'), position: { x: 160, y: 220 } }],
    })
    useCanvasStore.getState().addToZone('z1', 'n1')
    useCanvasStore.getState().deleteNode('z1')

    const nodes = useCanvasStore.getState().nodes
    expect(nodes.map((n) => n.id)).toEqual(['n1'])
    expect(nodes[0].parentId).toBeUndefined()
    expect(nodes[0].position).toEqual({ x: 160, y: 220 })
  })

  it('deleting a container still cascades to its children', () => {
    useCanvasStore.setState({
      nodes: [
        makeNode('px1', { type: 'proxmox', container_mode: true }),
        { ...makeNode('vm1'), parentId: 'px1' },
      ],
    })
    useCanvasStore.getState().deleteNode('px1')
    expect(useCanvasStore.getState().nodes).toHaveLength(0)
  })

  it('addNode nests a node under a zone without clamping it', () => {
    useCanvasStore.getState().addNode(zone('z1'))
    useCanvasStore.getState().addNode({
      ...makeNode('n1', { parent_id: 'z1' }),
      position: { x: 160, y: 220 },
    })
    const child = useCanvasStore.getState().nodes.find((n) => n.id === 'n1')!
    expect(child.parentId).toBe('z1')
    expect(child.extent).toBeUndefined()
    expect(child.position).toEqual({ x: 60, y: 120 })
  })

  it('updateNode can attach a node to a zone and detach it again', () => {
    useCanvasStore.setState({
      nodes: [zone('z1'), { ...makeNode('n1'), position: { x: 160, y: 220 } }],
    })
    useCanvasStore.getState().updateNode('n1', { parent_id: 'z1' })
    const attached = useCanvasStore.getState().nodes.find((n) => n.id === 'n1')!
    expect(attached.parentId).toBe('z1')
    expect(attached.extent).toBeUndefined()

    useCanvasStore.getState().updateNode('n1', { parent_id: undefined })
    const detached = useCanvasStore.getState().nodes.find((n) => n.id === 'n1')!
    expect(detached.parentId).toBeUndefined()
    expect(detached.position).toEqual({ x: 160, y: 220 })
  })
})

describe('canvasStore — batch parenting (#365)', () => {
  beforeEach(resetStore)

  it('addNodesToZone moves the whole selection, not just the first node', () => {
    useCanvasStore.setState({
      nodes: [
        zone('z1'),
        { ...makeNode('n1'), position: { x: 160, y: 220 } },
        { ...makeNode('n2'), position: { x: 200, y: 260 } },
        { ...makeNode('n3'), position: { x: 240, y: 300 } },
      ],
    })
    useCanvasStore.getState().addNodesToZone('z1', ['n1', 'n2', 'n3'])

    const nodes = useCanvasStore.getState().nodes
    for (const id of ['n1', 'n2', 'n3']) {
      const child = nodes.find((n) => n.id === id)!
      expect(child.parentId).toBe('z1')
      expect(child.data.parent_id).toBe('z1')
      expect(child.extent).toBeUndefined()
    }
    expect(nodes.find((n) => n.id === 'n1')!.position).toEqual({ x: 60, y: 120 })
    expect(nodes.find((n) => n.id === 'n3')!.position).toEqual({ x: 140, y: 200 })
  })

  it('undoes a batch add in a single step', () => {
    useCanvasStore.setState({
      nodes: [zone('z1'), makeNode('n1'), makeNode('n2')],
    })
    useCanvasStore.getState().addNodesToZone('z1', ['n1', 'n2'])
    useCanvasStore.getState().undo()

    const nodes = useCanvasStore.getState().nodes
    expect(nodes.find((n) => n.id === 'n1')!.parentId).toBeUndefined()
    expect(nodes.find((n) => n.id === 'n2')!.parentId).toBeUndefined()
  })

  it('skips a child whose own parent is in the same batch', () => {
    useCanvasStore.setState({
      nodes: [
        zone('z1'),
        { ...makeNode('host'), position: { x: 500, y: 500 } },
        {
          ...makeNode('vm'),
          position: { x: 20, y: 20 },
          parentId: 'host',
          data: { ...makeNode('vm').data, parent_id: 'host' },
        },
      ],
    })
    useCanvasStore.getState().addNodesToZone('z1', ['host', 'vm'])

    const nodes = useCanvasStore.getState().nodes
    expect(nodes.find((n) => n.id === 'host')!.parentId).toBe('z1')
    // The VM rides along with its host; re-parenting it would tear it out.
    expect(nodes.find((n) => n.id === 'vm')!.parentId).toBe('host')
    expect(nodes.find((n) => n.id === 'vm')!.position).toEqual({ x: 20, y: 20 })
  })

  it('refuses a node the zone itself descends from', () => {
    useCanvasStore.setState({
      nodes: [
        makeNode('outer'),
        {
          ...zone('z1'),
          parentId: 'outer',
          data: { ...zone('z1').data, parent_id: 'outer' },
        },
        makeNode('n1'),
      ],
    })
    useCanvasStore.getState().addNodesToZone('z1', ['outer', 'n1'])

    const nodes = useCanvasStore.getState().nodes
    expect(nodes.find((n) => n.id === 'outer')!.parentId).toBeUndefined()
    expect(nodes.find((n) => n.id === 'n1')!.parentId).toBe('z1')
  })

  it('is a no-op when nothing in the batch is eligible', () => {
    useCanvasStore.setState({ nodes: [zone('z1'), makeNode('n1')] })
    useCanvasStore.getState().addToZone('z1', 'n1')
    const before = useCanvasStore.getState().nodes
    useCanvasStore.getState().addNodesToZone('z1', ['n1', 'z1'])
    expect(useCanvasStore.getState().nodes).toBe(before)
  })

  it('addNodesToGroup clamps every child inside the group', () => {
    useCanvasStore.setState({
      nodes: [
        { ...makeNode('g1', { type: 'group' }), type: 'group', position: { x: 100, y: 100 } },
        { ...makeNode('n1'), position: { x: 160, y: 220 } },
        { ...makeNode('n2'), position: { x: 100, y: 100 } },
      ],
    })
    useCanvasStore.getState().addNodesToGroup('g1', ['n1', 'n2'])

    const nodes = useCanvasStore.getState().nodes
    expect(nodes.find((n) => n.id === 'n1')!.extent).toBe('parent')
    expect(nodes.find((n) => n.id === 'n1')!.position).toEqual({ x: 60, y: 120 })
    // Dropped on the group's own corner: clamped to the 8px inset.
    expect(nodes.find((n) => n.id === 'n2')!.position).toEqual({ x: 8, y: 8 })
  })

  it('removeNodesFromGroup detaches the whole selection in one undo step', () => {
    const z = zone('z1')
    useCanvasStore.setState({
      nodes: [
        z,
        { ...makeNode('n1'), parentId: 'z1', position: { x: 20, y: 20 }, data: { ...makeNode('n1').data, parent_id: 'z1' } },
        { ...makeNode('n2'), parentId: 'z1', position: { x: 40, y: 40 }, data: { ...makeNode('n2').data, parent_id: 'z1' } },
      ],
    })
    useCanvasStore.getState().removeNodesFromGroup('z1', ['n1', 'n2'])

    let nodes = useCanvasStore.getState().nodes
    expect(nodes.find((n) => n.id === 'n1')!.parentId).toBeUndefined()
    expect(nodes.find((n) => n.id === 'n1')!.position).toEqual({ x: 120, y: 120 })
    expect(nodes.find((n) => n.id === 'n2')!.position).toEqual({ x: 140, y: 140 })

    useCanvasStore.getState().undo()
    nodes = useCanvasStore.getState().nodes
    expect(nodes.find((n) => n.id === 'n1')!.parentId).toBe('z1')
    expect(nodes.find((n) => n.id === 'n2')!.parentId).toBe('z1')
  })

  it('removeNodesFromGroup ignores nodes that are not in that group', () => {
    useCanvasStore.setState({
      nodes: [
        zone('z1'),
        { ...makeNode('n1'), parentId: 'z1', position: { x: 20, y: 20 }, data: { ...makeNode('n1').data, parent_id: 'z1' } },
        makeNode('free'),
      ],
    })
    useCanvasStore.getState().removeNodesFromGroup('z1', ['n1', 'free', 'ghost'])

    const nodes = useCanvasStore.getState().nodes
    expect(nodes.find((n) => n.id === 'n1')!.parentId).toBeUndefined()
    expect(nodes.find((n) => n.id === 'free')!.position).toEqual(makeNode('free').position)
  })

  it('removeNodesFromGroup is a no-op when nothing in the batch is attached', () => {
    useCanvasStore.setState({ nodes: [zone('z1'), makeNode('n1')] })
    const before = useCanvasStore.getState().nodes
    useCanvasStore.getState().removeNodesFromGroup('z1', ['n1'])
    expect(useCanvasStore.getState().nodes).toBe(before)
  })

  it('addNodesToContainer parents every child of a container-mode node', () => {
    useCanvasStore.setState({
      nodes: [
        {
          ...makeNode('px1', { type: 'proxmox', container_mode: true }),
          position: { x: 100, y: 100 },
        },
        { ...makeNode('n1'), position: { x: 160, y: 220 } },
        { ...makeNode('n2'), position: { x: 180, y: 240 } },
      ],
    })
    useCanvasStore.getState().addNodesToContainer('px1', ['n1', 'n2'])

    const nodes = useCanvasStore.getState().nodes
    expect(nodes.find((n) => n.id === 'n1')!.parentId).toBe('px1')
    expect(nodes.find((n) => n.id === 'n2')!.parentId).toBe('px1')
    expect(nodes.find((n) => n.id === 'n2')!.extent).toBe('parent')
  })
})

describe('canvasStore — importZoneSubnet', () => {
  beforeEach(resetStore)

  const device = (id: string, ip: string | undefined, over: Partial<ReturnType<typeof makeNode>> = {}) => ({
    ...makeNode(id, { ip }),
    position: { x: 900, y: 900 },
    ...over,
  })

  it('moves every free device in range into the zone and reports the count', () => {
    useCanvasStore.setState({
      nodes: [zone('z1'), device('n1', '192.168.1.10'), device('n2', '192.168.1.11')],
    })

    const moved = useCanvasStore.getState().importZoneSubnet('z1', '192.168.1.0/24')

    expect(moved).toBe(2)
    const nodes = useCanvasStore.getState().nodes
    expect(nodes.find((n) => n.id === 'n1')!.parentId).toBe('z1')
    expect(nodes.find((n) => n.id === 'n2')!.parentId).toBe('z1')
  })

  it('leaves out-of-range and address-less devices on the canvas', () => {
    useCanvasStore.setState({
      nodes: [zone('z1'), device('in', '192.168.1.10'), device('out', '10.0.0.1'), device('bare', undefined)],
    })

    expect(useCanvasStore.getState().importZoneSubnet('z1', '192.168.1.0/24')).toBe(1)
    const nodes = useCanvasStore.getState().nodes
    expect(nodes.find((n) => n.id === 'out')!.parentId).toBeUndefined()
    expect(nodes.find((n) => n.id === 'bare')!.parentId).toBeUndefined()
  })

  it('never steals a node that already has a parent', () => {
    useCanvasStore.setState({
      nodes: [
        zone('z1'),
        zone('z2', 800, 100),
        { ...device('n1', '192.168.1.10'), parentId: 'z2' },
      ],
    })

    expect(useCanvasStore.getState().importZoneSubnet('z1', '192.168.1.0/24')).toBe(0)
    expect(useCanvasStore.getState().nodes.find((n) => n.id === 'n1')!.parentId).toBe('z2')
  })

  it('never swallows another zone, even one carrying an IP', () => {
    useCanvasStore.setState({
      nodes: [zone('z1'), { ...zone('z2', 800, 100), data: { ...zone('z2').data, ip: '192.168.1.9' } }],
    })

    expect(useCanvasStore.getState().importZoneSubnet('z1', '192.168.1.0/24')).toBe(0)
    expect(useCanvasStore.getState().nodes.find((n) => n.id === 'z2')!.parentId).toBeUndefined()
  })

  it('is a no-op for an invalid CIDR, a missing zone and a non-zone target', () => {
    useCanvasStore.setState({ nodes: [zone('z1'), device('n1', '192.168.1.10'), device('plain', '192.168.1.11')] })
    const before = useCanvasStore.getState().nodes

    expect(useCanvasStore.getState().importZoneSubnet('z1', 'nonsense')).toBe(0)
    expect(useCanvasStore.getState().importZoneSubnet('nope', '192.168.1.0/24')).toBe(0)
    expect(useCanvasStore.getState().importZoneSubnet('plain', '192.168.1.0/24')).toBe(0)
    expect(useCanvasStore.getState().nodes).toBe(before)
  })

  it('lays arrivals out on a non-overlapping grid inside the zone', () => {
    useCanvasStore.setState({
      nodes: [zone('z1'), device('n1', '192.168.1.10'), device('n2', '192.168.1.11')],
    })
    useCanvasStore.getState().importZoneSubnet('z1', '192.168.1.0/24')

    const nodes = useCanvasStore.getState().nodes
    const a = nodes.find((n) => n.id === 'n1')!.position
    const b = nodes.find((n) => n.id === 'n2')!.position
    expect(a).not.toEqual(b)
    // Zone-relative and clear of the label band at the top.
    for (const p of [a, b]) {
      expect(p.x).toBeGreaterThanOrEqual(0)
      expect(p.y).toBeGreaterThanOrEqual(40)
    }
  })

  it('packs around the boxes already inside the zone', () => {
    useCanvasStore.setState({
      nodes: [
        zone('z1'),
        { ...device('sitting', '10.0.0.1'), parentId: 'z1', position: { x: 16, y: 40 }, width: 160, height: 90 },
        device('n1', '192.168.1.10'),
      ],
    })
    useCanvasStore.getState().importZoneSubnet('z1', '192.168.1.0/24')

    const placed = useCanvasStore.getState().nodes.find((n) => n.id === 'n1')!.position
    expect(placed).not.toEqual({ x: 16, y: 40 })
  })

  it('grows the zone when the arrivals overflow its height', () => {
    const many = Array.from({ length: 12 }, (_, i) => device(`n${i}`, `192.168.1.${i + 10}`))
    useCanvasStore.setState({ nodes: [zone('z1'), ...many] })

    useCanvasStore.getState().importZoneSubnet('z1', '192.168.1.0/24')

    const z = useCanvasStore.getState().nodes.find((n) => n.id === 'z1')!
    expect(z.height!).toBeGreaterThan(300)

    // The grown height has to survive a save/load round-trip.
    const wire = serializeNode(z) as unknown as ApiNode
    expect(wire.height).toBe(z.height)
    const reloaded = deserializeApiNode(wire, new Map())
    expect(reloaded.height).toBe(z.height)
  })

  it('keeps the parent ahead of its new children, as React Flow requires', () => {
    useCanvasStore.setState({
      nodes: [device('n1', '192.168.1.10'), zone('z1'), device('n2', '192.168.1.11')],
    })
    useCanvasStore.getState().importZoneSubnet('z1', '192.168.1.0/24')

    const ids = useCanvasStore.getState().nodes.map((n) => n.id)
    expect(ids.indexOf('z1')).toBeLessThan(ids.indexOf('n1'))
    expect(ids.indexOf('z1')).toBeLessThan(ids.indexOf('n2'))
  })

  it('keeps an arrival that is itself a parent ahead of the children it leaves behind', () => {
    // A Proxmox host matches the subnet; its VM does not move, because it
    // already has a parent. The host must still be listed before the VM.
    useCanvasStore.setState({
      nodes: [
        device('proxmox', '192.168.1.10'),
        { ...device('vm1', '10.0.0.1'), parentId: 'proxmox' },
        zone('z1'),
      ],
    })

    expect(useCanvasStore.getState().importZoneSubnet('z1', '192.168.1.0/24')).toBe(1)

    const nodes = useCanvasStore.getState().nodes
    const ids = nodes.map((n) => n.id)
    expect(nodes.find((n) => n.id === 'proxmox')!.parentId).toBe('z1')
    expect(nodes.find((n) => n.id === 'vm1')!.parentId).toBe('proxmox')
    expect(ids.indexOf('z1')).toBeLessThan(ids.indexOf('proxmox'))
    expect(ids.indexOf('proxmox')).toBeLessThan(ids.indexOf('vm1'))
  })

  it('leaves an already-valid order untouched', () => {
    useCanvasStore.setState({
      nodes: [zone('z1'), device('a', '10.0.0.1'), device('b', '10.0.0.2'), device('hit', '192.168.1.10')],
    })
    useCanvasStore.getState().importZoneSubnet('z1', '192.168.1.0/24')

    // Only the matched node moves in the array; the untouched ones keep their
    // relative order.
    const ids = useCanvasStore.getState().nodes.map((n) => n.id)
    expect(ids.indexOf('a')).toBeLessThan(ids.indexOf('b'))
    expect(ids[0]).toBe('z1')
  })

  it('is additive: a second subnet keeps the first import inside', () => {
    useCanvasStore.setState({
      nodes: [zone('z1'), device('n1', '192.168.1.10'), device('n2', '10.0.0.5')],
    })
    useCanvasStore.getState().importZoneSubnet('z1', '192.168.1.0/24')
    useCanvasStore.getState().importZoneSubnet('z1', '10.0.0.0/8')

    const nodes = useCanvasStore.getState().nodes
    expect(nodes.find((n) => n.id === 'n1')!.parentId).toBe('z1')
    expect(nodes.find((n) => n.id === 'n2')!.parentId).toBe('z1')
  })

  it('marks the canvas unsaved and undoes the whole import in one step', () => {
    useCanvasStore.setState({
      nodes: [zone('z1'), device('n1', '192.168.1.10'), device('n2', '192.168.1.11')],
    })
    useCanvasStore.getState().importZoneSubnet('z1', '192.168.1.0/24')
    expect(useCanvasStore.getState().hasUnsavedChanges).toBe(true)

    useCanvasStore.getState().undo()

    const nodes = useCanvasStore.getState().nodes
    expect(nodes.find((n) => n.id === 'n1')!.parentId).toBeUndefined()
    expect(nodes.find((n) => n.id === 'n2')!.parentId).toBeUndefined()
  })
})

describe('canvasStore — nesting order with a container inside a zone', () => {
  beforeEach(resetStore)

  // A zone can hold a container, so the tree is two levels deep. React Flow
  // needs every parent to precede its children in the array; when it doesn't,
  // it drops the child's parent binding — the child loses its extent clamp and
  // drags anywhere on the canvas (#366 follow-up).
  const container = (id: string, parentId?: string) => ({
    ...makeNode(id, { type: 'proxmox', container_mode: true, ...(parentId ? { parent_id: parentId } : {}) }),
    position: { x: 50, y: 50 },
    width: 300,
    height: 200,
    ...(parentId ? { parentId } : {}),
  })
  const nested = (id: string, parentId: string) => ({
    ...makeNode(id, { type: 'vm', parent_id: parentId }),
    position: { x: 20, y: 30 },
    parentId,
    extent: 'parent' as const,
  })
  const order = () => useCanvasStore.getState().nodes.map((n) => n.id)

  it('addNodesToZone keeps the container ahead of its own children', () => {
    useCanvasStore.setState({ nodes: [zone('z1'), container('px'), nested('vm1', 'px')] })
    useCanvasStore.getState().addNodesToZone('z1', ['px'])

    const ids = order()
    expect(ids.indexOf('z1')).toBeLessThan(ids.indexOf('px'))
    expect(ids.indexOf('px')).toBeLessThan(ids.indexOf('vm1'))
    // The nested child rides along untouched: still clamped inside its container.
    const vm = useCanvasStore.getState().nodes.find((n) => n.id === 'vm1')!
    expect(vm.parentId).toBe('px')
    expect(vm.extent).toBe('parent')
  })

  it('loadCanvas reorders a container ahead of its child, not just the parentless nodes', () => {
    // Server order: the nested child was created before the container it now
    // sits in, so both land in the "has a parent" bucket child-first.
    useCanvasStore.getState().loadCanvas([nested('vm1', 'px'), container('px', 'z1'), zone('z1')], [])

    const ids = order()
    expect(ids.indexOf('z1')).toBeLessThan(ids.indexOf('px'))
    expect(ids.indexOf('px')).toBeLessThan(ids.indexOf('vm1'))
  })

  it('applyLayout reorders a container ahead of its child', () => {
    useCanvasStore.getState().applyLayout([nested('vm1', 'px'), container('px', 'z1'), zone('z1')], [])

    const ids = order()
    expect(ids.indexOf('px')).toBeLessThan(ids.indexOf('vm1'))
  })

  it('updateNode re-parenting keeps a nested container ahead of its child', () => {
    useCanvasStore.setState({ nodes: [nested('vm1', 'px'), container('px'), zone('z1')] })
    useCanvasStore.getState().updateNode('px', { parent_id: 'z1' })

    const ids = order()
    expect(ids.indexOf('z1')).toBeLessThan(ids.indexOf('px'))
    expect(ids.indexOf('px')).toBeLessThan(ids.indexOf('vm1'))
    const vm = useCanvasStore.getState().nodes.find((n) => n.id === 'vm1')!
    expect(vm.extent).toBe('parent')
  })

  it('pasteNodes appends a copied container ahead of its copied child', () => {
    useCanvasStore.setState({
      nodes: [
        zone('z1'),
        { ...container('px', 'z1'), selected: true },
        { ...nested('vm1', 'px'), selected: true },
      ],
      selectedNodeIds: ['px', 'vm1'],
    })
    useCanvasStore.getState().copySelectedNodes()
    useCanvasStore.getState().pasteNodes({ x: 600, y: 600 })

    const nodes = useCanvasStore.getState().nodes
    const pasted = nodes.filter((n) => n.selected)
    const parent = pasted.find((n) => n.data.type === 'proxmox')!
    const child = pasted.find((n) => n.data.type === 'vm')!
    expect(child.parentId).toBe(parent.id)
    expect(nodes.indexOf(parent)).toBeLessThan(nodes.indexOf(child))
  })
})