import assert from 'node:assert/strict';
import test from 'node:test';
import {
  buildInsufficientBalancePayload,
  isAllowedRechargeAmount,
  loadRechargeConfig,
} from './billing-recharge.mjs';

test('loadRechargeConfig parses tier list', () => {
  const previous = process.env.H5_RECHARGE_TIERS_CENTS;
  process.env.H5_RECHARGE_TIERS_CENTS = '500,1000,3000';
  try {
    const config = loadRechargeConfig();
    assert.deepEqual(config.tiersCents, [500, 1000, 3000]);
    assert.equal(config.minRechargeCents, 500);
  } finally {
    if (previous === undefined) delete process.env.H5_RECHARGE_TIERS_CENTS;
    else process.env.H5_RECHARGE_TIERS_CENTS = previous;
  }
});

test('isAllowedRechargeAmount only accepts configured tiers', () => {
  const config = { tiersCents: [500, 1000, 3000], minRechargeCents: 500 };
  assert.equal(isAllowedRechargeAmount(1000, config), true);
  assert.equal(isAllowedRechargeAmount(2000, config), false);
});

test('buildInsufficientBalancePayload includes recharge hints', () => {
  const config = { tiersCents: [500, 1000], minRechargeCents: 500 };
  const payload = buildInsufficientBalancePayload(0, config);
  assert.equal(payload.code, 'INSUFFICIENT_BALANCE');
  assert.equal(payload.balanceCents, 0);
  assert.deepEqual(payload.suggestedTiers, [500, 1000]);
});
