From 524005d7292832462965ae70954fde412a751321 Mon Sep 17 00:00:00 2001 From: Johnny Huynh <27847622+johnnyhuy@users.noreply.github.com> Date: Mon, 16 Nov 2026 13:18:03 +1100 Subject: [PATCH] fix: let visitors retry failed Scorecard loads Co-authored-by: OpenAI Codex --- .tmp/proof/issue-147-verification.txt | 49 +++++++++ .../scorecard/ScorecardPage.test.tsx | 99 +++++++++++++++++-- .../components/scorecard/ScorecardPage.tsx | 43 +++++--- 3 files changed, 174 insertions(+), 17 deletions(-) create mode 100644 .tmp/proof/issue-147-verification.txt diff --git a/.tmp/proof/issue-147-verification.txt b/.tmp/proof/issue-147-verification.txt new file mode 100644 index 0000000..4e93d67 --- /dev/null +++ b/.tmp/proof/issue-147-verification.txt @@ -0,0 +1,49 @@ +$ MISE_TRUSTED_CONFIG_PATHS="$PWD" mise run install +➤ YN0000: └ Completed in 133m 52s +➤ YN0000: · Done with warnings in 134m 12s +Finished in 897.68s + +$ MISE_TRUSTED_CONFIG_PATHS="$PWD" mise run lint +[lint] $ ./scripts/lint +Checked 2 files in packages/backend 2.60s +(node:3702) Warning: The 'NO_COLOR' env is ignored due to the 'FORCE_COLOR' env being set. +(Use `node --trace-warnings ...` to show where the warning was created) +Checked 3 files in plugins/permission-backend-module-default 0.10s +Checked 1 files in packages/backstage-theme-github 3.91s +(node:3702) Warning: The 'NO_COLOR' env is ignored due to the 'FORCE_COLOR' env being set. +(Use `node --trace-warnings ...` to show where the warning was created) +Checked 8 files in plugins/plausible 3.98s +(node:3702) Warning: The 'NO_COLOR' env is ignored due to the 'FORCE_COLOR' env being set. +(Use `node --trace-warnings ...` to show where the warning was created) +Checked 54 files in packages/app 4.93s +(node:3702) Warning: The 'NO_COLOR' env is ignored due to the 'FORCE_COLOR' env being set. +(Use `node --trace-warnings ...` to show where the warning was created) + +$ MISE_TRUSTED_CONFIG_PATHS="$PWD" mise run test -- --runInBand --verbose +PASS app packages/app/src/components/scorecard/ScorecardPage.test.tsx (17.797 s) + ScorecardPage + ✓ shows a catalog link instead of invalid metrics when the catalog is empty (253 ms) + ✓ renders metrics and scorecards when catalog entities are available (341 ms) + ✓ keeps catalog metrics when search filters have no matches (169 ms) + ✓ labels every filter and announces the matching entity count (153 ms) + ✓ combines all filters and clears them with focus even when no entities match (985 ms) + ✓ normalises search whitespace and case for " PUBLIC " (221 ms) + ✓ normalises search whitespace and case for " " (211 ms) + ✓ waits for the catalog before showing an empty state (48 ms) + ✓ reports catalog failures instead of showing an empty state (115 ms) + ✓ retries a failed catalog load and shows scorecards after a pending request (219 ms) + ✓ offers another retry after a second failure and handles an empty recovery (217 ms) + ✓ offers retry when the catalog rejects without an Error object (54 ms) + +Test Suites: 1 skipped, 10 passed, 10 of 11 total +Tests: 1 skipped, 68 passed, 69 total +Snapshots: 0 total +Time: 56.671 s +Ran all test suites in 4 projects. + +$ BROWSER=none MISE_TRUSTED_CONFIG_PATHS="$PWD" mise run dev +Starting the server at http://localhost:3000 +[0] Rspack compiled successfully + +$ curl --fail --silent --output /tmp/backstage-147-page.html --write-out 'Frontend HTTP %{http_code}\n' http://localhost:3000/scorecard +Frontend HTTP 200 diff --git a/packages/app/src/components/scorecard/ScorecardPage.test.tsx b/packages/app/src/components/scorecard/ScorecardPage.test.tsx index b91297b..c086b6e 100644 --- a/packages/app/src/components/scorecard/ScorecardPage.test.tsx +++ b/packages/app/src/components/scorecard/ScorecardPage.test.tsx @@ -1,4 +1,4 @@ -import { fireEvent, screen } from '@testing-library/react'; +import { act, fireEvent, screen, waitFor } from '@testing-library/react'; import { renderInTestApp, TestApiProvider } from '@backstage/test-utils'; import { catalogApiRef } from '@backstage/plugin-catalog-react'; import { ScorecardPage } from './ScorecardPage'; @@ -154,9 +154,12 @@ describe('ScorecardPage', () => { getEntities.mockResolvedValue({ items: [demoService, demoApi] }); await renderPage(); - fireEvent.change(screen.getByRole('textbox', { name: 'Search entities' }), { - target: { value: term }, - }); + fireEvent.change( + screen.getByRole('textbox', { name: 'Search entities' }), + { + target: { value: term }, + }, + ); expect(screen.getByText('demo-api')).toBeInTheDocument(); expect(screen.getByRole('status')).toHaveTextContent( @@ -170,7 +173,9 @@ describe('ScorecardPage', () => { await renderPage(); - expect(screen.getByRole('progressbar')).toBeInTheDocument(); + expect( + screen.getByRole('progressbar', { name: 'Loading scorecards' }), + ).toBeInTheDocument(); expect( screen.queryByText('No catalog entities to score'), ).not.toBeInTheDocument(); @@ -181,12 +186,94 @@ describe('ScorecardPage', () => { await renderPage(); + expect(screen.getByRole('alert')).toHaveTextContent( + 'Could not load scorecards', + ); + expect(screen.getByRole('alert')).toHaveTextContent('Catalog unavailable'); + expect(screen.getByRole('button', { name: 'Retry' })).toBeInTheDocument(); + expect( + screen.queryByText('No catalog entities to score'), + ).not.toBeInTheDocument(); + expect(screen.queryByText('Average Score')).not.toBeInTheDocument(); + }); + + it('retries a failed catalog load and shows scorecards after a pending request', async () => { + let resolveRetry: () => void; + const retry = new Promise<{ items: (typeof demoService)[] }>(resolve => { + resolveRetry = () => resolve({ items: [demoService] }); + }); + getEntities + .mockRejectedValueOnce(new Error('Catalog unavailable')) + .mockReturnValueOnce(retry); + + await renderPage(); + fireEvent.click(screen.getByRole('button', { name: 'Retry' })); + expect( - screen.getByText('Error loading entities: Catalog unavailable'), + screen.getByRole('progressbar', { name: 'Loading scorecards' }), ).toBeInTheDocument(); + expect(screen.queryByRole('alert')).not.toBeInTheDocument(); + expect( + screen.queryByRole('button', { name: 'Retry' }), + ).not.toBeInTheDocument(); + expect(screen.queryByText('Average Score')).not.toBeInTheDocument(); + expect(getEntities).toHaveBeenCalledTimes(2); + expect(getEntities).toHaveBeenLastCalledWith({ + filter: [{ kind: 'component' }, { kind: 'api' }, { kind: 'system' }], + }); + + await act(async () => resolveRetry()); + + expect(screen.getByText('demo-service')).toBeInTheDocument(); + expect(screen.getByText('Average Score')).toBeInTheDocument(); + expect(screen.getByRole('status')).toHaveTextContent('1 entity found'); + expect(screen.queryByRole('alert')).not.toBeInTheDocument(); + expect( + screen.queryByRole('progressbar', { name: 'Loading scorecards' }), + ).not.toBeInTheDocument(); + expect(getEntities).toHaveBeenCalledTimes(2); + }); + + it('offers another retry after a second failure and handles an empty recovery', async () => { + getEntities + .mockRejectedValueOnce(new Error('Catalog unavailable')) + .mockRejectedValueOnce(new Error('Still unavailable')) + .mockResolvedValueOnce({ items: [] }); + + await renderPage(); + fireEvent.click(screen.getByRole('button', { name: 'Retry' })); + + await waitFor(() => + expect(screen.getByRole('alert')).toHaveTextContent('Still unavailable'), + ); + expect(screen.queryByText('Catalog unavailable')).not.toBeInTheDocument(); + expect(screen.queryByText('Average Score')).not.toBeInTheDocument(); expect( screen.queryByText('No catalog entities to score'), ).not.toBeInTheDocument(); + fireEvent.click(screen.getByRole('button', { name: 'Retry' })); + + await waitFor(() => + expect(screen.getByRole('alert')).toHaveTextContent( + 'No catalog entities to score', + ), + ); + expect(screen.queryByText('Still unavailable')).not.toBeInTheDocument(); + expect( + screen.queryByRole('button', { name: 'Retry' }), + ).not.toBeInTheDocument(); expect(screen.queryByText('Average Score')).not.toBeInTheDocument(); + expect(getEntities).toHaveBeenCalledTimes(3); + }); + + it('offers retry when the catalog rejects without an Error object', async () => { + getEntities.mockRejectedValue(null); + + await renderPage(); + + expect(screen.getByRole('alert')).toHaveTextContent( + 'The catalog is unavailable.', + ); + expect(screen.getByRole('button', { name: 'Retry' })).toBeInTheDocument(); }); }); diff --git a/packages/app/src/components/scorecard/ScorecardPage.tsx b/packages/app/src/components/scorecard/ScorecardPage.tsx index d0ce6ac..7bff641 100644 --- a/packages/app/src/components/scorecard/ScorecardPage.tsx +++ b/packages/app/src/components/scorecard/ScorecardPage.tsx @@ -84,26 +84,38 @@ export const ScorecardPage = () => { const [entities, setEntities] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); + const [loadAttempt, setLoadAttempt] = useState(0); const [searchTerm, setSearchTerm] = useState(''); const [kindFilter, setKindFilter] = useState('all'); const [typeFilter, setTypeFilter] = useState('all'); useEffect(() => { + let active = true; const fetchEntities = async () => { + setLoading(true); + setError(null); try { - setLoading(true); const response = await catalogApi.getEntities({ filter: [{ kind: 'component' }, { kind: 'api' }, { kind: 'system' }], }); - setEntities(response.items); + if (active) setEntities(response.items); } catch (err) { - setError(err as Error); + if (active) { + setError( + err instanceof Error + ? err + : new Error('The catalog is unavailable.'), + ); + } } finally { - setLoading(false); + if (active) setLoading(false); } }; fetchEntities(); - }, [catalogApi]); + return () => { + active = false; + }; + }, [catalogApi, loadAttempt]); const entityScores = entities.map(calculateEntityScore); @@ -150,7 +162,7 @@ export const ScorecardPage = () => { minHeight: 400, }} > - + @@ -165,11 +177,20 @@ export const ScorecardPage = () => { subtitle="Track the health and quality of your software catalog" /> - - - Error loading entities: {error.message} - - + setLoadAttempt(attempt => attempt + 1)} + > + Retry + + } + > + Could not load scorecards + {error.message} + );