// CHAT-01 bis CHAT-05 (Vorlage v1.2): browser2chat.5 (Kopf), .6 (Seitenleiste), .7 (Konfiguration je Antwort, Kopieren/Wiederholen) // (.8 Player beim Scrollen: chat-player-scroll.mjs). Aufruf: node chat-layout.mjs // Testinstanz mit den Modellen „chat“ und „chat2“ (fake_llama_lang.py) und tts-base hinter der Studio-Weiterleitung (Datei „pcm“). import * as cdp from './lib.mjs'; const BASE=process.env.NEXUS_BASE||'http://127.0.0.1:18122', pw=process.argv[2]; const b=await cdp.start({port:9375,args:['--autoplay-policy=no-user-gesture-required']}); const ok=(n,c,d='')=>{console.log(`${c?'ok ':'FEHLER'} ${n}${d?' – '+d:''}`); if(!c) process.exitCode=1;}; const warte=ms=>new Promise(r=>setTimeout(r,ms)); const ev=x=>b.eval(x); const listeTitel=()=>ev(`[...document.querySelectorAll('#chat-liste li .titel')].map(x=>x.textContent)`); const frage=async t=>{ await ev(`document.getElementById('chat-text').value=${JSON.stringify(t)}`); await ev(`document.getElementById('chat-senden').click()`); const n0=await ev(`document.querySelectorAll('.blase.assistant').length`); for(let i=0;i<300;i++){ await warte(400); if(await ev(`!document.getElementById('chat-senden').disabled && document.querySelectorAll('.blase.assistant').length>${n0}`)) break; } await warte(1200); }; const waehle=async(id,v)=>{ await ev(`(()=>{const s=document.getElementById('${id}'); s.value=${JSON.stringify(v)}; s.dispatchEvent(new Event('change',{bubbles:true}));})()`); await warte(300); }; const klick=t=>ev(`(()=>{const k=[...document.querySelectorAll('button')].find(x=>x.textContent.trim()===${JSON.stringify(t)}); if(!k) return false; k.click(); return true;})()`); try{ await b.login(BASE,'claude',pw); // Personas anlegen (nur wenn nötig) await b.goto(BASE+'/ui/personas'); await warte(800); if(!/Luna/.test(await ev('document.body.innerText'))){ for(const [n,stil] of [['Luna','Freundlich und kurz.'],['Karl','Sachlich.']]){ await ev(`(async()=>{const csrf=document.body.getAttribute('data-csrf'); const d=new URLSearchParams({csrf,id:'',name:${JSON.stringify(n)},systemprompt:'Du bist '+${JSON.stringify(n)}+'.',stil:${JSON.stringify(stil)},stimme:''}); await fetch('/ui/personas/speichern',{method:'POST',headers:{'content-type':'application/x-www-form-urlencoded'},body:d});})()`); await warte(500); } } await b.goto(BASE+'/ui/chat'); await warte(1200); await ev(`document.getElementById('chat-max').value='30'`); await waehle('chat-sprache','tts-base'); await warte(1200); await ev(`(()=>{const s=document.getElementById('chat-stimme'); const o=[...s.options].find(o=>/Scotty/.test(o.textContent)); if(o){s.value=o.value; s.dispatchEvent(new Event('change',{bubbles:true}));}})()`); // ---- .5 Kopf const kopf=await ev(`(()=>{const k=document.getElementById('chat-kopf'), v=document.getElementById('chat-verlauf'); const pers=document.getElementById('chat-persona'); return {persona:[...pers.options].map(o=>o.textContent.trim()), kopfOben:k.getBoundingClientRect().bottom<=v.getBoundingClientRect().top+1, personaImKopf:k.contains(pers), stimmeImKopf:k.contains(document.getElementById('chat-stimmen-popover')), detailsOffen:document.getElementById('chat-persona-popover').open, stimmeOffen:document.getElementById('chat-stimmen-popover').open};})()`); ok('.5 Kopf: Persona-Auswahl (aus, Luna, Karl) und Stimme im Kopf, oberhalb des Verlaufs', kopf.personaImKopf&&kopf.stimmeImKopf&&kopf.kopfOben&&['aus','Luna','Karl'].every(n=>kopf.persona.includes(n)), JSON.stringify(kopf)); ok('.5 Popover sind zunächst zu', !kopf.detailsOffen && !kopf.stimmeOffen); const lunaWert=await ev(`[...document.getElementById('chat-persona').options].find(o=>o.textContent.trim()==='Luna').value`); await waehle('chat-persona',lunaWert); await ev(`document.getElementById('chat-persona-popover').open=true`); await warte(200); const det=await ev(`document.getElementById('chat-persona-details').textContent`); ok('.5 Detail-Popover nennt Name, Version, Stimme, Umfang (ohne Prompt-Text)', /Luna · Version 1 · Stimme: .* · Umfang/.test(det) && !/Du bist Luna/.test(det), det); await ev(`document.getElementById('chat-stimmen-popover').open=true`); await warte(200); const st=await ev(`(()=>{const p=document.getElementById('chat-stimmen-popover'); return {select:!!p.querySelector('#chat-stimme'), vorschau:!!p.querySelector('#chat-stimme-vorschau'), sichtbar:p.querySelector('#chat-stimme').getBoundingClientRect().width>0, optionen:[...p.querySelectorAll('#chat-stimme option')].map(o=>o.textContent.trim())};})()`); ok('.5 Stimmen-Popover: Auswahl und Vorschau-Knopf, Stimmen aus dem Studio', st.select&&st.vorschau&&st.sichtbar&&st.optionen.some(o=>/Scotty/.test(o)), JSON.stringify(st)); // ---- .7 Konfiguration je Antwort (mit Luna), danach Persona wechseln await frage('Erste Frage'); await waehle('chat-persona',await ev(`[...document.getElementById('chat-persona').options].find(o=>o.textContent.trim()==='Karl').value`)); await frage('Zweite Frage'); const metas=()=>ev(`[...document.querySelectorAll('.blase.assistant')].map(x=>(x.querySelector('.meta')||{}).textContent||'')`); let m=await metas(); ok('.7 Erste Antwort nennt Persona Luna (Version 1), zweite Karl, trotz Wechsel', /Persona Luna · Version 1/.test(m[0]) && /Persona Karl · Version 1/.test(m[1]), JSON.stringify(m)); const aktionen=await ev(`[...document.querySelectorAll('.blase.assistant')].map(x=>[...x.querySelectorAll('button')].map(k=>k.textContent.trim()).join(','))`); ok('.7 Kopieren und Vorlesen unter jeder Antwort, Wiederholen nur an der letzten', /Kopieren/.test(aktionen[0])&&/Vorlesen/.test(aktionen[0])&&!/Wiederholen/.test(aktionen[0])&&/Kopieren/.test(aktionen[1])&&/Wiederholen/.test(aktionen[1]), JSON.stringify(aktionen)); const n1=await ev(`document.querySelectorAll('.blase.assistant').length`); await ev(`[...document.querySelectorAll('.blase.assistant')].pop().querySelectorAll('button').forEach(k=>{if(k.textContent.trim()==='Wiederholen') k.click();})`); for(let i=0;i<300;i++){ await warte(400); if(await ev(`!document.getElementById('chat-senden').disabled`)) break; } await warte(1200); ok('.7 Wiederholen ersetzt die letzte Antwort (gleiche Anzahl)', (await ev(`document.querySelectorAll('.blase.assistant').length`))===n1); // Modellwechsel: kein neuer Chat const vorher=(await listeTitel()).length; await waehle('chat-modell','chat2'); await frage('Dritte Frage mit anderem Modell'); m=await metas(); ok('.6 Modellwechsel erzeugt keinen neuen Chat, die Konfiguration wird je Antwort festgehalten', (await listeTitel()).length===vorher && /Modell chat2/.test(m[m.length-1]) && /Modell chat /.test(m[0]), `Chats ${vorher}→${(await listeTitel()).length}; ${JSON.stringify(m.map(x=>x.slice(0,30)))}`); // nach Neuladen und Öffnen aus der Liste: Konfiguration bleibt await warte(1500); await b.goto(BASE+'/ui/chat'); await warte(1500); await ev(`document.querySelector('#chat-liste li .titel').click()`); await warte(800); m=await metas(); ok('.7 Nach Neuladen und Öffnen aus dem Verlauf stehen die Angaben je Antwort noch da', m.length>=3 && /Persona Luna/.test(m[0]) && /Persona Karl/.test(m[1]) && /Modell chat2/.test(m[m.length-1]), JSON.stringify(m.map(x=>x.slice(0,40)))); const nachOeffnen=await ev(`[...document.querySelectorAll('.blase.assistant')].map(x=>[...x.querySelectorAll('button')].map(k=>k.textContent.trim()).join(','))`); ok('.7 Auch im aus dem Verlauf geöffneten Chat steht „Vorlesen“ unter jeder Antwort (Sprachausgabe gewählt)', nachOeffnen.length>=3 && nachOeffnen.every(t=>/Vorlesen/.test(t)) && /Kopieren/.test(nachOeffnen[0]), JSON.stringify(nachOeffnen)); // ---- .6 Seitenleiste await ev(`document.getElementById('chat-neu').click()`); await warte(500); await ev(`document.getElementById('chat-max').value='30'`); await waehle('chat-modell','chat'); await frage('Anderes Thema Bananen'); let t=await listeTitel(); ok('.6 Zwei Chats in der Liste, der neue zuerst', t.length===2 && /Bananen/.test(t[0]), JSON.stringify(t)); await ev(`(()=>{const s=document.getElementById('chat-suche'); s.value='erste'; s.dispatchEvent(new Event('input',{bubbles:true}));})()`); await warte(400); t=await listeTitel(); ok('.6 Suche findet nach Titel und Inhalt', t.length===1 && /Erste/.test(t[0]), JSON.stringify(t)); await ev(`(()=>{const s=document.getElementById('chat-suche'); s.value='ananas'; s.dispatchEvent(new Event('input',{bubbles:true}));})()`); await warte(400); t=await listeTitel(); ok('.6 Suche ohne Treffer meldet „Keine Treffer“', t.length===0 && /Keine Treffer/.test(await ev(`document.getElementById('chat-liste').textContent`))); await ev(`(()=>{const s=document.getElementById('chat-suche'); s.value=''; s.dispatchEvent(new Event('input',{bubbles:true}));})()`); await warte(400); // Anheften: den älteren (zweiter Eintrag) anheften await ev(`[...document.querySelectorAll('#chat-liste li')][1].querySelectorAll('button').forEach(k=>{if(k.textContent.trim()==='Anheften') k.click();})`); await warte(600); t=await listeTitel(); const pin=await ev(`[...document.querySelectorAll('#chat-liste li')][0].querySelector('[aria-pressed]').getAttribute('aria-pressed')`); ok('.6 Angehefteter Chat steht oben (aria-pressed=true, Knopf „Lösen“)', /Erste/.test(t[0]) && pin==='true', JSON.stringify(t)+pin); await ev(`document.getElementById('chat-seite-knopf').click()`); await warte(300); const zu=await ev(`(()=>{const a=document.querySelector('.chat-seite'); return {exp:document.getElementById('chat-seite-knopf').getAttribute('aria-expanded'), breite:Math.round(a.getBoundingClientRect().width)};})()`); ok('.6 „Verlauf ein/aus“ klappt die Leiste ein (aria-expanded=false)', zu.exp==='false' && zu.breite<=1, JSON.stringify(zu)); await ev(`document.getElementById('chat-seite-knopf').click()`); await warte(300); // Löschen mit Rückfrage await ev(`[...document.querySelectorAll('#chat-liste li')][1].querySelectorAll('button').forEach(k=>{if(k.textContent.trim()==='Löschen') k.click();})`); await warte(300); const rueck=await ev(`[...document.querySelectorAll('#chat-liste li')][1].textContent`); ok('.6 Löschen fragt erst nach („Wirklich löschen?“), danach noch zwei Chats', /Wirklich löschen\?/.test(rueck) && (await listeTitel()).length===2); await ev(`[...document.querySelectorAll('#chat-liste li')][1].querySelectorAll('button').forEach(k=>{if(/Wirklich/.test(k.textContent)) k.click();})`); await warte(1000); ok('.6 Zweiter Klick löscht (ein Chat bleibt)', (await listeTitel()).length===1); await b.shot('/tmp/chat-layout.png'); }catch(e){console.log('Fehler im Lauf:',e.message); process.exitCode=1;} finally{b.close();}