import { readMfaConfig } from './check-mfa-config';

describe('readMfaConfig', () => {
  const original = process.env['MFA_FRESHNESS_SECONDS'];

  afterEach(() => {
    if (original === undefined) {
      delete process.env['MFA_FRESHNESS_SECONDS'];
    } else {
      process.env['MFA_FRESHNESS_SECONDS'] = original;
    }
  });

  it('defaults to 600 seconds when MFA_FRESHNESS_SECONDS is unset', () => {
    delete process.env['MFA_FRESHNESS_SECONDS'];
    expect(readMfaConfig()).toEqual({ freshnessSeconds: 600 });
  });

  it('defaults to 600 seconds when MFA_FRESHNESS_SECONDS is the empty string', () => {
    process.env['MFA_FRESHNESS_SECONDS'] = '';
    expect(readMfaConfig()).toEqual({ freshnessSeconds: 600 });
  });

  it('accepts a positive integer above the minimum', () => {
    process.env['MFA_FRESHNESS_SECONDS'] = '1200';
    expect(readMfaConfig()).toEqual({ freshnessSeconds: 1200 });
  });

  it('accepts the minimum (60) exactly', () => {
    process.env['MFA_FRESHNESS_SECONDS'] = '60';
    expect(readMfaConfig()).toEqual({ freshnessSeconds: 60 });
  });

  it('throws when MFA_FRESHNESS_SECONDS is below the 60 s floor', () => {
    process.env['MFA_FRESHNESS_SECONDS'] = '30';
    expect(() => readMfaConfig()).toThrow(/must be ≥ 60/);
  });

  it('throws when MFA_FRESHNESS_SECONDS is not a positive integer', () => {
    process.env['MFA_FRESHNESS_SECONDS'] = '0';
    expect(() => readMfaConfig()).toThrow(/positive integer/);
  });

  it('throws when MFA_FRESHNESS_SECONDS is negative', () => {
    process.env['MFA_FRESHNESS_SECONDS'] = '-600';
    expect(() => readMfaConfig()).toThrow(/positive integer/);
  });

  it('throws when MFA_FRESHNESS_SECONDS is non-numeric', () => {
    process.env['MFA_FRESHNESS_SECONDS'] = 'soon';
    expect(() => readMfaConfig()).toThrow(/positive integer/);
  });

  it('throws when MFA_FRESHNESS_SECONDS is a decimal', () => {
    process.env['MFA_FRESHNESS_SECONDS'] = '600.5';
    expect(() => readMfaConfig()).toThrow(/positive integer/);
  });
});
