68 lines
2.9 KiB
JavaScript
68 lines
2.9 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 extractConstValue(name) {
|
|
const declaration = `const ${name} = `;
|
|
const start = html.indexOf(declaration);
|
|
assert.notEqual(start, -1, `${name} should exist`);
|
|
const valueStart = start + declaration.length;
|
|
const end = html.indexOf(';', valueStart);
|
|
assert.notEqual(end, -1, `${name} should have a value`);
|
|
return html.slice(valueStart, end);
|
|
}
|
|
|
|
function textContent(markup) {
|
|
return markup.replace(/<[^>]+>/g, '').trim();
|
|
}
|
|
|
|
assert.match(html, /(?=[^>]*data-tab="container")(?=[^>]*aria-controls="inbound-panel-container")[^>]*>货柜节点<\/button>/);
|
|
assert.match(html, /id="inbound-panel-container"[\s\S]*id="inbound-detail-container-nodes"/);
|
|
|
|
const body = { innerHTML: '' };
|
|
const context = {
|
|
document: { getElementById: id => id === 'inbound-detail-container-nodes' ? body : null },
|
|
escapeInboundHtml: value => String(value == null ? '' : value),
|
|
};
|
|
const render = vm.runInNewContext(`(${extractFunction('renderInboundContainerNodes')})`, context);
|
|
render([]);
|
|
const defaultNodeNames = Array.from(body.innerHTML.matchAll(/<div class="inbound-container-node-name">([\s\S]*?)<\/div>/g), match => textContent(match[1]));
|
|
assert.equal(defaultNodeNames[6], '等待提柜');
|
|
|
|
const inboundOrders = vm.runInNewContext(`(${extractConstValue('INBOUND_ORDER_SEED')})`);
|
|
render(inboundOrders[0].containerNodes);
|
|
const seededNodeNames = Array.from(body.innerHTML.matchAll(/<div class="inbound-container-node-name">([\s\S]*?)<\/div>/g), match => textContent(match[1]));
|
|
assert.equal(seededNodeNames[6], '等待提柜');
|
|
|
|
render([
|
|
{ name: '国内报关完成', status: 'done', time: '2026-07-22 11:13' },
|
|
{ name: '实际开船', status: 'current', time: '-' },
|
|
{ name: '实际到港', status: 'pending', time: '-' },
|
|
]);
|
|
|
|
assert.equal((body.innerHTML.match(/class="inbound-container-node is-/g) || []).length, 3);
|
|
assert.match(body.innerHTML, /国内报关完成/);
|
|
assert.match(body.innerHTML, /实际开船/);
|
|
assert.match(body.innerHTML, /实际到港/);
|
|
assert.match(body.innerHTML, /inbound-container-node is-done/);
|
|
assert.match(body.innerHTML, /inbound-container-node is-current/);
|
|
assert.match(body.innerHTML, /inbound-container-node is-pending/);
|
|
assert.equal(textContent(body.innerHTML).includes('2026-07-22 11:13'), true);
|
|
|
|
console.log('inbound container nodes requirements: ok');
|