diff --git a/frontend/src/components/Scenarios/ScenarioRunPage.test.tsx b/frontend/src/components/Scenarios/ScenarioRunPage.test.tsx index 431bb5f3b0..4d02abaac0 100644 --- a/frontend/src/components/Scenarios/ScenarioRunPage.test.tsx +++ b/frontend/src/components/Scenarios/ScenarioRunPage.test.tsx @@ -216,6 +216,41 @@ describe('ScenarioRunPage', () => { await waitFor(() => expect(screen.getByRole('button', { name: 'First objective' })).toHaveFocus()) }) + it('restores focus to the objective opener when the objective dialog closes', async () => { + const user = userEvent.setup() + render() + const objective = screen.getByRole('button', { name: 'First objective' }) + await user.click(objective) + const dialog = within(await screen.findByRole('dialog')) + await user.click(dialog.getByRole('button', { name: 'Close', exact: true })) + await waitFor(() => expect(objective).toHaveFocus()) + }) + + it('restores focus to the objective opener after Escape and on reopening', async () => { + const user = userEvent.setup() + render() + const objective = screen.getByRole('button', { name: 'First objective' }) + await user.click(objective) + await screen.findByRole('dialog') + await user.keyboard('{Escape}') + await waitFor(() => expect(objective).toHaveFocus()) + await user.click(objective) + await screen.findByRole('dialog') + await user.keyboard('{Escape}') + await waitFor(() => expect(objective).toHaveFocus()) + }) + + it('restores focus to the technique opener when attack details are dismissed with Escape', async () => { + const user = userEvent.setup() + render() + await expand(user) + const details = screen.getAllByRole('button', { name: /^Open details for PromptSendingAttack:/ })[0] + await user.click(details) + await screen.findByRole('dialog') + await user.keyboard('{Escape}') + await waitFor(() => expect(details).toHaveFocus()) + }) + it('shows per-group and per-objective success rates without restoring paired outcomes', () => { render() expect(within(screen.getByLabelText('Scan summary')).getByText('50%', { exact: true })).toBeInTheDocument() diff --git a/frontend/src/components/Scenarios/ScenarioRunPage.tsx b/frontend/src/components/Scenarios/ScenarioRunPage.tsx index ccaa19709f..473012521b 100644 --- a/frontend/src/components/Scenarios/ScenarioRunPage.tsx +++ b/frontend/src/components/Scenarios/ScenarioRunPage.tsx @@ -209,6 +209,14 @@ function ScenarioRunContent({ scenarioResultId, attackResultId }: ScenarioRunCon }) } + const closeObjective = (): void => { + setObjective(null) + requestAnimationFrame(() => { + const trigger = objectiveTrigger.current + if (trigger?.isConnected) trigger.focus() + }) + } + const openAttemptDetails = ( attempt: ScenarioProgressResult | ScenarioHierarchyNode, trigger: HTMLElement, @@ -587,7 +595,7 @@ function ScenarioRunContent({ scenarioResultId, attackResultId }: ScenarioRunCon - { setObjective(null) }} +