-
Notifications
You must be signed in to change notification settings - Fork 2
fix: resolve empty resource tabs and missing peer component children #62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0b0851d
fix: resolve empty resource tabs and missing peer component children
bburda 2906d73
fix: also match top-level component_id and gate fallback on peer source
bburda 4e70ace
fix: abort in-flight entity fetches and dedupe peer /apps requests
bburda 31b93d6
Merge remote-tracking branch 'origin/main' into fix/resource-tabs-and…
bburda b405716
fix: abort in-flight mutations in UpdatesDashboard on unmount
bburda File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| // Copyright 2026 bburda | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| import { describe, it, expect, vi, beforeEach } from 'vitest'; | ||
| import { render, screen, waitFor } from '@testing-library/react'; | ||
| import { EntityResourceTabs } from './EntityResourceTabs'; | ||
| import type { ComponentTopic, Operation, Fault } from '@/lib/types'; | ||
|
|
||
| // ---- store mock ---- | ||
|
|
||
| const mockFetchEntityData = vi.fn(); | ||
| const mockFetchEntityOperations = vi.fn(); | ||
| const mockFetchConfigurations = vi.fn(); | ||
| const mockListEntityFaults = vi.fn(); | ||
| const mockSelectEntity = vi.fn(); | ||
|
|
||
| vi.mock('@/lib/store', () => ({ | ||
| useAppStore: vi.fn((selector: (s: Record<string, unknown>) => unknown) => | ||
| selector({ | ||
| selectEntity: mockSelectEntity, | ||
| fetchEntityData: mockFetchEntityData, | ||
| fetchEntityOperations: mockFetchEntityOperations, | ||
| fetchConfigurations: mockFetchConfigurations, | ||
| listEntityFaults: mockListEntityFaults, | ||
| configurations: new Map(), | ||
| }) | ||
| ), | ||
| })); | ||
|
|
||
| // ---- helpers ---- | ||
|
|
||
| function sampleTopics(): ComponentTopic[] { | ||
| return [ | ||
| { | ||
| topic: '/engine/temperature', | ||
| timestamp: Date.now(), | ||
| data: null, | ||
| status: 'metadata_only', | ||
| type: 'sensor_msgs/msg/Temperature', | ||
| }, | ||
| ]; | ||
| } | ||
|
|
||
| // ---- tests ---- | ||
|
|
||
| describe('EntityResourceTabs', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| mockFetchEntityData.mockResolvedValue([] as ComponentTopic[]); | ||
| mockFetchEntityOperations.mockResolvedValue([] as Operation[]); | ||
| mockFetchConfigurations.mockResolvedValue(undefined); | ||
| mockListEntityFaults.mockResolvedValue({ items: [] as Fault[], count: 0 }); | ||
| }); | ||
|
|
||
| it('fetches and displays data items on first render', async () => { | ||
| mockFetchEntityData.mockResolvedValue(sampleTopics()); | ||
|
|
||
| render(<EntityResourceTabs entityId="ecu-primary" entityType="components" />); | ||
|
|
||
| await waitFor(() => { | ||
| expect(mockFetchEntityData).toHaveBeenCalledWith('components', 'ecu-primary', expect.anything()); | ||
| }); | ||
|
|
||
| await waitFor(() => { | ||
| expect(screen.getByText('/engine/temperature')).toBeInTheDocument(); | ||
| }); | ||
| }); | ||
|
|
||
| it('re-fetches data when entityId changes (loadedTabs ref race)', async () => { | ||
| mockFetchEntityData.mockResolvedValue(sampleTopics()); | ||
|
|
||
| const { rerender } = render(<EntityResourceTabs entityId="ecu-primary" entityType="components" />); | ||
|
|
||
| // Wait for first fetch to complete | ||
| await waitFor(() => { | ||
| expect(screen.getByText('/engine/temperature')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| // Switch to a different entity - this is the scenario that was broken: | ||
| // the ref still had { data: true } from the first entity, so the load | ||
| // effect returned early and data stayed empty. | ||
| const secondTopics: ComponentTopic[] = [ | ||
| { | ||
| topic: '/brake/pressure', | ||
| timestamp: Date.now(), | ||
| data: null, | ||
| status: 'metadata_only', | ||
| type: 'sensor_msgs/msg/FluidPressure', | ||
| }, | ||
| ]; | ||
| mockFetchEntityData.mockResolvedValue(secondTopics); | ||
|
|
||
| rerender(<EntityResourceTabs entityId="ecu-mcu" entityType="components" />); | ||
|
|
||
| await waitFor(() => { | ||
| expect(mockFetchEntityData).toHaveBeenCalledWith('components', 'ecu-mcu', expect.anything()); | ||
| }); | ||
|
|
||
| await waitFor(() => { | ||
| expect(screen.getByText('/brake/pressure')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| // Old data should be gone | ||
| expect(screen.queryByText('/engine/temperature')).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('does not apply stale fetch result when entity changes mid-flight', async () => { | ||
| // First fetch returns a promise we control, so we can switch entities | ||
| // while it is still in-flight and verify the old result is discarded. | ||
| let resolveFirst: (value: ComponentTopic[]) => void = () => {}; | ||
| const firstPromise = new Promise<ComponentTopic[]>((resolve) => { | ||
| resolveFirst = resolve; | ||
| }); | ||
| mockFetchEntityData.mockReturnValueOnce(firstPromise); | ||
|
|
||
| const { rerender } = render(<EntityResourceTabs entityId="ecu-primary" entityType="components" />); | ||
|
|
||
| // Switch entity while the first fetch is still pending | ||
| const secondTopics: ComponentTopic[] = [ | ||
| { | ||
| topic: '/brake/pressure', | ||
| timestamp: Date.now(), | ||
| data: null, | ||
| status: 'metadata_only', | ||
| type: 'sensor_msgs/msg/FluidPressure', | ||
| }, | ||
| ]; | ||
| mockFetchEntityData.mockResolvedValueOnce(secondTopics); | ||
| rerender(<EntityResourceTabs entityId="ecu-mcu" entityType="components" />); | ||
|
|
||
| // New entity's fetch resolves and renders | ||
| await waitFor(() => { | ||
| expect(screen.getByText('/brake/pressure')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| // Late-resolve the first (aborted) fetch - it must NOT overwrite the | ||
| // current entity's data. | ||
| resolveFirst(sampleTopics()); | ||
| // Give the microtask queue a chance to run the (hopefully discarded) setData. | ||
| await Promise.resolve(); | ||
| await Promise.resolve(); | ||
|
|
||
| expect(screen.queryByText('/engine/temperature')).not.toBeInTheDocument(); | ||
| expect(screen.getByText('/brake/pressure')).toBeInTheDocument(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.