65 lines
3.9 KiB
JavaScript
65 lines
3.9 KiB
JavaScript
// Generate an offline browser fixture. Run with the directory containing the pinned React UMD files.
|
|
import {readFile,writeFile} from 'node:fs/promises'
|
|
import {join} from 'node:path'
|
|
const [deps,out] = process.argv.slice(2)
|
|
if (!deps || !out) throw Error('Usage: node scripts/browser-fixture.mjs DEPENDENCY_DIR OUTPUT.html')
|
|
const react = await readFile(join(deps,'react/umd/react.production.min.js'),'utf8')
|
|
const dom = await readFile(join(deps,'react-dom/umd/react-dom.production.min.js'),'utf8')
|
|
const src = (await readFile(new URL('../plugin.js',import.meta.url),'utf8'))
|
|
.replace(/^import .+\n/gm,'').replace(/^export (?=(?:async )?function)/gm,'').replace(/^export default /m,'const plugin = ')
|
|
const fixture = `
|
|
const {useEffect,useId,useState}=React;
|
|
const h=(type,props,key)=>React.createElement(type,{...props,key});
|
|
const owner={connectionId:'fixture',profile:'ops',targetProfile:'ops',mode:'remote'};
|
|
const atom=v=>({get:()=>v});
|
|
const actions=[], storage=new Map();
|
|
const host={state:{connectionId:atom('fixture'),profile:atom('ops'),gateway:atom('open'),
|
|
focusedStoredSessionId:atom('1'),focusedSessionOwner:atom(owner)},
|
|
profileRoutes:async()=>[owner],openSession:async(id,o)=>actions.push([id,o.route]),
|
|
requestProfile:async(_route,method,p)=>{
|
|
if(method==='profiles.list')return {profiles:[{name:'ops',display_name:'Athena'}]};
|
|
if(method!=='session.list')throw Error('Unexpected RPC');
|
|
if(p.scope==='live')return {scope:'live',sessions:[]};
|
|
const sessions=Array.from({length:27},(_,i)=>({id:String(i+1),session_id:String(i+1),title:'Chat '+(i+1),source:'local',started_at:1760000000}));
|
|
return {scope:'stored',next_cursor:null,sessions};
|
|
}};
|
|
const data=new Map();
|
|
const useQueryClient=()=>client;
|
|
const client={getQueryData:key=>data.get(JSON.stringify(key)),invalidateQueries:async()=>{}};
|
|
const useValue=a=>a.get();
|
|
function useQuery(o){
|
|
const key=JSON.stringify(o.queryKey), [s,set]=useState({});
|
|
useEffect(()=>{if(!o.enabled)return;let alive=true;const c=new AbortController();
|
|
o.queryFn({signal:c.signal}).then(v=>{if(alive){data.set(key,v);set({data:v,isSuccess:true});}},()=>{if(alive)set({isError:true});});
|
|
return ()=>{alive=false;c.abort();};
|
|
},[key,o.enabled]);
|
|
return {...s,isPending:!s.isSuccess&&!s.isError};
|
|
}
|
|
`;
|
|
const test = `
|
|
let pane;
|
|
plugin.register({register:v=>{pane=v;},storage:{get:k=>storage.get(k),set:(k,v)=>storage.set(k,v)},onEvent:()=>()=>{}});
|
|
ReactDOM.createRoot(document.getElementById('root')).render(pane.render());
|
|
const check=(v,label)=>{if(!v)throw Error(label);};
|
|
const wait=async(fn)=>{for(let n=0;n<60;n++){const v=fn();if(v)return v;await new Promise(r=>setTimeout(r,20));}throw Error('UI-Merkmal nicht gefunden');};
|
|
const button=t=>[...document.querySelectorAll('button')].find(b=>b.title===t||b.textContent===t);
|
|
(async()=>{
|
|
check(pane.data.dock.pane==='sessions'&&pane.title==='Übersicht','Dock');
|
|
const bot=await wait(()=>button('Athena'));
|
|
check(bot.getAttribute('aria-expanded')==='false','Initial collapsed');
|
|
bot.click();
|
|
const first=await wait(()=>button('Chat 1'));
|
|
check(bot.getAttribute('aria-expanded')==='true'&&actions.length===0,'Toggle opened a chat');
|
|
check(first.getAttribute('aria-current')==='true','Active marker');
|
|
check(storage.get('expanded-v1')?.length===1,'Persistent expansion');
|
|
check(!button('Chat 27'),'25 rows before expansion');
|
|
button('Weitere Sessions anzeigen').click();
|
|
await wait(()=>button('Chat 27'));
|
|
first.click();
|
|
await wait(()=>actions.length);
|
|
check(actions.length===1&&actions[0][0]==='1'&&actions[0][1]===owner,'Routed existing session');
|
|
document.body.dataset.test='pass';
|
|
})().catch(e=>{document.body.dataset.test='fail';document.body.dataset.reason=e.message;});
|
|
`;
|
|
await writeFile(out,`<!doctype html><meta charset="utf-8"><title>Canopy: isolierter UI-Test</title><div id="root"></div><script>${react}</script><script>${dom}</script><script>${fixture}\n${src}\n${test}</script>`)
|