/**
 * 6.5: 单测覆盖 ExamHomeworkRoleConfig 角色特性合并逻辑
 */

import { describe, it, expect } from "vitest"
import {
  getExamHomeworkFeatures,
  EXAM_HOMEWORK_ROLE_CONFIG,
  DEFAULT_EXAM_HOMEWORK_FEATURES,
  type ExamHomeworkRoleFeatures,
} from "./exam-homework-role-config"

describe("EXAM_HOMEWORK_ROLE_CONFIG", () => {
  it("admin has all management features but not student take", () => {
    const admin = EXAM_HOMEWORK_ROLE_CONFIG.admin
    expect(admin.canCreate).toBe(true)
    expect(admin.canManage).toBe(true)
    expect(admin.canPublish).toBe(true)
    expect(admin.canGrade).toBe(true)
    expect(admin.canProctor).toBe(true)
    expect(admin.canUseAi).toBe(true)
    expect(admin.canViewStats).toBe(true)
    expect(admin.canTake).toBe(false)
  })

  it("teacher has create/grade/build but not student take", () => {
    const teacher = EXAM_HOMEWORK_ROLE_CONFIG.teacher
    expect(teacher.canCreate).toBe(true)
    expect(teacher.canBuild).toBe(true)
    expect(teacher.canGrade).toBe(true)
    expect(teacher.canUseAi).toBe(true)
    expect(teacher.canTake).toBe(false)
  })

  it("student has take and viewResult but not manage/create", () => {
    const student = EXAM_HOMEWORK_ROLE_CONFIG.student
    expect(student.canTake).toBe(true)
    expect(student.canViewResult).toBe(true)
    expect(student.canCreate).toBe(false)
    expect(student.canManage).toBe(false)
    expect(student.canGrade).toBe(false)
    expect(student.canPublish).toBe(false)
  })

  it("grade_head can grade/publish/proctor but not create/build", () => {
    const gh = EXAM_HOMEWORK_ROLE_CONFIG.grade_head
    expect(gh.canGrade).toBe(true)
    expect(gh.canPublish).toBe(true)
    expect(gh.canProctor).toBe(true)
    expect(gh.canCreate).toBe(false)
    expect(gh.canBuild).toBe(false)
    expect(gh.canUseAi).toBe(false)
  })

  it("parent can only view results", () => {
    const parent = EXAM_HOMEWORK_ROLE_CONFIG.parent
    expect(parent.canViewResult).toBe(true)
    expect(parent.canCreate).toBe(false)
    expect(parent.canRead).toBe(false)
    expect(parent.canTake).toBe(false)
    expect(parent.canGrade).toBe(false)
  })
})

describe("getExamHomeworkFeatures", () => {
  it("returns default (all-false) for empty roles", () => {
    const features = getExamHomeworkFeatures([])
    expect(features).toEqual(DEFAULT_EXAM_HOMEWORK_FEATURES)
  })

  it("returns admin features for admin role", () => {
    const features = getExamHomeworkFeatures(["admin"])
    expect(features.canCreate).toBe(true)
    expect(features.canManage).toBe(true)
    expect(features.canTake).toBe(false)
  })

  it("returns student features for student role", () => {
    const features = getExamHomeworkFeatures(["student"])
    expect(features.canTake).toBe(true)
    expect(features.canCreate).toBe(false)
  })

  it("merges features via union for multiple roles", () => {
    // A user with both teacher and student roles should have both canCreate and canTake
    const features = getExamHomeworkFeatures(["teacher", "student"])
    expect(features.canCreate).toBe(true)
    expect(features.canGrade).toBe(true)
    expect(features.canTake).toBe(true)
    expect(features.canViewResult).toBe(true)
  })

  it("ignores unknown roles gracefully", () => {
    // Cast to bypass type-check for testing runtime safety
    const features = getExamHomeworkFeatures(["unknown_role" as never])
    expect(features).toEqual(DEFAULT_EXAM_HOMEWORK_FEATURES)
  })

  it("DEFAULT_EXAM_HOMEWORK_FEATURES has all features disabled", () => {
    const allFalse: ExamHomeworkRoleFeatures = {
      canCreate: false,
      canRead: false,
      canManage: false,
      canPublish: false,
      canBuild: false,
      canGrade: false,
      canProctor: false,
      canTake: false,
      canViewResult: false,
      canUseAi: false,
      canViewStats: false,
    }
    expect(DEFAULT_EXAM_HOMEWORK_FEATURES).toEqual(allFalse)
  })
})
