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

vi.mock('../../src/renderer/inbox/api.js', () => ({
  inboxApi: {
    getAutostart: vi.fn(async () => ({ openAtLogin: true })),
    setAutostart: vi.fn(async (open: boolean) => ({ openAtLogin: open }))
  }
}));

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

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

  it('renders toggle reflecting current state', async () => {
    render(<AutostartSection />);
    const toggle = await screen.findByRole('checkbox');
    expect(toggle).toBeChecked();
  });

  it('clicking toggle calls setAutostart', async () => {
    const { inboxApi } = await import('../../src/renderer/inbox/api.js');
    render(<AutostartSection />);
    const toggle = await screen.findByRole('checkbox');
    fireEvent.click(toggle);
    await waitFor(() => expect(inboxApi.setAutostart).toHaveBeenCalledWith(false));
  });
});
