← back to Dw Signup Fulfillment

verification/tk11285/auto-approve-cap-test.js

60 lines

'use strict';
// TRADE_AUTO_APPROVE_DAILY_CAP — the volume backstop on guarded auto-approve.
// Never touches the real data/auto-approve-ledger.json: points the module at a
// throwaway file via AUTO_APPROVE_LEDGER_PATH and removes it in a finally.
const fs = require('fs');
const os = require('os');
const path = require('path');

const DIR = fs.mkdtempSync(path.join(os.tmpdir(), 'tk-cap-'));
process.env.AUTO_APPROVE_LEDGER_PATH = path.join(DIR, 'ledger.json');

const ROOT = path.join(__dirname, '..', '..');
const REAL_LEDGER = path.join(ROOT, 'data', 'auto-approve-ledger.json');
const realBefore = fs.existsSync(REAL_LEDGER) ? fs.readFileSync(REAL_LEDGER) : null;

const check = (name, ok, detail) => ({ name, verdict: ok ? 'PASS' : 'FAIL', detail });
let results = [];
try {
  const ledger = require(path.join(ROOT, 'lib', 'auto-approve-ledger'));
  const CAP = 3;
  const capped = () => ledger.todayCount() >= CAP;   // the exact server.js predicate

  results.push(check('starts empty', ledger.todayCount() === 0, 'count=' + ledger.todayCount()));
  results.push(check('not capped while under', !capped()));

  ledger.record(); ledger.record();
  results.push(check('counts each auto-approve', ledger.todayCount() === 2, 'count=' + ledger.todayCount()));
  results.push(check('still under cap at 2/3', !capped()));

  ledger.record();
  results.push(check('capped exactly AT the cap (>=, not >)', capped(), 'count=' + ledger.todayCount()));

  // Durability: a pm2 reload / deploy must not reset the day's count.
  delete require.cache[require.resolve(path.join(ROOT, 'lib', 'auto-approve-ledger'))];
  const reloaded = require(path.join(ROOT, 'lib', 'auto-approve-ledger'));
  results.push(check('survives a restart (re-read from disk)', reloaded.todayCount() === 3, 'count=' + reloaded.todayCount()));

  // Day rollover: yesterday's exhausted count must not cap today.
  fs.writeFileSync(process.env.AUTO_APPROVE_LEDGER_PATH, JSON.stringify({ '2020-01-01': { count: 9999 } }));
  delete require.cache[require.resolve(path.join(ROOT, 'lib', 'auto-approve-ledger'))];
  const fresh = require(path.join(ROOT, 'lib', 'auto-approve-ledger'));
  results.push(check('a previous day does not cap today', fresh.todayCount() === 0, 'count=' + fresh.todayCount()));

  // The default must be a real bound, not unlimited.
  delete require.cache[require.resolve(path.join(ROOT, 'lib', 'config'))];
  const cfg = require(path.join(ROOT, 'lib', 'config'));
  results.push(check('cap defaults to a finite number', Number.isFinite(cfg.TRADE_AUTO_APPROVE_DAILY_CAP) && cfg.TRADE_AUTO_APPROVE_DAILY_CAP > 0,
    'default=' + cfg.TRADE_AUTO_APPROVE_DAILY_CAP));

  const realAfter = fs.existsSync(REAL_LEDGER) ? fs.readFileSync(REAL_LEDGER) : null;
  results.push(check('the REAL ledger was never written',
    String(realBefore) === String(realAfter), 'before=' + (realBefore ? realBefore.length : 'absent') + ' after=' + (realAfter ? realAfter.length : 'absent')));

  const failed = results.filter((r) => r.verdict === 'FAIL');
  console.log(JSON.stringify({ suite: 'auto-approve daily cap', passed: results.length - failed.length, failed: failed.length, results }, null, 2));
  process.exitCode = failed.length ? 1 : 0;
} finally {
  try { fs.rmSync(DIR, { recursive: true, force: true }); } catch { /* best effort */ }
}