import { describe, it, expect, beforeEach } from 'vitest'
import { setActivePinia, createPinia } from 'pinia'
import { useUserStore } from '../user'
import { fakeJwtWithRole } from '@/test/helpers'

describe('Permission: user store 权限判断', () => {
  beforeEach(() => {
    setActivePinia(createPinia())
    localStorage.clear()
  })

  describe('isAdmin', () => {
    it('administrator 角色返回 true', () => {
      const store = useUserStore()
      store.setToken(fakeJwtWithRole('administrator'), 'rt')
      store.setUser({ id: 1, username: 'admin', email: 'a@a.com', role: 'administrator', status: 1 })

      expect(store.isAdmin).toBe(true)
    })

    it('developer 角色返回 false', () => {
      const store = useUserStore()
      store.setUser({ id: 2, username: 'dev', email: 'd@d.com', role: 'developer', status: 1 })

      expect(store.isAdmin).toBe(false)
    })

    it('accessor 角色返回 false', () => {
      const store = useUserStore()
      store.setUser({ id: 3, username: 'acc', email: 'c@c.com', role: 'accessor', status: 1 })

      expect(store.isAdmin).toBe(false)
    })

    it('未登录（user 为 null）返回 false', () => {
      const store = useUserStore()

      expect(store.isAdmin).toBe(false)
    })
  })

  describe('hasPermission', () => {
    it('admin 对 write 操作返回 true', () => {
      const store = useUserStore()
      store.setToken(fakeJwtWithRole('administrator'), 'rt')
      store.setUser({ id: 1, username: 'admin', email: 'a@a.com', role: 'administrator', status: 1 })

      expect(store.hasPermission('write')).toBe(true)
    })

    it('admin 对 read 操作返回 true', () => {
      const store = useUserStore()
      store.setToken(fakeJwtWithRole('administrator'), 'rt')
      store.setUser({ id: 1, username: 'admin', email: 'a@a.com', role: 'administrator', status: 1 })

      expect(store.hasPermission('read')).toBe(true)
    })

    it('非 admin 对 write 操作返回 false', () => {
      const store = useUserStore()
      store.setUser({ id: 2, username: 'dev', email: 'd@d.com', role: 'developer', status: 1 })

      expect(store.hasPermission('write')).toBe(false)
    })

    it('非 admin 对 read 操作返回 true', () => {
      const store = useUserStore()
      store.setUser({ id: 3, username: 'acc', email: 'c@c.com', role: 'accessor', status: 1 })

      expect(store.hasPermission('read')).toBe(true)
    })

    it('未登录时 write 返回 false', () => {
      const store = useUserStore()

      expect(store.hasPermission('write')).toBe(false)
    })

    it('未登录时 read 返回 true', () => {
      const store = useUserStore()

      expect(store.hasPermission('read')).toBe(true)
    })
  })

  describe('角色切换响应', () => {
    it('用户 token 从 accessor 切换为 administrator 后 isAdmin 变为 true', () => {
      const store = useUserStore()
      store.setToken(fakeJwtWithRole('accessor'), 'rt')
      store.setUser({ id: 1, username: 'u', email: 'u@u.com', role: 'accessor', status: 1 })
      expect(store.isAdmin).toBe(false)

      store.setToken(fakeJwtWithRole('administrator'), 'rt')
      store.setUser({ id: 1, username: 'u', email: 'u@u.com', role: 'administrator', status: 1 })
      expect(store.isAdmin).toBe(true)
    })

    it('logout 后 isAdmin 恢复为 false', () => {
      const store = useUserStore()
      store.setToken(fakeJwtWithRole('administrator'), 'rt')
      store.setUser({ id: 1, username: 'a', email: 'a@a.com', role: 'administrator', status: 1 })
      expect(store.isAdmin).toBe(true)

      store.logout()
      expect(store.isAdmin).toBe(false)
      expect(store.hasPermission('write')).toBe(false)
    })
  })
})
