Files
ManagerAccount/test/asset-project-allocations.test.js
2026-09-23 15:54:40 +07:00

39 lines
2.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const test = require('node:test');
const assert = require('node:assert/strict');
const { normalizeExportAllocations, addAllocations, releaseAllocations } = require('../backend/asset-project-allocations');
test('multiple exports retain project quantities and normalize duplicate project rows', () => {
const rows = normalizeExportAllocations({ allocations: [
{ projectName: ' A ', quantity: 2 }, { projectName: 'B', quantity: 3 }, { projectName: 'a', quantity: '1' }
] });
assert.deepEqual(rows, [{ projectName: 'A', quantity: 3 }, { projectName: 'B', quantity: 3 }]);
const current = [{ projectName: 'Old', borrowerName: 'An', quantity: 2 }];
assert.equal(addAllocations(JSON.stringify(current), rows, 'Binh').reduce((sum, row) => sum + row.quantity, 0), 8);
assert.equal(current.length, 1);
});
test('invalid project quantities are rejected instead of silently rounded', () => {
for (const quantity of [0, -1, 1.5, '2abc', true, null, {}, 2147483648]) {
assert.throws(() => normalizeExportAllocations({ allocations: [{ projectName: 'A', quantity }] }));
}
for (const allocations of [[], null, {}, [{ quantity: 1 }], [{ projectName: 'x'.repeat(151), quantity: 1 }]]) {
assert.throws(() => normalizeExportAllocations({ allocations }));
}
assert.deepEqual(normalizeExportAllocations({ projectName: 'Legacy API', quantity: 2 }), [{ projectName: 'Legacy API', quantity: 2 }]);
});
test('returns consume unattributed loans then the borrower’s oldest project exports', () => {
const current = [
{ projectName: 'A', borrowerName: 'An', quantity: 2 },
{ projectName: 'B', borrowerName: 'An', quantity: 4 },
{ projectName: 'C', borrowerName: 'Binh', quantity: 3 }
];
assert.deepEqual(releaseAllocations(current, 'An', 2, 8), current);
assert.deepEqual(releaseAllocations(current, 'an', 5, 8), [
{ projectName: 'B', borrowerName: 'An', quantity: 3 },
{ projectName: 'C', borrowerName: 'Binh', quantity: 3 }
]);
assert.equal(current[0].quantity, 2);
assert.deepEqual(releaseAllocations(current, 'An', 8, 8), [current[2]]);
});