import { fireEvent, render, screen } from "@testing-library/react";
import { describe, expect, it } from "vitest";
import AssistantWorkspace, { AssistantSetupPanel } from "./AssistantWorkspace";

describe("AssistantWorkspace", () => {
  it("collapses setup after the first conversation turn and allows reopening it", () => {
    const { rerender } = render(
      <AssistantSetupPanel summary="Use setup before starting." hasConversation={false}>
        <div>Setup body</div>
      </AssistantSetupPanel>
    );

    expect(screen.getByText("Use setup before starting.")).toBeInTheDocument();
    expect(screen.getByText("Setup body")).toBeInTheDocument();

    rerender(
      <AssistantSetupPanel summary="Use setup before starting." hasConversation>
        <div>Setup body</div>
      </AssistantSetupPanel>
    );

    expect(screen.queryByText("Use setup before starting.")).not.toBeInTheDocument();
    expect(screen.queryByText("Setup body")).not.toBeInTheDocument();

    fireEvent.click(screen.getByRole("button", { name: "Show setup" }));
    expect(screen.getByText("Setup body")).toBeInTheDocument();
  });

  it("shows setup by default before the conversation starts", () => {
    render(
      <AssistantWorkspace
        title="Assistant shell"
        description="Conversation first."
        setupSummary="Use setup before starting."
        hasConversation={false}
        setupContent={<div>Setup body</div>}
        toolsContent={<div>Tools body</div>}
        conversationContent={<div>Conversation body</div>}
        secondaryRail={<div>Prompt rail</div>}
      />
    );

    expect(screen.getByText("Use setup before starting.")).toBeInTheDocument();
    expect(screen.getByText("Setup body")).toBeInTheDocument();
    expect(screen.getByText("Tools body")).toBeInTheDocument();
  });

  it("reopens setup after the conversation is reset", () => {
    const { rerender } = render(
      <AssistantSetupPanel summary="Use setup before starting." hasConversation>
        <div>Setup body</div>
      </AssistantSetupPanel>
    );

    expect(screen.queryByText("Setup body")).not.toBeInTheDocument();

    rerender(
      <AssistantSetupPanel summary="Use setup before starting." hasConversation={false}>
        <div>Setup body</div>
      </AssistantSetupPanel>
    );

    expect(screen.getByText("Use setup before starting.")).toBeInTheDocument();
    expect(screen.getByText("Setup body")).toBeInTheDocument();
  });
});
