Self-hosted homelab infrastructure visualizer — interactive network diagram with live status monitoring
TypeScript diagramhomelabhomelab-infrastructure-observabilitynetworkproxmox-veself-hostedzigbeezwavezwave-js
import { useState } from 'react'
import { render, screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { documentsApi } from '@/api/client'
import { DocEditor } from '../components/DocEditor'
vi.mock('@/api/client', () => ({
documentsApi: { block: vi.fn() },
}))
const api = vi.mocked(documentsApi)
/**
* A stateful host, so the textarea really holds what was typed.
*
* The plain `setup` below keeps `body` fixed and spies on `onChange`, which is
* right for asserting what the editor asked for — but it means the `/` never
* lands in the value, and the slash-replacement arithmetic is measured against
* a value that does contain it.
*/
function setupLive(initial = '') {
const onChange = vi.fn()
function Host() {
const [body, setBody] = useState(initial)
return (
<DocEditor
body={body}
onChange={(next) => { onChange(next); setBody(next) }}
onSave={vi.fn()}
onCancel={vi.fn()}
dirty
saving={false}
deviceId="dev-1"
/>
)
}
render(<Host />)
return { onChange }
}
function setup(overrides: Partial<React.ComponentProps<typeof DocEditor>> = {}) {
const props = {
body: '',
onChange: vi.fn(),
onSave: vi.fn(),
onCancel: vi.fn(),
dirty: false,
saving: false,
deviceId: 'dev-1',
...overrides,
}
render(<DocEditor {...props} />)
return props
}
beforeEach(() => {
vi.clearAllMocks()
})
describe('DocEditor — undo', () => {
const source = () => screen.getByLabelText('Document source') as HTMLTextAreaElement
it('takes back a typed burst', async () => {
const user = userEvent.setup()
setupLive('start')
await user.click(source())
await user.keyboard(' and more')
expect(source().value).toBe('start and more')
await user.keyboard('{Control>}z{/Control}')
expect(source().value).toBe('start')
})
it('takes back a toolbar insertion, and only that', async () => {
const user = userEvent.setup()
setupLive('start')
await user.click(source())
await user.keyboard('!')
await user.click(screen.getByTitle('Bullet list'))
expect(source().value).toBe('start!\n- ')
// The insert hands focus back on the next frame; a real user pressing the
// shortcut is long past it.
await user.click(source())
await user.keyboard('{Control>}z{/Control}')
expect(source().value).toBe('start!')
await user.keyboard('{Control>}z{/Control}')
expect(source().value).toBe('start')
})
it('takes back an inserted block, which the browser could not', async () => {
const user = userEvent.setup()
api.block.mockResolvedValue({ data: { block: 'device-info', markdown: '| IP | 10.0.0.1 |' } } as never)
setupLive('')
await user.click(source())
await user.keyboard('/')
await user.click(await screen.findByText('/device'))
await waitFor(() => expect(source().value).toBe('| IP | 10.0.0.1 |\n'))
// Setting the value from code is exactly what empties the browser's own
// undo stack, so this is the case the editor's history exists for.
await user.click(source())
await user.keyboard('{Control>}z{/Control}')
expect(source().value).toBe('')
})
it('redoes what it took back, on either binding', async () => {
const user = userEvent.setup()
setupLive('start')
await user.click(source())
await user.keyboard('!')
await user.keyboard('{Control>}z{/Control}')
expect(source().value).toBe('start')
await user.keyboard('{Control>}{Shift>}z{/Shift}{/Control}')
expect(source().value).toBe('start!')
await user.keyboard('{Control>}z{/Control}')
await user.keyboard('{Control>}y{/Control}')
expect(source().value).toBe('start!')
})
it('drops the redo once editing carries on down a new branch', async () => {
const user = userEvent.setup()
setupLive('start')
await user.click(source())
await user.keyboard('!')
await user.keyboard('{Control>}z{/Control}')
await user.click(screen.getByTitle('Bullet list'))
await user.click(source())
await user.keyboard('{Control>}{Shift>}z{/Shift}{/Control}')
expect(source().value).toBe('start\n- ')
})
it('does nothing, and changes nothing, with no history behind it', async () => {
const user = userEvent.setup()
const props = setup({ body: 'start' })
await user.click(screen.getByLabelText('Document source'))
await user.keyboard('{Control>}z{/Control}')
expect(props.onChange).not.toHaveBeenCalled()
})
})
describe('DocEditor', () => {
it('shows the source in an editable field', () => {
setup({ body: '# NAS' })
expect(screen.getByLabelText('Document source')).toHaveValue('# NAS')
})
it('renders the preview beside it', () => {
setup({ body: '# NAS' })
expect(screen.getByRole('heading', { name: 'NAS' })).toBeInTheDocument()
})
it('saves on Ctrl+S even when the button is out of reach', async () => {
const user = userEvent.setup()
const props = setup({ body: 'text', dirty: true })
await user.click(screen.getByLabelText('Document source'))
await user.keyboard('{Control>}s{/Control}')
expect(props.onSave).toHaveBeenCalled()
})
it('disables Save until something has changed', () => {
setup({ dirty: false })
expect(screen.getByRole('button', { name: /save/i })).toBeDisabled()
})
it('says so while a save is in flight', () => {
setup({ dirty: true, saving: true })
expect(screen.getByRole('button', { name: /saving/i })).toBeDisabled()
})
it('leaves edit mode on Escape', async () => {
const user = userEvent.setup()
const props = setup()
await user.click(screen.getByLabelText('Document source'))
await user.keyboard('{Escape}')
expect(props.onCancel).toHaveBeenCalled()
})
it('opens the insert menu on a slash at the start of a line', async () => {
const user = userEvent.setup()
setup()
await user.click(screen.getByLabelText('Document source'))
await user.keyboard('/')
expect(await screen.findByLabelText('Insert a block')).toBeInTheDocument()
})
it('leaves a slash mid-sentence alone', async () => {
const user = userEvent.setup()
setup({ body: 'and/or' })
const field = screen.getByLabelText('Document source')
await user.click(field)
// Caret at the end, so the slash is not at the start of a line.
await user.keyboard('{End}/')
expect(screen.queryByLabelText('Insert a block')).not.toBeInTheDocument()
})
it('offers the generated blocks when the document describes a device', async () => {
const user = userEvent.setup()
setup()
await user.click(screen.getByLabelText('Document source'))
await user.keyboard('/')
expect(await screen.findByText('/device')).toBeInTheDocument()
expect(screen.getByText('/services')).toBeInTheDocument()
})
it('offers only the plain snippets for a page that describes nothing', async () => {
const user = userEvent.setup()
setup({ deviceId: null })
await user.click(screen.getByLabelText('Document source'))
await user.keyboard('/')
await screen.findByLabelText('Insert a block')
expect(screen.queryByText('/device')).not.toBeInTheDocument()
expect(screen.getByText('/table')).toBeInTheDocument()
})
it('fetches a generated block and inserts it in place of the slash', async () => {
const user = userEvent.setup()
api.block.mockResolvedValue({ data: { block: 'device-info', markdown: '| IP | 10.0.0.1 |' } } as never)
const props = setup()
await user.click(screen.getByLabelText('Document source'))
await user.keyboard('/')
await user.click(await screen.findByText('/device'))
await waitFor(() => expect(api.block).toHaveBeenCalledWith('device-info', 'dev-1'))
expect(props.onChange).toHaveBeenLastCalledWith('| IP | 10.0.0.1 |\n')
})
it('inserts a plain snippet without calling the server', async () => {
const user = userEvent.setup()
const props = setup()
await user.click(screen.getByLabelText('Document source'))
await user.keyboard('/')
await user.click(await screen.findByText('/task'))
await waitFor(() => expect(props.onChange).toHaveBeenCalledWith('- [ ] \n- [ ] \n'))
expect(api.block).not.toHaveBeenCalled()
})
it('narrows the menu as you type', async () => {
const user = userEvent.setup()
setup()
await user.click(screen.getByLabelText('Document source'))
await user.keyboard('/')
await user.type(await screen.findByLabelText('Insert a block'), 'serv')
expect(screen.getByText('/services')).toBeInTheDocument()
expect(screen.queryByText('/table')).not.toBeInTheDocument()
})
it('closes the menu on Escape without leaving edit mode', async () => {
const user = userEvent.setup()
const props = setup()
await user.click(screen.getByLabelText('Document source'))
await user.keyboard('/')
await screen.findByLabelText('Insert a block')
await user.keyboard('{Escape}')
expect(screen.queryByLabelText('Insert a block')).not.toBeInTheDocument()
expect(props.onCancel).not.toHaveBeenCalled()
})
// ── the slash and its query are consumed by the insert ──────────────────
it('replaces only the slash when nothing was typed after it', async () => {
const user = userEvent.setup()
const { onChange } = setupLive()
// Typed rather than seeded, so the caret is unambiguously at the end.
await user.click(screen.getByLabelText('Document source'))
await user.keyboard('Intro{Enter}/')
await user.click(await screen.findByText('/task'))
await waitFor(() => expect(onChange).toHaveBeenLastCalledWith('Intro\n- [ ] \n- [ ] \n'))
})
it('does not eat the text before the slash when a query was typed', async () => {
const user = userEvent.setup()
const { onChange } = setupLive()
await user.click(screen.getByLabelText('Document source'))
await user.keyboard('Intro{Enter}/')
// The query goes into the menu's own field, never into the document — so it
// is not part of what the insert has to remove.
await user.type(await screen.findByLabelText('Insert a block'), 'task')
await user.click(await screen.findByText('/task'))
await waitFor(() => expect(onChange).toHaveBeenLastCalledWith('Intro\n- [ ] \n- [ ] \n'))
})
})