import { describe, it, expect, beforeEach } from 'vitest'
import { setActivePinia, createPinia } from 'pinia'
import { usePermissionStore } from '@/stores/permission'

describe('Permission Store', () => {
  beforeEach(() => {
    setActivePinia(createPinia())
    localStorage.clear()
  })

  describe('基础功能', () => {
    it('应该正确初始化状态', () => {
      const store = usePermissionStore()
      
      expect(store.roles).toEqual([])
      expect(store.permissions).toEqual([])
      expect(store.menus).toEqual([])
      expect(store.loaded).toBe(false)
    })

    it('应该正确设置权限数据', () => {
      const store = usePermissionStore()
      
      store.setPermissionData({
        roles: ['admin'],
        permissions: ['user:read', 'user:delete'],
        menus: [
          {
            id: 1,
            name: '仪表盘',
            path: '/dashboard',
            icon: 'Odometer',
            sort: 1
          }
        ]
      })
      
      expect(store.roles).toEqual(['admin'])
      expect(store.permissions).toEqual(['user:read', 'user:delete'])
      expect(store.menus).toHaveLength(1)
      expect(store.loaded).toBe(true)
    })

    it('应该正确清除权限数据', () => {
      const store = usePermissionStore()
      
      store.setPermissionData({
        roles: ['admin'],
        permissions: ['user:read'],
        menus: []
      })
      
      store.clearPermissionData()
      
      expect(store.roles).toEqual([])
      expect(store.permissions).toEqual([])
      expect(store.menus).toEqual([])
      expect(store.loaded).toBe(false)
    })
  })

  describe('权限检查方法', () => {
    it('应该正确检查单个角色', () => {
      const store = usePermissionStore()
      store.setPermissionData({
        roles: ['admin', 'user'],
        permissions: [],
        menus: []
      })
      
      expect(store.hasRole('admin')).toBe(true)
      expect(store.hasRole('manager')).toBe(false)
    })

    it('应该正确检查多个角色（满足任一即可）', () => {
      const store = usePermissionStore()
      store.setPermissionData({
        roles: ['user'],
        permissions: [],
        menus: []
      })
      
      expect(store.hasRole(['admin', 'user'])).toBe(true)
      expect(store.hasRole(['admin', 'manager'])).toBe(false)
    })

    it('应该正确检查单个权限', () => {
      const store = usePermissionStore()
      store.setPermissionData({
        roles: [],
        permissions: ['user:read', 'user:delete'],
        menus: []
      })
      
      expect(store.hasPermission('user:read')).toBe(true)
      expect(store.hasPermission('user:create')).toBe(false)
    })

    it('应该正确检查多个权限（满足任一即可）', () => {
      const store = usePermissionStore()
      store.setPermissionData({
        roles: [],
        permissions: ['user:read'],
        menus: []
      })
      
      expect(store.hasPermission(['user:read', 'user:create'])).toBe(true)
      expect(store.hasPermission(['user:create', 'user:update'])).toBe(false)
    })
  })

  describe('localStorage 持久化', () => {
    it('应该正确保存到 localStorage', () => {
      const store = usePermissionStore()
      
      store.setPermissionData({
        roles: ['admin'],
        permissions: ['user:read'],
        menus: [
          {
            id: 1,
            name: '仪表盘',
            path: '/dashboard',
            sort: 1
          }
        ]
      })
      
      const stored = localStorage.getItem('permission')
      expect(stored).toBeTruthy()
      
      const data = JSON.parse(stored!)
      expect(data.roles).toEqual(['admin'])
      expect(data.permissions).toEqual(['user:read'])
      expect(data.menus).toHaveLength(1)
    })

    it('应该正确从 localStorage 恢复', () => {
      localStorage.setItem('permission', JSON.stringify({
        roles: ['user'],
        permissions: ['user:read:self'],
        menus: []
      }))
      
      const store = usePermissionStore()
      store.initFromStorage()
      
      expect(store.roles).toEqual(['user'])
      expect(store.permissions).toEqual(['user:read:self'])
      expect(store.loaded).toBe(true)
    })

    it('清除数据时应该同时清除 localStorage', () => {
      const store = usePermissionStore()
      
      store.setPermissionData({
        roles: ['admin'],
        permissions: [],
        menus: []
      })
      
      store.clearPermissionData()
      
      expect(localStorage.getItem('permission')).toBeNull()
    })
  })
})
