-
Notifications
You must be signed in to change notification settings - Fork 18
fix(studio): refactor GuardrailsTable to DataView #1043
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 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
33 changes: 33 additions & 0 deletions
33
web/packages/studio/src/components/dataViews/GuardrailChecksDataView/ResultIndicator.tsx
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,33 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import { Badge } from '@nvidia/foundations-react-core'; | ||
| import type { Verdict } from '@studio/api/guardrail-checks/types'; | ||
| import { ArrowRight, Clock, ShieldCheck } from 'lucide-react'; | ||
| import type { FC } from 'react'; | ||
|
|
||
| /** Solid status badge for a check's latest-run verdict (purple guarded / green allowed). */ | ||
| export const ResultIndicator: FC<{ status: Verdict | undefined }> = ({ status }) => { | ||
| if (status === 'blocked') { | ||
| return ( | ||
| <Badge color="purple" kind="solid"> | ||
| <ShieldCheck size={14} /> | ||
| Guarded | ||
| </Badge> | ||
| ); | ||
| } | ||
| if (status === 'success') { | ||
| return ( | ||
| <Badge color="green" kind="solid"> | ||
| <ArrowRight size={14} /> | ||
| Allowed | ||
| </Badge> | ||
| ); | ||
| } | ||
| return ( | ||
| <Badge color="gray" kind="solid"> | ||
| <Clock size={14} /> | ||
| Not run | ||
| </Badge> | ||
| ); | ||
| }; |
92 changes: 92 additions & 0 deletions
92
web/packages/studio/src/components/dataViews/GuardrailChecksDataView/ResultSummary.tsx
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,92 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import { Flex, Panel, Stack, Text } from '@nvidia/foundations-react-core'; | ||
| import type { GuardrailCheckEntity } from '@studio/api/guardrail-checks/types'; | ||
| import { getLatestRunStatus } from '@studio/components/dataViews/GuardrailChecksDataView/checkStatus'; | ||
| import type { FC } from 'react'; | ||
|
|
||
| interface ResultSummaryProps { | ||
| checks: GuardrailCheckEntity[]; | ||
| } | ||
|
|
||
| /** Segment/legend colors for the result summary: purple guarded, green allowed, gray not-run. */ | ||
| const GUARDED_BG = 'bg-[var(--color-purple-600)]'; | ||
| const ALLOWED_BG = 'bg-[var(--color-green-200)]'; | ||
| const NOTRUN_BG = 'bg-[var(--color-gray-200)]'; | ||
|
|
||
| /** Count of checks by their latest-run verdict: allowed, guarded, or never run. */ | ||
| const summarizeResults = (checks: GuardrailCheckEntity[]) => { | ||
| let allowed = 0; | ||
| let guarded = 0; | ||
| let notRun = 0; | ||
|
|
||
| for (const check of checks) { | ||
| const status = getLatestRunStatus(check); | ||
| if (status === 'success') { | ||
| allowed += 1; | ||
| } else if (status === 'blocked') { | ||
| guarded += 1; | ||
| } else { | ||
| notRun += 1; | ||
| } | ||
| } | ||
|
|
||
| return { allowed, guarded, notRun }; | ||
| }; | ||
|
|
||
| /** One proportional segment of the summary bar; renders nothing when its share is zero. */ | ||
| const BarSegment: FC<{ colorClassName: string; pct: number }> = ({ colorClassName, pct }) => | ||
| pct > 0 ? ( | ||
| <div | ||
| className={`h-full ${colorClassName}`} | ||
| // eslint-disable-next-line no-restricted-syntax -- width is a runtime proportion | ||
| style={{ width: `${pct}%` }} | ||
| /> | ||
| ) : null; | ||
|
|
||
| /** One legend entry: a colored dot + label on the left, the count on the right. */ | ||
| const LegendRow: FC<{ dotClassName: string; label: string; value: number }> = ({ | ||
| dotClassName, | ||
| label, | ||
| value, | ||
| }) => ( | ||
| <Flex align="center" justify="between"> | ||
| <Flex align="center" gap="density-sm"> | ||
| <span className={`inline-block h-2.5 w-2.5 shrink-0 rounded-full ${dotClassName}`} /> | ||
| <Text kind="label/regular/sm">{label}</Text> | ||
| </Flex> | ||
| <Text kind="label/bold/sm">{value}</Text> | ||
| </Flex> | ||
| ); | ||
|
|
||
| /** Proportional bar + legend breaking down a set of checks by their latest-run verdict. */ | ||
| export const ResultSummary: FC<ResultSummaryProps> = ({ checks }) => { | ||
| const { allowed, guarded, notRun } = summarizeResults(checks); | ||
|
|
||
| // Bar proportions span every check: guarded, then allowed, then not-run at the end. | ||
| const total = guarded + allowed + notRun; | ||
| const pct = (n: number) => (total > 0 ? (n / total) * 100 : 0); | ||
|
|
||
| return ( | ||
| <Panel slotHeading="Result Summary"> | ||
| <Stack gap="density-lg"> | ||
| {/* Left → right: guarded (purple), allowed (green), not-run (gray) at the end. */} | ||
| <div | ||
| className="flex h-2 w-full overflow-hidden rounded-full bg-surface-sunken" | ||
| role="img" | ||
| aria-label={`${guarded} guarded, ${allowed} allowed, ${notRun} not run`} | ||
| > | ||
| <BarSegment colorClassName={GUARDED_BG} pct={pct(guarded)} /> | ||
| <BarSegment colorClassName={ALLOWED_BG} pct={pct(allowed)} /> | ||
| <BarSegment colorClassName={NOTRUN_BG} pct={pct(notRun)} /> | ||
| </div> | ||
| <Stack gap="density-sm"> | ||
| <LegendRow dotClassName={GUARDED_BG} label="Guarded" value={guarded} /> | ||
| <LegendRow dotClassName={ALLOWED_BG} label="Allowed" value={allowed} /> | ||
| <LegendRow dotClassName={NOTRUN_BG} label="Not run" value={notRun} /> | ||
| </Stack> | ||
| </Stack> | ||
| </Panel> | ||
| ); | ||
| }; | ||
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
190 changes: 190 additions & 0 deletions
190
web/packages/studio/src/components/dataViews/GuardrailChecksDataView/index.test.tsx
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,190 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import { | ||
| GUARDRAIL_CHECKS_ENTITY_TYPE, | ||
| type GuardrailCheckEntity, | ||
| type Verdict, | ||
| } from '@studio/api/guardrail-checks/types'; | ||
| import { GuardrailChecksDataView } from '@studio/components/dataViews/GuardrailChecksDataView'; | ||
| import { XL_SELECTOR_TIMEOUT } from '@studio/tests/util/constants'; | ||
| import { TestProviders } from '@studio/tests/util/TestProviders'; | ||
| import { fireEvent, render, screen, waitFor } from '@testing-library/react'; | ||
| import userEvent from '@testing-library/user-event'; | ||
| import { createMemoryRouter, RouterProvider } from 'react-router-dom'; | ||
|
|
||
| const makeCheck = ({ | ||
| id, | ||
| input, | ||
| output, | ||
| status, | ||
| }: { | ||
| id: string; | ||
| input: string; | ||
| output?: string; | ||
| status?: Verdict; | ||
| }): GuardrailCheckEntity => ({ | ||
| entity_type: GUARDRAIL_CHECKS_ENTITY_TYPE, | ||
| id, | ||
| parent: 'cfg-1', | ||
| db_version: 1, | ||
| name: id, | ||
| workspace: 'default', | ||
| created_at: '2026-04-12T11:00:00.000Z', | ||
| created_by: 'user@example.com', | ||
| updated_at: '2026-04-12T11:00:00.000Z', | ||
| updated_by: 'user@example.com', | ||
| data: { | ||
| messages: [ | ||
| { role: 'user', content: input }, | ||
| ...(output ? [{ role: 'assistant' as const, content: output }] : []), | ||
| ], | ||
| runs: status | ||
| ? [{ run_at: '2026-04-12T11:05:00.000Z', status, rails_status: {}, config_version: 1 }] | ||
| : [], | ||
| }, | ||
| }); | ||
|
|
||
| const GUARDED = makeCheck({ | ||
| id: 'chk-guarded', | ||
| input: 'My SSN is 123-45-6789', | ||
| output: 'I cannot help with that', | ||
| status: 'blocked', | ||
| }); | ||
| const ALLOWED = makeCheck({ | ||
| id: 'chk-allowed', | ||
| input: 'What is the weather today', | ||
| output: 'It is sunny', | ||
| status: 'success', | ||
| }); | ||
| const NOT_RUN = makeCheck({ id: 'chk-not-run', input: 'Hello there' }); | ||
|
|
||
| const CHECKS = [GUARDED, ALLOWED, NOT_RUN]; | ||
|
|
||
| const renderComponent = (checks: GuardrailCheckEntity[] = CHECKS) => { | ||
| const router = createMemoryRouter([ | ||
| { path: '/', element: <GuardrailChecksDataView checks={checks} /> }, | ||
| ]); | ||
|
|
||
| return render( | ||
| <TestProviders> | ||
| <RouterProvider router={router} /> | ||
| </TestProviders> | ||
| ); | ||
| }; | ||
|
|
||
| describe('GuardrailChecksDataView', () => { | ||
| it('renders a row per check with its input and output', async () => { | ||
| renderComponent(); | ||
|
|
||
| expect( | ||
| await screen.findByText('My SSN is 123-45-6789', undefined, { timeout: XL_SELECTOR_TIMEOUT }) | ||
| ).toBeInTheDocument(); | ||
| expect(screen.getByText('I cannot help with that')).toBeInTheDocument(); | ||
| expect(screen.getByText('What is the weather today')).toBeInTheDocument(); | ||
| expect(screen.getByText('Hello there')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('renders the Input, Output, and Result columns', async () => { | ||
| renderComponent(); | ||
|
|
||
| for (const header of ['Input', 'Output', 'Result']) { | ||
| expect( | ||
| await screen.findByRole('columnheader', { name: header }, { timeout: XL_SELECTOR_TIMEOUT }) | ||
| ).toBeInTheDocument(); | ||
| } | ||
| }); | ||
|
|
||
| it('maps each latest-run verdict to its result badge', async () => { | ||
| renderComponent(); | ||
|
|
||
| await screen.findByText('Hello there', undefined, { timeout: XL_SELECTOR_TIMEOUT }); | ||
|
|
||
| const rowFor = (input: string) => screen.getByRole('row', { name: new RegExp(input) }); | ||
|
|
||
| expect(rowFor('My SSN is 123-45-6789')).toHaveTextContent('Guarded'); | ||
| expect(rowFor('What is the weather today')).toHaveTextContent('Allowed'); | ||
| // A check with no runs has never been evaluated — it must not read as a passing test. | ||
| expect(rowFor('Hello there')).toHaveTextContent('Not run'); | ||
| }); | ||
|
|
||
| it('falls back to a dash when a check has no assistant output', async () => { | ||
| renderComponent([NOT_RUN]); | ||
|
|
||
| expect( | ||
| await screen.findByText('Hello there', undefined, { timeout: XL_SELECTOR_TIMEOUT }) | ||
| ).toBeInTheDocument(); | ||
| expect(screen.getByText('—')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('narrows rows to those matching the search text', async () => { | ||
| const user = userEvent.setup(); | ||
| renderComponent(); | ||
|
|
||
| await screen.findByText('Hello there', undefined, { timeout: XL_SELECTOR_TIMEOUT }); | ||
| await user.type(screen.getByPlaceholderText('Search tests...'), 'weather'); | ||
|
|
||
| await waitFor(() => expect(screen.queryByText('Hello there')).not.toBeInTheDocument(), { | ||
| timeout: XL_SELECTOR_TIMEOUT, | ||
| }); | ||
| expect(screen.getByText('What is the weather today')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('searches assistant output as well as user input', async () => { | ||
| const user = userEvent.setup(); | ||
| renderComponent(); | ||
|
|
||
| await screen.findByText('Hello there', undefined, { timeout: XL_SELECTOR_TIMEOUT }); | ||
| await user.type(screen.getByPlaceholderText('Search tests...'), 'cannot help'); | ||
|
|
||
| await waitFor(() => expect(screen.queryByText('Hello there')).not.toBeInTheDocument(), { | ||
| timeout: XL_SELECTOR_TIMEOUT, | ||
| }); | ||
| expect(screen.getByText('My SSN is 123-45-6789')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('filters by result and restores every row when the filter is cleared', async () => { | ||
| renderComponent(); | ||
|
|
||
| await screen.findByText('Hello there', undefined, { timeout: XL_SELECTOR_TIMEOUT }); | ||
|
|
||
| fireEvent.click(screen.getByTestId('open-filters-button')); | ||
| fireEvent.click(await screen.findByTestId('column-filter-status')); | ||
| fireEvent.click(await screen.findByRole('option', { name: 'Guarded' })); | ||
|
|
||
| await waitFor(() => expect(screen.queryByText('Hello there')).not.toBeInTheDocument(), { | ||
| timeout: XL_SELECTOR_TIMEOUT, | ||
| }); | ||
| expect(screen.getByText('My SSN is 123-45-6789')).toBeInTheDocument(); | ||
| expect(screen.queryByText('What is the weather today')).not.toBeInTheDocument(); | ||
|
|
||
| fireEvent.click(screen.getByTestId('clear-filters')); | ||
|
|
||
| expect( | ||
| await screen.findByText('Hello there', undefined, { timeout: XL_SELECTOR_TIMEOUT }) | ||
| ).toBeInTheDocument(); | ||
| expect(screen.getByText('What is the weather today')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('shows the no-tests empty state when there are no checks', async () => { | ||
| renderComponent([]); | ||
|
|
||
| expect( | ||
| await screen.findByText('No tests yet', undefined, { timeout: XL_SELECTOR_TIMEOUT }) | ||
| ).toBeInTheDocument(); | ||
| expect(screen.queryByRole('button', { name: /Clear Filters/i })).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('offers a way out when a search matches nothing', async () => { | ||
| const user = userEvent.setup(); | ||
| renderComponent(); | ||
|
|
||
| await screen.findByText('Hello there', undefined, { timeout: XL_SELECTOR_TIMEOUT }); | ||
| await user.type(screen.getByPlaceholderText('Search tests...'), 'no-such-test'); | ||
|
|
||
| expect( | ||
| await screen.findByText('No Results Found', undefined, { timeout: XL_SELECTOR_TIMEOUT }) | ||
| ).toBeInTheDocument(); | ||
| expect(screen.getByRole('button', { name: /Clear Filters/i })).toBeInTheDocument(); | ||
| }); | ||
| }); |
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.