import { jsx as _jsx } from "react/jsx-runtime";
import { afterEach, describe, expect, test, vi } from 'vitest';
import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react';
import { ToolCallCard } from '../ToolCallCard';
const buildToolCall = (overrides = {}) => ({
    id: 'call-1',
    name: 'test_tool',
    args: '{"key":"value"}',
    status: 'requires_approval',
    startedAt: Date.now(),
    updatedAt: Date.now(),
    ...overrides
});
const buildInterruption = (overrides = {}) => ({
    id: 'interruption-1',
    runId: 'run-1',
    callId: 'call-1',
    toolName: 'test_tool',
    toolArguments: '{"key":"value"}',
    agentName: 'Chat Agent',
    message: 'Allow this tool to continue?',
    createdAt: Date.now(),
    updatedAt: Date.now(),
    status: 'pending',
    ...overrides
});
afterEach(() => {
    cleanup();
});
describe('ToolCallCard', () => {
    test('renders approval callout and handles approval action', async () => {
        let resolveApprove;
        const approveMock = vi.fn(() => new Promise((resolve) => {
            resolveApprove = resolve;
        }));
        const denyMock = vi.fn();
        render(_jsx(ToolCallCard, { toolCall: buildToolCall(), index: 0, context: 'stream', expanded: false, onToggleExpansion: () => { }, formatStatusLabel: (status) => status ?? '', summarizeValue: (value) => value ?? '', pendingInterruption: buildInterruption(), onApproveInterruption: approveMock, onDenyInterruption: denyMock }));
        expect(screen.getByText('Tool permission required')).toBeDefined();
        expect(screen.getByText('Allow this tool to continue?')).toBeDefined();
        fireEvent.click(screen.getByRole('button', { name: 'Approve' }));
        expect(approveMock).toHaveBeenCalledWith('interruption-1');
        expect(screen.getByRole('button', { name: 'Approving…' })).toBeDefined();
        resolveApprove?.();
        await waitFor(() => {
            expect(screen.getByRole('button', { name: 'Approve' })).toBeDefined();
        });
        expect(denyMock).not.toHaveBeenCalled();
    });
    test('shows resolved status when interruption already approved', () => {
        const approvedInterruption = buildInterruption({ status: 'approved' });
        expect(approvedInterruption.status).toBe('approved');
        render(_jsx(ToolCallCard, { toolCall: buildToolCall({ status: 'completed' }), index: 0, context: 'history', expanded: false, onToggleExpansion: () => { }, formatStatusLabel: (status) => status ?? '', summarizeValue: (value) => value ?? '', pendingInterruption: approvedInterruption }));
        expect(screen.getByText('Permission granted')).toBeDefined();
        expect(screen.getByText('Approved')).toBeDefined();
        expect(screen.queryByText(/^Approve$/i)).toBeNull();
        expect(screen.queryByText(/^Deny$/i)).toBeNull();
    });
});
