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

import { APP } from "../../src/shared/app";
import { App } from "../../src/web/app";
import { renderWithProviders } from "./test-utils";

describe("App", () => {
  test("渲染 Layout 骨架和品牌名", () => {
    window.fetch = (() => {
      return new Response(
        JSON.stringify({ ok: true, service: "test-app", timestamp: new Date().toISOString(), version: "0.1.0" }),
        {
          headers: { "Content-Type": "application/json" },
          status: 200,
        },
      );
    }) as unknown as typeof fetch;

    renderWithProviders(createElement(App));

    expect(screen.getByText(APP.title)).not.toBeNull();
    expect(screen.getByText("系统")).not.toBeNull();
    expect(screen.getByText("明亮")).not.toBeNull();
    expect(screen.getByText("黑暗")).not.toBeNull();
  });

  test("渲染 Admin 侧边栏菜单项", () => {
    window.fetch = (() => {
      return new Response(
        JSON.stringify({ ok: true, service: "test-app", timestamp: new Date().toISOString(), version: "0.1.0" }),
        {
          headers: { "Content-Type": "application/json" },
          status: 200,
        },
      );
    }) as unknown as typeof fetch;

    renderWithProviders(createElement(App));

    expect(screen.getAllByText("总览").length).toBeGreaterThan(0);
    expect(screen.getAllByText("项目管理").length).toBeGreaterThan(0);
  });

  test("Sider 渲染侧边栏菜单", () => {
    window.fetch = (() => {
      return new Response(
        JSON.stringify({ ok: true, service: "test-app", timestamp: new Date().toISOString(), version: "0.1.0" }),
        {
          headers: { "Content-Type": "application/json" },
          status: 200,
        },
      );
    }) as unknown as typeof fetch;

    renderWithProviders(createElement(App));

    const sider = document.querySelector(".ant-layout-sider");
    expect(sider).not.toBeNull();
    const menu = document.querySelector(".ant-menu");
    expect(menu).not.toBeNull();
  });

  test("Admin header 显示管理台标题", () => {
    window.fetch = (() => {
      return new Response(
        JSON.stringify({ ok: true, service: "test-app", timestamp: new Date().toISOString(), version: "0.1.0" }),
        {
          headers: { "Content-Type": "application/json" },
          status: 200,
        },
      );
    }) as unknown as typeof fetch;

    renderWithProviders(createElement(App));

    expect(screen.getByText("管理台")).not.toBeNull();
  });
});
