import { describe, it, expect } from 'vitest'
import { ADMIN_ONLY_PATH_PREFIXES, isAdminOnlyPath } from '../permissions'

describe('ADMIN_ONLY_PATH_PREFIXES', () => {
  it('contains all 8 admin-only paths', () => {
    expect(ADMIN_ONLY_PATH_PREFIXES).toHaveLength(8)
    expect(ADMIN_ONLY_PATH_PREFIXES).toContain('/users')
    expect(ADMIN_ONLY_PATH_PREFIXES).toContain('/roles')
    expect(ADMIN_ONLY_PATH_PREFIXES).toContain('/route-groups')
    expect(ADMIN_ONLY_PATH_PREFIXES).toContain('/api-keys')
    expect(ADMIN_ONLY_PATH_PREFIXES).toContain('/mq-status')
    expect(ADMIN_ONLY_PATH_PREFIXES).toContain('/failed-messages')
    expect(ADMIN_ONLY_PATH_PREFIXES).toContain('/logs/requests')
    expect(ADMIN_ONLY_PATH_PREFIXES).toContain('/logs/operations')
  })
})

describe('isAdminOnlyPath', () => {
  it('matches exact admin-only paths', () => {
    for (const path of ADMIN_ONLY_PATH_PREFIXES) {
      expect(isAdminOnlyPath(path)).toBe(true)
    }
  })

  it('matches sub-paths via prefix matching', () => {
    expect(isAdminOnlyPath('/users/123')).toBe(true)
    expect(isAdminOnlyPath('/users/123/edit')).toBe(true)
    expect(isAdminOnlyPath('/roles/5')).toBe(true)
    expect(isAdminOnlyPath('/logs/requests/detail')).toBe(true)
    expect(isAdminOnlyPath('/logs/operations/42')).toBe(true)
    expect(isAdminOnlyPath('/failed-messages/retry')).toBe(true)
  })

  it('does not match non-admin paths', () => {
    expect(isAdminOnlyPath('/')).toBe(false)
    expect(isAdminOnlyPath('/login')).toBe(false)
    expect(isAdminOnlyPath('/products')).toBe(false)
    expect(isAdminOnlyPath('/orders')).toBe(false)
    expect(isAdminOnlyPath('/dashboard')).toBe(false)
  })

  it('does not match prefix-similar but distinct paths', () => {
    expect(isAdminOnlyPath('/users-export')).toBe(false)
    expect(isAdminOnlyPath('/roles-backup')).toBe(false)
    expect(isAdminOnlyPath('/route-groups-old')).toBe(false)
    expect(isAdminOnlyPath('/mq-status-history')).toBe(false)
  })
})
