'use strict';
// Unit test for the client-RX coverage frontend gate (SP2).
//
// The coverage toggle (#nqCoverage) and its legend (#nqCovLegend) are built
// inline inside node-reach.js's load() — a large async function that depends on
// api()/Leaflet, so it can't be invoked headless. Instead we extract the exact
// actions-HTML concatenation block from the real source and evaluate it in a vm
// sandbox with both values of window.MC_CLIENT_RX_COVERAGE. This exercises the
// real source markup logic (no hand-copied duplicate) for the gate.
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const vm = require('vm');
const src = fs.readFileSync(path.join(__dirname, 'public', 'node-reach.js'), 'utf8');
// Slice the actions-HTML expression: from the '
'";
const startIdx = src.indexOf(startMarker);
assert.ok(startIdx >= 0, 'could not locate nq-actions block start in node-reach.js');
const endMarker = "'
';";
const endIdx = src.indexOf(endMarker, startIdx);
assert.ok(endIdx >= 0, 'could not locate nq-actions block end in node-reach.js');
// Drop the trailing ";" so we can wrap the concatenation as a single expression.
let block = src.slice(startIdx, endIdx + endMarker.length).replace(/;\s*$/, '');
// Render the actions HTML for a given flag value via a controlled sandbox.
function renderActions(flag) {
const sandbox = {
window: { MC_CLIENT_RX_COVERAGE: flag },
coverageOn: false,
statsHtml: '',
};
vm.createContext(sandbox);
return vm.runInContext('(' + block + ')', sandbox);
}
// Flag OFF ⇒ no coverage checkbox, no legend.
const off = renderActions(false);
assert.ok(!off.includes('id="nqCoverage"'),
'flag false: actions HTML must NOT contain id="nqCoverage"');
assert.ok(!off.includes('id="nqCovLegend"'),
'flag false: actions HTML must NOT contain id="nqCovLegend"');
// Flag ON ⇒ coverage checkbox and legend present.
const on = renderActions(true);
assert.ok(on.includes('id="nqCoverage"'),
'flag true: actions HTML MUST contain id="nqCoverage"');
assert.ok(on.includes('id="nqCovLegend"'),
'flag true: actions HTML MUST contain id="nqCovLegend"');
// #19: the legend visibility is class-driven (.is-hidden), not an inline
// style="display:..." that CSS can't override. coverageOn is false in this
// sandbox, so the legend must carry is-hidden and no inline display style.
assert.ok(/class="nq-cov-legend[^"]*\bis-hidden\b/.test(on),
'flag true + coverage off: legend must use the is-hidden class');
assert.ok(!on.includes('style="display:'),
'legend must not use an inline display style (#19)');
// Sanity: the non-gated controls render regardless of the flag.
assert.ok(off.includes('id="nqIncoming"') && on.includes('id="nqIncoming"'),
'incoming filter must render irrespective of the coverage flag');
console.log('coverage gate (node-reach actions HTML) OK');