Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions frontend/src/components/Scenarios/ScenarioRunPage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(<Wrapper path={scenarioRunRoutePath(RUN_ID)} />)
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(<Wrapper path={scenarioRunRoutePath(RUN_ID)} />)
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(<Wrapper path={scenarioRunRoutePath(RUN_ID)} />)
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(<Wrapper path={scenarioRunRoutePath(RUN_ID)} />)
expect(within(screen.getByLabelText('Scan summary')).getByText('50%', { exact: true })).toBeInTheDocument()
Expand Down
10 changes: 9 additions & 1 deletion frontend/src/components/Scenarios/ScenarioRunPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -587,7 +595,7 @@ function ScenarioRunContent({ scenarioResultId, attackResultId }: ScenarioRunCon
</DialogBody>
</DialogSurface>
</Dialog>
<ObjectiveDetailsDialog objective={objective} onClose={() => { setObjective(null) }}
<ObjectiveDetailsDialog objective={objective} onClose={closeObjective}
attempts={objective ? attemptsBySeed.get(objective.id) ?? [] : []}
onOpenAttempt={openDetails} scenarioResultId={scenarioResultId} />
</main>
Expand Down
Loading