/**
 * 单元测试：菜单结构与路由重定向
 *
 * _Requirements: 1.1, 8.3, 10.1, 10.2_
 *
 * - 验证 NAV_ITEMS 包含 7 个一级菜单项且子项正确
 * - 验证 `/` 重定向到 `/dashboard`
 * - 验证 `/log-viewer` 重定向到 `/etl-tasks?tab=queue`
 */

import { describe, it, expect } from "vitest";
import { render } from "@testing-library/react";
import { MemoryRouter, Routes, Route, Navigate, useLocation } from "react-router-dom";
import { NAV_ITEMS } from "../App";
import type { MenuProps } from "antd";

/* ------------------------------------------------------------------ */
/*  菜单结构测试（纯数据，无 DOM）                                       */
/* ------------------------------------------------------------------ */

type MenuItem = NonNullable<MenuProps["items"]>[number] & {
  key?: string;
  label?: string;
  children?: MenuItem[];
};

describe("菜单结构验证 (Requirement 1.1)", () => {
  const items = NAV_ITEMS as MenuItem[];

  it("NAV_ITEMS 包含 7 个一级菜单项", () => {
    expect(items).toHaveLength(7);
  });

  it("7 个一级菜单项的 label 和顺序正确", () => {
    const labels = items.map((item) => item.label);
    expect(labels).toEqual([
      "运行状态",
      "ETL 任务管理",
      "小程序任务管理",
      "触发器管理",
      "租户管理员",
      "系统设置",
      "日志调试",
    ]);
  });

  it("「小程序任务管理」包含 4 个子项：定时任务、转移日志、待审核任务、参数管理", () => {
    const taskEngine = items.find((i) => i.label === "小程序任务管理");
    expect(taskEngine).toBeDefined();
    const children = taskEngine!.children ?? [];
    expect(children).toHaveLength(4);
    expect(children.map((c) => c.label)).toEqual([
      "定时任务",
      "转移日志",
      "待审核任务",
      "参数管理",
    ]);
  });

  it("「系统设置」包含 2 个子项：环境配置、触发器配置", () => {
    const settings = items.find((i) => i.label === "系统设置");
    expect(settings).toBeDefined();
    const children = settings!.children ?? [];
    expect(children).toHaveLength(2);
    expect(children.map((c) => c.label)).toEqual(["环境配置", "触发器配置"]);
  });

  it("「日志调试」包含 3 个子项：DevTrace、AI 调用明细、数据库查看器", () => {
    const logs = items.find((i) => i.label === "日志调试");
    expect(logs).toBeDefined();
    const children = logs!.children ?? [];
    expect(children).toHaveLength(3);
    expect(children.map((c) => c.label)).toEqual([
      "DevTrace",
      "AI 调用明细",
      "数据库查看器",
    ]);
  });

  it("无子项的一级菜单（运行状态、ETL 任务管理、触发器管理、租户管理员）没有 children", () => {
    const noChildrenLabels = ["运行状态", "ETL 任务管理", "触发器管理", "租户管理员"];
    for (const label of noChildrenLabels) {
      const item = items.find((i) => i.label === label);
      expect(item).toBeDefined();
      expect((item as MenuItem).children).toBeUndefined();
    }
  });
});

/* ------------------------------------------------------------------ */
/*  路由重定向测试                                                      */
/* ------------------------------------------------------------------ */

/**
 * 辅助组件：捕获当前 location 用于断言。
 * 渲染后通过 testId 读取 pathname + search。
 */
function LocationDisplay() {
  const location = useLocation();
  return (
    <div data-testid="location">
      {location.pathname}
      {location.search}
    </div>
  );
}

/**
 * 最小路由配置：只包含重定向规则和一个 LocationDisplay 兜底，
 * 不需要渲染完整 App（避免 mock 大量依赖）。
 */
function RedirectTestApp() {
  return (
    <Routes>
      <Route path="/" element={<Navigate to="/dashboard" replace />} />
      <Route path="/log-viewer" element={<Navigate to="/etl-tasks?tab=queue" replace />} />
      <Route path="*" element={<LocationDisplay />} />
    </Routes>
  );
}

describe("路由重定向 (Requirements 8.3, 10.1, 10.2)", () => {
  it("/ 重定向到 /dashboard", () => {
    const { getByTestId } = render(
      <MemoryRouter initialEntries={["/"]}>
        <RedirectTestApp />
      </MemoryRouter>,
    );
    expect(getByTestId("location").textContent).toBe("/dashboard");
  });

  it("/log-viewer 重定向到 /etl-tasks?tab=queue", () => {
    const { getByTestId } = render(
      <MemoryRouter initialEntries={["/log-viewer"]}>
        <RedirectTestApp />
      </MemoryRouter>,
    );
    expect(getByTestId("location").textContent).toBe("/etl-tasks?tab=queue");
  });
});
