The open-source agent harness - the runtime layer that turns an LLM into a working agent.
import { AssistantRuntimeProvider, useExternalStoreRuntime, type ThreadMessageLike } from '@assistant-ui/react';
import { act, fireEvent, render, screen } from '@testing-library/react';
import type { ReactNode } from 'react';
import { describe, expect, it, vi } from 'vitest';
import { ChatFileDownload } from '@/atoms/ChatFileDownload.js';
import { FilePreviewLoaderScope, FilePreviewProvider, FilePreviewTurnScope } from '@/filePreview/FilePreviewContext.js';
/** Enables inline previews, so image, PDF and HTML artifacts render as preview cards. */
function WithPreviews({ children }: { children: ReactNode }) {
const runtime = useExternalStoreRuntime({
messages: [] as ThreadMessageLike[],
isRunning: false,
convertMessage: (next: ThreadMessageLike) => next,
onNew: async () => {},
});
return (
<AssistantRuntimeProvider runtime={runtime}>
<FilePreviewProvider>
<FilePreviewTurnScope turnId="turn-1">
<FilePreviewLoaderScope load={async () => new Blob(['x'])}>{children}</FilePreviewLoaderScope>
</FilePreviewTurnScope>
</FilePreviewProvider>
</AssistantRuntimeProvider>
);
}
describe('ChatFileDownload', () => {
it('renders direct download links with singular and plural summaries', () => {
const { rerender } = render(
<ChatFileDownload
files={[{ name: 'report.pdf', path: '/report.pdf' }]}
fileDownloadBaseUrl="https://files.example.com"
/>,
);
expect(screen.getByText('1 file generated')).toBeInTheDocument();
const reportLink = screen.getByRole('link', { name: 'Download report.pdf' });
expect(reportLink).toHaveAttribute('href', 'https://files.example.com/report.pdf');
expect(reportLink).toHaveAttribute('download', 'report.pdf');
rerender(
<ChatFileDownload
files={[
{ name: 'report.pdf', path: '/report.pdf' },
{ name: 'data.csv', path: '/data.csv' },
]}
fileDownloadBaseUrl="/downloads"
/>,
);
expect(screen.getByText('2 files generated')).toBeInTheDocument();
expect(screen.getByRole('link', { name: 'Download data.csv' })).toHaveAttribute('href', '/downloads/data.csv');
});
it('encodes artifact names that would otherwise break or rewrite the URL', () => {
render(
<ChatFileDownload
files={[
{ name: 'my report#final,v2.csv', path: '/tmp/my report#final,v2.csv' },
{ name: 'a&b?c%d.txt', path: '/tmp/a&b?c%d.txt' },
{ name: 'ünïcode.txt', path: '/tmp/ünïcode.txt' },
]}
fileDownloadBaseUrl="https://files.example.com"
/>,
);
expect(screen.getByRole('link', { name: 'Download my report#final,v2.csv' })).toHaveAttribute(
'href',
'https://files.example.com/tmp/my%20report%23final%2Cv2.csv',
);
expect(screen.getByRole('link', { name: 'Download a&b?c%d.txt' })).toHaveAttribute(
'href',
'https://files.example.com/tmp/a%26b%3Fc%25d.txt',
);
expect(screen.getByRole('link', { name: 'Download ünïcode.txt' })).toHaveAttribute(
'href',
'https://files.example.com/tmp/%C3%BCn%C3%AFcode.txt',
);
});
it('encodes the download link on a preview card too', () => {
render(
<WithPreviews>
<ChatFileDownload
files={[{ name: 'chart #1?.png', path: '/tmp/chart #1?.png' }]}
fileDownloadBaseUrl="https://files.example.com"
/>
</WithPreviews>,
);
expect(screen.getByRole('link', { name: 'Download chart #1?.png' })).toHaveAttribute(
'href',
'https://files.example.com/tmp/chart%20%231%3F.png',
);
});
it('shows per-file progress and suppresses duplicate artifact downloads', async () => {
let resolveDownload: (() => void) | undefined;
const downloadPromise = new Promise<void>(resolve => {
resolveDownload = resolve;
});
const onDownloadArtifact = vi.fn(() => downloadPromise);
render(
<ChatFileDownload
files={[{ name: 'bundle.zip', path: '/bundle.zip' }]}
onDownloadArtifact={onDownloadArtifact}
/>,
);
fireEvent.click(screen.getByRole('link', { name: 'Download bundle.zip' }));
expect(onDownloadArtifact).toHaveBeenCalledWith('/bundle.zip', 'bundle.zip');
const downloadingLink = screen.getByRole('link', { name: 'Downloading bundle.zip' });
expect(downloadingLink).toHaveAttribute('aria-busy', 'true');
expect(screen.getByRole('status', { name: 'Loading' })).toBeInTheDocument();
fireEvent.click(downloadingLink);
expect(onDownloadArtifact).toHaveBeenCalledOnce();
if (resolveDownload === undefined) {
throw new Error('Expected the download resolver to be assigned');
}
const completeDownload = resolveDownload;
await act(async () => {
completeDownload();
await downloadPromise;
});
expect(screen.getByRole('link', { name: 'Download bundle.zip' })).not.toHaveAttribute('aria-busy');
expect(screen.queryByRole('status', { name: 'Loading' })).not.toBeInTheDocument();
});
it('renders read-only filenames without interactive download controls', () => {
render(
<ChatFileDownload
readOnly
files={[
{ name: 'one.txt', path: '/one.txt' },
{ name: 'two.txt', path: '/two.txt' },
]}
fileDownloadBaseUrl="/downloads"
onDownloadArtifact={vi.fn(async () => {})}
/>,
);
expect(screen.getByText('one.txt')).toBeInTheDocument();
expect(screen.getByText('two.txt')).toBeInTheDocument();
expect(screen.queryAllByRole('link')).toHaveLength(0);
expect(screen.queryByLabelText(/Download /)).not.toBeInTheDocument();
});
it('shows a read-only tooltip on hover when configured', () => {
render(
<ChatFileDownload
readOnly
readOnlyTooltip="Download File is not available in read-only mode"
files={[{ name: 'report.txt', path: '/report.txt' }]}
/>,
);
fireEvent.mouseEnter(screen.getByText('report.txt').parentElement ?? screen.getByText('report.txt'));
expect(screen.getByRole('tooltip')).toHaveTextContent('Download File is not available in read-only mode');
});
it('marks files as unavailable when no download mechanism is configured', () => {
render(<ChatFileDownload files={[{ name: 'offline.txt', path: '/offline.txt' }]} />);
const unavailable = screen.getByLabelText('Download offline.txt');
expect(unavailable).not.toHaveAttribute('href');
expect(unavailable).toHaveClass('pointer-events-none', 'opacity-60');
expect(screen.queryByRole('link', { name: 'Download offline.txt' })).not.toBeInTheDocument();
});
});