import { estadoVigencia, hoyEnAsuncion, whereEstado } from './vigencia-lista';

describe('vigencia de una lista', () => {
  const hoy = new Date('2026-11-05');
  const lista = (extra: any = {}) => ({ activa: true, fecha_inicio: new Date('2026-01-01'), fecha_fin: null, ...extra });

  it('hoyEnAsuncion a las 22:00 local es ese día', () => {
    expect(hoyEnAsuncion(new Date('2026-11-06T01:00:00Z')).toISOString()).toBe('2026-11-05T00:00:00.000Z');
  });

  it('hoyEnAsuncion a las 23:59 local sigue siendo ese día y a las 00:00 es el siguiente', () => {
    expect(hoyEnAsuncion(new Date('2026-11-06T02:59:00Z')).toISOString()).toBe('2026-11-05T00:00:00.000Z');
    expect(hoyEnAsuncion(new Date('2026-11-06T03:00:00Z')).toISOString()).toBe('2026-11-06T00:00:00.000Z');
  });

  describe('estadoVigencia', () => {
    it('inactiva gana a todo', () => {
      expect(estadoVigencia(lista({ activa: false, fecha_inicio: new Date('2026-11-06') }), hoy)).toBe('inactiva');
      expect(estadoVigencia(lista({ activa: false, fecha_fin: new Date('2026-11-04') }), hoy)).toBe('inactiva');
    });
    it('empieza mañana es programada', () => {
      expect(estadoVigencia(lista({ fecha_inicio: new Date('2026-11-06') }), hoy)).toBe('programada');
    });
    it('terminó ayer es vencida', () => {
      expect(estadoVigencia(lista({ fecha_fin: new Date('2026-11-04') }), hoy)).toBe('vencida');
    });
    it('termina hoy es vigente', () => {
      expect(estadoVigencia(lista({ fecha_fin: new Date('2026-11-05') }), hoy)).toBe('vigente');
    });
    it('empieza hoy y sin fin es vigente', () => {
      expect(estadoVigencia(lista({ fecha_inicio: new Date('2026-11-05') }), hoy)).toBe('vigente');
    });
  });

  describe('whereEstado', () => {
    it('vencida: ya empezó y terminó (una que no empezó es programada, no vencida)', () =>
      expect(whereEstado('vencida', hoy)).toEqual({ activa: true, fecha_inicio: { lte: hoy }, fecha_fin: { lt: hoy } }));
    it('programada', () => expect(whereEstado('programada', hoy)).toEqual({ activa: true, fecha_inicio: { gt: hoy } }));
    it('vigente', () =>
      expect(whereEstado('vigente', hoy)).toEqual({ activa: true, fecha_inicio: { lte: hoy }, OR: [{ fecha_fin: null }, { fecha_fin: { gte: hoy } }] }));
    it('activa e inactiva', () => {
      expect(whereEstado('activa', hoy)).toEqual({ activa: true });
      expect(whereEstado('inactiva', hoy)).toEqual({ activa: false });
    });
    it('desconocido o vacío no filtra', () => {
      expect(whereEstado('otra', hoy)).toEqual({});
      expect(whereEstado('', hoy)).toEqual({});
      expect(whereEstado(undefined, hoy)).toEqual({});
    });
  });
});
