/**
 * 权限常量与路由权限映射测试
 */
import { describe, expect, it } from "vitest";
import {
  ADMIN_PERMISSIONS,
  IAM_PERMISSIONS,
  PERMISSIONS,
  ROUTE_PERMISSIONS,
  ADMIN_ERROR_PREFIX,
  CONSUMED_ERROR_PREFIXES,
} from "./permissions";

describe("permissions constants", () => {
  it("ADMIN_PERMISSIONS 全部以 ADMIN_ 前缀", () => {
    for (const code of Object.values(ADMIN_PERMISSIONS)) {
      expect(code.startsWith("ADMIN_")).toBe(true);
    }
  });

  it("IAM_PERMISSIONS 全部以 IAM_ 前缀", () => {
    for (const code of Object.values(IAM_PERMISSIONS)) {
      expect(code.startsWith("IAM_")).toBe(true);
    }
  });

  it("PERMISSIONS 合并 ADMIN + IAM", () => {
    expect(PERMISSIONS.DASHBOARD_VIEW).toBe("ADMIN_DASHBOARD_VIEW");
    expect(PERMISSIONS.USER_READ).toBe("IAM_USER_READ");
  });

  it("包含必需的管理权限点", () => {
    expect(ADMIN_PERMISSIONS.SCHOOL_READ).toBeDefined();
    expect(ADMIN_PERMISSIONS.GRADE_MANAGE).toBeDefined();
    expect(ADMIN_PERMISSIONS.ANNOUNCEMENT_MANAGE).toBeDefined();
    expect(ADMIN_PERMISSIONS.FILE_MANAGE).toBeDefined();
    expect(ADMIN_PERMISSIONS.AI_CONFIGURE).toBeDefined();
    expect(ADMIN_PERMISSIONS.INVITATION_CODE_MANAGE).toBeDefined();
  });

  it("ROUTE_PERMISSIONS 覆盖所有 admin 路由", () => {
    const expectedRoutes = [
      "/admin/dashboard",
      "/admin/users",
      "/admin/roles",
      "/admin/permissions",
      "/admin/viewports",
      "/admin/organization",
      "/admin/system",
      "/admin/classes",
      "/admin/teachers",
      "/admin/students",
      "/admin/audit-logs",
      "/admin/audit-logs/overview",
      "/admin/audit-logs/data-changes",
      "/admin/audit-logs/login-logs",
      "/admin/school",
      "/admin/school/schools",
      "/admin/school/grades",
      "/admin/school/grades/insights",
      "/admin/school/departments",
      "/admin/school/academic-year",
      "/admin/announcements",
      "/admin/invitation-codes",
      "/admin/files",
      "/admin/ai-settings",
    ];
    for (const route of expectedRoutes) {
      expect(ROUTE_PERMISSIONS[route]).toBeTruthy();
    }
  });

  it("ROUTE_PERMISSIONS 审计子路由映射到 AUDIT_READ", () => {
    expect(ROUTE_PERMISSIONS["/admin/audit-logs/overview"]).toBe(
      ADMIN_PERMISSIONS.AUDIT_READ,
    );
    expect(ROUTE_PERMISSIONS["/admin/audit-logs/data-changes"]).toBe(
      ADMIN_PERMISSIONS.AUDIT_READ,
    );
  });

  it("ADMIN_ERROR_PREFIX 为 ADMIN_", () => {
    expect(ADMIN_ERROR_PREFIX).toBe("ADMIN_");
  });

  it("CONSUMED_ERROR_PREFIXES 包含所有上游错误前缀", () => {
    expect(CONSUMED_ERROR_PREFIXES).toContain("IAM_");
    expect(CONSUMED_ERROR_PREFIXES).toContain("BFF_TEACHER_");
    expect(CONSUMED_ERROR_PREFIXES).toContain("GW_");
    expect(CONSUMED_ERROR_PREFIXES).toContain("ADMIN_");
  });
});
