// Zustand child-store 单测
// 依据：02-architecture-design.md §4.3 状态管理分层、ISSUE-009 纯前端切换
// 覆盖：setChildren 默认选中 / switchChild 不调后端 / localStorage 持久化 / reset

import { describe, it, expect, beforeEach, vi } from "vitest";
import { useChildStore } from "./child-store";
import type { ChildInfo } from "@/types";

const mockChildren: ChildInfo[] = [
  {
    id: "student-001",
    name: "张小明",
    grade: "grade.7",
    schoolName: "实验中学",
    classId: "class-7-1",
    className: "初一(1)班",
  },
  {
    id: "student-002",
    name: "张小红",
    grade: "grade.5",
    schoolName: "实验小学",
    classId: "class-5-2",
    className: "五年级(2)班",
  },
];

beforeEach(() => {
  localStorage.clear();
  // 重置 store 到初始状态
  useChildStore.setState({
    children: [],
    currentChildId: null,
    isLoading: false,
  });
});

describe("setChildren", () => {
  it("设置子女列表", () => {
    useChildStore.getState().setChildren(mockChildren);
    expect(useChildStore.getState().children).toHaveLength(2);
  });

  it("无 currentChildId 时默认选第一个活跃子女", () => {
    useChildStore.getState().setChildren(mockChildren);
    expect(useChildStore.getState().currentChildId).toBe("student-001");
  });

  it("已存在的 currentChildId 保持不变", () => {
    localStorage.setItem("parent_current_child_id", "student-002");
    useChildStore.setState({ currentChildId: "student-002" });
    useChildStore.getState().setChildren(mockChildren);
    expect(useChildStore.getState().currentChildId).toBe("student-002");
  });

  it("currentChildId 不在列表中时重选第一个", () => {
    useChildStore.setState({ currentChildId: "student-999" });
    useChildStore.getState().setChildren(mockChildren);
    expect(useChildStore.getState().currentChildId).toBe("student-001");
  });

  it("跳过已归档子女作为默认选择", () => {
    const withArchived: ChildInfo[] = [
      { ...mockChildren[0]!, isArchived: true },
      mockChildren[1]!,
    ];
    useChildStore.getState().setChildren(withArchived);
    expect(useChildStore.getState().currentChildId).toBe("student-002");
  });

  it("默认选中后写入 localStorage", () => {
    useChildStore.getState().setChildren(mockChildren);
    expect(localStorage.getItem("parent_current_child_id")).toBe("student-001");
  });

  it("空列表时 currentChildId 为 null", () => {
    useChildStore.getState().setChildren([]);
    expect(useChildStore.getState().currentChildId).toBeNull();
  });
});

describe("switchChild（ISSUE-009：纯前端切换）", () => {
  beforeEach(() => {
    useChildStore.getState().setChildren(mockChildren);
  });

  it("切换到列表中存在的子女", () => {
    useChildStore.getState().switchChild("student-002");
    expect(useChildStore.getState().currentChildId).toBe("student-002");
  });

  it("切换不存在的子女被忽略", () => {
    useChildStore.getState().switchChild("student-999");
    expect(useChildStore.getState().currentChildId).toBe("student-001");
  });

  it("切换后写入 localStorage", () => {
    useChildStore.getState().switchChild("student-002");
    expect(localStorage.getItem("parent_current_child_id")).toBe("student-002");
  });

  it("切换不触发后端请求（无 fetch 调用）", () => {
    const fetchSpy = vi.spyOn(globalThis, "fetch");
    useChildStore.getState().switchChild("student-002");
    expect(fetchSpy).not.toHaveBeenCalled();
    fetchSpy.mockRestore();
  });
});

describe("setLoading", () => {
  it("设置 loading 状态", () => {
    useChildStore.getState().setLoading(true);
    expect(useChildStore.getState().isLoading).toBe(true);
    useChildStore.getState().setLoading(false);
    expect(useChildStore.getState().isLoading).toBe(false);
  });
});

describe("reset", () => {
  it("清空所有状态", () => {
    useChildStore.getState().setChildren(mockChildren);
    useChildStore.getState().switchChild("student-002");
    useChildStore.getState().setLoading(true);
    useChildStore.getState().reset();
    expect(useChildStore.getState().children).toEqual([]);
    expect(useChildStore.getState().currentChildId).toBeNull();
    expect(useChildStore.getState().isLoading).toBe(false);
  });

  it("清空 localStorage 中的 current_child_id", () => {
    useChildStore.getState().setChildren(mockChildren);
    useChildStore.getState().reset();
    expect(localStorage.getItem("parent_current_child_id")).toBeNull();
  });
});
