import { describe, it, expect } from 'vitest';
import {
  generateSessionToken,
  hashSessionToken,
  isSessionExpired,
  sessionExpiry,
  SESSION_TTL_MS,
} from '@/lib/auth/tokens';

describe('tokens', () => {
  it('genera tokens distintos en hex de 64 chars', () => {
    const a = generateSessionToken();
    const b = generateSessionToken();
    expect(a).toMatch(/^[0-9a-f]{64}$/);
    expect(a).not.toBe(b);
  });

  it('hashea el token de forma determinista y distinta al token', () => {
    const t = generateSessionToken();
    expect(hashSessionToken(t)).toBe(hashSessionToken(t));
    expect(hashSessionToken(t)).not.toBe(t);
  });

  it('marca expirada una sesión pasada y válida una futura', () => {
    const now = new Date('2026-05-30T12:00:00Z');
    expect(isSessionExpired(new Date('2026-05-29T12:00:00Z'), now)).toBe(true);
    expect(isSessionExpired(new Date('2026-05-31T12:00:00Z'), now)).toBe(false);
  });

  it('sessionExpiry suma el TTL al instante dado', () => {
    const now = new Date('2026-05-30T12:00:00Z');
    expect(sessionExpiry(now).getTime()).toBe(now.getTime() + SESSION_TTL_MS);
  });
});
