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

import { ToolPart } from "../../../../src/web/features/chat/parts/ToolPart";
import { renderWithProviders } from "../../test-utils";

describe("ToolPart 工具显示名", () => {
  test("无 toolMetadata 时使用 toolName", () => {
    const part = {
      input: { timezone: "Asia/Shanghai" },
      output: { iso: "2024-01-01T00:00:00.000Z", local: "2024年1月1日", timestamp: 1704067200000 },
      toolCallId: "call-1",
      toolName: "getCurrentTime",
      type: "tool-getCurrentTime",
    };

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

    expect(screen.getByText(/getCurrentTime/)).toBeTruthy();
  });

  test("有 toolMetadata.displayName 时优先使用显示名", () => {
    const part = {
      input: { timezone: "Asia/Shanghai" },
      output: { iso: "2024-01-01T00:00:00.000Z", local: "2024年1月1日", timestamp: 1704067200000 },
      toolCallId: "call-1",
      toolMetadata: { displayName: "获取当前时间" },
      toolName: "getCurrentTime",
      type: "tool-getCurrentTime",
    };

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

    expect(screen.getByText("获取当前时间")).toBeTruthy();
    expect(screen.queryByText(/getCurrentTime/)).toBeNull();
  });

  test("toolMetadata.displayName 非字符串时回退到 toolName", () => {
    const part = {
      input: {},
      output: {},
      toolCallId: "call-2",
      toolMetadata: { displayName: 123 },
      type: "tool-someTool",
    };

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

    expect(screen.getByText("someTool")).toBeTruthy();
  });

  test("错误状态时使用显示名", () => {
    const part = {
      errorText: "超时",
      toolCallId: "call-3",
      toolMetadata: { displayName: "获取当前时间" },
      type: "tool-getCurrentTime",
    };

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

    expect(screen.getByText(/获取当前时间.*失败/)).toBeTruthy();
  });
});

describe("ToolPart 入参/出参分层展示", () => {
  test("输入流式状态时面板展开显示入参区块标题", () => {
    const part = {
      input: { timezone: "Asia/Shanghai" },
      toolCallId: "call-stream",
      toolName: "getCurrentTime",
      type: "tool-getCurrentTime",
    };

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

    const inputLabels = screen.getAllByText("入参");
    expect(inputLabels.length).toBeGreaterThan(0);
  });

  test("点击面板展开后显示入参出参区块", () => {
    const part = {
      input: { timezone: "Asia/Shanghai" },
      output: { iso: "2024-01-01T00:00:00.000Z" },
      toolCallId: "call-1",
      toolName: "getCurrentTime",
      type: "tool-getCurrentTime",
    };

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

    const header = container.querySelector(".ant-collapse-header");
    expect(header).toBeTruthy();
    if (header) fireEvent.click(header);

    expect(screen.getByText("入参")).toBeTruthy();
    expect(screen.getByText("出参")).toBeTruthy();
  });

  test("输入流式状态时显示生成中文字", () => {
    const part = {
      toolCallId: "call-stream",
      toolName: "getCurrentTime",
      type: "tool-getCurrentTime",
    };

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

    expect(screen.getByText("生成中...")).toBeTruthy();
  });
});

describe("ToolPart 错误区块", () => {
  test("点击错误面板展开后显示错误标题和错误文本（由 HighlightBlock 渲染）", () => {
    const part = {
      errorText: "网络超时",
      toolCallId: "call-err",
      toolName: "getCurrentTime",
      type: "tool-getCurrentTime",
    };

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

    const header = container.querySelector(".ant-collapse-header");
    expect(header).toBeTruthy();
    if (header) fireEvent.click(header);

    expect(screen.getByText("错误")).toBeTruthy();
    expect(screen.getByText("网络超时")).toBeTruthy();
    // 错误文本在 HighlightBlock 中渲染，带 code-block 结构
    expect(container.querySelector(".code-block")).toBeTruthy();
  });
});
