import { describe, it, expect } from 'bun:test';
import { toIsoSqlUTC, ymdUTC, addMonthsUTC } from '../../apps/web/src/lib/server/datetime.ts';

describe('apps/web datetime wrapper', () => {
  it('toIsoSqlUTC serializa en UTC con milisegundos', () => {
    const d = new Date(Date.UTC(2024, 0, 31, 23, 59, 59, 123));
    expect(toIsoSqlUTC(d)).toBe('2024-01-31 23:59:59.123');
  });

  it('ymdUTC devuelve YYYY-MM-DD en UTC', () => {
    const d = new Date(Date.UTC(2024, 7, 9, 10, 11, 12)); // 2024-08-09
    expect(ymdUTC(d)).toBe('2024-08-09');
  });

  it('addMonthsUTC suma meses en UTC preservando día cuando es posible', () => {
    const d1 = new Date(Date.UTC(2024, 0, 15)); // 2024-01-15
    const plus1 = addMonthsUTC(d1, 1);
    expect(ymdUTC(plus1)).toBe('2024-02-15');

    const d2 = new Date(Date.UTC(2024, 1, 29)); // 2024-02-29 (bisiesto)
    const plus1b = addMonthsUTC(d2, 1);
    expect(ymdUTC(plus1b)).toBe('2024-03-29');

    const d3 = new Date(Date.UTC(2024, 11, 15)); // 2024-12-15
    const plus2 = addMonthsUTC(d3, 2);
    expect(ymdUTC(plus2)).toBe('2025-02-15');
  });
});
