Files
nexus/scripts/ui-pruefung/engine-oberflaeche.mjs

67 lines
5.4 KiB
JavaScript

// admin2engineoberflaeche.2 und .3 (#1101): Die Oberfläche von llama.cpp über das Gateway im Chromium öffnen (Knopf auf
// der Karte), eigene Herkunft, eine Antwort streamen, und was die Seite nicht darf (Cookie, Gateway, Formular).
// Aufruf: node engine-oberflaeche.mjs <Testpasswort> <API-Key> (Benutzer `claude`, `NEXUS_BASE`, Standard http://127.0.0.1:18127;
// `NEXUS_MODELL` Standard `qwen-klein`; Testinstanz mit echtem llama.cpp, einem kleinen Modell und `engine_ui_listen`).
// Nicht gegen die Produktion.
import * as cdp from './lib.mjs';
const BASE = process.env.NEXUS_BASE || 'http://127.0.0.1:18127', pw = process.argv[2], key = process.argv[3];
const MODELL = process.env.NEXUS_MODELL || 'qwen-klein';
const AUS = process.env.NEXUS_AUS || '/tmp';
const b = await cdp.start({ port: 9379 });
const ergebnisse = [];
const pruefe = (name, ok, extra = '') => { ergebnisse.push(ok); console.log(ok ? 'OK ' : 'FEHL', name, extra); };
const warte = ms => new Promise(r => setTimeout(r, ms));
try {
await b.login(BASE, 'claude', pw);
// Modell laden (eine Anfrage), damit die Engine eine Instanz hat.
const geladen = await b.eval(`fetch(${JSON.stringify(BASE + '/v1/chat/completions')},{method:'POST',headers:{'content-type':'application/json',authorization:'Bearer ${key}'},body:JSON.stringify({model:${JSON.stringify(MODELL)},max_tokens:4,messages:[{role:'user',content:'Hallo'}]})}).then(r=>r.status)`);
pruefe('Modell geladen (Anfrage 200)', geladen === 200, `(${geladen})`);
await warte(5500);
await b.goto(BASE + '/ui/engines');
const hat = await b.eval(`!!document.querySelector('form[action="/ui/engine/llama.cpp/oeffnen"] button')`);
pruefe('Karte llama.cpp hat den Knopf „Oberfläche öffnen“', hat);
const hinweis = await b.eval(`/ohne Warteschlange, am Scheduler vorbei und ohne Eintrag im Anfrage-Protokoll/.test(document.body.innerText)`);
pruefe('Hinweis auf den direkten Weg steht auf der Karte', hinweis);
await b.eval(`(()=>{const f=document.querySelector('form[action="/ui/engine/llama.cpp/oeffnen"]'); f.target='_self'; f.submit();})()`);
await warte(6000);
const ort = JSON.parse(await b.eval(`JSON.stringify({pfad:location.pathname, herkunft:location.origin, eigene:window.origin, titel:document.title, el:document.querySelectorAll('textarea,input,button').length})`));
console.log('Seite:', JSON.stringify({ ...ort, pfad: ort.pfad.replace(/[0-9a-f]{64}/, '<token>') }));
const GATEWAY = new URL(BASE).origin;
pruefe('Adresse hat das Token im Pfad', /^\/t\/[0-9a-f]{64}\/$/.test(ort.pfad));
pruefe('Die Oberfläche läuft auf einer anderen Herkunft als das Gateway', ort.herkunft !== GATEWAY && ort.eigene === ort.herkunft, `(${ort.herkunft})`);
pruefe('Die Oberfläche ist da (Titel „llama-ui“, Bedienelemente)', ort.titel === 'llama-ui' && ort.el > 3, `(${ort.titel}, ${ort.el})`);
await b.shot(`${AUS}/engine-oberflaeche-1.png`);
// Eine Frage stellen: Antwort kommt als Strom durch die Weiterleitung.
await b.eval(`document.querySelector('textarea').focus()`);
await b.send('Input.insertText', { text: 'Sag nur das Wort Hallo.' });
await warte(500);
await b.send('Input.dispatchKeyEvent', { type: 'keyDown', key: 'Enter', code: 'Enter', windowsVirtualKeyCode: 13, text: '\r' });
await b.send('Input.dispatchKeyEvent', { type: 'keyUp', key: 'Enter', code: 'Enter', windowsVirtualKeyCode: 13 });
await warte(15000);
const antwort = await b.eval(`document.body.innerText`);
pruefe('Eine Antwort ist angekommen (Token-Anzeige der Oberfläche)', /tokens/.test(antwort) && /t\/s/.test(antwort));
await b.shot(`${AUS}/engine-oberflaeche-2.png`);
// Sicherheit: Was darf die Seite der Engine NICHT?
const e = JSON.parse(await b.eval(`(async()=>{const r={};
r.cookie = document.cookie;
try { await fetch(${JSON.stringify(GATEWAY)} + '/ui/engines', {credentials:'include'}); r.gateway='erreicht'; } catch(e) { r.gateway='blockiert'; }
try { const x = await fetch('/ui/engines'); r.eigeneUi = x.status; } catch(e) { r.eigeneUi='Fehler'; }
try { const x = await fetch('/v1/models'); r.eigeneApi = x.status; } catch(e) { r.eigeneApi='Fehler'; }
try { localStorage.setItem('x','1'); r.speicher='ok'; } catch(e) { r.speicher='gesperrt'; }
return JSON.stringify(r); })()`));
console.log('Eigenschaften:', JSON.stringify(e));
pruefe('Kein Cookie des Gateways sichtbar', e.cookie === '');
pruefe('Anfragen der Seite an das Gateway sind blockiert (connect-src)', e.gateway === 'blockiert');
pruefe('Die zweite Herkunft kennt weder /ui noch /v1', e.eigeneUi === 404 && e.eigeneApi === 404, `(${e.eigeneUi}, ${e.eigeneApi})`);
pruefe('Speicher der Seite geht (die Oberfläche braucht ihn)', e.speicher === 'ok');
// Ein Formular an das Gateway (zum Beispiel „Oberfläche öffnen“ oder eine Verwaltungsaktion) wird nicht abgeschickt.
const vorher = await b.eval('location.href');
await b.eval(`(()=>{const f=document.createElement('form'); f.method='post'; f.action=${JSON.stringify(GATEWAY)} + '/ui/engine/llama.cpp/oeffnen'; document.body.appendChild(f); f.submit();})()`);
await warte(2000);
const nachher = await b.eval('location.href');
pruefe('Ein Formular an das Gateway wird nicht abgeschickt (form-action)', vorher === nachher);
} finally { b.close(); }
const fehl = ergebnisse.filter(x => !x).length;
console.log(fehl ? `${fehl} Prüfung(en) fehlgeschlagen` : `Alle ${ergebnisse.length} Prüfungen bestanden`);
process.exit(fehl ? 1 : 0);