const assert = require('node:assert/strict'); const fs = require('node:fs'); const vm = require('node:vm'); const source = fs.readFileSync('web/templates/index.html', 'utf8'); const base = fs.readFileSync('web/templates/base.html', 'utf8'); const nodes = {}; const listeners = {}; const calls = []; const context = vm.createContext({ document: { getElementById: id => nodes[id] ??= {}, createElement: () => ({}), addEventListener: (name, handler) => listeners[name] = handler, }, setTimeout: () => {}, cart: [], allComponents: [], currentTab: 'cpu', getTabForCategory: () => 'cpu', getDisplayPrice: () => 1, qualityDotHtml: () => '', formatMoney: String, formatPriceOrNA: String, removeFromCart: lot => calls.push(['remove', lot]), updateMultiQuantity: (lot, value) => calls.push(['quantity', lot, value]), onRentalConditionChange: (lot, value) => calls.push(['condition', lot, value]), showAutocompleteEditItem: lot => calls.push(['edit', lot]), }); function loadFunction(text, name) { const start = text.indexOf(`function ${name}(`); assert(start >= 0); const end = text.indexOf('\n}', start); // Functions in base.html have eight spaces of indentation. const baseEnd = text.indexOf('\n }', start); vm.runInContext(text.slice(start, (end < 0 ? baseEnd + 10 : end + 2)), context); } vm.runInContext(fs.readFileSync('web/static/html-escape.js', 'utf8'), context); loadFunction(source, 'renderMultiSelectTab'); vm.runInContext(source.split('