import { 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));

    // Tabs, search, create button always visible
    expect(screen.getByText("进行中")).not.toBeNull();
    expect(screen.getByText("已归档")).not.toBeNull();
    expect(screen.getByText("搜索")).not.toBeNull();
    expect(screen.getByText("新建项目")).not.toBeNull();

    // Table renders (shows loading or empty state)
    await waitFor(
      () => {
        const body = document.body.textContent ?? "";
        expect(body).toContain("项目名称");
      },
      { timeout: 10000 },
    );
  });

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

    const createBtn = screen.getByText("新建项目");
    createBtn.click();

    await waitFor(
      () => {
        expect(screen.getByText("确定")).not.toBeNull();
      },
      { timeout: 5000 },
    );
  });
});
