import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { render } from 'ink-testing-library';
import React, { act } from 'react';
import { SuccessDisplay } from '../../components/shared/SuccessDisplay.js';

describe('SuccessDisplay', () => {
  beforeEach(() => {
    vi.useFakeTimers();
  });

  afterEach(() => {
    vi.useRealTimers();
  });

  it('renders the success message', () => {
    const onDismiss = vi.fn();
    const { lastFrame } = render(
      React.createElement(SuccessDisplay, { message: 'Aktion erfolgreich.', onDismiss }),
    );
    expect(lastFrame()).toContain('Aktion erfolgreich.');
  });

  it('renders with green Erfolg label', () => {
    const onDismiss = vi.fn();
    const { lastFrame } = render(
      React.createElement(SuccessDisplay, { message: 'Ok', onDismiss }),
    );
    expect(lastFrame()).toContain('Erfolg');
  });

  it('calls onDismiss after 3 seconds', async () => {
    const onDismiss = vi.fn();
    await act(async () => {
      render(React.createElement(SuccessDisplay, { message: 'Ok', onDismiss }));
    });
    expect(onDismiss).not.toHaveBeenCalled();
    await act(async () => {
      vi.advanceTimersByTime(3000);
    });
    expect(onDismiss).toHaveBeenCalledOnce();
  });

  it('does not call onDismiss before 3 seconds', async () => {
    const onDismiss = vi.fn();
    await act(async () => {
      render(React.createElement(SuccessDisplay, { message: 'Ok', onDismiss }));
    });
    await act(async () => {
      vi.advanceTimersByTime(2999);
    });
    expect(onDismiss).not.toHaveBeenCalled();
  });
});
