'use strict'; // The shared left menu — docs/Design/Page/Left-Menu.md. // // This is the COMBINED suite for that design: the pure helpers, the template, the // side it appears on (and its mirroring under a right-to-left language), the // theming, and the layout it shares with the table page. Files under test are the // ones listed in the Related files table of the design doc. // // Every Admin Panel page has the same menu beside its content, and the markup was // retyped 44 times across seven templates — with, in two of those pages, one // click handler per entry instead of one per menu. // // Run: node tests/leftMenu.test.cjs const assert = require('assert'); const fs = require('fs'); const path = require('path'); let passed = 0; function test(name, fn) { fn(); passed += 1; console.log(' ok -', name); } const root = path.join(__dirname, '..'); const read = p => fs.readFileSync(path.join(root, p), 'utf8'); const libSrc = read('models/lib/leftMenu.js'); const jade = read('client/components/settings/leftMenu.jade'); const css = read('client/components/settings/settingBody.css'); const tableCss = read('client/components/settings/tablePage.css'); const doc = read('docs/Design/Page/Left-Menu.md'); const lib = {}; new Function('exports', libSrc.replace(/export function/g, 'function') + '\nexports.buildMenuItems = buildMenuItems;\nexports.activeCount = activeCount;' + '\nexports.leftMenuData = leftMenuData;\nexports.paneTitle = paneTitle;')(lib); console.log('leftMenu:'); // ── pure helpers ──────────────────────────────────────────────────────────── test('buildMenuItems marks exactly one entry active', () => { const items = lib.buildMenuItems([ { id: 'a', icon: 'fa-key', labelKey: 'registration' }, { id: 'b', icon: 'fa-envelope', labelKey: 'email' }, { id: 'c', icon: 'fa-users', labelKey: 'accounts' }, ], 'b'); assert.strictEqual(items.length, 3); assert.strictEqual(lib.activeCount(items), 1, 'a menu must never highlight two rows - that is what per-entry isXActive ' + 'helpers drift into'); assert.strictEqual(items[1].active, true); assert.strictEqual(items[0].active, false); }); test('an unknown or missing active id highlights nothing (negative)', () => { const items = [{ id: 'a', labelKey: 'x' }, { id: 'b', labelKey: 'y' }]; assert.strictEqual(lib.activeCount(lib.buildMenuItems(items, 'nope')), 0); assert.strictEqual(lib.activeCount(lib.buildMenuItems(items, undefined)), 0); assert.strictEqual(lib.activeCount(lib.buildMenuItems(items, '')), 0); }); test('a numeric id still matches the active id', () => { const items = lib.buildMenuItems([{ id: 1, labelKey: 'x' }], '1'); assert.strictEqual(items[0].active, true, 'compared as strings'); }); test('conditional entries and separators are handled', () => { // A page builds its list with holes (the E-mail entry is absent on Sandstorm). const items = lib.buildMenuItems([ { id: 'a', labelKey: 'x' }, null, { separator: true }, { id: 'b', labelKey: 'y' }, ], 'b'); assert.strictEqual(items.length, 3, 'the null hole is dropped, not rendered'); assert.deepStrictEqual(items[1], { separator: true }); assert.strictEqual(items[2].active, true); }); test('buildMenuItems survives junk input (negative)', () => { assert.deepStrictEqual(lib.buildMenuItems(null, 'a'), []); assert.deepStrictEqual(lib.buildMenuItems(undefined, undefined), []); assert.strictEqual(lib.activeCount(null), 0); // An entry with no id/icon/label renders as empty strings, not "undefined". const [item] = lib.buildMenuItems([{}], ''); assert.deepStrictEqual( { id: item.id, icon: item.icon, labelKey: item.labelKey }, { id: '', icon: '', labelKey: '' }); }); test('the page keeps its own handler class alongside the shared one', () => { const [item] = lib.buildMenuItems([{ id: 'a', labelKey: 'x' }], 'a', 'js-setting-menu'); assert.strictEqual(item.jsClass, 'js-setting-menu'); // Per-entry override wins, so a page can keep one odd entry on its own handler. const [own] = lib.buildMenuItems([{ id: 'a', labelKey: 'x', jsClass: 'js-other' }], 'a', 'js-setting-menu'); assert.strictEqual(own.jsClass, 'js-other'); }); // ── the template ──────────────────────────────────────────────────────────── test('the template renders the menu once, from the item list', () => { assert.ok(/template\(name="leftMenu"\)/.test(jade)); assert.ok(/\.side-menu/.test(jade) && /each items/.test(jade), 'one .side-menu, driven by the items'); assert.ok(/js-left-menu-item/.test(jade), 'every entry carries the shared handler class'); assert.ok(/data-id="\{\{id\}\}"/.test(jade), 'the id is what a handler reads'); assert.ok(/\{\{_ labelKey\}\}/.test(jade), 'labels are i18n keys, not literals'); assert.ok(/if separator/.test(jade), 'group separators are supported'); }); test('the template pins no physical side', () => { // The side comes from the document direction and logical CSS. A physical side // in the markup is how you get a right-hand menu whose contents still read // left. (The class name js-left-menu-item is a NAME, not a placement.) const markup = jade.replace(/^\s*\/\/-.*$/gm, ''); assert.ok(!/style=/.test(markup), 'no inline styles to hide a side in'); assert.ok(!/(padding|margin|border)-(left|right)|text-align:\s*(left|right)|float:/.test(markup), 'no physical side properties in the markup'); }); // ── side and mirroring ────────────────────────────────────────────────────── test('the menu mirrors under a right-to-left language', () => { const menu = css.slice(css.indexOf('.side-menu {'), css.indexOf('.content-body .main-body {')); // Physical properties do not flip; logical ones do. assert.ok(!/padding-left|padding-right|margin-left|margin-right/.test(menu), 'use padding-inline-* / margin-inline-*, never the physical side'); assert.ok(/padding-inline-start/.test(menu), 'the entry indent must follow the reading direction'); assert.ok(/margin-inline-end/.test(menu), 'the gap after the icon must follow the reading direction'); // An inset shadow offset sideways is physical too - it shaded the wrong inner // edge once the menu mirrored to the right. assert.ok(!/box-shadow: inset -/.test(menu), 'an inset shadow must not be offset on the X axis'); }); test('a long menu scrolls inside the panel instead of spilling out of it', () => { // Admin Panel / Problems is the longest menu: 15 entries. The panel is a flex item // stretched to the row height, so its background, border and rounded corners stop at // the bottom of the page - and with the default `overflow: visible` the entries kept // rendering past that edge, on the page's grey with no panel behind them. const panel = /\.side-menu \{([^}]*)\}/.exec(css); assert.ok(panel, 'the panel must be styled'); assert.ok(/overflow-y:\s*auto/.test(panel[1]), 'a menu taller than the page must scroll inside its own panel'); assert.ok(/min-height:\s*0/.test(panel[1]), 'without min-height:0 a flex item will not shrink below its content, and the ' + 'overflow never engages'); // The panel keeps its own background, so whatever is visible always sits on it. assert.ok(/background-color:/.test(panel[1]), 'the panel still has its background'); }); test('the design doc states which side, both ways', () => { assert.ok(/left-to-right/i.test(doc) && /right-to-left/i.test(doc), 'both directions must be described'); assert.ok(/mirror/i.test(doc), 'and that the panel mirrors'); assert.ok(/dir=rtl/.test(doc), 'naming the mechanism'); }); // ── theme ─────────────────────────────────────────────────────────────────── test('the selected entry looks like the selected tab in the bar above', () => { // Filled with the theme colour, label and icon white - the same treatment // settingHeader.css gives `.setting-header-btn.active`. Before this the entry kept // the panel grey on white, which is easy to miss. const fill = /\.side-menu ul li\.active,\s*[^{]*li\.active:hover \{([^}]*)\}/.exec(css); assert.ok(fill, 'the selected entry must be filled'); assert.ok(/background: var\(--theme-accent, #[0-9a-f]{6}\)/.test(fill[1]), 'a per-user accent fills it, falling back to the WeKan header blue so the menu ' + 'matches the bar when no colour is chosen'); // :hover must be in that same selector list. `li:hover` sets white at the SAME // specificity and comes later, so without it the fill vanishes under the pointer. assert.ok(/li\.active:hover/.test(css), 'hovering the selected entry must not wash it back to white'); // Label AND icon go white together. Comments are stripped first: the rule explains // in prose why it is not `inherit`, which a naive text search would trip over. const code = css.replace(/\/\*[\s\S]*?\*\//g, ''); const text = /\.side-menu ul li\.active > a,\s*[^{]*li\.active > a i \{([^}]*)\}/.exec(code); assert.ok(text, 'label and icon must be coloured together'); assert.ok(/color: #fff/.test(text[1]), 'white on the filled background'); assert.ok(!/inherit/.test(text[1]), 'inheriting the panel grey is what made the selected entry invisible'); }); test('the design doc explains the theming', () => { assert.ok(/## Theme/.test(doc)); assert.ok(/--theme-accent/.test(doc) && /Change color/.test(doc), 'name the per-user override and where it is set'); assert.ok(/inherit/.test(doc), 'and the fallback that keeps the default look'); }); // ── layout shared with the table page ─────────────────────────────────────── test('narrow windows stack the menu above the content', () => { const at = tableCss.indexOf('@media screen and (max-width: 800px)'); assert.ok(at > 0, 'the stacking rule lives with the table page and covers both'); const block = tableCss.slice(at); assert.ok(/\.side-menu \{[^}]*width:\s*100%/.test(block)); assert.ok(/\.content-body \{\s*flex-wrap:\s*wrap/.test(block)); }); // ── the doc ───────────────────────────────────────────────────────────────── test('the related-files table lists files that exist', () => { assert.ok(doc.includes('| File Path | File Type | Description |'), 'the related-files table must have those three columns'); const paths = [...doc.matchAll(/\| `([a-z][\w./-]+\.(?:jade|css|js|cjs))` \|/g)].map(m => m[1]); assert.ok(paths.length >= 10, `expected the full file list, found ${paths.length}`); for (const rel of paths) { assert.ok(fs.existsSync(path.join(root, rel)), `related file missing: ${rel}`); } assert.strictEqual(new Set(paths).size, paths.length, 'no file listed twice'); }); test('the two page designs cross-link', () => { assert.ok(/\[Table\]\(Table\.md\)/.test(doc), 'Left-Menu.md must link to Table.md - the two compose on the same page'); assert.ok(fs.existsSync(path.join(root, 'docs/Design/Page/Table.md'))); }); // ── the template and the pages must agree on the data shape ───────────────── test('what the pages pass is what the template iterates', () => { // The whole Admin Panel lost its left menu to this. leftMenu.jade iterates // `each items`, and every page handed it the bare ARRAY from buildMenuItems - so // Spacebars looked `items` up ON the array, found nothing, and rendered an empty // panel on Settings, People, Features, Attachments, Version and Problems alike. // Nothing failed: the template was fine, the helper was fine, only the seam between // them was wrong. Neither half can be checked alone, so check the seam. const iterated = /^\s*each (\w+)\s*$/m.exec(jade); assert.ok(iterated, 'the template must iterate a named variable'); const key = iterated[1]; const context = lib.leftMenuData([{ id: 'a', labelKey: 'x' }], 'a'); assert.ok(!Array.isArray(context), 'a bare array cannot carry a named property for the template to iterate'); assert.ok(Array.isArray(context[key]), `the template iterates \`${key}\`, so the data context must have a \`${key}\` array`); assert.strictEqual(context[key].length, 1, 'and it must hold the built entries'); }); test('every page builds its context through that one helper', () => { // Six pages, one shape. Calling buildMenuItems directly returns the array again and // silently empties that page's menu, so no page may do it. const pages = ['settingBody', 'peopleBody', 'attachments', 'adminReports']; for (const page of pages) { const src = read(`client/components/settings/${page}.js`); const helper = /menuItems\(\) \{[\s\S]*?\n \},/.exec(src); assert.ok(helper, `${page}: must have a menuItems helper`); assert.ok(/leftMenuData\(/.test(helper[0]), `${page}: must build its context with leftMenuData`); assert.ok(!/return buildMenuItems\(/.test(helper[0]), `${page}: returning the bare array renders an empty menu`); } }); // ── pages converted to the shared menu ────────────────────────────────────── test('Admin Panel / Problems renders the shared menu from data', () => { const jade = read('client/components/settings/adminReports.jade'); // Strip line comments: the collapsed handlers are DESCRIBED in a comment there. const js = read('client/components/settings/adminReports.js').replace(/^\s*\/\/.*$/gm, ''); assert.ok(/\+leftMenu\(menuItems\)/.test(jade), 'renders the shared menu'); assert.ok(!/\.side-menu/.test(jade), 'no hand-written .side-menu markup left'); assert.ok(/PROBLEMS_MENU = \[/.test(js), 'its entries are a data list'); assert.ok(/leftMenuData\(PROBLEMS_MENU/.test(js), 'built by the shared helper'); // Twelve identical per-entry handlers collapsed to one on the shared class. assert.strictEqual((js.match(/'click a\.js-report-/g) || []).length, 0, 'the per-entry handlers must be gone'); assert.strictEqual((js.match(/'click \.js-left-menu-item'/g) || []).length, 1, 'exactly one menu handler'); // The active row is rendered from activeReport, so the hand DOM toggling that // used to fight a re-render must be gone. assert.ok(!/side-menu li\.active'\)\.removeClass/.test(js), 'no manual active-class toggling'); }); test('the shared menu reproduces each page icon shape', () => { // Same styling after conversion: an empty span.emoji-icon (Settings, // Features), a coloured wrapper (People / Locked users), or a bare icon. assert.ok(/if iconWrapCls/.test(jade) && /if emoji/.test(jade), 'all three icon shapes must be supported'); const [plain] = lib.buildMenuItems([{ id: 'a', icon: 'fa-list', labelKey: 'x' }], 'a'); assert.strictEqual(plain.emoji, false); assert.strictEqual(plain.iconWrapCls, ''); const [emoji] = lib.buildMenuItems([{ id: 'b', icon: 'fa-key', labelKey: 'y', emoji: true }], 'b'); assert.strictEqual(emoji.emoji, true); const [red] = lib.buildMenuItems([{ id: 'c', icon: 'fa-lock', labelKey: 'z', iconWrapCls: 'text-red' }], 'c'); assert.strictEqual(red.iconWrapCls, 'text-red'); }); test('Settings renders the shared menu too', () => { // The menu builders take the current user now: multitenancy option D gives an // Organization's own admin a shorter menu (docs/Design/Multitenancy/Multitenancy.md). const pages = [ ['settingBody', 'settingsMenu(', 'js-setting-menu'], ]; for (const [file, list, jsClass] of pages) { const pageJade = read(`client/components/settings/${file}.jade`); const pageJs = read(`client/components/settings/${file}.js`); assert.ok(/\+leftMenu\(menuItems\)/.test(pageJade), `${file}: renders the shared menu`); assert.ok(!/\.side-menu/.test(pageJade), `${file}: no hand-written menu markup left`); assert.ok(pageJs.includes(`leftMenuData(${list}`), `${file}: built by the shared helper`); assert.ok(pageJs.includes(jsClass), `${file}: keeps its own handler class`); } }); test('the Sandstorm exception moved to People with the E-mail pane', () => { // Login and E-mail are People panes now, so the entry that is dropped on a // Sandstorm deployment lives with them. const people = read('client/components/settings/peopleBody.js'); assert.ok(/isSandstorm \? null :/.test(people), 'the e-mail entry is still absent on Sandstorm - a dropped null, not empty markup'); assert.ok(/function peopleMenu\(/.test(people), 'so the menu is built by a function, which can read Meteor.settings at call time ' + '(it takes the user now, to drop the tabs a tenant admin may not open)'); const settings = read('client/components/settings/settingBody.js'); assert.ok(!/isSandstorm/.test(settings), 'and Settings no longer needs the check - its conditional entry left'); }); test('Settings still maps every pane it can open to one active id', () => { const js = read('client/components/settings/settingBody.js'); assert.ok(/function activeSettingId/.test(js), 'the per-pane vars are mapped to ONE active id'); // Every pane the menu can open must be mapped, or that entry never highlights. // No registration-setting/email-setting: those two moved to Admin Panel / People. for (const id of ['tableVisibilityMode-setting', 'announcement-setting', 'accessibility-setting', 'layout-setting', 'webhook-setting']) { assert.ok(js.includes(`'${id}'`), `${id} must be in both the menu and the id map`); } for (const gone of ['registration-setting', 'email-setting']) { assert.ok(!js.includes(`'${gone}'`), `${gone} moved to People and must not linger here`); } }); test('Admin Panel / People renders the shared menu from data', () => { const pageJade = read('client/components/settings/peopleBody.jade'); const pageJs = read('client/components/settings/peopleBody.js').replace(/^\s*\/\/.*$/gm, ''); assert.ok(/\+leftMenu\(menuItems\)/.test(pageJade), 'renders the shared menu'); assert.ok(!/\.side-menu/.test(pageJade), 'no hand-written menu markup left'); // A function rather than a bare array since the E-mail entry it gained is dropped // on Sandstorm, which has to be read at call time. assert.ok(/function peopleMenu\(user\)/.test(pageJs) && /leftMenuData\(peopleMenu\(/.test(pageJs), 'its entries are data, built by the shared helper - and take the user, since an ' + 'Organization\'s own admin gets a shorter menu (multitenancy option D)'); // Seven near-identical handlers collapsed to one. for (const old of ['js-org-menu', 'js-team-menu', 'js-people-menu', 'js-locked-users-menu', 'js-roles-menu', 'js-templates-menu', 'js-domains-menu']) { assert.ok(!pageJs.includes(old), `${old} must be gone`); } assert.strictEqual((pageJs.match(/'click \.js-left-menu-item'/g) || []).length, 1, 'exactly one menu handler'); // The per-pane extras must survive the collapse. for (const extra of ['refreshOrgsCount', 'refreshTeamsCount', 'refreshUsersCount']) { assert.ok(pageJs.includes(extra), `${extra} must still run on its pane`); } // Active row from state, not from a hand-toggled class. The page opens on the // FIRST entry of the menu this user actually has: Login for the site admin, as // before - and Organizations for an Organization's own admin, who has no Login // pane (multitenancy option D). One helper decides both, so the open pane and the // highlighted row can never disagree. assert.ok(/function firstPeoplePaneId\(user\)/.test(pageJs), 'the first pane of the user\'s own menu'); assert.ok(/activeMenuId = new ReactiveVar\('registration-setting'\)/.test(pageJs), 'the open pane is state, and starts on the first entry in the menu'); assert.ok(/registrationSetting = new ReactiveVar\(true\)/.test(pageJs), 'and that pane is the one whose var starts true'); assert.ok(/orgSetting = new ReactiveVar\(false\)/.test(pageJs), 'the pane that used to open first must no longer also start true'); // An Organization's own admin has no Login pane, so their page opens elsewhere - // decided in an autorun once the user is known, never at onCreated, where the user // document has often not arrived yet. assert.ok(/this\.openPaneDecided = false;/.test(pageJs) && /const openPaneId = firstPeoplePaneId\(user\);/.test(pageJs), 'the open pane is corrected once, when the user is actually known'); assert.ok(!/side-menu li\.active'\)\.removeClass/.test(pageJs), 'no manual active-class toggling'); assert.ok(/iconWrapCls: 'text-red'/.test(pageJs), 'Locked users keeps its red lock'); }); test('EVERY Admin Panel page renders the shared menu', () => { // The design is only worth having if nothing is left outside it. // Translation and Version moved INTO Settings as panes, so neither has a menu of // its own any more - they are entries in the Settings menu. const pages = ['settingBody', 'peopleBody', 'attachments', 'adminReports']; for (const page of pages) { const pageJade = read(`client/components/settings/${page}.jade`); assert.ok(/\+leftMenu\(menuItems\)/.test(pageJade), `${page}: renders the shared menu`); assert.ok(!/\.side-menu/.test(pageJade), `${page}: no hand-written menu markup left`); } }); test('the Attachments Sandstorm pane is commented out, code and all', () => { // It only had content inside a grain - the MongoDB 3 -> FerretDB migration // report and a button that DELETED the raw MongoDB 3 files. Compacting the // database frees that space too, so the pane is commented out rather than // deleted: no menu entry, no template, no helpers, no state, no handler, and // nothing of it runs. The grain migration itself is untouched. const js = read('client/components/settings/attachments.js'); const pageJade = read('client/components/settings/attachments.jade'); const liveJs = js.replace(/^\s*\/\/.*$/gm, ''); const liveJade = pageJade.replace(/^\s*\/\/-.*$/gm, ''); assert.ok(/\/\/ \{ id: 'sandstorm', icon: 'fa-hdd-o', label: 'Sandstorm'/.test(js), 'the menu entry is kept as a comment'); assert.ok(!/sandstorm/i.test(liveJs), 'no live line of the page mentions Sandstorm any more'); assert.ok(!/sandstorm/i.test(liveJade), 'and its pane is commented out of the template'); // Everything else on the page keeps working: the storages, their stats, the // database migration and Backup. for (const kept of ['isBackupActive', 'isDatabaseMigrationActive', 'isGridFsActive', 'isFilesystemActive', 'isS3Active', 'isMoveActive']) { assert.ok(liveJs.includes(kept) && liveJade.includes(kept), `${kept} must still be live`); } // The literal-label path the entry used is still supported by the design, for // the next proper noun that needs it. assert.ok(/if labelKey[\s\S]*\{\{_ labelKey\}\}[\s\S]*else[\s\S]*\{\{label\}\}/.test(jade), 'the template falls back to a literal label'); const [lit] = lib.buildMenuItems([{ id: 's', label: 'Sandstorm' }], 's'); assert.strictEqual(lit.label, 'Sandstorm'); assert.strictEqual(lit.labelKey, ''); }); test('no pane repeats the title the section already rendered', () => { // The heading comes from the menu entry now, so a pane that also prints its own // name shows the same word twice - every Attachments pane did (`h3 Backup` under // a title reading "Backup"). A heading that says something the label does not - // Limits' "Attachment And API File Size Limits", Locked users' "Brute Force // Protection Settings" - is not a repeat, and stays. const en = JSON.parse(read('imports/i18n/data/en.i18n.json')); const pages = ['settingBody', 'peopleBody', 'attachments', 'adminReports', 'informationBody', 'translationBody']; // Every menu label rendered anywhere in the Admin Panel. const labels = new Set(); for (const page of ['settingBody', 'peopleBody', 'attachments', 'adminReports']) { const src = read(`client/components/settings/${page}.js`); for (const m of src.matchAll(/labelKey: '([\w-]+)'/g)) labels.add(m[1]); for (const m of src.matchAll(/label: '([^']+)'/g)) labels.add(m[1]); } for (const page of pages) { const pageJade = read(`client/components/settings/${page}.jade`); for (const m of pageJade.matchAll(/^\s*h[1-3] (?:\{\{_ '([\w-]+)'\}\}|(\S.*))$/gm)) { const [, key, literal] = m; if (key) { assert.ok(!labels.has(key), `${page}: a heading of {{_ '${key}'}} repeats the menu label above it`); } else if (literal) { const text = literal.trim(); assert.ok(!labels.has(text) && !Object.entries(en).some( ([k, v]) => labels.has(k) && v === text), `${page}: a heading of "${text}" repeats the menu label above it`); } } } }); test('a heading names a group and cannot be clicked or selected', () => { const [heading] = lib.buildMenuItems([{ heading: true, labelKey: 'reports' }], 'reports'); assert.strictEqual(heading.heading, true); assert.strictEqual(heading.labelKey, 'reports'); // No id, no icon, no handler class - so there is nothing to click, and the page's // click handler has no data-id to read even if something reached it. assert.strictEqual(heading.id, undefined); assert.strictEqual(heading.jsClass, undefined); // And it can never be the active row, or a group name could become a pane title. assert.strictEqual(heading.active, undefined); assert.deepStrictEqual(lib.paneTitle([{ heading: true, labelKey: 'reports' }], 'reports'), {}); assert.strictEqual(lib.activeCount(lib.buildMenuItems( [{ heading: true, labelKey: 'reports' }, { id: 'a', labelKey: 'x' }], 'a')), 1); // The template renders it as text in an