|
| 1 | +// Copyright 2026 mfaferek93 |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +/** |
| 16 | + * Two entities reporting the SAME fault code, which is legal - a code is only |
| 17 | + * unique within one entity. Everything here failed while the dashboard's caches |
| 18 | + * were keyed by code alone: expanding one row opened both, and clearing the |
| 19 | + * second row cleared the first entity's fault. |
| 20 | + */ |
| 21 | + |
| 22 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 23 | +import { render, screen, waitFor, fireEvent } from '@testing-library/react'; |
| 24 | +import { FaultsDashboard } from './FaultsDashboard'; |
| 25 | +import type { Fault } from '@/lib/types'; |
| 26 | + |
| 27 | +const mockFetchFaults = vi.fn(); |
| 28 | +const mockClearFault = vi.fn(); |
| 29 | +const mockGetFaultWithEnvironmentData = vi.fn(); |
| 30 | + |
| 31 | +let storeState: Record<string, unknown> = {}; |
| 32 | + |
| 33 | +vi.mock('@/lib/store', () => ({ |
| 34 | + useAppStore: Object.assign( |
| 35 | + vi.fn((selector?: (s: Record<string, unknown>) => unknown) => (selector ? selector(storeState) : storeState)), |
| 36 | + { getState: () => storeState } |
| 37 | + ), |
| 38 | +})); |
| 39 | + |
| 40 | +function fault(entityId: string): Fault { |
| 41 | + return { |
| 42 | + code: 'LIDAR_RANGE_INVALID', |
| 43 | + message: `range invalid on ${entityId}`, |
| 44 | + severity: 'error', |
| 45 | + status: 'active', |
| 46 | + timestamp: '2026-08-20T10:00:00Z', |
| 47 | + entity_id: entityId, |
| 48 | + entity_type: 'app', |
| 49 | + }; |
| 50 | +} |
| 51 | + |
| 52 | +beforeEach(() => { |
| 53 | + vi.clearAllMocks(); |
| 54 | + mockGetFaultWithEnvironmentData.mockResolvedValue({ environment_data: { snapshots: [] } }); |
| 55 | + storeState = { |
| 56 | + faults: [fault('app_a'), fault('app_b')], |
| 57 | + isLoadingFaults: false, |
| 58 | + faultsError: null, |
| 59 | + fetchFaults: mockFetchFaults, |
| 60 | + clearFault: mockClearFault, |
| 61 | + getFaultWithEnvironmentData: mockGetFaultWithEnvironmentData, |
| 62 | + isConnected: true, |
| 63 | + }; |
| 64 | +}); |
| 65 | + |
| 66 | +describe('FaultsDashboard with colliding fault codes', () => { |
| 67 | + it('expands only the clicked row and fetches only its entity', async () => { |
| 68 | + render(<FaultsDashboard />); |
| 69 | + // Flat list view: the grouped default splits by entity, which would |
| 70 | + // hide the collision the caches must survive. |
| 71 | + fireEvent.click(screen.getByRole('switch', { name: /group by entity/i })); |
| 72 | + |
| 73 | + const rows = screen.getAllByText('LIDAR_RANGE_INVALID'); |
| 74 | + expect(rows).toHaveLength(2); |
| 75 | + fireEvent.click(rows[0]!); |
| 76 | + |
| 77 | + await waitFor(() => expect(mockGetFaultWithEnvironmentData).toHaveBeenCalledTimes(1)); |
| 78 | + expect(mockGetFaultWithEnvironmentData).toHaveBeenCalledWith('apps', 'app_a', 'LIDAR_RANGE_INVALID'); |
| 79 | + // The sibling with the same code stays collapsed: exactly one row shows |
| 80 | + // the expanded empty-environment marker. |
| 81 | + await waitFor(() => expect(screen.getAllByText(/no environment data available/i)).toHaveLength(1)); |
| 82 | + }); |
| 83 | + |
| 84 | + it("clears the clicked row's entity, not the first entity with that code", async () => { |
| 85 | + render(<FaultsDashboard />); |
| 86 | + fireEvent.click(screen.getByRole('switch', { name: /group by entity/i })); |
| 87 | + |
| 88 | + const clearButtons = screen.getAllByTitle('Clear fault'); |
| 89 | + expect(clearButtons).toHaveLength(2); |
| 90 | + fireEvent.click(clearButtons[1]!); |
| 91 | + |
| 92 | + await waitFor(() => expect(mockClearFault).toHaveBeenCalledTimes(1)); |
| 93 | + expect(mockClearFault).toHaveBeenCalledWith('apps', 'app_b', 'LIDAR_RANGE_INVALID'); |
| 94 | + }); |
| 95 | + |
| 96 | + it('keeps evidence on screen when a refetch answers 404 (null)', async () => { |
| 97 | + mockGetFaultWithEnvironmentData |
| 98 | + .mockResolvedValueOnce({ |
| 99 | + environment_data: { snapshots: [{ type: 'freeze_frame', name: 'ff', data: { level: 82 } }] }, |
| 100 | + }) |
| 101 | + .mockResolvedValueOnce(null); |
| 102 | + render(<FaultsDashboard />); |
| 103 | + fireEvent.click(screen.getByRole('switch', { name: /group by entity/i })); |
| 104 | + |
| 105 | + const row = screen.getAllByText('LIDAR_RANGE_INVALID')[0]!; |
| 106 | + fireEvent.click(row); |
| 107 | + await waitFor(() => expect(screen.getByText(/snapshots \(1\)/i)).toBeInTheDocument()); |
| 108 | + |
| 109 | + // Collapse, re-expand: the second fetch resolves null (the store's |
| 110 | + // documented 404 shape). The cached evidence must survive it. |
| 111 | + fireEvent.click(row); |
| 112 | + fireEvent.click(row); |
| 113 | + await waitFor(() => expect(mockGetFaultWithEnvironmentData).toHaveBeenCalledTimes(2)); |
| 114 | + expect(screen.getByText(/snapshots \(1\)/i)).toBeInTheDocument(); |
| 115 | + }); |
| 116 | +}); |
0 commit comments