import { describe, it, expect } from 'vitest';

import { previousPeriodBounds } from '@/lib/analytics/range';

describe('previousPeriodBounds', () => {
  it('returns the equal-length window immediately preceding the given bounds', () => {
    const from = new Date('2026-05-01T00:00:00.000Z');
    const to = new Date('2026-05-31T00:00:00.000Z'); // 30-day span
    const prior = previousPeriodBounds({ from, to });

    // Prior window is contiguous (prior.to === current.from) and equal length.
    expect(prior.to.toISOString()).toBe('2026-05-01T00:00:00.000Z');
    expect(prior.from.toISOString()).toBe('2026-04-01T00:00:00.000Z');
  });

  it('preserves sub-day spans (e.g. a single-day window)', () => {
    const from = new Date('2026-05-10T00:00:00.000Z');
    const to = new Date('2026-05-10T23:59:59.999Z');
    const prior = previousPeriodBounds({ from, to });

    const lenMs = to.getTime() - from.getTime();
    expect(prior.to.getTime()).toBe(from.getTime());
    expect(prior.from.getTime()).toBe(from.getTime() - lenMs);
  });

  it('does not mutate the input bounds', () => {
    const from = new Date('2026-05-01T00:00:00.000Z');
    const to = new Date('2026-05-31T00:00:00.000Z');
    const fromCopy = from.getTime();
    const toCopy = to.getTime();
    previousPeriodBounds({ from, to });
    expect(from.getTime()).toBe(fromCopy);
    expect(to.getTime()).toBe(toCopy);
  });
});
