const test = require('node:test'); const assert = require('node:assert/strict'); const fs = require('node:fs'); const vm = require('node:vm'); const source = fs.readFileSync(require.resolve('../public/js/app.js'), 'utf8'); function fixture() { const nodes = { borrowAssetIdInput: { value: '17' }, borrowAssetUserInput: { value: 'An' }, borrowAssetProjectInput: { value: 'A' }, borrowQuantityInput: { value: '2' }, borrowAssetNoteInput: { value: '' }, borrowCurrentEndingInput: { value: '7' }, borrowAllocationSummary: { classList: { toggle() {} } } }; const extra = [{ projectName: 'B', quantity: 3 }]; const tbody = { innerHTML: '' }; const button = { disabled: false }; const requests = [], warnings = []; const fetch = async (url, options) => { requests.push({ url, options }); return { ok: true, json: async () => ({ success: true }) }; }; const document = { addEventListener() {}, getElementById: id => nodes[id] || null, querySelector: selector => selector === '.assets-table-body' ? tbody : selector.includes('button[type="submit"]') ? button : null, querySelectorAll: selector => selector.includes('borrow-project-row') ? extra.map(row => ({ querySelector: kind => ({ value: kind === 'select' ? row.projectName : String(row.quantity) }) })) : [] }; const context = vm.createContext({ document, window: { fetch }, fetch, console }); vm.runInContext(`${source}\napp = Object.create(AccountManager.prototype); globalThis.manager = app;`, context); const manager = context.manager; Object.assign(manager, { assets: [{ AssetId: 17, AssetCode: 'D', AssetName: 'Drill', Quantity: 10, ImportInPeriod: 0, ExportInPeriod: 3, EndingBalance: 7, Unit: 'cái', Project: 'Last project', ProjectAllocations: JSON.stringify([{ projectName: '', borrowerName: 'An', quantity: 1 }, { projectName: 'Beta', borrowerName: 'Binh', quantity: 2 }]) }], assetPage: 1, assetPageSize: 20, selectedAssetIds: new Set(), apiBase: '/api', ensureAssetManagePermission: () => true, canCurrentUserManageAssets: () => true, getAuthHeaders: () => ({}), notifyWarning: message => warnings.push(message), notifyFailure: message => warnings.push(message), notifySuccess() {}, closeModals() {}, refreshAssetsUI: async () => {}, setupAssetRowListeners() {}, setupAssetPagerListeners() {} }); return { manager, nodes, extra, requests, warnings, tbody, button }; } test('initial table, refreshed table and mobile show all projects with escaped names and quantities', () => { const { manager, tbody } = fixture(); for (const html of [manager.getAssetsContent(), manager.getAssetMobileCardHtml(manager.assets[0], 1, true)]) { assert.match(html, /<Alpha>: 1<\/strong>/); assert.match(html, /Beta: 2<\/strong>/); assert.doesNotMatch(html, //); } manager.renderAssetsTableBody(); assert.match(tbody.innerHTML, /Beta: 2<\/strong>/); manager.assetSearchTerm = 'alpha'; assert.equal(manager.getFilteredAssets().length, 1); }); test('unknown legacy exports are explicit and zero outstanding exports show no project', () => { const { manager } = fixture(); const asset = { Quantity: 5, ExportInPeriod: 3, Project: 'Last project' }; assert.match(manager.formatAssetProjectsText(asset), /Chưa xác định dự án: 3/); assert.doesNotMatch(manager.formatAssetProjectsText(asset), /Last project/); assert.equal(manager.formatAssetProjectsHtml({ ...asset, ExportInPeriod: 0 }), '-'); }); test('export form submits every project in a single request and updates combined total', async () => { const { manager, requests, nodes, button } = fixture(); manager.updateBorrowAllocationSummary(); assert.match(nodes.borrowAllocationSummary.textContent, /Tổng xuất: 5 \/ 7/); assert.match(nodes.borrowAllocationSummary.textContent, /Còn lại: 2/); await manager.handleBorrowAssetSubmit({ preventDefault() {} }); assert.equal(requests.length, 1); assert.deepEqual(JSON.parse(requests[0].options.body).allocations, [{ projectName: 'A', quantity: 2 }, { projectName: 'B', quantity: 3 }]); assert.equal(button.disabled, false); }); test('form rejects missing projects, fractional quantities and combined over-allocation', async () => { for (const row of [{ projectName: '', quantity: 1 }, { projectName: 'B', quantity: 1.5 }, { projectName: 'B', quantity: 6 }]) { const { manager, requests, extra, warnings } = fixture(); extra[0] = row; await manager.handleBorrowAssetSubmit({ preventDefault() {} }); assert.equal(requests.length, 0); assert.equal(warnings.length, 1); } }); test('available quantity is capped by total even when an imported balance is inflated', () => { const { manager } = fixture(); manager.assets[0].EndingBalance = 100; assert.equal(manager.getAssetAvailableForExport(manager.assets[0]), 7); });