/* eslint-disable @typescript-eslint/require-await */
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 = (async () => {
      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("渲染侧边栏菜单项", () => {
    window.fetch = (async () => {
      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);
    expect(screen.getAllByText("用户管理").length).toBeGreaterThan(0);
    expect(screen.getAllByText("系统设置").length).toBeGreaterThan(0);
  });

  test("Header 不包含侧边栏折叠按钮", () => {
    window.fetch = (async () => {
      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 toggleButtons = document.querySelectorAll(".app-sidebar-toggle");
    expect(toggleButtons.length).toBe(0);
  });
});
