// @vitest-environment jsdom import { cleanup, fireEvent, render, screen } from '@testing-library/react'; import { afterEach, describe, expect, it, vi } from 'vitest'; import { FileOpsSummary } from '../../src/components/FileOpsSummary'; import type { FileOpEntry } from '../../src/runtime/file-ops'; function entry(partial: Partial & { path: string }): FileOpEntry { return { fullPath: `/repo/${partial.path}`, ops: ['read'], opCounts: { read: 1, write: 0, edit: 0, delete: 0 }, total: 1, status: 'done', ...partial, }; } describe('FileOpsSummary', () => { afterEach(() => cleanup()); it('renders nothing when there are no entries', () => { const { container } = render( , ); expect(container.firstChild).toBeNull(); }); it('starts collapsed while streaming and surfaces per-op totals in the header', () => { render( , ); expect(screen.getByText(/Write 1/)).toBeTruthy(); expect(screen.getByText(/Edit 3/)).toBeTruthy(); expect(screen.getByText(/Delete 1/)).toBeTruthy(); expect(screen.getByText(/Read 2/)).toBeTruthy(); // While streaming we collapse the file list so the running pill stays compact. expect(screen.queryByTestId('file-ops-row-a.ts')).toBeNull(); const toggle = screen.getByTestId('file-ops-toggle'); expect(toggle.getAttribute('aria-expanded')).toBe('false'); }); it('opens by default once the run is no longer streaming and lists every touched file', () => { render( , ); expect(screen.getByTestId('file-ops-row-a.ts')).toBeTruthy(); expect(screen.getByTestId('file-ops-row-b.ts')).toBeTruthy(); expect(screen.getByTestId('file-ops-toggle').getAttribute('aria-expanded')).toBe('true'); }); it('reopens once streaming flips to false unless the user collapsed it manually', () => { const { rerender } = render( , ); expect(screen.getByTestId('file-ops-toggle').getAttribute('aria-expanded')).toBe('false'); rerender( , ); expect(screen.getByTestId('file-ops-toggle').getAttribute('aria-expanded')).toBe('true'); }); it('shows the open button only for files that are present in the project file set', () => { const onRequestOpenFile = vi.fn(); render( , ); expect(screen.getByTestId('file-ops-row-open-a.ts')).toBeTruthy(); expect(screen.queryByTestId('file-ops-row-open-missing.ts')).toBeNull(); fireEvent.click(screen.getByTestId('file-ops-row-open-a.ts')); expect(onRequestOpenFile).toHaveBeenCalledWith('a.ts'); }); it('does not show the open button for deleted files', () => { const onRequestOpenFile = vi.fn(); render( , ); expect(screen.getByTestId('file-ops-row-gone.ts')).toBeTruthy(); expect(screen.queryByTestId('file-ops-row-open-gone.ts')).toBeNull(); }); it('flags a row as running when its status is running and as error when isError', () => { render( , ); fireEvent.click(screen.getByTestId('file-ops-toggle')); const pending = screen.getByTestId('file-ops-row-pending.ts'); const broken = screen.getByTestId('file-ops-row-broken.ts'); expect(pending.className).toContain('file-ops-row--running'); expect(broken.className).toContain('file-ops-row--error'); }); });