Files
nexus/scripts/ui-pruefung/system.mjs

48 lines
4.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// admin2system (0.1.25, #1221): Seite „System“ im echten Browser.
// Aufruf: node system.mjs <Admin-Passwort> <Nutzer-Passwort> <Ausgabeordner>. NEXUS_BASE (Standard http://127.0.0.1:18121).
// Voraussetzung: Konten `claude` (admin) und `nutzer`; die Testinstanz läuft seit über zwei Minuten (erste automatische Sicherung).
import * as cdp from './lib.mjs';
import fs from 'node:fs';
const BASE = process.env.NEXUS_BASE || 'http://127.0.0.1:18121';
const [pwa, pwn, out] = process.argv.slice(2);
fs.mkdirSync(out, { recursive: true });
const sleep = ms => new Promise(r => setTimeout(r, ms));
const res = []; const ok = (n, c, d = '') => { res.push(!!c); console.log((c ? 'ok ' : 'FEHLER') + ' ' + n + (d ? ' – ' + d : '')); };
const b = await cdp.start({ port: 9385 });
await b.send('Runtime.enable'); await b.send('Log.enable');
const fehler = () => b.events.filter(e => e.method === 'Runtime.exceptionThrown' || (e.method === 'Log.entryAdded' && e.params.entry.level === 'error' && !/status of 40[13]/.test(e.params.entry.text))).length;
const q = js => b.eval(js);
try {
await b.login(BASE, 'claude', pwa);
await b.goto(BASE + '/ui/system'); await sleep(1200);
const menue = await q(`[...document.querySelectorAll('header nav a')].map(a => a.textContent.trim().replace(/\\d+$/, '')).join('|')`);
ok('Menü (Admin): 11 Einträge mit System', menue.split('|').length === 11 && menue.endsWith('System'), menue);
const karten = await q(`[...document.querySelectorAll('main section.card h2')].map(h => h.textContent.trim()).join('|')`);
ok('Karten: Version, Aufbewahrung, Datenbank, Sicherung, Diagnose', karten === 'Version|Aufbewahrung|Datenbank|Sicherung|Diagnose', karten);
const aufb = await q(`[...document.querySelectorAll('#aufbewahrung tbody tr')].map(r => r.children[0].textContent + '=' + r.children[1].textContent).join('; ')`);
ok('Aufbewahrung: sieben Zeilen, Ereignisse 35 Tage, Audit unbegrenzt', aufb.split(';').length === 7 && aufb.includes('Ereignisse=35 Tage') && aufb.includes('Audit=unbegrenzt'), aufb);
const tab = await q(`(() => { const t = document.querySelector('#datenbank table'); const n = t.querySelectorAll('thead th').length; return [...t.querySelectorAll('tbody tr')].every(r => r.children.length === n) ? t.querySelectorAll('tbody tr').length : -1; })()`);
ok('Datenbank: Tabellenzeilen passen zum Kopf', tab > 5, String(tab));
const sich = await q(`document.querySelector('#sicherung').textContent.replace(/\\s+/g,' ')`);
ok('Sicherung: eine automatische Datei steht da', /nexus-auto-\d{8}T\d{6}Z/.test(sich), sich.slice(0, 160));
await b.shot(`${out}/system-oben.png`, { full: true });
// Update-Prüfung von hier (ohne Netz erwartet die Instanz einen Fehlerhinweis, nicht einen Absturz)
const status = await q(`fetch('/ui/system/update-pruefen',{method:'POST',headers:{'content-type':'application/x-www-form-urlencoded'},body:'csrf='+document.body.dataset.csrf+'&kanal=regulaer',redirect:'manual'}).then(r => r.type + ':' + r.status)`);
ok('Update-Prüfung auf der Seite System antwortet', /opaqueredirect|303/.test(status), status);
// Diagnoseknopf: Archiv laden und Inhalt prüfen
const dl = await q(`fetch('/ui/system/diagnose',{method:'POST',headers:{'content-type':'application/x-www-form-urlencoded'},body:'csrf='+document.body.dataset.csrf}).then(async r => { const buf = new Uint8Array(await r.arrayBuffer()); return r.status + '|' + r.headers.get('content-disposition') + '|' + r.headers.get('cache-control') + '|' + buf.length + '|' + buf[0] + ',' + buf[1]; })`);
const [st, cd, cc, len, magic] = dl.split('|');
ok('Diagnoseknopf: 200, Anhang, no-store, gzip-Kopf', st === '200' && /attachment; filename="nexus-diag-/.test(cd) && cc === 'no-store' && Number(len) > 500 && magic === '31,139', dl);
ok('Keine Konsolenfehler (Admin)', fehler() === 0, String(fehler()));
// Nutzer
await b.login(BASE, 'nutzer', pwn);
await b.goto(BASE + '/ui'); await sleep(900);
const nm = await q(`[...document.querySelectorAll('header nav a')].map(a => a.textContent.trim()).join('|')`);
ok('Menü (Nutzer): ohne System', !nm.includes('System'), nm);
const codes = await q(`Promise.all([fetch('/ui/system').then(r => r.status), fetch('/ui/system/diagnose',{method:'POST',headers:{'content-type':'application/x-www-form-urlencoded'},body:'csrf='+document.body.dataset.csrf}).then(r => r.status)]).then(a => a.join(','))`);
ok('Nutzer: Seite und Diagnoseknopf liefern 403', codes === '403,403', codes);
} catch (e) { console.log('FEHLER Ausnahme', e.message); res.push(false); }
await b.close?.();
const n = res.filter(Boolean).length; console.log(`\n${n}/${res.length} Prüfungen bestanden`);
process.exit(n === res.length ? 0 : 1);