/* Run with node --test web/tests/core.test.cjs. No packages required. */ const test = require('node:test'); const assert = require('node:assert/strict'); const fs = require('node:fs'); const path = require('node:path'); const vm = require('node:vm'); function application(options = {}) { const storage = () => ({ getItem: () => null, setItem() {}, removeItem() {} }); const context = { window: {}, document: { querySelector: selector => selector === 'meta[name="allow-registration"]' && options.registration !== undefined ? { content: options.registration } : null }, sessionStorage: storage(), localStorage: storage(), AbortController, URLSearchParams, Blob, URL, Response, fetch, setTimeout, clearTimeout, requestAnimationFrame: fn => fn(), performance, console }; context.window.window = context.window; vm.createContext(context); for (const name of ['i18n.js', 'core.js', 'views.js']) vm.runInContext(fs.readFileSync(path.join(__dirname, '../js', name), 'utf8'), context); return context.window.MBS; } test('all four languages include every statically used translation key', () => { const app = application(); const code = ['core.js', 'views.js', 'app.js'].map(file => fs.readFileSync(path.join(__dirname, '../js', file), 'utf8')).join('\n'); const keys = [...code.matchAll(/'((?:app|auth|common|confirm|dashboard|diffConfigs|files|footer|logs|nav|routers|settings|sidebar|toast|topbar|ui)\.[A-Za-z]+)'/g)].map(match => match[1]); for (const lang of ['pl', 'en', 'es', 'no']) { for (const key of keys.filter(key => key.includes('.'))) { // Dynamic weekday suffix is validated by the schedule editor test. if (key === 'settings.weekday') continue; const value = key.split('.').reduce((node, part) => node && node[part], app.translations[lang]); assert.equal(typeof value, 'string', `${lang}: ${key}`); } } }); test('untrusted text and diff search results are HTML escaped', () => { const app = application(); assert.equal(app.escape('', right_file_name: '', diff_text: '', lines: [{ type: 'modified', left_number: 1, right_number: 1, left_text: '', right_text: '&password' }] }; const html = app.diffResult(); assert.ok(!html.includes('', timestamp: '2026-01-01' }] } : endpoint === '/routers' ? [{ id: 1, name: 'one', last_connection_status: null }] : []; const html = await app.views.dashboard(); assert.ok(html.includes('<script>bad</script>')); assert.ok(!html.includes('