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

/**
 * 权限码归一化工具测试
 * 测试三段式与四段式权限码的互相兼容匹配
 */

describe('权限码归一化工具', () => {
  /**
   * 模拟 isPermissionCodeMatch 函数逻辑
   */
  const isPermissionCodeMatch = (requested: string, granted: string): boolean => {
    if (requested === granted) return true
    const normalizeCode = (code: string): string => {
      return code.replace(/\.ALL$/, '')
    }
    return normalizeCode(requested) === normalizeCode(granted)
  }

  /**
   * 模拟 hasPermissionCode 函数逻辑
   */
  const hasPermissionCode = (permissions: string[], permission: string): boolean => {
    if (permissions.includes(permission)) return true
    return permissions.some(p => isPermissionCodeMatch(permission, p))
  }

  describe('isPermissionCodeMatch - 双向匹配', () => {
    it('应完全匹配相同的权限码', () => {
      expect(isPermissionCodeMatch('system.api-key.view.ALL', 'system.api-key.view.ALL')).toBe(true)
      expect(isPermissionCodeMatch('system.api-key.view', 'system.api-key.view')).toBe(true)
      expect(isPermissionCodeMatch('user.index.view.ALL', 'user.index.view.ALL')).toBe(true)
    })

    it('三段式请求应匹配四段式权限', () => {
      expect(isPermissionCodeMatch('system.api-key.view', 'system.api-key.view.ALL')).toBe(true)
      expect(isPermissionCodeMatch('system.api-key.create', 'system.api-key.create.ALL')).toBe(true)
      expect(isPermissionCodeMatch('system.api-key.enable', 'system.api-key.enable.ALL')).toBe(true)
      expect(isPermissionCodeMatch('system.api-key.disable', 'system.api-key.disable.ALL')).toBe(true)
      expect(isPermissionCodeMatch('system.api-key.delete', 'system.api-key.delete.ALL')).toBe(true)
      expect(isPermissionCodeMatch('system.api-key.reset', 'system.api-key.reset.ALL')).toBe(true)
      expect(isPermissionCodeMatch('system.api-key.manage', 'system.api-key.manage.ALL')).toBe(true)
    })

    it('四段式请求应匹配三段式权限（向后兼容）', () => {
      expect(isPermissionCodeMatch('system.api-key.view.ALL', 'system.api-key.view')).toBe(true)
      expect(isPermissionCodeMatch('system.api-key.create.ALL', 'system.api-key.create')).toBe(true)
    })

    it('不同权限码不应匹配', () => {
      expect(isPermissionCodeMatch('system.api-key.view', 'system.api-key.create.ALL')).toBe(false)
      expect(isPermissionCodeMatch('user.index.view.ALL', 'system.api-key.view.ALL')).toBe(false)
      expect(isPermissionCodeMatch('activity.index.view', 'user.index.view.ALL')).toBe(false)
    })
  })

  describe('hasPermissionCode - 权限检查', () => {
    const permissions = [
      'system.api-key.view.ALL',
      'system.api-key.create.ALL',
      'system.api-key.enable.ALL',
      'system.api-key.disable.ALL',
      'user.index.view.ALL',
      'activity.index.create.ALL'
    ]

    it('应精确匹配四段式权限码', () => {
      expect(hasPermissionCode(permissions, 'system.api-key.view.ALL')).toBe(true)
      expect(hasPermissionCode(permissions, 'user.index.view.ALL')).toBe(true)
    })

    it('三段式请求应能匹配四段式权限', () => {
      expect(hasPermissionCode(permissions, 'system.api-key.view')).toBe(true)
      expect(hasPermissionCode(permissions, 'system.api-key.create')).toBe(true)
      expect(hasPermissionCode(permissions, 'user.index.view')).toBe(true)
    })

    it('不存在的权限应返回false', () => {
      expect(hasPermissionCode(permissions, 'system.api-key.delete.ALL')).toBe(false)
      expect(hasPermissionCode(permissions, 'system.config.manage.ALL')).toBe(false)
    })

    it('空权限列表应返回false', () => {
      expect(hasPermissionCode([], 'system.api-key.view')).toBe(false)
      expect(hasPermissionCode([], 'system.api-key.view.ALL')).toBe(false)
    })
  })
})
