'use strict'; // Shared HTML helpers + page shell (layout, page/act wrappers) for every module pack. Labels are bound with // so Playwright getByLabel works; banners use role=status / role=alert. const R = require('./registry'); const { AppError } = require('./errors'); const esc = (v) => String(v ?? '').replace(/[&<>"']/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[c])); let fid = 0; const inp = (name, label, { type = 'text', value = '', tid, attrs = '' } = {}) => { const id = `f${++fid}`; return `${esc(label)} `; }; const sel = (name, label, opts, { value = '', tid } = {}) => { const id = `f${++fid}`; return `${esc(label)} ${opts.map((o) => { const [v, t] = Array.isArray(o) ? o : [o, o]; return `${esc(t)}`; }).join('')}`; }; const hid = (name, value) => ``; // MOCK_UI_DRIFT (co-worker self-heal demo only; default empty = no change): JSON map of button label -> drifted label, // e.g. MOCK_UI_DRIFT='{"Submit":"Submit Request"}' simulates a patch renaming a button. const UI_DRIFT = (() => { try { return JSON.parse(process.env.MOCK_UI_DRIFT || '{}'); } catch { return {}; } })(); const drift = (label) => (Object.prototype.hasOwnProperty.call(UI_DRIFT, label) ? UI_DRIFT[label] : label); const btn = (label, attrs = '') => `${esc(drift(label))}`; const form = (action, inner, method = 'post') => `${inner}`; const table = (name, heads, rows, tid) => `${heads.map((h) => `${esc(h)}`).join('')}${rows.join('')}`; const tr = (cells, attrs = '') => `${cells.map((c) => `${c}`).join('')}`; const money = (n) => (n === null || n === undefined ? '' : Number(n).toFixed(2)); const dl = (pairs) => `${pairs.filter(Boolean).map(([k, v, tid]) => `${esc(k)}${v}`).join('')}`; const T = (s) => esc(String(s).replace(/_/g, ' ')); const back = (p) => hid('__back', p); const roleOf = (req) => req.get('X-Mock-Role') || ((/(?:^|;\s*)mock_role=([a-z_0-9]+)/.exec(req.get('cookie') || '') || [])[1]) || null; function layout(req, role, title, body, status = 200) { const ok = req.query.ok ? `${esc(req.query.ok)}` : ''; const er = req.query.error ? `${esc(req.query.error)}` : ''; const p = role && R.persona(role); const tasks = p ? `Tasks ${R.tasksFor(role).map((t) => `${esc(t.label)}`).join(' ')}` : ''; const who = p ? `Persona: ${esc(p.label)} ${form('/worklist', btn(`Notifications`, `aria-label="Notifications" data-testid="notifications-btn"`), 'get')} ${R.worklist(role).length} ${form('/logout', btn('Sign out'))}` : ''; const mods = R.packs().map((m) => m.name).join(' | '); return { status, html: `${esc(title)} - Mock Fusion Mock Fusion Practice pod (local test double, not Oracle software)${who}${tasks} ${esc(title)}${ok}${er}${body} ` }; } const forbidden = (role, moduleId) => new AppError(403, 'FORBIDDEN', `Persona ${role} has no access to the ${moduleId} module.`); /** GET page wrapper. moduleId (optional) restricts the page to personas registered by that pack (else 403). */ function page(fn, moduleId) { return (req, res) => { const role = roleOf(req); if (!R.persona(role)) return res.redirect('/login'); try { if (moduleId && !R.inModule(role, moduleId)) throw forbidden(role, moduleId); const out = fn(req, role); if (out) res.status(out.status).send(out.html); } catch (e) { if (e instanceof AppError) { const o = layout({ query: { error: e.message } }, role, e.status === 403 ? 'Not Authorized' : 'Error', ''); return res.status(e.status).send(o.html); } console.error(e); res.status(500).send('Internal error'); } }; } /** POST action wrapper: fn returns [redirectPath, successMessage]; AppError -> redirect back with ?error=. */ function act(fn, moduleId) { return (req, res) => { const role = roleOf(req); if (!R.persona(role)) return res.redirect('/login'); try { if (moduleId && !R.inModule(role, moduleId)) throw forbidden(role, moduleId); const [path, msg] = fn(req, role); res.redirect(`${path}${path.includes('?') ? '&' : '?'}ok=${encodeURIComponent(msg)}`); } catch (e) { if (!(e instanceof AppError)) { console.error(e); return res.status(500).send('Internal error'); } const b = req.body.__back || (req.get('referer') ? new URL(req.get('referer')).pathname : '/'); res.redirect(`${b}?error=${encodeURIComponent(e.message)}`); } }; } /** JSON wrapper for REST routers. Return created(body) for 201. */ function wrap(fn) { return (req, res) => { try { const out = fn(req, roleOf(req)); if (out && out.__status) return res.status(out.__status).json(out.body); res.json(out); } catch (e) { if (e instanceof AppError) return res.status(e.status).json({ title: e.code, error: e.code, message: e.message, detail: e.message }); console.error(e); res.status(500).json({ error: 'INTERNAL', message: e.message }); } }; } const created = (body) => ({ __status: 201, body }); module.exports = { esc, inp, sel, hid, btn, form, table, tr, money, dl, T, back, roleOf, layout, page, act, wrap, created };