← back to Dw Daily Catchup 20260909
test/activation-allowance.test.js
46 lines
'use strict';
const test = require('node:test');
const assert = require('node:assert/strict');
const { activationAllowance, assertDailyCatchup } = require('../lib/activation-allowance');
const authorization = require('../config/daily-catchup-2026-09-09.json');
const base = { dailyUsed: 37, hourlyUsed: 21, requested: 463, now: new Date('2026-09-09T18:20:00Z') };
const catchup = extra => ({ ...base, catchupDate: '2026-09-09', authorization, ...extra });
test('normal scheduled runs retain the21/hour cap', () => {
assert.equal(activationAllowance(base).cap, 0);
assert.equal(activationAllowance({ ...base, hourlyUsed: 0 }).cap, 21);
assert.equal(activationAllowance({ ...base, hourlyUsed: 20 }).cap, 1);
});
test('explicit current-day catch-up can use remaining daily allowance', () => {
const result = activationAllowance(catchup());
assert.equal(result.cap, 462); assert.equal(result.mode, 'daily-catchup');
assert.equal(activationAllowance(catchup({ requested: 1 })).cap, 1);
});
test('retries cannot exceed500 total activations', () => {
assert.equal(activationAllowance(catchup({ dailyUsed: 498 })).cap, 1);
assert.equal(activationAllowance(catchup({ dailyUsed: 499 })).cap, 0);
assert.equal(activationAllowance(catchup({ dailyUsed: 500 })).cap, 0);
assert.equal(activationAllowance(catchup({ dailyUsed: 501 })).cap, 0);
});
test('having an authorization file does not enable catch-up for a scheduled invocation', () => {
assert.equal(activationAllowance({ ...base, authorization }).cap, 0);
});
test('missing, altered, future and expired authorizations fail closed', () => {
for (const change of [{ authorization: null }, { authorization: { ...authorization, max_daily: 501 } },
{ authorization: { ...authorization, max_additional: 463 } },
{ authorization: { ...authorization, approved_by: 'someone else' } }, { catchupDate: '2026-09-10' },
{ now: new Date('2026-09-10T00:00:00Z') }, { now: new Date('2026-09-08T23:59:59Z') }])
assert.throws(() => activationAllowance(catchup(change)));
});
test('the publication-loop date check stops a run crossing midnight', () => {
assert.doesNotThrow(() => assertDailyCatchup('2026-09-09', authorization, new Date('2026-09-09T23:59:59Z')));
assert.throws(() => assertDailyCatchup('2026-09-09', authorization, new Date('2026-09-10T00:00:00Z')));
});
test('a reset ledger cannot grant the original allowance again', () => {
assert.throws(() => activationAllowance(catchup({ dailyUsed: 0 })));
});
test('malformed and inflated limits cannot disable either cap', () => {
for (const change of [{ requested: NaN }, { requested: -1 }, { requested: 0 }, { requested: Infinity },
{ dailyCap: 501 }, { hourlyCap: 22 }, { dailyUsed: NaN }, { hourlyUsed: -1 }])
assert.throws(() => activationAllowance(catchup(change)));
});