54 lines
3.4 KiB
JavaScript
54 lines
3.4 KiB
JavaScript
const test = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const { normalizeExportItems } = require('./asset-export-requests');
|
|
const { addProjectExports, releaseAllocations } = require('./asset-project-allocations');
|
|
const { createExportTestApp } = require('./asset-export-test-fixture');
|
|
|
|
test('project export proposals require positive integer quantities, project names and unique inventory IDs', () => {
|
|
for (const value of [[], null, [{ assetId: 0, projectName: 'A', quantity: 1 }],
|
|
[{ assetId: 1, projectName: '', quantity: 1 }], [{ assetId: 1, projectName: 'A', quantity: 1.5 }],
|
|
[{ assetId: 1, projectName: 'A', quantity: -1 }],
|
|
[{ assetId: 1, projectName: 'A', quantity: 1 }, { assetId: 1, projectName: 'B', quantity: 1 }],
|
|
Array.from({ length: 101 }, (_, i) => ({ assetId: i + 1, projectName: 'A', quantity: 1 }))]) {
|
|
assert.throws(() => normalizeExportItems(value));
|
|
}
|
|
assert.deepEqual(normalizeExportItems([{ assetId: 1, allocations: [
|
|
{ projectName: 'Dự án A', quantity: 2 }, { projectName: 'Dự án B', quantity: 3 }
|
|
] }])[0].allocations.map(row => row.quantity), [2, 3]);
|
|
});
|
|
|
|
test('permanent project exports consume the existing stock ledger without adding returnable borrowers', () => {
|
|
const { prepareAssetExport } = createExportTestApp({});
|
|
const asset = { AssetId: 1, AssetCode: 'TS-001', Quantity: 12, ImportInPeriod: 3,
|
|
ExportInPeriod: 4, EndingBalance: 11, NewQuantity: 3, UsedQuantity: 8,
|
|
Borrower: 'An (2)', ProjectAllocations: JSON.stringify([{ projectName: 'Cũ', borrowerName: '', quantity: 2 }]) };
|
|
const plan = prepareAssetExport(asset, [{ projectName: 'Dự án A', quantity: 2 }, { projectName: 'Dự án B', quantity: 3 }], 'Dự án mới');
|
|
assert.equal(plan.exportInPeriod, 9);
|
|
assert.equal(plan.endingBalance, 6);
|
|
assert.equal(plan.newQuantity, 0);
|
|
assert.equal(plan.usedQuantity, 6);
|
|
assert.equal(plan.borrower, undefined);
|
|
assert.equal(asset.Borrower, 'An (2)');
|
|
assert.throws(() => prepareAssetExport(asset, [{ projectName: 'A', quantity: 12 }]), /vượt quá/);
|
|
assert.throws(() => prepareAssetExport({ ...asset, EndingBalance: 0 }, [{ projectName: 'A', quantity: 1 }]), /hết tồn/);
|
|
});
|
|
|
|
test('returning a loan preserves all quantities permanently issued into projects', () => {
|
|
const legacyLoan = [{ projectName: 'Mượn cũ', borrowerName: 'An', quantity: 3 }];
|
|
const allocations = addProjectExports(legacyLoan, [{ projectName: 'Dự án A', quantity: 4 }]);
|
|
const returned = releaseAllocations(allocations, 'An', 3, 3);
|
|
assert.deepEqual(returned, [{ projectName: 'Dự án A', borrowerName: '', quantity: 4, allocationType: 'export' }]);
|
|
});
|
|
|
|
test('production session middleware protects proposal creation, listing and decisions', async t => {
|
|
const { productionApp } = createExportTestApp({});
|
|
const server = productionApp.listen(0, '127.0.0.1');
|
|
await new Promise(resolve => server.once('listening', resolve));
|
|
t.after(() => { server.closeAllConnections(); return new Promise(resolve => server.close(resolve)); });
|
|
for (const [path, method] of [['/api/asset-export-requests', 'GET'], ['/api/asset-export-requests', 'POST'],
|
|
['/api/asset-export-requests/1/process', 'POST']]) {
|
|
const response = await fetch(`http://127.0.0.1:${server.address().port}${path}`, { method });
|
|
assert.equal(response.status, 401);
|
|
}
|
|
});
|