// @vitest-environment jsdom
import { describe, it, expect, vi, beforeEach } from 'vitest';
import '@testing-library/jest-dom/vitest';
import { render, screen, fireEvent, cleanup, waitFor } from '@testing-library/react';

vi.mock('../../src/renderer/inbox/api.js', () => ({
  inboxApi: {
    runBackup: vi.fn(async () => ({ ok: true })),
    runExport: vi.fn(async () => ({ ok: true })),
    runImport: vi.fn(async () => ({ ok: true })),
    runSync: vi.fn(async () => ({ ok: true })),
    runExportTelemetry: vi.fn(async () => ({ ok: true }))
  }
}));

import { BackupSection } from '../../src/renderer/inbox/components/settings/BackupSection';

describe('BackupSection', () => {
  beforeEach(() => {
    vi.clearAllMocks();
    cleanup();
  });

  it('renders 5 buttons', () => {
    render(<BackupSection />);
    expect(screen.getByRole('button', { name: /지금 백업/ })).toBeInTheDocument();
    expect(screen.getByRole('button', { name: /^내보내기/ })).toBeInTheDocument();
    expect(screen.getByRole('button', { name: /백업에서 복원/ })).toBeInTheDocument();
    expect(screen.getByRole('button', { name: /지금 동기화/ })).toBeInTheDocument();
    expect(screen.getByRole('button', { name: /사용 로그/ })).toBeInTheDocument();
  });

  it('clicking 지금 백업 calls runBackup', async () => {
    const { inboxApi } = await import('../../src/renderer/inbox/api.js');
    render(<BackupSection />);
    fireEvent.click(screen.getByRole('button', { name: /지금 백업/ }));
    await waitFor(() => expect(inboxApi.runBackup).toHaveBeenCalled());
  });
});
