import { describe, it, expect } from 'vitest'
import { PERMISSIONS, PERMISSION_CATALOG, isPermissionKey } from './permissions'

describe('permissions catalog', () => {
  const permissionValues = Object.values(PERMISSIONS)
  const catalogKeys = PERMISSION_CATALOG.map((p) => p.key)

  it('every PERMISSIONS value appears as a key in PERMISSION_CATALOG', () => {
    for (const value of permissionValues) {
      expect(catalogKeys).toContain(value)
    }
  })

  it('every PERMISSION_CATALOG key exists as a value in PERMISSIONS', () => {
    for (const key of catalogKeys) {
      expect(permissionValues).toContain(key)
    }
  })

  it('every catalog entry has a non-empty description and a boolean isOrgGrantable', () => {
    for (const entry of PERMISSION_CATALOG) {
      expect(typeof entry.description).toBe('string')
      expect(entry.description.trim().length).toBeGreaterThan(0)
      expect(typeof entry.isOrgGrantable).toBe('boolean')
    }
  })

  it('catalog has no duplicate keys', () => {
    const seen = new Set<string>()
    for (const key of catalogKeys) {
      expect(seen.has(key), `duplicate catalog key: ${key}`).toBe(false)
      seen.add(key)
    }
  })

  it('isPermissionKey returns true for every catalog key and false for unknown strings', () => {
    for (const key of catalogKeys) {
      expect(isPermissionKey(key)).toBe(true)
    }
    expect(isPermissionKey('not.a.permission')).toBe(false)
  })
})
