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

vi.mock('server-only', () => ({}))
vi.mock('@/lib/auth', () => ({ getSessionUser: async () => null, getMyParticipant: async () => null }))
vi.mock('@/lib/supabase/admin', () => ({ createAdminClient: () => ({}) }))

/** Nash, 2026-10-01: "From any terminal, any other user should not have the ability to do that." */
describe('purchasingRefusal — the server side of the admin/moderator rule', () => {
  const base = { id: 'u1', email: 'a@b.c', name: 'A', company: null, originId: 'u1' }
  it('refuses a customer with 403', async () => {
    const { purchasingRefusal } = await import('@/lib/api-guard')
    const res = purchasingRefusal({ ...base, role: 'dispatcher', internal: false, surfaces: [] })
    expect(res?.status).toBe(403)
    expect((await res!.json()).error).toBe('Purchasing is not available on this account.')
  })
  it('lets an admin and a moderator through', async () => {
    const { purchasingRefusal } = await import('@/lib/api-guard')
    expect(purchasingRefusal({ ...base, role: 'admin', internal: true, surfaces: [] })).toBeNull()
    expect(purchasingRefusal({ ...base, role: 'broker', internal: false, surfaces: ['moderation'] })).toBeNull()
  })
})
