109 lines
5.2 KiB
JavaScript
109 lines
5.2 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');
|
|
|
|
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}`);
|
|
}
|
|
|
|
function textContent(markup) {
|
|
return markup.replace(/<[^>]+>/g, '').trim();
|
|
}
|
|
|
|
const inboundPage = html.match(/<div class="page-content" id="page-inbound">([\s\S]*?)<!-- ========== PAGE: 头程订单详情/)?.[1] || '';
|
|
assert.ok(inboundPage, 'inbound order list should exist');
|
|
|
|
const headers = Array.from(inboundPage.matchAll(/<th(?:\s[^>]*)?>([\s\S]*?)<\/th>/g), match => textContent(match[1]));
|
|
assert.deepEqual(headers.slice(0, 5), ['序号', '', '操作', '入仓码', '订单状态']);
|
|
|
|
const body = { innerHTML: '' };
|
|
const total = { textContent: '' };
|
|
const order = {
|
|
id: 'order-1', orderNo: 'RK-1', inboundNo: 'RK-1', channel: '美线快线', destinationWarehouse: 'ONT8',
|
|
boxes: 1, carrier: '顺丰', expressNo: 'SF-1', skus: 'SKU-1', grossWeight: '10 kg', netWeight: '9 kg',
|
|
volume: '0.1 m3', quantity: 10, unitPrice: '$1/kg', billableVolume: '0.1 m3', billableWeight: '10 kg',
|
|
remark: '-', shipmentNo: '-', trackingNo: '-', receivedAt: '-', businessDate: '2026-09-02',
|
|
status: '待确认', measurement: { rows: [] }, measurementConfirmed: false
|
|
};
|
|
const renderContext = {
|
|
document: { getElementById: id => ({ 'inbound-table-body': body, 'inbound-total': total })[id] || null },
|
|
inboundFilteredOrders: null,
|
|
INBOUND_ORDER_SEED: [order],
|
|
escapeInboundHtml: value => String(value == null ? '' : value),
|
|
inboundStatusClass: () => 'status-pending'
|
|
};
|
|
const renderInboundOrders = vm.runInNewContext(`(${extractFunction('renderInboundOrders')})`, renderContext);
|
|
renderInboundOrders();
|
|
|
|
const rowCells = Array.from(body.innerHTML.matchAll(/<td(?:\s[^>]*)?>([\s\S]*?)<\/td>/g), match => match[1]);
|
|
assert.equal(textContent(rowCells[0]), '1');
|
|
assert.match(rowCells[2], />查看<\/button>/);
|
|
assert.match(rowCells[2], /class="inbound-action"[^>]*>确认计费重<\/button>/);
|
|
assert.doesNotMatch(rowCells[2], /inbound-action-confirm/);
|
|
assert.match(rowCells[2], /onclick="showInboundOrderDetail\('order-1','measure'\)"[^>]*>确认计费重<\/button>/);
|
|
assert.equal(textContent(rowCells[3]), 'RK-1');
|
|
assert.equal(textContent(rowCells[4]), '待确认');
|
|
|
|
assert.match(inboundPage, />待确认<\/option>/);
|
|
assert.doesNotMatch(inboundPage, /待客户确认/);
|
|
assert.match(html, /<b>待发送数据<\/b><small id="inbound-node-shipping-data">/);
|
|
assert.doesNotMatch(html, /<b>待发运数据<\/b><small id="inbound-node-shipping-data">/);
|
|
|
|
const measurementModal = html.match(/<div class="topup-overlay hidden" id="inbound-measurement-modal"[^>]*>([\s\S]*?)<\/div>\s*<\/div>/)?.[1] || '';
|
|
assert.ok(measurementModal, 'measurement confirmation modal should exist');
|
|
assert.match(measurementModal, /<h3 id="inbound-measurement-title">确认<\/h3>/);
|
|
assert.match(measurementModal, /请确认是否接受后台测量结果?确认后将按测量结果计费,且不可修改。/);
|
|
assert.doesNotMatch(measurementModal, /inbound-modal-measure-summary|inbound-modal-measure-rows|箱规长\(cm\)/);
|
|
assert.match(measurementModal, />确认<\/button>/);
|
|
|
|
const selectedTabs = [];
|
|
const visitedPages = [];
|
|
const detailContext = {
|
|
inboundOrderById: () => order,
|
|
switchPage: page => visitedPages.push(page),
|
|
switchInboundDetailTab: tab => selectedTabs.push(tab),
|
|
renderInboundMeasureSummary() {},
|
|
renderInboundMeasureRows() {},
|
|
renderInboundCustomsDocs() {},
|
|
inboundDetailField: () => '',
|
|
escapeInboundHtml: value => String(value == null ? '' : value),
|
|
inboundDetailOrderId: null,
|
|
document: {
|
|
getElementById: id => id === 'inbound-detail-confirm-btn'
|
|
? { textContent: '', innerHTML: '', hidden: false, disabled: false }
|
|
: { textContent: '', innerHTML: '', hidden: false }
|
|
}
|
|
};
|
|
const showInboundOrderDetail = vm.runInNewContext(`(${extractFunction('showInboundOrderDetail')})`, detailContext);
|
|
showInboundOrderDetail('order-1', 'measure');
|
|
assert.deepEqual(visitedPages, ['inbound-detail']);
|
|
assert.deepEqual(selectedTabs, ['measure']);
|
|
|
|
order.measurementConfirmed = true;
|
|
const confirmedButton = { textContent: '', innerHTML: '', hidden: false, disabled: false };
|
|
const confirmedDetailContext = {
|
|
...detailContext,
|
|
inboundOrderById: () => order,
|
|
document: {
|
|
getElementById: id => id === 'inbound-detail-confirm-btn' ? confirmedButton : { textContent: '', innerHTML: '', hidden: false }
|
|
}
|
|
};
|
|
const showConfirmedInboundOrderDetail = vm.runInNewContext(`(${extractFunction('showInboundOrderDetail')})`, confirmedDetailContext);
|
|
showConfirmedInboundOrderDetail('order-1', 'measure');
|
|
assert.equal(confirmedButton.disabled, true);
|
|
assert.equal(confirmedButton.hidden, false);
|
|
assert.equal(confirmedButton.textContent, '已确认');
|
|
|
|
console.log('inbound order list requirements: ok');
|