import { describe, it, expect } from 'vitest'
import { hashPassword, verifyPassword, createSession, getSessionUser, deleteSession } from './auth'
import { prisma } from './prisma'

describe('password hashing', () => {
  it('acepta la contraseña correcta y rechaza la incorrecta', () => {
    const stored = hashPassword('SuperSecreta123')
    expect(stored).toContain(':')
    expect(verifyPassword('SuperSecreta123', stored)).toBe(true)
    expect(verifyPassword('otra', stored)).toBe(false)
  })
  it('rechaza un formato inválido sin lanzar', () => {
    expect(verifyPassword('x', 'no-tiene-formato')).toBe(false)
  })
})

describe('sesiones', () => {
  it('crea una sesión y devuelve el usuario; la borra después', async () => {
    const user = await prisma.user.create({
      data: { email: `t${Date.now()}@test.cl`, passwordHash: hashPassword('x12345678'), name: 'Test' },
    })
    const token = await createSession(user.id)
    const found = await getSessionUser(token)
    expect(found?.id).toBe(user.id)
    await deleteSession(token)
    expect(await getSessionUser(token)).toBeNull()
    await prisma.user.delete({ where: { id: user.id } })
  })
})
