// 权限点常量单测
// 依据：F7 裁决（权限点命名 <RESOURCE>_<ACTION>[_<SCOPE>]）、project_rules §3.1
// 覆盖：PERMISSIONS 常量定义 / 命名规范 / Permission 类型导出

import { describe, it, expect } from "vitest";
import { PERMISSIONS } from "./permissions";

describe("PERMISSIONS 常量定义", () => {
  it("包含仪表盘查看权限", () => {
    expect(PERMISSIONS.DASHBOARD_VIEW).toBe("DASHBOARD_VIEW");
  });

  it("包含子女成绩查看权限", () => {
    expect(PERMISSIONS.CHILD_GRADE_VIEW).toBe("CHILD_GRADE_VIEW");
  });

  it("包含子女考勤查看权限", () => {
    expect(PERMISSIONS.CHILD_ATTENDANCE_VIEW).toBe("CHILD_ATTENDANCE_VIEW");
  });

  it("包含子女作业查看权限", () => {
    expect(PERMISSIONS.CHILD_HOMEWORK_VIEW).toBe("CHILD_HOMEWORK_VIEW");
  });

  it("包含子女学情查看权限", () => {
    expect(PERMISSIONS.CHILD_WEAKNESS_VIEW).toBe("CHILD_WEAKNESS_VIEW");
  });

  it("包含子女学习趋势查看权限", () => {
    expect(PERMISSIONS.CHILD_TREND_VIEW).toBe("CHILD_TREND_VIEW");
  });

  it("包含子女考试查看权限", () => {
    expect(PERMISSIONS.CHILD_EXAM_VIEW).toBe("CHILD_EXAM_VIEW");
  });

  it("包含子女班级查看权限", () => {
    expect(PERMISSIONS.CHILD_CLASS_VIEW).toBe("CHILD_CLASS_VIEW");
  });

  it("包含子女学习路径查看权限", () => {
    expect(PERMISSIONS.CHILD_LEARNING_PATH_VIEW).toBe(
      "CHILD_LEARNING_PATH_VIEW",
    );
  });

  it("包含个人信息查看权限", () => {
    expect(PERMISSIONS.PROFILE_VIEW).toBe("PROFILE_VIEW");
  });

  it("包含通知查看权限", () => {
    expect(PERMISSIONS.NOTIFICATION_VIEW).toBe("NOTIFICATION_VIEW");
  });

  it("包含通知偏好更新权限", () => {
    expect(PERMISSIONS.NOTIFICATION_PREFERENCE_UPDATE).toBe(
      "NOTIFICATION_PREFERENCE_UPDATE",
    );
  });
});

describe("PERMISSIONS 命名规范（F7：<RESOURCE>_<ACTION>[_<SCOPE>]）", () => {
  it("所有权限点 key 与 value 一致", () => {
    for (const [key, value] of Object.entries(PERMISSIONS)) {
      expect(key).toBe(value);
    }
  });

  it("所有权限点 value 匹配 <RESOURCE>_<ACTION> 命名格式", () => {
    const pattern = /^[A-Z]+(_[A-Z]+)+$/;
    for (const value of Object.values(PERMISSIONS)) {
      expect(value).toMatch(pattern);
    }
  });

  it("所有权限点 value 唯一", () => {
    const values = Object.values(PERMISSIONS);
    const unique = new Set(values);
    expect(unique.size).toBe(values.length);
  });
});

describe("PERMISSIONS 覆盖 AppShell 导航所需权限点", () => {
  it("包含 AppShell 中所有 NAV_ITEM.permission 引用的权限点", () => {
    // 与 src/components/AppShell.tsx 中的 NAV_ITEMS 对齐
    const required = [
      "DASHBOARD_VIEW",
      "CHILD_GRADE_VIEW",
      "CHILD_EXAM_VIEW",
      "CHILD_ATTENDANCE_VIEW",
      "CHILD_CLASS_VIEW",
      "CHILD_HOMEWORK_VIEW",
      "CHILD_WEAKNESS_VIEW",
      "CHILD_LEARNING_PATH_VIEW",
      "CHILD_TREND_VIEW",
      "NOTIFICATION_VIEW",
      "NOTIFICATION_PREFERENCE_UPDATE",
      "PROFILE_VIEW",
    ];
    const available = Object.values(PERMISSIONS);
    for (const p of required) {
      expect(available).toContain(p);
    }
  });
});
