// CoursePlanDetail 组件单测
// 依据：02-architecture-design.md §15 组件设计
// 覆盖：基本信息 / 章节列表（按 sortOrder 排序）/ 状态徽标 / 章节课时与学时

import { describe, it, expect } from "vitest";
import { render, screen } from "@testing-library/react";
import { CoursePlanDetail } from "./CoursePlanDetail";
import type { CoursePlanDetail as CoursePlanDetailType } from "@/types";

const mockDetail: CoursePlanDetailType = {
  id: "cp-001",
  title: "初一数学下学期课程计划",
  description: "涵盖一元二次方程、不等式组、函数与图像等核心章节",
  subject: "数学",
  className: "初一(1)班",
  teacherName: "王老师",
  startDate: "2026-02-15",
  endDate: "2026-07-10",
  status: "active",
  chapterCount: 8,
  chapters: [
    {
      id: "ch-02",
      title: "第二章 不等式与不等式组",
      description: "掌握一元一次不等式及不等式组的解法",
      sortOrder: 2,
      lessonCount: 6,
      estimatedHours: 4.5,
    },
    {
      id: "ch-01",
      title: "第一章 一元二次方程",
      description: "学习一元二次方程的概念、解法及其应用",
      sortOrder: 1,
      lessonCount: 8,
      estimatedHours: 6,
    },
    {
      id: "ch-03",
      title: "第三章 函数与图像",
      description: "理解函数概念，掌握一次函数与反比例函数",
      sortOrder: 3,
      lessonCount: 10,
      estimatedHours: 8,
    },
  ],
};

describe("CoursePlanDetail 基本信息", () => {
  it("渲染标题与描述", () => {
    render(<CoursePlanDetail coursePlan={mockDetail} />);
    expect(
      screen.getByText("初一数学下学期课程计划"),
    ).toBeInTheDocument();
    expect(
      screen.getByText(
        "涵盖一元二次方程、不等式组、函数与图像等核心章节",
      ),
    ).toBeInTheDocument();
  });

  it("渲染学科标签与状态徽标", () => {
    render(<CoursePlanDetail coursePlan={mockDetail} />);
    expect(screen.getByText("数学")).toBeInTheDocument();
    expect(screen.getByText("进行中")).toBeInTheDocument();
  });

  it("渲染班级、教师、章节数等基本信息", () => {
    render(<CoursePlanDetail coursePlan={mockDetail} />);
    expect(screen.getByText("初一(1)班")).toBeInTheDocument();
    expect(screen.getByText("王老师")).toBeInTheDocument();
    expect(screen.getByText("8")).toBeInTheDocument();
  });
});

describe("CoursePlanDetail 章节列表", () => {
  it("渲染全部章节并按 sortOrder 升序排列", () => {
    const { container } = render(
      <CoursePlanDetail coursePlan={mockDetail} />,
    );
    const chapterHeadings = container.querySelectorAll("h4");
    expect(chapterHeadings).toHaveLength(3);
    // sortOrder: 1, 2, 3
    expect(chapterHeadings[0]!.textContent).toBe("第一章 一元二次方程");
    expect(chapterHeadings[1]!.textContent).toBe("第二章 不等式与不等式组");
    expect(chapterHeadings[2]!.textContent).toBe("第三章 函数与图像");
  });

  it("渲染章节序号", () => {
    render(<CoursePlanDetail coursePlan={mockDetail} />);
    expect(screen.getByText("第 1 章")).toBeInTheDocument();
    expect(screen.getByText("第 2 章")).toBeInTheDocument();
    expect(screen.getByText("第 3 章")).toBeInTheDocument();
  });

  it("渲染章节课时数与预计学时", () => {
    render(<CoursePlanDetail coursePlan={mockDetail} />);
    expect(screen.getByText("8 课时")).toBeInTheDocument();
    expect(screen.getByText("6 课时")).toBeInTheDocument();
    expect(screen.getByText("10 课时")).toBeInTheDocument();
    expect(screen.getByText("预计 6 学时")).toBeInTheDocument();
    expect(screen.getByText("预计 4.5 学时")).toBeInTheDocument();
    expect(screen.getByText("预计 8 学时")).toBeInTheDocument();
  });

  it("渲染章节描述", () => {
    render(<CoursePlanDetail coursePlan={mockDetail} />);
    expect(
      screen.getByText("学习一元二次方程的概念、解法及其应用"),
    ).toBeInTheDocument();
    expect(
      screen.getByText("掌握一元一次不等式及不等式组的解法"),
    ).toBeInTheDocument();
  });
});
