import { describe, it, expect } from 'vitest'
import { needsStepUp, recordStepUp, policyForHost, NO_STEPUP_POLICY } from '../src/human/stepup/stepup.js'
import type { StepUpPolicy } from '../src/types.js'
import { principal, makeHost, uuid } from './_helpers.js'

const NOW = 1_700_000_000
const POLICY: StepUpPolicy = { required: true, maxAgeSeconds: 300, requiredMethod: 'passkey' }

describe('step-up before session open (T8, Finding-3)', () => {
  it('fresh login but stepUpAt=null → needsStepUp true (login ≠ step-up)', () => {
    const p = principal('acct-A', { authAt: NOW, stepUpAt: null, amr: ['passkey'] })
    expect(needsStepUp(p, POLICY, NOW)).toBe(true)
  })

  it('stale step-up (older than maxAgeSeconds) → true', () => {
    const p = principal('acct-A', { stepUpAt: NOW - 301, amr: ['passkey', 'stepup'], stepUpMethod: 'passkey' })
    expect(needsStepUp(p, POLICY, NOW)).toBe(true)
  })

  it('required method not the LAST step-up method → true', () => {
    const p = principal('acct-A', { stepUpAt: NOW, amr: ['totp'], stepUpMethod: 'totp' })
    expect(needsStepUp(p, POLICY, NOW)).toBe(true)
  })

  it('fresh passkey step-up satisfies passkey policy → false (F1)', () => {
    const p = principal('acct-A', { stepUpAt: NOW, amr: ['passkey'], stepUpMethod: 'passkey' })
    expect(needsStepUp(p, POLICY, NOW)).toBe(false)
  })

  it('fresh TOTP step-up does NOT satisfy passkey policy → true (F1 factor-downgrade regression)', () => {
    // A weaker (TOTP) step-up performed just now must not satisfy a passkey requirement, even though
    // it is fresh — the required method itself must be the LAST step-up.
    const p = principal('acct-A', { stepUpAt: NOW, amr: ['passkey', 'totp'], stepUpMethod: 'totp' })
    expect(needsStepUp(p, POLICY, NOW)).toBe(true)
  })

  it('login-time passkey in amr but no step-up (stepUpMethod null) → true (login ≠ step-up)', () => {
    const p = principal('acct-A', { stepUpAt: NOW, amr: ['passkey'], stepUpMethod: null })
    expect(needsStepUp(p, POLICY, NOW)).toBe(true)
  })

  it('recordStepUp returns a NEW principal (immutable), original untouched', () => {
    const p = principal('acct-A', { stepUpAt: null, amr: ['totp'], stepUpMethod: 'totp' })
    const stepped = recordStepUp(p, 'passkey', NOW)
    expect(stepped).not.toBe(p)
    expect(p.stepUpAt).toBeNull()
    expect(p.amr).toEqual(['totp'])
    expect(p.stepUpMethod).toBe('totp')
    expect(stepped.stepUpAt).toBe(NOW)
    expect(stepped.amr).toContain('passkey')
    expect(needsStepUp(stepped, POLICY, NOW)).toBe(false)
  })

  it('recordStepUp stamps stepUpMethod with the method used (F1)', () => {
    const p = principal('acct-A', { stepUpAt: null, amr: ['passkey'], stepUpMethod: null })
    const stepped = recordStepUp(p, 'passkey', NOW)
    expect(stepped.stepUpMethod).toBe('passkey')
    expect(stepped.stepUpAt).toBe(NOW)
  })

  it('policyForHost yields a REQUIRED passkey freshness requirement', () => {
    const policy = policyForHost(makeHost('acct-A', uuid()))
    expect(policy.required).toBe(true)
    expect(policy.requiredMethod).toBe('passkey')
    expect(policy.maxAgeSeconds).toBeGreaterThan(0)
  })

  it('NO_STEPUP_POLICY is the v0.9 never-required default', () => {
    expect(NO_STEPUP_POLICY.required).toBe(false)
    expect(NO_STEPUP_POLICY.requiredMethod).toBe('passkey')
    expect(NO_STEPUP_POLICY.maxAgeSeconds).toBeGreaterThan(0)
  })
})
