import { describe, it, expect } from "vitest"

import { assertClassInScope } from "./scope-check"
import type { DataScope } from "@/shared/types/permissions"

describe("assertClassInScope (P1-4)", () => {
  const classId = "class-1"
  const otherClassId = "class-2"

  it("all scope 始终允许", () => {
    const scope: DataScope = { type: "all" }
    expect(assertClassInScope(scope, classId)).toBeNull()
  })

  it("children scope 始终允许（行级过滤由 data-access 处理）", () => {
    const scope: DataScope = { type: "children", childrenIds: [] }
    expect(assertClassInScope(scope, classId)).toBeNull()
  })

  it("grade_managed scope 始终允许（行级过滤由 data-access 处理）", () => {
    const scope: DataScope = { type: "grade_managed", gradeIds: ["g1"] }
    expect(assertClassInScope(scope, classId)).toBeNull()
  })

  it("class_taught scope 且 classId 在列表中时允许", () => {
    const scope: DataScope = {
      type: "class_taught",
      classIds: [classId, otherClassId],
    }
    expect(assertClassInScope(scope, classId)).toBeNull()
  })

  it("class_taught scope 且 classId 不在列表中时返回 class_taught 错误码", () => {
    const scope: DataScope = {
      type: "class_taught",
      classIds: [otherClassId],
    }
    expect(assertClassInScope(scope, classId)).toBe("class_taught")
  })

  it("class_members scope 且 classId 在列表中时允许", () => {
    const scope: DataScope = {
      type: "class_members",
      classIds: [classId],
    }
    expect(assertClassInScope(scope, classId)).toBeNull()
  })

  it("class_members scope 且 classId 不在列表中时返回 class_members 错误码", () => {
    const scope: DataScope = {
      type: "class_members",
      classIds: [otherClassId],
    }
    expect(assertClassInScope(scope, classId)).toBe("class_members")
  })

  it("owned scope 始终拒绝，返回 denied 错误码", () => {
    const scope: DataScope = { type: "owned", userId: "user-1" }
    expect(assertClassInScope(scope, classId)).toBe("denied")
  })

  it("返回的是错误码字符串而非完整消息（i18n 由调用方处理）", () => {
    const scope: DataScope = {
      type: "class_taught",
      classIds: [],
    }
    const result = assertClassInScope(scope, classId)
    expect(result).toBe("class_taught")
    // 确保不是完整的英文消息
    expect(result).not.toContain("can only")
    expect(result).not.toContain("You")
  })
})
