import { describe, expect, it } from 'vitest'
import { signPortalSession, verifyPortalToken } from '@zync/auth'
import {
  DEFAULT_PORTAL_VISIBILITY,
  resolvePortalVisibility,
} from '@zync/db/queries'

describe('portal session claims', () => {
  it('includes the spec-scoped customer and portal role claims', async () => {
    const token = await signPortalSession(
      {
        tenantId: 'tenant-1',
        customerId: 'customer-1',
        userId: 'portal-user-1',
        portalRole: 'client',
        portalSessionId: 'session-1',
        sessionStartedAt: 123,
        sub: 'ignored-by-signer',
      },
      'secret',
      60,
    )

    const claims = await verifyPortalToken(token, 'secret')

    expect(claims.sub).toBe('portal-user-1')
    expect(claims.customerId).toBe('customer-1')
    expect(claims.tenantId).toBe('tenant-1')
    expect(claims.portalRole).toBe('client')
    expect(claims.role).toBe('portal_customer')
  })
})

describe('resolvePortalVisibility', () => {
  it('returns the full default config when storage is empty', () => {
    expect(resolvePortalVisibility(undefined)).toEqual(DEFAULT_PORTAL_VISIBILITY)
  })

  it('merges stored overrides onto the default config', () => {
    expect(
      resolvePortalVisibility({
        show_contracts: true,
        show_files: true,
      }),
    ).toEqual({
      ...DEFAULT_PORTAL_VISIBILITY,
      show_contracts: true,
      show_files: true,
    })
  })
})
