update file tài sản
This commit is contained in:
173
test/asset-archive-ui.test.js
Normal file
173
test/asset-archive-ui.test.js
Normal file
@@ -0,0 +1,173 @@
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
const vm = require('node:vm');
|
||||
|
||||
const source = fs.readFileSync(path.join(__dirname, '../public/js/app.js'), 'utf8');
|
||||
|
||||
function fixture({ allowed = true, confirmed = true, responseSuccess = true } = {}) {
|
||||
const requests = [];
|
||||
const refreshed = [];
|
||||
const notifications = [];
|
||||
const button = { disabled: false };
|
||||
const fetch = async (url, options) => {
|
||||
requests.push({ url, options });
|
||||
return { ok: responseSuccess, json: async () => ({ success: responseSuccess, data: { archivedCount: 2 } }) };
|
||||
};
|
||||
const document = {
|
||||
addEventListener() {},
|
||||
getElementById: id => id === 'archiveAllAssetsBtn' ? button : null
|
||||
};
|
||||
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, {
|
||||
apiBase: '/api',
|
||||
assets: [{ AssetId: 1 }, { AssetId: 2 }],
|
||||
archivedAssets: [],
|
||||
assetBorrows: [],
|
||||
selectedAssetIds: new Set([1, 2]),
|
||||
canCurrentUserManageAssets: () => allowed,
|
||||
ensureAssetManagePermission: () => allowed,
|
||||
confirmBulkAssetDelete: async (count, all) => {
|
||||
assert.equal(count, 2);
|
||||
assert.equal(all, true);
|
||||
return confirmed;
|
||||
},
|
||||
refreshAssetsUI: async () => { refreshed.push('assets'); manager.assets = []; },
|
||||
refreshAssetBorrowsUI: async () => { refreshed.push('borrows'); },
|
||||
notifySuccess: message => notifications.push(message),
|
||||
notifyFailure: message => notifications.push(message),
|
||||
notifyWarning: message => notifications.push(message)
|
||||
});
|
||||
return { manager, requests, refreshed, notifications, button };
|
||||
}
|
||||
|
||||
function borrow(BorrowId, overrides = {}) {
|
||||
return {
|
||||
BorrowId, AssetId: BorrowId, AssetCode: 'SAME-CODE', AssetName: 'Máy đo',
|
||||
RequestType: 'borrow', RequestStatus: 'approved', BorrowQuantity: 5,
|
||||
ReturnedQuantity: 0, RemainingQuantity: 5, IsArchived: false,
|
||||
...overrides
|
||||
};
|
||||
}
|
||||
|
||||
test('all history retains completed loans and partial loans with related returns', () => {
|
||||
const { manager } = fixture();
|
||||
manager.assetBorrows = [
|
||||
borrow(1),
|
||||
borrow(2, { RelatedReturnCount: 1, ReturnedQuantity: 2, RemainingQuantity: 3 }),
|
||||
borrow(3, { RequestStatus: 'returned', ReturnedQuantity: 5, RemainingQuantity: 0 }),
|
||||
borrow(4, { RequestType: 'return' })
|
||||
];
|
||||
assert.deepEqual(Array.from(manager.getFilteredAssetBorrows(), row => row.BorrowId), [1, 2, 3, 4]);
|
||||
});
|
||||
|
||||
test('outstanding loans include archived, partial, and pending-return loans, excluding other statuses', () => {
|
||||
const { manager } = fixture();
|
||||
manager.assetBorrowTypeFilter = 'outstanding';
|
||||
manager.assetBorrows = [
|
||||
borrow(1),
|
||||
borrow(2, { IsArchived: true, ReturnedQuantity: 2, RemainingQuantity: 3, RelatedReturnCount: 1 }),
|
||||
borrow(3, { IsArchived: 1, RelatedReturnCount: 1, AvailableReturnQuantity: 0 }),
|
||||
borrow(4, { RequestStatus: 'pending' }),
|
||||
borrow(5, { RequestStatus: 'rejected' }),
|
||||
borrow(6, { RequestStatus: 'returned' }),
|
||||
borrow(7, { RemainingQuantity: 0 }),
|
||||
borrow(8, { RequestType: 'return' })
|
||||
];
|
||||
assert.deepEqual(Array.from(manager.getFilteredAssetBorrows(), row => row.BorrowId), [1, 2, 3]);
|
||||
manager.assetBorrowCatalogFilter = 'archived';
|
||||
assert.deepEqual(Array.from(manager.getFilteredAssetBorrows(), row => row.BorrowId), [2, 3]);
|
||||
manager.assetBorrowCatalogFilter = 'current';
|
||||
assert.deepEqual(Array.from(manager.getFilteredAssetBorrows(), row => row.BorrowId), [1]);
|
||||
manager.assetBorrowSearchTerm = 'missing';
|
||||
assert.equal(manager.getFilteredAssetBorrows().length, 0);
|
||||
});
|
||||
|
||||
test('partial archived loans permit another return only for quantity not reserved by pending returns', () => {
|
||||
const { manager } = fixture();
|
||||
const partial = borrow(1, { IsArchived: true, RelatedReturnCount: 1, ReturnedQuantity: 2, RemainingQuantity: 3, AvailableReturnQuantity: 3 });
|
||||
assert.equal(manager.canCreateAssetReturnRequestFromBorrow(partial), true);
|
||||
assert.equal(manager.canCreateAssetReturnRequestFromBorrow({ ...partial, AvailableReturnQuantity: 0 }), false);
|
||||
assert.equal(manager.canCreateAssetReturnRequestFromBorrow({ ...partial, RequestStatus: 'pending' }), false);
|
||||
assert.match(manager.getArchivedAssetBadgeHtml(partial), /Tài sản cũ/);
|
||||
});
|
||||
|
||||
test('legacy borrower summaries remain visible without any borrow requests or current assets', () => {
|
||||
const { manager } = fixture();
|
||||
manager.assets = [];
|
||||
manager.archivedAssets = [
|
||||
{ AssetId: 1, AssetCode: 'REUSED', Borrower: [{ name: 'An', quantity: 2 }], ExportInPeriod: 2, IsArchived: true },
|
||||
{ AssetId: 2, AssetCode: 'REUSED', Borrower: '', ExportInPeriod: 3, IsArchived: true },
|
||||
{ AssetId: 3, AssetCode: 'DONE', Borrower: '', ExportInPeriod: 0, IsArchived: true }
|
||||
];
|
||||
assert.deepEqual(Array.from(manager.getArchivedOutstandingAssets(), row => row.AssetId), [1, 2]);
|
||||
manager.archivedAssetSearchTerm = 'An';
|
||||
assert.deepEqual(Array.from(manager.getArchivedOutstandingAssets(), row => row.AssetId), [1]);
|
||||
manager.getCurrentUserBorrowerNameKeys = () => ['an'];
|
||||
assert.deepEqual(Array.from(manager.getCurrentUserBorrowedAssets(), row => [row.AssetId, row.BorrowedQuantityByCurrentUser]), [[1, 2]]);
|
||||
});
|
||||
|
||||
test('same-code current and archived assets remain separate in my borrowed assets', () => {
|
||||
const { manager } = fixture();
|
||||
manager.getCurrentUserBorrowerNameKeys = () => ['an'];
|
||||
manager.assets = [{ AssetId: 2, AssetCode: 'REUSED', Borrower: [{ name: 'An', quantity: 1 }] }];
|
||||
manager.archivedAssets = [{ AssetId: 1, AssetCode: 'REUSED', Borrower: [{ name: 'An', quantity: 3 }], IsArchived: true }];
|
||||
assert.deepEqual(Array.from(manager.getCurrentUserBorrowedAssets(), row => [row.AssetId, row.BorrowedQuantityByCurrentUser]), [[2, 1], [1, 3]]);
|
||||
});
|
||||
|
||||
test('archive all requires confirmation, calls archive endpoint once and refreshes both datasets', async () => {
|
||||
const { manager, requests, refreshed, button } = fixture();
|
||||
await manager.handleArchiveAllAssets();
|
||||
assert.equal(requests.length, 1);
|
||||
assert.equal(requests[0].url, '/api/assets/archive-all');
|
||||
assert.equal(requests[0].options.method, 'POST');
|
||||
assert.deepEqual(refreshed, ['assets', 'borrows']);
|
||||
assert.equal(manager.selectedAssetIds.size, 0);
|
||||
assert.equal(manager.assetPage, 1);
|
||||
assert.equal(manager.archivingAssets, false);
|
||||
assert.equal(button.disabled, true, 'Empty current catalog cannot be archived again');
|
||||
});
|
||||
|
||||
test('cancel or missing permission leaves the catalog untouched', async () => {
|
||||
for (const options of [{ confirmed: false }, { allowed: false }]) {
|
||||
const { manager, requests, refreshed } = fixture(options);
|
||||
await manager.handleArchiveAllAssets();
|
||||
assert.equal(requests.length, 0);
|
||||
assert.equal(refreshed.length, 0);
|
||||
assert.equal(manager.selectedAssetIds.size, 2);
|
||||
assert.equal(manager.assets.length, 2);
|
||||
}
|
||||
});
|
||||
|
||||
test('failed archive preserves current selection and supports retry', async () => {
|
||||
const { manager, requests, refreshed, notifications, button } = fixture({ responseSuccess: false });
|
||||
await manager.handleArchiveAllAssets();
|
||||
assert.equal(requests.length, 1);
|
||||
assert.equal(refreshed.length, 0);
|
||||
assert.equal(manager.selectedAssetIds.size, 2);
|
||||
assert.equal(manager.archivingAssets, false);
|
||||
assert.equal(button.disabled, false);
|
||||
assert.match(notifications[0], /thất bại/);
|
||||
});
|
||||
|
||||
test('archive double clicks issue one request while confirmation is pending', async () => {
|
||||
const { manager, requests } = fixture();
|
||||
let resolveConfirmation;
|
||||
manager.confirmBulkAssetDelete = () => new Promise(resolve => { resolveConfirmation = resolve; });
|
||||
const pending = manager.handleArchiveAllAssets();
|
||||
await manager.handleArchiveAllAssets();
|
||||
resolveConfirmation(true);
|
||||
await pending;
|
||||
assert.equal(requests.length, 1);
|
||||
});
|
||||
|
||||
test('archived pending borrows cannot be approved, while archived returns can', () => {
|
||||
const { manager } = fixture();
|
||||
const pending = borrow(1, { RequestStatus: 'pending', IsArchived: true });
|
||||
assert.doesNotMatch(manager.buildPendingAssetRequestCardHtml(pending), /asset-request-approve-btn/);
|
||||
assert.match(manager.buildPendingAssetRequestCardHtml(pending), /asset-request-reject-btn/);
|
||||
assert.match(manager.buildPendingAssetRequestCardHtml({ ...pending, RequestType: 'return' }), /asset-request-approve-btn/);
|
||||
});
|
||||
Reference in New Issue
Block a user