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

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

describe("MarkdownTable 渲染表格", () => {
  test("渲染原生 table 元素并添加 class", () => {
    const children = createElement(
      "thead",
      null,
      createElement("tr", null, createElement("th", null, "列1"), createElement("th", null, "列2")),
    );

    renderWithProviders(createElement(MarkdownTable, { children }));

    const table = document.querySelector(".markdown-table");
    expect(table).toBeTruthy();
    expect(table!.tagName).toBe("TABLE");
    expect(screen.getByText("列1")).toBeTruthy();
    expect(screen.getByText("列2")).toBeTruthy();
  });

  test("正确传递 children 内容", () => {
    const children = createElement(
      "tbody",
      null,
      createElement("tr", null, createElement("td", null, "值1"), createElement("td", null, "值2")),
    );

    renderWithProviders(createElement(MarkdownTable, { children }));

    expect(screen.getByText("值1")).toBeTruthy();
    expect(screen.getByText("值2")).toBeTruthy();
  });

  test("只有传入 table 元素时才有 class", () => {
    const { container } = renderWithProviders(createElement(MarkdownTable, { children: null }));

    expect(container.querySelector(".markdown-table")).toBeTruthy();
  });
});
