import { fireEvent, screen } from "@testing-library/react";
import { describe, expect, test } from "bun:test";
import { createElement } from "react";

import { ReasoningPart } from "../../../../src/web/consoles/workbench/components/chat/parts/ReasoningPart";
import { renderWithProviders } from "../../test-utils";

describe("ReasoningPart", () => {
  test("流式状态时自动展开显示文本", () => {
    const part = { state: "streaming", text: "正在思考...", type: "reasoning" };

    renderWithProviders(createElement(ReasoningPart, { part }));

    expect(screen.getByText("正在思考...")).toBeTruthy();
    expect(screen.getByText("思考中")).toBeTruthy();
  });

  test("完成状态时显示思考完成标签", () => {
    const part = { state: "complete", text: "思考内容", type: "reasoning" };

    renderWithProviders(createElement(ReasoningPart, { part }));

    expect(screen.getByText("思考完成")).toBeTruthy();
  });

  test("无状态时默认收起", () => {
    const part = { text: "默认内容", type: "reasoning" };

    const { container } = renderWithProviders(createElement(ReasoningPart, { part }));

    const expanded = container.querySelector('[aria-expanded="true"]');
    expect(expanded).toBeNull();
  });

  test("用户点击折叠后尊重用户意图", () => {
    const part = { state: "streaming", text: "思考中内容", type: "reasoning" };

    const { container } = renderWithProviders(createElement(ReasoningPart, { part }));

    const collapseHeader = container.querySelector(".ant-collapse-header");
    expect(collapseHeader).toBeTruthy();

    if (collapseHeader) {
      fireEvent.click(collapseHeader);
    }

    expect(screen.getByText("思考中内容")).toBeTruthy();
  });
});
