import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { relativeTime, daysUntilPurge } from '../relative-time';

const NOW_UNIX = 1779552000; // fixed reference instant

beforeEach(() => {
  vi.useFakeTimers();
  vi.setSystemTime(new Date(NOW_UNIX * 1000));
});

afterEach(() => {
  vi.useRealTimers();
});

describe('relativeTime', () => {
  it('returns "just now" under 60s', () => {
    expect(relativeTime(NOW_UNIX - 30)).toBe('just now');
  });
  it('returns minutes under an hour', () => {
    expect(relativeTime(NOW_UNIX - 600)).toBe('10m ago');
  });
  it('returns hours under a day', () => {
    expect(relativeTime(NOW_UNIX - 7200)).toBe('2h ago');
  });
  it('returns days under a week', () => {
    expect(relativeTime(NOW_UNIX - 3 * 86400)).toBe('3d ago');
  });
  it('returns weeks under a month', () => {
    expect(relativeTime(NOW_UNIX - 14 * 86400)).toBe('2w ago');
  });
  it('returns months above 30 days', () => {
    expect(relativeTime(NOW_UNIX - 90 * 86400)).toBe('3mo ago');
  });
});

describe('daysUntilPurge', () => {
  it('returns null for forever retention', () => {
    expect(daysUntilPurge(NOW_UNIX - 5 * 86400, { kind: 'forever' })).toBeNull();
  });
  it('returns remaining days for a recent trash', () => {
    expect(daysUntilPurge(NOW_UNIX - 8 * 86400, { kind: 'days', value: 30 })).toBe(22);
  });
  it('clamps to zero when retention already elapsed', () => {
    expect(daysUntilPurge(NOW_UNIX - 60 * 86400, { kind: 'days', value: 30 })).toBe(0);
  });
});
