M5-T07: add latest-reading cards and Recharts trend charts; readings return most-recent rows
This commit is contained in:
@@ -0,0 +1,256 @@
|
||||
/**
|
||||
* Tests for energy/EnergyCharts.tsx
|
||||
*
|
||||
* Coverage:
|
||||
* 1. Renders chart title with device name.
|
||||
* 2. Shows loading state while readings/metrics are loading.
|
||||
* 3. Shows error state when either query fails.
|
||||
* 4. Shows empty state when no readings are in the time window.
|
||||
* 5. Renders chart container when data is available.
|
||||
* 6. Tolerates missing keys in payload (no crash when key absent).
|
||||
* 7. Time-range segmented control is rendered.
|
||||
*
|
||||
* NOTE: Recharts itself is NOT mocked — we let it render (jsdom-compatible
|
||||
* rendering), but we only assert on data-testid wrappers, not Recharts internals.
|
||||
* Recharts SVG rendering may produce warnings in jsdom; these are expected and
|
||||
* benign (jsdom lacks ResizeObserver / SVG layout).
|
||||
*/
|
||||
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
import { screen, waitFor } from '@testing-library/react'
|
||||
import { renderWithProviders } from '../test-utils'
|
||||
import { EnergyCharts } from './EnergyCharts'
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Fixtures
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
const UUID = 'device-uuid-test'
|
||||
const DEVICE_NAME = 'SDM120 Main'
|
||||
|
||||
const METRICS_RESP = {
|
||||
profile: 'sdm120',
|
||||
metrics: [
|
||||
{ key: 'voltage', label: 'Voltage', unit: 'V', device_class: 'voltage' },
|
||||
{ key: 'current', label: 'Current', unit: 'A', device_class: 'current' },
|
||||
],
|
||||
}
|
||||
|
||||
const READING = {
|
||||
recorded_at: '2026-06-22T10:00:00Z',
|
||||
payload: { voltage: 230.2, current: 1.3 },
|
||||
}
|
||||
|
||||
const READINGS_RESP = {
|
||||
items: [READING],
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Mock apiClient
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
const mockGet = vi.fn()
|
||||
|
||||
vi.mock('../api/client', () => ({
|
||||
default: {
|
||||
GET: (...args: unknown[]) => mockGet(...args),
|
||||
POST: vi.fn(),
|
||||
PATCH: vi.fn(),
|
||||
DELETE: vi.fn(),
|
||||
},
|
||||
ApiError: class ApiError extends Error {
|
||||
status: number
|
||||
body: unknown
|
||||
constructor(status: number, body: unknown) {
|
||||
super(`API error ${status}`)
|
||||
this.name = 'ApiError'
|
||||
this.status = status
|
||||
this.body = body
|
||||
}
|
||||
},
|
||||
registerLoginRedirect: vi.fn(),
|
||||
}))
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Helpers
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
function setupDefaultMocks() {
|
||||
mockGet.mockImplementation((path: string) => {
|
||||
if (path === '/api/modbus/devices/{uuid}/metrics') {
|
||||
return Promise.resolve({ data: METRICS_RESP })
|
||||
}
|
||||
if (path === '/api/modbus/devices/{uuid}/readings') {
|
||||
return Promise.resolve({ data: READINGS_RESP })
|
||||
}
|
||||
return Promise.resolve({ data: null })
|
||||
})
|
||||
}
|
||||
|
||||
function renderChart() {
|
||||
return renderWithProviders(
|
||||
<EnergyCharts uuid={UUID} deviceName={DEVICE_NAME} />,
|
||||
{ initialPath: '/energy' },
|
||||
)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Tests
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
describe('EnergyCharts — rendering', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
setupDefaultMocks()
|
||||
})
|
||||
|
||||
it('renders chart title with device name', async () => {
|
||||
renderChart()
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByTestId(`energy-charts-title-${UUID}`)).toBeInTheDocument()
|
||||
})
|
||||
|
||||
expect(screen.getByTestId(`energy-charts-title-${UUID}`).textContent).toBe(DEVICE_NAME)
|
||||
})
|
||||
|
||||
it('renders time-range segmented control', async () => {
|
||||
renderChart()
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByTestId(`energy-charts-preset-${UUID}`)).toBeInTheDocument()
|
||||
})
|
||||
})
|
||||
|
||||
it('shows loading state while data is being fetched', () => {
|
||||
// Never resolve
|
||||
mockGet.mockReturnValue(new Promise(() => {}))
|
||||
renderChart()
|
||||
|
||||
expect(screen.getByTestId(`energy-charts-loading-${UUID}`)).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('shows error state when readings query fails', async () => {
|
||||
mockGet.mockRejectedValue(new Error('Network error'))
|
||||
renderChart()
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByTestId(`energy-charts-error-${UUID}`)).toBeInTheDocument()
|
||||
})
|
||||
})
|
||||
|
||||
it('shows empty state when no readings in time window', async () => {
|
||||
mockGet.mockImplementation((path: string) => {
|
||||
if (path === '/api/modbus/devices/{uuid}/metrics') {
|
||||
return Promise.resolve({ data: METRICS_RESP })
|
||||
}
|
||||
if (path === '/api/modbus/devices/{uuid}/readings') {
|
||||
return Promise.resolve({ data: { items: [] } })
|
||||
}
|
||||
return Promise.resolve({ data: null })
|
||||
})
|
||||
|
||||
renderChart()
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByTestId(`energy-charts-empty-${UUID}`)).toBeInTheDocument()
|
||||
})
|
||||
})
|
||||
|
||||
it('renders chart container when data is available', async () => {
|
||||
renderChart()
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByTestId(`energy-charts-chart-${UUID}`)).toBeInTheDocument()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('EnergyCharts — truncation notice', () => {
|
||||
beforeEach(() => vi.clearAllMocks())
|
||||
|
||||
it('shows truncation notice when readings count equals the limit cap (1000)', async () => {
|
||||
// Build exactly 1000 synthetic readings to trigger the truncation hint.
|
||||
const truncatedItems = Array.from({ length: 1000 }, (_, i) => ({
|
||||
recorded_at: new Date(Date.now() - (999 - i) * 5000).toISOString(),
|
||||
payload: { voltage: 230 + i * 0.01 },
|
||||
}))
|
||||
|
||||
mockGet.mockImplementation((path: string) => {
|
||||
if (path === '/api/modbus/devices/{uuid}/metrics') {
|
||||
return Promise.resolve({ data: METRICS_RESP })
|
||||
}
|
||||
if (path === '/api/modbus/devices/{uuid}/readings') {
|
||||
return Promise.resolve({ data: { items: truncatedItems } })
|
||||
}
|
||||
return Promise.resolve({ data: null })
|
||||
})
|
||||
|
||||
renderChart()
|
||||
|
||||
await waitFor(() => {
|
||||
expect(
|
||||
screen.getByTestId(`energy-charts-truncated-${UUID}`),
|
||||
).toBeInTheDocument()
|
||||
})
|
||||
})
|
||||
|
||||
it('does NOT show truncation notice when readings count is below limit', async () => {
|
||||
// Default mock has exactly 1 item — well below 1000.
|
||||
setupDefaultMocks()
|
||||
renderChart()
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByTestId(`energy-charts-chart-${UUID}`)).toBeInTheDocument()
|
||||
})
|
||||
|
||||
expect(screen.queryByTestId(`energy-charts-truncated-${UUID}`)).not.toBeInTheDocument()
|
||||
})
|
||||
})
|
||||
|
||||
describe('EnergyCharts — payload key tolerance', () => {
|
||||
beforeEach(() => vi.clearAllMocks())
|
||||
|
||||
it('does not crash when payload is missing a metric key', async () => {
|
||||
mockGet.mockImplementation((path: string) => {
|
||||
if (path === '/api/modbus/devices/{uuid}/metrics') {
|
||||
return Promise.resolve({ data: METRICS_RESP })
|
||||
}
|
||||
if (path === '/api/modbus/devices/{uuid}/readings') {
|
||||
// payload is missing 'current' key
|
||||
return Promise.resolve({
|
||||
data: { items: [{ recorded_at: '2026-06-22T10:00:00Z', payload: { voltage: 230.2 } }] },
|
||||
})
|
||||
}
|
||||
return Promise.resolve({ data: null })
|
||||
})
|
||||
|
||||
// Should render without throwing
|
||||
expect(() => renderChart()).not.toThrow()
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByTestId(`energy-charts-${UUID}`)).toBeInTheDocument()
|
||||
})
|
||||
})
|
||||
|
||||
it('does not crash when payload is entirely empty', async () => {
|
||||
mockGet.mockImplementation((path: string) => {
|
||||
if (path === '/api/modbus/devices/{uuid}/metrics') {
|
||||
return Promise.resolve({ data: METRICS_RESP })
|
||||
}
|
||||
if (path === '/api/modbus/devices/{uuid}/readings') {
|
||||
return Promise.resolve({
|
||||
data: { items: [{ recorded_at: '2026-06-22T10:00:00Z', payload: {} }] },
|
||||
})
|
||||
}
|
||||
return Promise.resolve({ data: null })
|
||||
})
|
||||
|
||||
expect(() => renderChart()).not.toThrow()
|
||||
|
||||
// All values null → empty state (no chart) or chart with no data points shown.
|
||||
await waitFor(() => {
|
||||
expect(screen.getByTestId(`energy-charts-${UUID}`)).toBeInTheDocument()
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,333 @@
|
||||
/**
|
||||
* EnergyCharts — self-contained Recharts wrapper for Modbus device trend charts.
|
||||
*
|
||||
* Design decisions (M5 decision 11):
|
||||
* - Recharts is imported ONLY in this file (isolation, easy to swap later).
|
||||
* - The component is fully self-contained: it owns its own data fetching via
|
||||
* useReadings / useMetrics hooks and renders loading/error/empty states.
|
||||
* - Time-range selection is internal; readings are always fetched with a window
|
||||
* + limit cap — never a full-table pull.
|
||||
* - Metric labels and units come from GET /metrics; missing keys in payload are
|
||||
* tolerated (a null data point is emitted so the line simply has a gap).
|
||||
*/
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Recharts imports — keep ALL recharts imports inside this file.
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
import {
|
||||
LineChart,
|
||||
Line,
|
||||
XAxis,
|
||||
YAxis,
|
||||
CartesianGrid,
|
||||
Tooltip as RechartsTooltip,
|
||||
Legend,
|
||||
ResponsiveContainer,
|
||||
} from 'recharts'
|
||||
|
||||
import { useState, useMemo } from 'react'
|
||||
import {
|
||||
Stack,
|
||||
Group,
|
||||
Text,
|
||||
Loader,
|
||||
Alert,
|
||||
SegmentedControl,
|
||||
Paper,
|
||||
Title,
|
||||
Badge,
|
||||
Box,
|
||||
} from '@mantine/core'
|
||||
|
||||
import { useReadings, useMetrics } from './hooks'
|
||||
import type { MetricInfo } from './hooks'
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Constants
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/** Must match the limit passed to useReadings below. */
|
||||
const CHART_READINGS_LIMIT = 1000
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Types
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
interface EnergyChartsProps {
|
||||
/** UUID of the Modbus device to chart. */
|
||||
uuid: string
|
||||
/** Friendly name for the chart title. */
|
||||
deviceName: string
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Time-range presets
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
interface TimePreset {
|
||||
label: string
|
||||
value: string
|
||||
/** How many milliseconds of history to show. */
|
||||
spanMs: number
|
||||
}
|
||||
|
||||
const TIME_PRESETS: TimePreset[] = [
|
||||
{ label: '1 h', value: '1h', spanMs: 60 * 60 * 1000 },
|
||||
{ label: '6 h', value: '6h', spanMs: 6 * 60 * 60 * 1000 },
|
||||
{ label: '24 h', value: '24h', spanMs: 24 * 60 * 60 * 1000 },
|
||||
]
|
||||
|
||||
const DEFAULT_PRESET = '1h'
|
||||
|
||||
/** Derive ISO start/end strings for a given preset. */
|
||||
function presetWindow(spanMs: number): { start: string; end: string } {
|
||||
const end = new Date()
|
||||
const start = new Date(end.getTime() - spanMs)
|
||||
return { start: start.toISOString(), end: end.toISOString() }
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Metric colour palette — cycles through a fixed set
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
const LINE_COLORS = [
|
||||
'#4c9cdb',
|
||||
'#f59f00',
|
||||
'#51cf66',
|
||||
'#f03e3e',
|
||||
'#cc5de8',
|
||||
'#20c997',
|
||||
'#fd7e14',
|
||||
'#74c0fc',
|
||||
]
|
||||
|
||||
function lineColor(index: number): string {
|
||||
return LINE_COLORS[index % LINE_COLORS.length]
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Helper: format a recorded_at timestamp for the X-axis tick
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
function formatTimeTick(isoString: string): string {
|
||||
try {
|
||||
const d = new Date(isoString)
|
||||
return d.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })
|
||||
} catch {
|
||||
return isoString
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Helper: safe numeric read from payload — returns null if key absent/non-numeric
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
function safePayloadValue(
|
||||
payload: Record<string, unknown> | null | undefined,
|
||||
key: string,
|
||||
): number | null {
|
||||
if (!payload) return null
|
||||
const raw = payload[key]
|
||||
if (raw === null || raw === undefined) return null
|
||||
const n = Number(raw)
|
||||
return Number.isFinite(n) ? n : null
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// EnergyCharts component
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export function EnergyCharts({ uuid, deviceName }: EnergyChartsProps) {
|
||||
const [activePreset, setActivePreset] = useState<string>(DEFAULT_PRESET)
|
||||
|
||||
const selectedPreset = TIME_PRESETS.find((p) => p.value === activePreset) ?? TIME_PRESETS[0]
|
||||
const { start, end } = useMemo(
|
||||
() => presetWindow(selectedPreset.spanMs),
|
||||
// Recompute whenever the preset changes; intentionally excludes Date.now()
|
||||
// so the window doesn't drift on every render — it only resets on tab change.
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
[activePreset],
|
||||
)
|
||||
|
||||
const readingsQuery = useReadings(uuid, { start, end, limit: CHART_READINGS_LIMIT })
|
||||
const metricsQuery = useMetrics(uuid)
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Derive chart data: [{recorded_at, voltage: 230.2, current: 1.3, ...}, ...]
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
const metricsData = metricsQuery.data?.metrics
|
||||
|
||||
const chartData = useMemo(() => {
|
||||
const metrics: MetricInfo[] = metricsData ?? []
|
||||
const readings = readingsQuery.data?.items ?? []
|
||||
return readings.map((row) => {
|
||||
const point: Record<string, string | number | null> = {
|
||||
recorded_at: row.recorded_at,
|
||||
}
|
||||
for (const m of metrics) {
|
||||
// Tolerate missing keys — null produces a gap in the line, not a crash.
|
||||
point[m.key] = safePayloadValue(row.payload as Record<string, unknown>, m.key)
|
||||
}
|
||||
return point
|
||||
})
|
||||
}, [readingsQuery.data, metricsData])
|
||||
|
||||
const metrics: MetricInfo[] = metricsQuery.data?.metrics ?? []
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// States: loading / error / empty
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
const isLoading = readingsQuery.isLoading || metricsQuery.isLoading
|
||||
const isError = readingsQuery.isError || metricsQuery.isError
|
||||
|
||||
/**
|
||||
* True when the returned row count equals the limit cap, meaning the window
|
||||
* contains more data than was fetched. The backend returns the most-recent N
|
||||
* rows in this case, so the chart shows the latest segment — but a hint is
|
||||
* shown so the user knows the full window is not displayed.
|
||||
*/
|
||||
const isTruncated =
|
||||
!isLoading &&
|
||||
!isError &&
|
||||
(readingsQuery.data?.items.length ?? 0) >= CHART_READINGS_LIMIT
|
||||
|
||||
return (
|
||||
<Paper withBorder p="md" data-testid={`energy-charts-${uuid}`}>
|
||||
<Stack gap="sm">
|
||||
{/* Header row */}
|
||||
<Group justify="space-between" align="center" wrap="nowrap">
|
||||
<Title order={4} data-testid={`energy-charts-title-${uuid}`}>
|
||||
{deviceName}
|
||||
</Title>
|
||||
<SegmentedControl
|
||||
size="xs"
|
||||
value={activePreset}
|
||||
onChange={setActivePreset}
|
||||
data={TIME_PRESETS.map((p) => ({ value: p.value, label: p.label }))}
|
||||
data-testid={`energy-charts-preset-${uuid}`}
|
||||
/>
|
||||
</Group>
|
||||
|
||||
{/* Loading */}
|
||||
{isLoading && (
|
||||
<Group justify="center" py="lg" data-testid={`energy-charts-loading-${uuid}`}>
|
||||
<Loader size="sm" />
|
||||
<Text size="sm" c="dimmed">
|
||||
Loading readings…
|
||||
</Text>
|
||||
</Group>
|
||||
)}
|
||||
|
||||
{/* Error */}
|
||||
{!isLoading && isError && (
|
||||
<Alert color="red" data-testid={`energy-charts-error-${uuid}`}>
|
||||
Failed to load readings or metrics. Please try again.
|
||||
</Alert>
|
||||
)}
|
||||
|
||||
{/* Empty */}
|
||||
{!isLoading && !isError && chartData.length === 0 && (
|
||||
<Text
|
||||
size="sm"
|
||||
c="dimmed"
|
||||
ta="center"
|
||||
py="lg"
|
||||
data-testid={`energy-charts-empty-${uuid}`}
|
||||
>
|
||||
No readings in this time window.
|
||||
</Text>
|
||||
)}
|
||||
|
||||
{/* Truncation notice — shown when window has more data than the fetch limit */}
|
||||
{isTruncated && (
|
||||
<Text
|
||||
size="xs"
|
||||
c="dimmed"
|
||||
ta="right"
|
||||
data-testid={`energy-charts-truncated-${uuid}`}
|
||||
>
|
||||
显示最近 {CHART_READINGS_LIMIT} 条;完整长时段走势待降采样
|
||||
</Text>
|
||||
)}
|
||||
|
||||
{/* Chart */}
|
||||
{!isLoading && !isError && chartData.length > 0 && (
|
||||
<Box data-testid={`energy-charts-chart-${uuid}`}>
|
||||
{/* Render one chart per metric for clarity (avoids mixed Y-axis units) */}
|
||||
{metrics.map((metric, idx) => {
|
||||
// Check if this metric has any non-null data points; skip if all null.
|
||||
const hasData = chartData.some((pt) => pt[metric.key] !== null)
|
||||
if (!hasData) return null
|
||||
|
||||
return (
|
||||
<Box key={metric.key} mb="md">
|
||||
<Group gap="xs" mb={4}>
|
||||
<Text size="xs" fw={600} c="dimmed">
|
||||
{metric.label}
|
||||
</Text>
|
||||
{metric.unit && (
|
||||
<Badge size="xs" variant="outline" color="gray">
|
||||
{metric.unit}
|
||||
</Badge>
|
||||
)}
|
||||
</Group>
|
||||
<ResponsiveContainer width="100%" height={160}>
|
||||
<LineChart
|
||||
data={chartData}
|
||||
margin={{ top: 4, right: 8, left: 0, bottom: 0 }}
|
||||
>
|
||||
<CartesianGrid strokeDasharray="3 3" opacity={0.4} />
|
||||
<XAxis
|
||||
dataKey="recorded_at"
|
||||
tickFormatter={formatTimeTick}
|
||||
tick={{ fontSize: 10 }}
|
||||
minTickGap={40}
|
||||
/>
|
||||
<YAxis
|
||||
tick={{ fontSize: 10 }}
|
||||
width={50}
|
||||
tickFormatter={(v: number) =>
|
||||
metric.unit ? `${v} ${metric.unit}` : String(v)
|
||||
}
|
||||
/>
|
||||
<RechartsTooltip
|
||||
formatter={(value) => {
|
||||
const n = value as number | null | undefined
|
||||
if (n == null) return ['–', metric.label] as [string, string]
|
||||
return [
|
||||
`${n}${metric.unit ? ' ' + metric.unit : ''}`,
|
||||
metric.label,
|
||||
] as [string, string]
|
||||
}}
|
||||
labelFormatter={(label) => {
|
||||
try {
|
||||
return new Date(String(label)).toLocaleString()
|
||||
} catch {
|
||||
return String(label)
|
||||
}
|
||||
}}
|
||||
/>
|
||||
<Legend />
|
||||
<Line
|
||||
type="monotone"
|
||||
dataKey={metric.key}
|
||||
name={metric.label}
|
||||
stroke={lineColor(idx)}
|
||||
dot={false}
|
||||
isAnimationActive={false}
|
||||
connectNulls={false}
|
||||
/>
|
||||
</LineChart>
|
||||
</ResponsiveContainer>
|
||||
</Box>
|
||||
)
|
||||
})}
|
||||
</Box>
|
||||
)}
|
||||
</Stack>
|
||||
</Paper>
|
||||
)
|
||||
}
|
||||
@@ -212,3 +212,136 @@ describe('useTestReadDevice', () => {
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('useLatestReading', () => {
|
||||
beforeEach(() => vi.clearAllMocks())
|
||||
|
||||
it('calls GET /api/modbus/devices/{uuid}/latest with path param', async () => {
|
||||
mockGet.mockResolvedValue({
|
||||
data: { found: true, recorded_at: '2026-06-22T10:00:00Z', payload: { voltage: 230.2 } },
|
||||
})
|
||||
|
||||
const { Wrapper } = makeWrapper()
|
||||
const { useLatestReading } = await import('./hooks')
|
||||
const { result } = renderHook(() => useLatestReading('test-uuid-1'), { wrapper: Wrapper })
|
||||
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true))
|
||||
|
||||
expect(mockGet).toHaveBeenCalledWith('/api/modbus/devices/{uuid}/latest', {
|
||||
params: { path: { uuid: 'test-uuid-1' } },
|
||||
})
|
||||
expect(result.current.data?.found).toBe(true)
|
||||
expect(result.current.data?.payload).toEqual({ voltage: 230.2 })
|
||||
})
|
||||
|
||||
it('returns found=false when device has no readings', async () => {
|
||||
mockGet.mockResolvedValue({
|
||||
data: { found: false, recorded_at: null, payload: null },
|
||||
})
|
||||
|
||||
const { Wrapper } = makeWrapper()
|
||||
const { useLatestReading } = await import('./hooks')
|
||||
const { result } = renderHook(() => useLatestReading('test-uuid-2'), { wrapper: Wrapper })
|
||||
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true))
|
||||
expect(result.current.data?.found).toBe(false)
|
||||
expect(result.current.data?.payload).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
describe('useMetrics', () => {
|
||||
beforeEach(() => vi.clearAllMocks())
|
||||
|
||||
it('calls GET /api/modbus/devices/{uuid}/metrics with path param', async () => {
|
||||
mockGet.mockResolvedValue({
|
||||
data: {
|
||||
profile: 'sdm120',
|
||||
metrics: [{ key: 'voltage', label: 'Voltage', unit: 'V', device_class: 'voltage' }],
|
||||
},
|
||||
})
|
||||
|
||||
const { Wrapper } = makeWrapper()
|
||||
const { useMetrics } = await import('./hooks')
|
||||
const { result } = renderHook(() => useMetrics('test-uuid-1'), { wrapper: Wrapper })
|
||||
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true))
|
||||
|
||||
expect(mockGet).toHaveBeenCalledWith('/api/modbus/devices/{uuid}/metrics', {
|
||||
params: { path: { uuid: 'test-uuid-1' } },
|
||||
})
|
||||
expect(result.current.data?.metrics).toHaveLength(1)
|
||||
expect(result.current.data?.metrics[0].key).toBe('voltage')
|
||||
})
|
||||
})
|
||||
|
||||
describe('useReadings', () => {
|
||||
beforeEach(() => vi.clearAllMocks())
|
||||
|
||||
it('calls GET /api/modbus/devices/{uuid}/readings with window and limit', async () => {
|
||||
mockGet.mockResolvedValue({
|
||||
data: { items: [{ recorded_at: '2026-06-22T10:00:00Z', payload: { voltage: 230.2 } }] },
|
||||
})
|
||||
|
||||
const { Wrapper } = makeWrapper()
|
||||
const { useReadings } = await import('./hooks')
|
||||
const params = { start: '2026-06-22T09:00:00Z', end: '2026-06-22T10:00:00Z', limit: 500 }
|
||||
const { result } = renderHook(() => useReadings('test-uuid-1', params), { wrapper: Wrapper })
|
||||
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true))
|
||||
|
||||
expect(mockGet).toHaveBeenCalledWith('/api/modbus/devices/{uuid}/readings', {
|
||||
params: {
|
||||
path: { uuid: 'test-uuid-1' },
|
||||
query: {
|
||||
start: '2026-06-22T09:00:00Z',
|
||||
end: '2026-06-22T10:00:00Z',
|
||||
limit: 500,
|
||||
},
|
||||
},
|
||||
})
|
||||
expect(result.current.data?.items).toHaveLength(1)
|
||||
})
|
||||
|
||||
it('caps limit at READINGS_MAX_LIMIT (1000) even if caller requests more', async () => {
|
||||
mockGet.mockResolvedValue({ data: { items: [] } })
|
||||
|
||||
const { Wrapper } = makeWrapper()
|
||||
const { useReadings } = await import('./hooks')
|
||||
const { result } = renderHook(
|
||||
() => useReadings('test-uuid-1', { limit: 99999 }),
|
||||
{ wrapper: Wrapper },
|
||||
)
|
||||
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true))
|
||||
|
||||
// The query param limit should be capped at 1000.
|
||||
expect(mockGet).toHaveBeenCalledWith('/api/modbus/devices/{uuid}/readings',
|
||||
expect.objectContaining({
|
||||
params: expect.objectContaining({
|
||||
query: expect.objectContaining({ limit: 1000 }),
|
||||
}),
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
it('omits start/end from query params when not provided', async () => {
|
||||
mockGet.mockResolvedValue({ data: { items: [] } })
|
||||
|
||||
const { Wrapper } = makeWrapper()
|
||||
const { useReadings } = await import('./hooks')
|
||||
const { result } = renderHook(
|
||||
() => useReadings('test-uuid-1', {}),
|
||||
{ wrapper: Wrapper },
|
||||
)
|
||||
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true))
|
||||
|
||||
expect(mockGet).toHaveBeenCalledWith('/api/modbus/devices/{uuid}/readings',
|
||||
expect.objectContaining({
|
||||
params: expect.objectContaining({
|
||||
query: expect.not.objectContaining({ start: expect.anything() }),
|
||||
}),
|
||||
}),
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -8,6 +8,9 @@
|
||||
* ['modbus-devices'] — device list
|
||||
* ['modbus-device', uuid] — single device
|
||||
* ['modbus-profiles'] — profile list (rarely changes)
|
||||
* ['modbus-latest', uuid] — latest reading per device
|
||||
* ['modbus-metrics', uuid] — profile metric metadata per device
|
||||
* ['modbus-readings', uuid, params] — time-range readings per device
|
||||
*
|
||||
* On success, mutations invalidate the device list so the UI refreshes.
|
||||
*/
|
||||
@@ -25,6 +28,22 @@ export type ModbusDeviceCreate = components['schemas']['ModbusDeviceCreate']
|
||||
export type ModbusDeviceUpdate = components['schemas']['ModbusDeviceUpdate']
|
||||
export type ProfileSummary = components['schemas']['ProfileSummary']
|
||||
export type ModbusTestReadResponse = components['schemas']['ModbusTestReadResponse']
|
||||
export type ModbusLatestResponse = components['schemas']['ModbusLatestResponse']
|
||||
export type ModbusMetricsResponse = components['schemas']['ModbusMetricsResponse']
|
||||
export type MetricInfo = components['schemas']['MetricInfo']
|
||||
export type ModbusReadingResponse = components['schemas']['ModbusReadingResponse']
|
||||
export type ModbusReadingsResponse = components['schemas']['ModbusReadingsResponse']
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Reading query params
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export interface ReadingsQueryParams {
|
||||
start?: string | null
|
||||
end?: string | null
|
||||
/** Capped server-side; default max is 1000 to avoid pulling full history. */
|
||||
limit?: number
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Query: list all devices
|
||||
@@ -112,3 +131,70 @@ export function useTestReadDevice() {
|
||||
}),
|
||||
})
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Query: latest reading for a device
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export function useLatestReading(uuid: string) {
|
||||
return useQuery({
|
||||
queryKey: ['modbus-latest', uuid],
|
||||
queryFn: async () => {
|
||||
const res = await apiClient.GET('/api/modbus/devices/{uuid}/latest', {
|
||||
params: { path: { uuid } },
|
||||
})
|
||||
return res.data
|
||||
},
|
||||
// Refresh every 10 s to show up-to-date readings.
|
||||
refetchInterval: 10_000,
|
||||
})
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Query: profile metric metadata for a device (label/unit per key)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export function useMetrics(uuid: string) {
|
||||
return useQuery({
|
||||
queryKey: ['modbus-metrics', uuid],
|
||||
queryFn: async () => {
|
||||
const res = await apiClient.GET('/api/modbus/devices/{uuid}/metrics', {
|
||||
params: { path: { uuid } },
|
||||
})
|
||||
return res.data
|
||||
},
|
||||
// Metrics are static (tied to profile version); 5 min stale time.
|
||||
staleTime: 5 * 60 * 1000,
|
||||
})
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Query: time-range readings for a device (window + limit — never full-table)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/** Hard upper-bound on readings fetched; prevents accidental full-table pulls. */
|
||||
const READINGS_MAX_LIMIT = 1000
|
||||
|
||||
export function useReadings(uuid: string, params: ReadingsQueryParams) {
|
||||
const { start, end, limit } = params
|
||||
const effectiveLimit = Math.min(limit ?? READINGS_MAX_LIMIT, READINGS_MAX_LIMIT)
|
||||
|
||||
return useQuery({
|
||||
queryKey: ['modbus-readings', uuid, { start, end, limit: effectiveLimit }],
|
||||
queryFn: async () => {
|
||||
const res = await apiClient.GET('/api/modbus/devices/{uuid}/readings', {
|
||||
params: {
|
||||
path: { uuid },
|
||||
query: {
|
||||
...(start ? { start } : {}),
|
||||
...(end ? { end } : {}),
|
||||
limit: effectiveLimit,
|
||||
},
|
||||
},
|
||||
})
|
||||
return res.data
|
||||
},
|
||||
// Enabled only when we have valid uuid; start/end may be null (full window).
|
||||
enabled: !!uuid,
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user