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

import { ProjectsPage } from "../../../src/web/pages/projects";
import { renderWithProviders } from "../test-utils";

describe("ProjectsPage", () => {
  test("渲染 Tab、搜索框、新建按钮和表格", async () => {
    renderWithProviders(createElement(ProjectsPage));

    expect(screen.getByText("进行中")).not.toBeNull();
    expect(screen.getByText("已归档")).not.toBeNull();
    expect(screen.getByText("新建项目")).not.toBeNull();
    expect(screen.getByPlaceholderText("搜索项目名称或描述")).not.toBeNull();

    await waitFor(
      () => {
        const body = document.body.textContent ?? "";
        expect(body).toContain("项目名称");
      },
      { timeout: 10000 },
    );
  });

  test("新建按钮点击打开弹窗", async () => {
    renderWithProviders(createElement(ProjectsPage));

    const createBtn = screen.getByRole("button", { name: /新建项目/ });
    fireEvent.click(createBtn);

    await waitFor(
      () => {
        expect(document.body.querySelector(".ant-modal")).not.toBeNull();
      },
      { timeout: 10000 },
    );
  });
});
