import {
  evaluateHoReserveCoverage,
  reserveAssetsUsdFromGlRows,
  resetHoReserveCoverageCache,
} from './chain138-ho-reserve-coverage';

describe('reserveAssetsUsdFromGlRows', () => {
  it('sums Dr balances on configured reserve GL codes only', () => {
    const total = reserveAssetsUsdFromGlRows(
      [
        { glCode: '1000', side: 'Dr', amount: '900000000000.00' },
        { glCode: '1050', side: 'Dr', amount: '250000000000.00' },
        { glCode: '13010', side: 'Dr', amount: '999998500.00' },
      ],
      ['1000', '1050'],
    );
    expect(total).toBeCloseTo(1_150_000_000_000, 0);
  });
});

describe('evaluateHoReserveCoverage', () => {
  it('marks sufficient when reserve assets exceed 1.2× issued c* USD', () => {
    const row = evaluateHoReserveCoverage({
      reserveAssetsUsd: 1_150_000_000_000,
      issuedCStarUsd: 344_561_282_129,
      multiplier: 1.2,
      headOfficeId: 1,
      reserveAssetGlCodes: ['1000', '1050'],
      tokenCount: 13,
    });

    expect(row.status).toBe('sufficient');
    expect(row.requiredReserveUsd).toBeCloseTo(413_473_538_555, -3);
    expect(row.coverageRatio).toBeCloseTo(2.78, 2);
  });

  it('marks insufficient when reserve assets fall below 1.2× issued c* USD', () => {
    const row = evaluateHoReserveCoverage({
      reserveAssetsUsd: 400_000_000_000,
      issuedCStarUsd: 344_561_282_129,
      multiplier: 1.2,
    });

    expect(row.status).toBe('insufficient');
    expect(row.coverageRatio).toBeLessThan(1);
  });
});

describe('resetHoReserveCoverageCache', () => {
  it('clears cache without throwing', () => {
    expect(() => resetHoReserveCoverageCache()).not.toThrow();
  });
});
