89 lines
3.8 KiB
JavaScript
89 lines
3.8 KiB
JavaScript
const assert = require('node:assert/strict');
|
|
const fs = require('node:fs');
|
|
const vm = require('node:vm');
|
|
|
|
const html = fs.readFileSync('workbench.html', 'utf8');
|
|
const listPage = html.match(/<div class="page-content" id="page-cargo-disposal-list">([\s\S]*?)<!-- ========== PAGE: 新增货物废弃/)?.[1] || '';
|
|
assert.match(listPage, /<th>操作<\/th>/, 'view and edit should share an operation column');
|
|
|
|
function extractFunction(name) {
|
|
const start = html.indexOf(`function ${name}(`);
|
|
assert.notEqual(start, -1, `${name} should exist`);
|
|
const bodyStart = html.indexOf('{', start);
|
|
let depth = 0;
|
|
for (let index = bodyStart; index < html.length; index += 1) {
|
|
if (html[index] === '{') depth += 1;
|
|
if (html[index] === '}') depth -= 1;
|
|
if (depth === 0) return html.slice(start, index + 1);
|
|
}
|
|
throw new Error(`Unable to extract ${name}`);
|
|
}
|
|
|
|
const editModal = html.match(/<div class="modal-overlay" id="cargo-disposal-edit-modal"([\s\S]*?)<!-- ========== MODAL:/)?.[1] || '';
|
|
assert.ok(editModal, 'cargo disposal edit modal should exist');
|
|
assert.match(editModal, /id="cargo-disposal-edit-inbound-search"/);
|
|
assert.match(editModal, /id="cargo-disposal-edit-inbound-cards"/);
|
|
assert.match(editModal, /id="cargo-disposal-edit-fee"/);
|
|
assert.match(editModal, /id="cargo-disposal-edit-error"/);
|
|
assert.match(editModal, /onsubmit="submitCargoDisposalEdit\(event\)"/);
|
|
|
|
const tbody = { innerHTML: '' };
|
|
const controls = {
|
|
'cargo-disposal-list-body': tbody,
|
|
'cargo-disposal-total': { textContent: '' },
|
|
'cargo-disposal-page-number': { textContent: '' },
|
|
'cargo-disposal-prev': { disabled: false },
|
|
'cargo-disposal-next': { disabled: false }
|
|
};
|
|
const renderCargoDisposalList = vm.runInNewContext(
|
|
`(${extractFunction('renderCargoDisposalList')})`,
|
|
{
|
|
document: { getElementById: id => controls[id] || null },
|
|
cargoDisposalFilteredOrders: null,
|
|
loadCargoDisposalOrders: () => [{
|
|
id: 'order-1', orderNo: 'FH001', inboundNo: 'RK001', disposalBoxes: 2,
|
|
warehouse: '美西洛杉矶仓', feeAmount: 10, outboundOrderNo: 'CK001',
|
|
billingNo: 'JF001', status: '待处理', createdAt: '2026-09-04 09:00'
|
|
}],
|
|
CARGO_DISPOSAL_PAGE_SIZE: 10,
|
|
cargoDisposalCurrentPage: 1,
|
|
escapeCargoDisposalHtml: value => String(value == null ? '' : value)
|
|
}
|
|
);
|
|
renderCargoDisposalList();
|
|
assert.match(tbody.innerHTML, />查看<\/button>[\s\S]*?>编辑<\/button>/, 'each row should show edit after view');
|
|
assert.match(tbody.innerHTML, /openCargoDisposalEdit\(this\.dataset\.cargoDisposalId\)/);
|
|
|
|
const mergeCargoDisposalOrder = vm.runInNewContext(`(${extractFunction('mergeCargoDisposalOrder')})`);
|
|
const original = {
|
|
id: 'order-1', orderNo: 'FH001', tailOrderNo: 'LC001', outboundOrderNo: 'CK001',
|
|
billingNo: 'JF001', status: '处理中', createdAt: '2026-09-01 08:00', currency: 'USD',
|
|
inboundNo: 'OLD', warehouse: '旧仓', disposalBoxes: 2, feeAmount: 10,
|
|
items: [{ sku: 'OLD-SKU', name: '旧商品', availableBoxes: 4, disposalBoxes: 2 }]
|
|
};
|
|
const updated = mergeCargoDisposalOrder(original, {
|
|
inbound: {
|
|
inboundNo: 'RK009', warehouse: '美东新泽西仓',
|
|
items: [
|
|
{ id: 'one', sku: 'SKU-1', name: '商品一', availableBoxes: 8 },
|
|
{ id: 'two', sku: 'SKU-2', name: '商品二', availableBoxes: 5 }
|
|
]
|
|
},
|
|
items: [
|
|
{ id: 'one', disposalBoxes: 3 },
|
|
{ id: 'two', disposalBoxes: 1 }
|
|
]
|
|
}, 5);
|
|
|
|
assert.deepEqual(JSON.parse(JSON.stringify(updated)), {
|
|
id: 'order-1', orderNo: 'FH001', tailOrderNo: 'LC001', outboundOrderNo: 'CK001',
|
|
billingNo: 'JF001', status: '处理中', createdAt: '2026-09-01 08:00', currency: 'USD',
|
|
inboundNo: 'RK009', warehouse: '美东新泽西仓', disposalBoxes: 4, feeAmount: 20,
|
|
items: [
|
|
{ sku: 'SKU-1', name: '商品一', availableBoxes: 8, disposalBoxes: 3 },
|
|
{ sku: 'SKU-2', name: '商品二', availableBoxes: 5, disposalBoxes: 1 }
|
|
]
|
|
});
|
|
|
|
console.log('cargo disposal edit: ok');
|