import { describe, it, expect } from 'vitest';
import { detectGap } from '../reconcile.js';

describe('detectGap', () => {
  it('detects gap when oldest reconcile is newer than newest persisted', () => {
    expect(detectGap('2024-01-02T00:00:00Z', '2024-01-01T00:00:00Z')).toBe(true);
  });

  it('does not detect gap when overlap exists', () => {
    expect(detectGap('2024-01-01T00:00:00Z', '2024-01-02T00:00:00Z')).toBe(false);
  });

  it('does not detect gap when timestamps are equal', () => {
    expect(detectGap('2024-01-01T00:00:00Z', '2024-01-01T00:00:00Z')).toBe(false);
  });

  it('returns false when oldest reconcile is null', () => {
    expect(detectGap(null, '2024-01-01T00:00:00Z')).toBe(false);
  });

  it('returns false when newest persisted is null', () => {
    expect(detectGap('2024-01-01T00:00:00Z', null)).toBe(false);
  });

  it('returns false when both are null', () => {
    expect(detectGap(null, null)).toBe(false);
  });

  it('handles timezone offsets correctly', () => {
    // 2024-01-01T12:00:00Z == 2024-01-01T14:00:00+02:00
    expect(detectGap('2024-01-01T12:00:00Z', '2024-01-01T14:00:00+02:00')).toBe(false);
    expect(detectGap('2024-01-01T13:00:00Z', '2024-01-01T14:00:00+02:00')).toBe(true);
  });
});
