diff --git a/app/renderer/sections/Sessions.test.tsx b/app/renderer/sections/Sessions.test.tsx index 97f334b6..c0abf7ca 100644 --- a/app/renderer/sections/Sessions.test.tsx +++ b/app/renderer/sections/Sessions.test.tsx @@ -255,7 +255,7 @@ describe('Sessions', () => { getSessions.mockResolvedValue(rows.slice(1)) view.rerender() await screen.findByText(/5 sessions ยท \$13\.52/) - expect(onSessionClose).toHaveBeenCalled() + await waitFor(() => expect(onSessionClose).toHaveBeenCalled()) }) it('shows the selection chips, per-row contributions, and the separately-labeled full cost', async () => { diff --git a/src/dashboard.tsx b/src/dashboard.tsx index 5eb9d562..ed8cd90d 100644 --- a/src/dashboard.tsx +++ b/src/dashboard.tsx @@ -1560,7 +1560,7 @@ function ScrollableViewport({ children, width, lineScroll = true }: { children: ) } -export function InteractiveDashboard({ initialProjects, initialDailyHistoryProjects, initialPeriod, initialProvider, initialPlanUsages, initialDurable, refreshSeconds, projectFilter, excludeFilter, customRange, customRangeLabel, initialDay, windowColumns, initialIndexPendingFiles, initialHistoryIndexing = false, initialCacheWasCold = false, initialHistoryIndex, autoFallbackFromEmptyToday = false, terminateProcess }: { +export function InteractiveDashboard({ initialProjects, initialDailyHistoryProjects, initialPeriod, initialProvider, initialPlanUsages, initialDurable, refreshSeconds, projectFilter, excludeFilter, customRange, customRangeLabel, initialDay, windowColumns, initialIndexPendingFiles, initialHistoryIndexing = false, initialCacheWasCold = false, initialHistoryIndex, autoFallbackFromEmptyToday = false, terminateProcess, onHistoryIndexed }: { initialProjects: ProjectSummary[] initialDailyHistoryProjects?: ProjectSummary[] initialPeriod: Period @@ -1581,6 +1581,7 @@ export function InteractiveDashboard({ initialProjects, initialDailyHistoryProje initialCacheWasCold?: boolean initialHistoryIndex?: DashboardHistoryIndex autoFallbackFromEmptyToday?: boolean + onHistoryIndexed?: () => void /// CLI-only hard stop after Ink has been asked to restore the terminal. /// Component tests and embedders omit it and receive ordinary Ink exit. terminateProcess?: (exitCode: number) => void @@ -1618,6 +1619,8 @@ export function InteractiveDashboard({ initialProjects, initialDailyHistoryProje const [indexPhase, setIndexPhase] = useState(initialCacheWasCold ? 'week' : 'cached') const historyIndexRef = useRef(initialHistoryIndex ?? null) const autoFallbackAppliedRef = useRef(false) + const onHistoryIndexedRef = useRef(onHistoryIndexed) + onHistoryIndexedRef.current = onHistoryIndexed const quitArmedRef = useRef(false) // #1143: first q during the cold-start fill arms a confirmation so the user // sees feedback instead of a silent ~16.5s drain. The second q (or any @@ -1867,7 +1870,9 @@ export function InteractiveDashboard({ initialProjects, initialDailyHistoryProje if (!cancelled) console.error(error) }).finally(() => { clearInterval(progressId) - if (!cancelled) setIndexing(false) + if (cancelled) return + setIndexing(false) + onHistoryIndexedRef.current?.() }) return () => { cancelled = true diff --git a/tests/dashboard-progressive-startup.test.ts b/tests/dashboard-progressive-startup.test.ts index fb225563..0ab2466f 100644 --- a/tests/dashboard-progressive-startup.test.ts +++ b/tests/dashboard-progressive-startup.test.ts @@ -102,6 +102,8 @@ async function renderAutoPeriodDashboard(initialProjects: ProjectSummary[], init stdout.rows = 50 const frames: string[] = [] stdout.on('data', chunk => frames.push(stripAnsi(String(chunk)))) + let indexed!: () => void + const settled = new Promise(resolve => { indexed = resolve }) const app = render(React.createElement(InteractiveDashboard, { initialProjects, initialPeriod: 'today', @@ -112,19 +114,15 @@ async function renderAutoPeriodDashboard(initialProjects: ProjectSummary[], init initialHistoryIndexing: true, initialCacheWasCold: true, autoFallbackFromEmptyToday: true, + onHistoryIndexed: indexed, }), { stdin, stdout, debug: true, interactive: true, patchConsole: false }) onTestFinished(() => app.unmount()) - return { app, frames } -} - -async function waitForFrame(app: ReturnType, frames: string[], predicate: (frame: string) => boolean): Promise { - for (let attempt = 0; attempt < 200; attempt++) { + const settledFrame = async (): Promise => { + await settled await app.waitUntilRenderFlush() - const frame = frames.filter(value => value.trim()).at(-1) ?? '' - if (predicate(frame)) return frame - await new Promise(resolve => setTimeout(resolve, 20)) + return frames.filter(value => value.trim()).at(-1) ?? '' } - return frames.filter(value => value.trim()).at(-1) ?? '' + return { app, frames, settledFrame } } describe('interactive dashboard progressive startup', () => { @@ -135,17 +133,13 @@ describe('interactive dashboard progressive startup', () => { ) expect(paint.result.filteredProjects).toEqual([]) - const { app, frames } = await renderAutoPeriodDashboard( + const { app, frames, settledFrame } = await renderAutoPeriodDashboard( paint.result.filteredProjects, paint.result.initialDurable, ) await app.waitUntilRenderFlush() expect(frames.filter(value => value.trim()).at(-1)).toContain('[ Today ]') - const frame = await waitForFrame( - app, - frames, - value => value.includes('[ 7 Days ]') && !value.includes('indexing'), - ) + const frame = await settledFrame() expect(frame).toContain('[ 7 Days ]') expect(frame).toContain('proj') @@ -159,17 +153,13 @@ describe('interactive dashboard progressive startup', () => { ) expect(paint.result.filteredProjects).toHaveLength(1) - const { app, frames } = await renderAutoPeriodDashboard( + const { app, frames, settledFrame } = await renderAutoPeriodDashboard( paint.result.filteredProjects, paint.result.initialDurable, ) await app.waitUntilRenderFlush() expect(frames.filter(value => value.trim()).at(-1)).toContain('[ Today ]') - const frame = await waitForFrame( - app, - frames, - value => value.includes('[ Today ]') && !value.includes('indexing'), - ) + const frame = await settledFrame() expect(frame).toContain('[ Today ]') expect(frame).not.toContain('[ 7 Days ]')