'use strict';
// Linked Monitors editor: round trip of an existing expression through
// expr_to_ui()/ui_to_expr() with a minimal jQuery stand-in that follows
// jQuery's select semantics (val() is null when no option matches).
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const vm = require('vm');
class El {
constructor(tag) {
this.tag = tag;
this.options = [];
this.selected = -1;
this.value = '';
this.handlers = {};
this.children = [];
this.text = '';
}
}
function wrap(el) {
const w = {
el: el,
append(x) {
if (typeof x === 'string') {
const m = x.match(/^') return wrap(new El('option'));
if (x === '[name="newMonitor[LinkedMonitors]"]') return wrap(input);
if (x === '#LinkedMonitorsUI') return wrap(ui);
throw new Error('unexpected selector ' + x);
}
const src = fs.readFileSync(
path.join(__dirname, '../../web/js/MonitorLinkExpression.js'), 'utf8');
const ctx = vm.createContext({
console: {log() {}},
$j: $j,
mid: 1,
monitors: [{Id: '1', Name: 'A'}, {Id: '2', Name: 'B'}, {Id: '3', Name: 'C'}, {Id: '4', Name: 'D'}],
zones: [{Id: '10', MonitorId: '2', Name: 'Z'}],
});
// setSelectValue() lives in the skin's skin.js, which every page loads
const skinSrc = fs.readFileSync(
path.join(__dirname, '../../web/skins/classic/js/skin.js'), 'utf8');
const helper = skinSrc.match(/^function setSelectValue\([\s\S]*?\n}\n/m);
if (!helper) throw new Error('setSelectValue not found in skin.js');
vm.runInContext(helper[0], ctx);
vm.runInContext(src, ctx);
let passed = 0;
let failed = 0;
function test(name, fn) {
try {
fn();
console.log(' ok ' + name);
passed++;
} catch (e) {
console.error(' FAIL ' + name);
console.error(' ' + (e.stack || e.message));
failed++;
}
}
// Load expr into the editor, optionally change one select, return what the
// editor would save.
function edit(expr, change) {
input.value = expr;
vm.runInContext('expr_to_ui(' + JSON.stringify(expr) + ', $j("#LinkedMonitorsUI"))', ctx);
const selects = ui.children.filter((c) => c.tag == 'select');
if (change) change(selects);
vm.runInContext('update_expr()', ctx);
return input.value;
}
function choose(select, value) {
const i = select.options.findIndex((o) => o.value === value);
assert(i >= 0, 'no option ' + value);
select.selected = i;
}
test('untouched expression without outer brackets is kept', () => {
assert.strictEqual(edit('2|3|4'), '2|3|4');
});
test('bracketed expression is kept', () => {
assert.strictEqual(edit('(2|3)&4'), '(2|3)&4');
assert.strictEqual(edit('(2|3)&(4|2:10)'), '(2|3)&(4|2:10)');
});
test('blank bracket choice writes nothing, not a 0', () => {
const out = edit('(2|3)&4', (s) => choose(s[0], ''));
assert.strictEqual(out, '2|3)&4');
assert(!/0/.test(out));
});
test('blank bracket on a closing bracket writes nothing', () => {
const out = edit('(2|3)&4', (s) => choose(s[s.length - 2], ''));
assert(!/0/.test(out), out);
});
test('comma operator is shown as or and saved as |', () => {
assert.strictEqual(edit('2,3'), '2|3');
assert.strictEqual(edit('2,3,4'), '2|3|4');
});
test('single linked monitor in brackets is kept', () => {
assert.strictEqual(edit('(2)'), '(2)');
});
test('more brackets than the editor offers are kept', () => {
assert.strictEqual(edit('((((2|3))))&4'), '((((2|3))))&4');
});
test('link to a monitor that is gone is kept', () => {
assert.strictEqual(edit('2|9'), '2|9');
assert.strictEqual(edit('2|3|9'), '2|3|9');
});
test('zone link round trips', () => {
assert.strictEqual(edit('2:10|3'), '2:10|3');
});
test('editor never writes the text null', () => {
const exprs = ['2,3', '(2)', '2|3|9', '((((2|3))))&4', '2|3|4', '(2|3)&4'];
for (const e of exprs) assert(!/null/.test(edit(e)), e);
});
console.log(passed + ' passed, ' + failed + ' failed');
process.exit(failed ? 1 : 0);