import { jsx as _jsx } from "react/jsx-runtime";
import { afterEach, describe, expect, it, vi } from 'vitest';
import { cleanup, fireEvent, render, screen } from '@testing-library/react';
import { ThemeProvider } from '../../theme';
import { SessionHistory } from '../ChatDialog/components/SessionHistory';
const buildSession = (overrides = {}) => ({
    id: 'session-id',
    title: 'Example session',
    createdAt: Date.now(),
    updatedAt: Date.now(),
    messageCount: 2,
    lastMessageAt: Date.now(),
    lastMessagePreview: 'Last message preview',
    ...overrides
});
const renderSessionHistory = (overrideProps = {}) => {
    const resumeHandler = overrideProps.onResume ?? vi.fn();
    const deleteHandler = overrideProps.onDelete ?? vi.fn();
    const props = {
        sessions: overrideProps.sessions ?? [],
        onResume: resumeHandler,
        onDelete: deleteHandler,
        activeSessionId: overrideProps.activeSessionId,
        emptyStateMessage: overrideProps.emptyStateMessage,
        title: overrideProps.title
    };
    const result = render(_jsx(ThemeProvider, { initialThemeName: 'light', children: _jsx(SessionHistory, { ...props }) }));
    return {
        resumeHandler,
        deleteHandler,
        ...result
    };
};
afterEach(() => {
    cleanup();
});
describe('SessionHistory', () => {
    it('renders an empty state when no sessions are provided', () => {
        const emptyMessage = 'No history yet';
        renderSessionHistory({ emptyStateMessage: emptyMessage });
        expect(screen.getByText(emptyMessage)).toBeDefined();
    });
    it('invokes the resume handler when a session item is clicked', () => {
        const session = buildSession({ id: 'session-a', title: 'Session A' });
        const { resumeHandler } = renderSessionHistory({ sessions: [session] });
        fireEvent.click(screen.getByRole('button', { name: 'Resume session “Session A”' }));
        expect(resumeHandler).toHaveBeenCalledTimes(1);
        expect(resumeHandler).toHaveBeenCalledWith('session-a');
    });
    it('invokes the delete handler without triggering resume when delete button clicked', () => {
        const session = buildSession({ id: 'session-b', title: 'Session B' });
        const { deleteHandler, resumeHandler } = renderSessionHistory({ sessions: [session] });
        fireEvent.click(screen.getByRole('button', { name: 'Delete session “Session B”' }));
        expect(deleteHandler).toHaveBeenCalledTimes(1);
        expect(deleteHandler).toHaveBeenCalledWith('session-b');
        expect(resumeHandler).not.toHaveBeenCalled();
    });
});
