const assert = require('node:assert/strict'); const fs = require('node:fs'); const os = require('node:os'); const path = require('node:path'); const {spawnSync} = require('node:child_process'); const index = fs.readFileSync('web/templates/index.html', 'utf8'); const base = fs.readFileSync('web/templates/base.html', 'utf8'); function extract(source, name) { const match = source.match(new RegExp(`^( *)(?:async )?function ${name}\\(`, 'm')); assert(match, `Missing function ${name}`); const end = source.indexOf(`\n${match[1]}}`, match.index); assert(end > match.index); return source.slice(match.index, end + match[1].length + 2); } const functions = [ ...['renderMultiSelectTab', 'renderMultiSelectTabWithSections', 'renderRentalTab', '_renderBOMParsedTable'].map(n => extract(index, n)), ...['showToast', 'loadSyncInfo', 'repairPendingChanges'].map(n => extract(base, n)), ].join('\n'); const ids = new Set([...functions.matchAll(/getElementById\('([^']+)'\)/g)].map(m => m[1])); const fixtures = [...ids].map(id => id.endsWith('-body') ? `
` : `
`).join(''); const listeners = index.split('`); const chrome = process.env.CHROME_BIN || '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'; try { const result = spawnSync(chrome, ['--headless', '--disable-gpu', '--no-first-run', '--disable-background-networking', '--user-data-dir=' + path.join(directory, 'profile'), '--virtual-time-budget=1000', '--dump-dom', 'file://' + target], {encoding: 'utf8', timeout: 30000}); assert.ifError(result.error); assert.equal(result.status, 0, result.stderr); const outcome = result.stdout.match(/
([\s\S]*?)<\/pre>/)?.[1];
    assert.equal(outcome, 'PASS', outcome || 'Browser did not finish');
    console.log('Browser HTML security regressions passed');
} finally {
    fs.rmSync(directory, {recursive: true, force: true});
}