// frontend/tests/automation/RulesView.test.tsx
import { fireEvent, render, screen } from "@testing-library/react";
import { describe, expect, it, vi } from "vitest";
import { RulesView } from "@/components/automation/RulesView";
import type { RuleOut } from "@/lib/types";

const rule: RuleOut = {
  id: "r1",
  name: "수아님 메일은 바로",
  cat: "mail",
  trigger: "수아님 발신 메일",
  cond: null,
  action: "즉시 알림 + 3줄 요약",
  on: true,
  last: "오늘 09:12",
  runs: 9,
  fresh: false,
  source: "user",
};

describe("RulesView", () => {
  it("토글 스위치 클릭 시 toggle(id) 호출", () => {
    const toggle = vi.fn();
    render(<RulesView rules={[rule]} toggle={toggle} />);
    fireEvent.click(screen.getByLabelText(/수아님 메일은 바로 끄기/));
    expect(toggle).toHaveBeenCalledWith("r1");
  });

  it("카테고리 배지(메일)와 켜짐 카운트", () => {
    render(<RulesView rules={[rule]} toggle={() => {}} />);
    expect(screen.getByText("메일")).toBeInTheDocument();
    expect(screen.getByText("1/1 켜짐")).toBeInTheDocument();
  });

  it("off 규칙은 rl-row off 클래스", () => {
    render(<RulesView rules={[{ ...rule, on: false }]} toggle={() => {}} />);
    expect(screen.getByText("수아님 메일은 바로").closest(".rl-row")).toHaveClass("off");
  });
});
