diff --git a/frontend/src/app/AppShell.recordSelection.test.tsx b/frontend/src/app/AppShell.recordSelection.test.tsx new file mode 100644 index 0000000..167c4f0 --- /dev/null +++ b/frontend/src/app/AppShell.recordSelection.test.tsx @@ -0,0 +1,68 @@ +import '@testing-library/jest-dom/vitest' +import { fireEvent, render, screen, waitFor, within } from '@testing-library/react' +import { afterEach, beforeEach, expect, it, vi } from 'vitest' +import { draftFixture, entryFixture, findingFixture } from '../test/fixtures' +import { cleanupControllerTest, getTauriMock, setupControllerTest, useAppController } from './useAppController.testHarness' + +const { AppShell } = await import('./AppShell') +const tauri = getTauriMock() +function Subject() { return } + +beforeEach(() => { + setupControllerTest() + HTMLElement.prototype.scrollIntoView = vi.fn() + tauri.listDrafts.mockResolvedValue([ + draftFixture({ id: 'draft-new', title: 'Newer test cases', updatedAt: '2026-11-13T09:00:00Z' }), + draftFixture({ id: 'draft-old', title: 'Older test cases', updatedAt: '2026-07-22T00:00:00Z' }), + ]) + tauri.listFindings.mockResolvedValue([ + findingFixture({ id: 'finding-new', title: 'Newer finding', updatedAt: '2026-11-13T09:00:00Z' }), + findingFixture({ id: 'finding-old', title: 'Older finding', updatedAt: '2026-07-22T00:00:00Z' }), + ]) + tauri.readNoteSource.mockResolvedValue({ entry: entryFixture({ body: '

Pay with a saved card.

' }), revision: 0 }) +}) +afterEach(cleanupControllerTest) + +const openTab = async (name: RegExp) => fireEvent.click(await screen.findByRole('tab', { name })) +const shownRecord = (title: string) => screen.findByRole('heading', { level: 2, name: title }) + +async function selectRecord(listName: string, title: string) { + fireEvent.click(await within(await screen.findByRole('complementary', { name: listName })).findByText(title)) + await shownRecord(title) +} + +async function openAndCancelDraft(createLabel: string, titleLabel: string) { + const create = screen.getByRole('button', { name: createLabel }) + await waitFor(() => expect(create).toBeEnabled()) + fireEvent.click(create) + await screen.findByRole('textbox', { name: titleLabel }) + fireEvent.click(screen.getByRole('button', { name: 'Cancel' })) + await waitFor(() => expect(screen.queryByRole('textbox', { name: titleLabel })).not.toBeInTheDocument()) +} + +it('keeps the selected test cases when another tab opens and a new draft is cancelled', async () => { + render() + await openTab(/Test cases/) + await selectRecord('Test cases records', 'Older test cases') + + await openTab(/Findings/) + await shownRecord('Newer finding') + await openTab(/Test cases/) + expect(await shownRecord('Older test cases')).toBeInTheDocument() + + await openAndCancelDraft('Create test cases', 'Test case title') + expect(await shownRecord('Older test cases')).toBeInTheDocument() +}) + +it('keeps the selected finding when the Note opens and a new draft is cancelled', async () => { + render() + await openTab(/Findings/) + await selectRecord('Findings records', 'Older finding') + + await openTab(/Note/) + await openTab(/Findings/) + expect(await shownRecord('Older finding')).toBeInTheDocument() + + await openAndCancelDraft('Create finding', 'Finding title') + expect(await shownRecord('Older finding')).toBeInTheDocument() +}) diff --git a/frontend/src/app/AppShell.tsx b/frontend/src/app/AppShell.tsx index ddb6844..1670ec8 100644 --- a/frontend/src/app/AppShell.tsx +++ b/frontend/src/app/AppShell.tsx @@ -184,7 +184,8 @@ export function AppShell(c: AppController) { error={c.error} isBusy={c.isBusy} activeGenerationJob={c.activeTestwareJob} - initialSelectedRecordId={c.focusedRecordId} + initialSelectedRecordId={c.selectedRecordIds.testware} + onSelectedRecordChange={c.selectRecord.testware} createdRecordEditTarget={c.createdRecordEditTarget?.sessionId === c.activeSession?.id && c.createdRecordEditTarget?.kind === 'testware' ? c.createdRecordEditTarget : null} onCreatedRecordEditTargetConsumed={c.consumeCreatedRecordEditTarget} loadState={c.draftLoadState} @@ -215,7 +216,8 @@ export function AppShell(c: AppController) { error={c.error} isBusy={c.isBusy} activeGenerationJob={c.activeFindingJob} - initialSelectedRecordId={c.focusedRecordId} + initialSelectedRecordId={c.selectedRecordIds.findings} + onSelectedRecordChange={c.selectRecord.findings} createdRecordEditTarget={c.createdRecordEditTarget?.sessionId === c.activeSession?.id && c.createdRecordEditTarget?.kind === 'finding' ? c.createdRecordEditTarget : null} onCreatedRecordEditTargetConsumed={c.consumeCreatedRecordEditTarget} loadState={c.findingLoadState} diff --git a/frontend/src/app/sessionNavigationActions.ts b/frontend/src/app/sessionNavigationActions.ts index 67c6078..efe38b4 100644 --- a/frontend/src/app/sessionNavigationActions.ts +++ b/frontend/src/app/sessionNavigationActions.ts @@ -24,7 +24,7 @@ type SessionNavigationContext = { requestSessionNavigation: (view: MainView, navigate: () => Promise) => Promise setActiveView: Dispatch> setError: Dispatch> - setFocusedRecordId: Dispatch> + focusRecord: (view: MainView, recordId: string | null) => void setPendingSettingsSection: Dispatch> setSettingsSection: Dispatch> } @@ -48,7 +48,7 @@ export function createSessionNavigationActions(ctx: SessionNavigationContext) { const session = ctx.sessions.find((candidate) => candidate.id === sessionId) ?? await reopenSession(sessionId) if (!ctx.sessionActions.sessionNavigationIsCurrent(epoch)) return await ctx.sessionActions.openSession(session, () => { - ctx.setFocusedRecordId(recordId) + ctx.focusRecord(view, recordId) ctx.setActiveView(view) }, epoch) } catch (cause) { @@ -72,7 +72,7 @@ export function createSessionNavigationActions(ctx: SessionNavigationContext) { return } if (!route.sessionId || ctx.activeSessionId === route.sessionId) { - ctx.setFocusedRecordId(route.recordId) + ctx.focusRecord(route.view, route.recordId) ctx.requestActiveView(route.view) return } @@ -82,7 +82,7 @@ export function createSessionNavigationActions(ctx: SessionNavigationContext) { const session = ctx.sessions.find((candidate) => candidate.id === route.sessionId) ?? await reopenSession(route.sessionId!) if (!ctx.sessionActions.sessionNavigationIsCurrent(epoch)) return await ctx.sessionActions.openSession(session, () => { - ctx.setFocusedRecordId(route.recordId) + ctx.focusRecord(route.view, route.recordId) ctx.setActiveView(route.view) }, epoch) } catch (cause) { diff --git a/frontend/src/app/useAppController.navigation.ts b/frontend/src/app/useAppController.navigation.ts index 6f19fd9..976bdca 100644 --- a/frontend/src/app/useAppController.navigation.ts +++ b/frontend/src/app/useAppController.navigation.ts @@ -33,7 +33,7 @@ type AppControllerNavigationContext = { settingsSection: string | null setActiveView: Dispatch> setError: Dispatch> - setFocusedRecordId: Dispatch> + focusRecord: (view: MainView, recordId: string | null) => void setCreatedRecordEditTarget: Dispatch> setPendingNavigationView: Dispatch> setPendingSettingsSection: Dispatch> @@ -58,7 +58,7 @@ export function useAppControllerNavigation(ctx: AppControllerNavigationContext) const view = target.kind === 'testware' ? 'testware' : 'findings' finishActiveViewNavigation(view, () => { if (!isCurrent()) return - ctx.setFocusedRecordId(null) + ctx.focusRecord(view, null) ctx.setCreatedRecordEditTarget(target) ctx.setActiveView(view) }) @@ -190,7 +190,7 @@ export function useAppControllerNavigation(ctx: AppControllerNavigationContext) requestSessionNavigation, setActiveView: ctx.setActiveView, setError: ctx.setError, - setFocusedRecordId: ctx.setFocusedRecordId, + focusRecord: ctx.focusRecord, setPendingSettingsSection: ctx.setPendingSettingsSection, setSettingsSection: ctx.setSettingsSection, }) diff --git a/frontend/src/app/useAppController.ts b/frontend/src/app/useAppController.ts index 28aec38..496e3e5 100644 --- a/frontend/src/app/useAppController.ts +++ b/frontend/src/app/useAppController.ts @@ -30,12 +30,12 @@ import { useSettingsSectionFocus } from './useAppController.settingsFocus' import { useAppSettlement } from './appSettlement' import { useAppControllerNavigation } from './useAppController.navigation' import { useAttachmentCleanup } from './useAttachmentCleanup' +import { useRecordSelection } from './useRecordSelection' import { useSummaryRecovery } from './useSummaryRecovery' import { captureAvailableRecordContext } from './conversationPresentation' import { useSessionChat } from './useSessionChat' import { findingFromTestCase } from '../findings/fromTestCase' import type { TestRunCase } from '../tauri' -export { mergeRecordLists } from './useRecordHydration' export function useAppController() { const { sessions, @@ -68,7 +68,7 @@ export function useAppController() { const pendingNavigationContinuationRef = useRef<(() => void | Promise) | null>(null) const pendingNavigationEpochRef = useRef(0) const [settingsSection, setSettingsSection] = useState(null) - const [focusedRecordId, setFocusedRecordId] = useState(null) + const { focusedRecordId, selectedRecordIds, selectRecord, focusRecord } = useRecordSelection() const [createdRecordEditTarget, setCreatedRecordEditTarget] = useState(null) const [initialNavigationRoute] = useState(() => parseNavigationRoute(window.location.hash)) const settingsReturnViewRef = useRef('sessions') @@ -208,7 +208,7 @@ export function useAppController() { discardSettingsDraft, focusedRecordId, pendingNavigationContinuationRef, pendingNavigationEpochRef, pendingNavigationView, recordActions, sessionActions, sessions, settingsDirty, settingsReturnViewRef, settingsSection, - setActiveView, setError, setFocusedRecordId, setCreatedRecordEditTarget, setPendingNavigationView, setPendingSettingsSection, setSettingsSection, + setActiveView, setError, focusRecord, setCreatedRecordEditTarget, setPendingNavigationView, setPendingSettingsSection, setSettingsSection, }) usePendingChangeProtection({ hasPendingChanges: settlement.hasPendingChanges, @@ -257,7 +257,7 @@ export function useAppController() { busyAction, deleteConfirmation, deleteCopy, error, - focusedRecordId, + focusedRecordId, selectedRecordIds, selectRecord, isBusy, canSwitchSession, latestNoteGenerationUndo, noteBody, noteBodyHtml, diff --git a/frontend/src/app/useRecordSelection.test.ts b/frontend/src/app/useRecordSelection.test.ts new file mode 100644 index 0000000..ce70349 --- /dev/null +++ b/frontend/src/app/useRecordSelection.test.ts @@ -0,0 +1,12 @@ +import { act, renderHook } from '@testing-library/react' +import { expect, it } from 'vitest' +import { useRecordSelection } from './useRecordSelection' + +it('keeps the record navigation asked for while the view has no record to show yet', () => { + const { result } = renderHook(() => useRecordSelection()) + act(() => result.current.focusRecord('findings', 'finding-old')) + act(() => result.current.selectRecord.findings(null)) + expect(result.current.selectedRecordIds.findings).toBe('finding-old') + act(() => result.current.selectRecord.findings('finding-new')) + expect(result.current.selectedRecordIds).toEqual({ testware: null, findings: 'finding-new' }) +}) diff --git a/frontend/src/app/useRecordSelection.ts b/frontend/src/app/useRecordSelection.ts new file mode 100644 index 0000000..b14fc35 --- /dev/null +++ b/frontend/src/app/useRecordSelection.ts @@ -0,0 +1,29 @@ +import { useCallback, useMemo, useState } from 'react' +import type { MainView } from '../ui/types' + +export type RecordView = 'testware' | 'findings' +type RecordSelection = Record + +const isRecordView = (view: MainView): view is RecordView => view === 'testware' || view === 'findings' + +/** + * The record each record view shows. A view unmounts when another tab is open, so its selection is kept here: + * returning to Test cases or Findings, or leaving a new draft, shows the record that was selected, not the newest. + * `focusedRecordId` is the record navigation last opened, which the route names. + */ +export function useRecordSelection() { + const [focusedRecordId, setFocusedRecordId] = useState(null) + const [selectedRecordIds, setSelectedRecordIds] = useState({ testware: null, findings: null }) + const select = useCallback((view: RecordView, id: string | null) => + setSelectedRecordIds((previous) => previous[view] === id ? previous : { ...previous, [view]: id }), []) + // A view shows no record while its records load, so an empty report keeps the record navigation asked for. + const selectRecord = useMemo(() => ({ + testware: (id: string | null) => { if (id) select('testware', id) }, + findings: (id: string | null) => { if (id) select('findings', id) }, + }), [select]) + const focusRecord = useCallback((view: MainView, id: string | null) => { + setFocusedRecordId(id) + if (isRecordView(view)) select(view, id) + }, [select]) + return { focusedRecordId, selectedRecordIds, selectRecord, focusRecord } +} diff --git a/frontend/src/ui/format.ts b/frontend/src/ui/format.ts index 585225f..1d00c5e 100644 --- a/frontend/src/ui/format.ts +++ b/frontend/src/ui/format.ts @@ -98,10 +98,6 @@ function escapeRegExp(value: string): string { return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') } -export function statusLabel(status: string): string { - return status.replace(/([A-Z])/g, ' $1').toLowerCase() -} - /** A day and month, such as `13 Nov` in a day-first locale. */ export function formatShortDate(value: string): string { const date = new Date(value) diff --git a/frontend/src/views/FindingsView.tsx b/frontend/src/views/FindingsView.tsx index d42fa74..0b9180d 100644 --- a/frontend/src/views/FindingsView.tsx +++ b/frontend/src/views/FindingsView.tsx @@ -66,6 +66,7 @@ export function FindingsView({ isBusy, activeGenerationJob, initialSelectedRecordId = null, + onSelectedRecordChange, createdRecordEditTarget = null, onCreatedRecordEditTargetConsumed, loadState = 'ready', @@ -90,7 +91,9 @@ export function FindingsView({ error: string | null isBusy: boolean activeGenerationJob: GenerationJobStatus | null + /** The record to show first; told each record the view selects, so it can be shown again after the view returns. */ initialSelectedRecordId?: string | null + onSelectedRecordChange?: (id: string | null) => void createdRecordEditTarget?: CreatedRecordEditTarget | null onCreatedRecordEditTargetConsumed?: (target: CreatedRecordEditTarget) => void loadState?: RecordLoadState @@ -121,6 +124,7 @@ export function FindingsView({ isBusy={isBusy} activeGenerationJob={activeGenerationJob} initialSelectedRecordId={initialSelectedRecordId} + onSelectedRecordChange={onSelectedRecordChange} createdRecordEditTarget={createdRecordEditTarget} onCreatedRecordEditTargetConsumed={onCreatedRecordEditTargetConsumed} loadState={loadState} diff --git a/frontend/src/views/TestwareView.tsx b/frontend/src/views/TestwareView.tsx index a6b19ed..c1386a1 100644 --- a/frontend/src/views/TestwareView.tsx +++ b/frontend/src/views/TestwareView.tsx @@ -1,7 +1,7 @@ import type { RecordChatActions } from '../app/sessionChat.context' import type { RecordCreationCommands } from './RecordCreationActions' import { Box } from 'lucide-react' -import { useEffect, useRef } from 'react' +import { useCallback, useEffect, useRef } from 'react' import { Badge } from '../components/Badge' import type { RichEditorImageUpload } from '../editor/RichTextEditor' import { testwareGenerationSummary } from '../testware/generationPreferences' @@ -48,6 +48,7 @@ export function TestwareView({ isBusy, activeGenerationJob, initialSelectedRecordId = null, + onSelectedRecordChange, createdRecordEditTarget = null, onCreatedRecordEditTargetConsumed, loadState = 'ready', @@ -80,7 +81,9 @@ export function TestwareView({ error: string | null isBusy: boolean activeGenerationJob: GenerationJobStatus | null + /** The record to show first; told each record the view selects, so it can be shown again after the view returns. */ initialSelectedRecordId?: string | null + onSelectedRecordChange?: (id: string | null) => void createdRecordEditTarget?: CreatedRecordEditTarget | null onCreatedRecordEditTargetConsumed?: (target: CreatedRecordEditTarget) => void loadState?: RecordLoadState @@ -97,6 +100,11 @@ export function TestwareView({ }) { const testRuns = useTestRuns(sessionId) useShownTestRun(recordChat, sessionId, testRuns) + const { keepForDraft } = testRuns + const selectDraft = useCallback((id: string | null) => { + keepForDraft(id) + onSelectedRecordChange?.(id) + }, [keepForDraft, onSelectedRecordChange]) const runsOf = (draftId: string) => testRuns.summaries.filter((summary) => summary.draftId === draftId) return ( ) : null} - onSelectedRecordChange={testRuns.keepForDraft} + onSelectedRecordChange={selectDraft} busyActionFor={(draft, kind) => { switch (kind) { case 'delete':