import { act, fireEvent, render, screen } from "@testing-library/react";
import { describe, expect, it } from "vitest";
import FloatingRouterHelper from "./FloatingRouterHelper";
import { dispatchAppCommand } from "../utils/appCommands";

describe("FloatingRouterHelper", () => {
  it("renders one shared help launcher when closed", () => {
    render(<FloatingRouterHelper />);

    const button = screen.getByRole("button", { name: "Open help and assist launcher" });
    expect(button).toHaveTextContent("Help");
    expect(screen.queryByText("Get support")).not.toBeInTheDocument();
    expect(screen.queryByText("Open router helper")).not.toBeInTheDocument();
  });

  it("opens the support tab from the support command", () => {
    render(<FloatingRouterHelper />);

    act(() => {
      dispatchAppCommand("support:open");
    });

    expect(screen.getByRole("tabpanel", { name: "Support tab" })).toBeInTheDocument();
    expect(screen.getByRole("tab", { name: "Support" })).toHaveAttribute("aria-selected", "true");
    expect(screen.getByRole("link", { name: "Open Slack support" })).toBeInTheDocument();
  });

  it("opens the assist tab from the helper command", () => {
    render(<FloatingRouterHelper />);

    act(() => {
      dispatchAppCommand("router_helper:open");
    });

    expect(screen.getByRole("tabpanel", { name: "Assist tab" })).toBeInTheDocument();
    expect(screen.getByRole("tab", { name: "Assist" })).toHaveAttribute("aria-selected", "true");
    expect(screen.queryByText("SECURITY CHECK")).not.toBeInTheDocument();
    expect(screen.getByRole("button", { name: "Ask assist" })).toBeDisabled();
  });

  it("lets the user switch between assist and support in one launcher", () => {
    render(<FloatingRouterHelper />);

    fireEvent.click(screen.getByRole("button", { name: "Open help and assist launcher" }));
    fireEvent.click(screen.getByRole("tab", { name: "Support" }));
    expect(screen.getByRole("tabpanel", { name: "Support tab" })).toBeInTheDocument();

    fireEvent.click(screen.getByRole("tab", { name: "Assist" }));
    expect(screen.getByRole("tabpanel", { name: "Assist tab" })).toBeInTheDocument();
  });
});
