import { render, screen, act } from "@testing-library/react";
import { expect, test, vi, beforeEach } from "vitest";
import App from "./App";
import { api } from "./api";

vi.mock("./api", () => {
  return {
    api: {
      getInitialState: vi.fn(),
      showSettingsWindow: vi.fn(),
      updateConfig: vi.fn(),
      getHotkeyStatus: vi.fn(),
    },
  };
});

beforeEach(() => {
  vi.clearAllMocks();
});

test("mounts App and fetches initial state", async () => {
  const mockInitialPayload = {
    config: {
      strip_utms: true,
      fix_quotes: true,
      fix_newlines: true,
      play_sound: true,
      autostart: false,
      hotkey: "Alt+Shift+C",
      custom_params: [],
    },
    hotkey_conflict: false,
    hwid: "device-123",
    is_licensed: true,
  };

  vi.mocked(api.getInitialState).mockResolvedValue(mockInitialPayload);
  vi.mocked(api.showSettingsWindow).mockResolvedValue(undefined);

  await act(async () => {
    render(<App />);
  });

  expect(api.getInitialState).toHaveBeenCalledTimes(1);
  expect(api.showSettingsWindow).toHaveBeenCalledTimes(1);

  expect(screen.getByText("Alt")).toBeInTheDocument();
  expect(screen.getByText("Shift")).toBeInTheDocument();
  expect(screen.getAllByText("C").length).toBe(2); // One in logo, one in keycap
  expect(screen.getByText("Play sound on clean")).toBeInTheDocument();
});
