-
Notifications
You must be signed in to change notification settings - Fork 0
fix: harden Data tab against sibling transform crashes and entity races #68
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
45ebc34
fix: harden Data tab against sibling transform crashes and entity races
bburda bc9a9e1
fix: harden access parsing and abort stale resource-count fetches
bburda 14e5e00
fix: prevent fault-code collisions and zero-value write-UI hiding
bburda 520df06
fix: stabilise synthetic fault codes and preserve falsy scalar values
bburda d2dde56
fix: guard transformFaultsResponse against non-array items field
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,119 @@ | ||
| import { describe, it, expect, vi, beforeEach } from 'vitest'; | ||
| import { render, screen } from '@testing-library/react'; | ||
| import userEvent from '@testing-library/user-event'; | ||
| import { DataPanel } from './DataPanel'; | ||
| import type { ComponentTopic } from '@/lib/types'; | ||
|
|
||
| vi.mock('@/lib/store', () => ({ | ||
| useAppStore: vi.fn((selector: (s: { isConnected: boolean }) => unknown) => selector({ isConnected: true })), | ||
| })); | ||
|
|
||
| // Capture the latest `initialValue` so tests can assert what the form would | ||
| // receive when the user clicks "Copy to Publish". | ||
| const publishFormInitialValues: unknown[] = []; | ||
| vi.mock('@/components/TopicPublishForm', () => ({ | ||
| TopicPublishForm: ({ initialValue }: { initialValue: unknown }) => { | ||
| publishFormInitialValues.push(initialValue); | ||
| return <div data-testid="topic-publish-form" />; | ||
| }, | ||
| })); | ||
|
|
||
| vi.mock('@/components/JsonFormViewer', () => ({ | ||
| JsonFormViewer: () => <div data-testid="json-form-viewer" />, | ||
| })); | ||
|
|
||
| beforeEach(() => { | ||
| publishFormInitialValues.length = 0; | ||
| }); | ||
|
|
||
| function makeTopic(overrides: Partial<ComponentTopic> = {}): ComponentTopic { | ||
| return { | ||
| topic: '/test/data', | ||
| timestamp: 0, | ||
| data: null, | ||
| status: 'data', | ||
| ...overrides, | ||
| }; | ||
| } | ||
|
|
||
| describe('DataPanel canWrite', () => { | ||
| it('shows write form when access is write even if data is 0 and no type is present', () => { | ||
| // Regression for the falsy-scalar bug: a counter reading of exactly 0 | ||
| // with an explicit `access: 'write'` must not hide the write section. | ||
| render(<DataPanel topic={makeTopic({ access: 'write', data: 0 })} entityId="motor" />); | ||
| expect(screen.getByText('Write Value')).toBeInTheDocument(); | ||
| expect(screen.getByTestId('topic-publish-form')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('shows write form when access is readwrite even if data is false and no type is present', () => { | ||
| render(<DataPanel topic={makeTopic({ access: 'readwrite', data: false })} entityId="motor" />); | ||
| expect(screen.getByText('Write Value')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('hides write form when access is read regardless of data or type', () => { | ||
| render( | ||
| <DataPanel | ||
| topic={makeTopic({ access: 'read', type: 'sensor_msgs/msg/Temperature', data: { value: 23 } })} | ||
| entityId="motor" | ||
| /> | ||
| ); | ||
| expect(screen.queryByText('Write Value')).not.toBeInTheDocument(); | ||
| expect(screen.queryByText('Publish Message')).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('falls back to typed-topic heuristic when access is absent', () => { | ||
| render(<DataPanel topic={makeTopic({ type: 'std_msgs/msg/String', data: null })} entityId="motor" />); | ||
| expect(screen.getByText('Publish Message')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('hides write form when access is absent and there is no type hint and no value', () => { | ||
| render(<DataPanel topic={makeTopic({ data: null })} entityId="motor" />); | ||
| expect(screen.queryByText('Publish Message')).not.toBeInTheDocument(); | ||
| expect(screen.queryByText('Write Value')).not.toBeInTheDocument(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('DataPanel publishValue initialization', () => { | ||
| it('preserves a scalar zero data value as the initial publish value when no default is set', () => { | ||
| // `||` would have collapsed `0` to `{}`; `??` keeps the falsy scalar. | ||
| render(<DataPanel topic={makeTopic({ access: 'write', data: 0 })} entityId="motor" />); | ||
| expect(publishFormInitialValues.at(-1)).toBe(0); | ||
| }); | ||
|
|
||
| it('prefers type_info.default_value over data when both are present', () => { | ||
| render( | ||
| <DataPanel | ||
| topic={makeTopic({ | ||
| access: 'write', | ||
| data: 0, | ||
| type_info: { schema: {}, default_value: 42 as unknown as Record<string, unknown> }, | ||
| })} | ||
| entityId="motor" | ||
| /> | ||
| ); | ||
| expect(publishFormInitialValues.at(-1)).toBe(42); | ||
| }); | ||
| }); | ||
|
|
||
| describe('DataPanel Copy to Publish', () => { | ||
| it('copies a scalar zero value from the last received data into the publish form', async () => { | ||
| // With the previous truthy guard, clicking Copy did nothing for a | ||
| // valid reading of exactly 0 (or false / ''). Presence check fixes it. | ||
| render( | ||
| <DataPanel | ||
| topic={makeTopic({ | ||
| access: 'write', | ||
| data: 0, | ||
| type_info: { schema: {}, default_value: 42 as unknown as Record<string, unknown> }, | ||
| })} | ||
| entityId="motor" | ||
| /> | ||
| ); | ||
|
|
||
| expect(publishFormInitialValues.at(-1)).toBe(42); | ||
|
|
||
| await userEvent.click(screen.getByRole('button', { name: /copy to publish/i })); | ||
|
|
||
| expect(publishFormInitialValues.at(-1)).toBe(0); | ||
| }); | ||
| }); |
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.