web/website/tests/cp-v2-workspace-chrome.test.mjs

12 lines
2.1 KiB
JavaScript

import test from 'node:test';import assert from 'node:assert/strict';import {readFileSync} from 'node:fs';import {runInNewContext} from 'node:vm';
test('all native workspaces put reload below content and hide routine status after load',async()=>{
class Node{constructor(tag,text,cls){this.tag=tag;this.textContent=text;this.className=cls;this.children=[];this.style={};this.contentWindow={postMessage(){}};}append(...nodes){this.children.push(...nodes);}replaceChildren(...nodes){this.children=nodes;}setAttribute(){}querySelector(){return null;}remove(){}}
const host=new Node('div'),window={addEventListener(){},removeEventListener(){}},el=(...args)=>new Node(...args);
let source=readFileSync(new URL('../control-panel/public/cp-v2/cp-v2-native-services.mjs',import.meta.url),'utf8').replace(/^import[^\n]*\n/gm,'').replaceAll('export ','');
const mount=runInNewContext(source+';mountNativeService',{canSubmitDevRequest:()=>false,createCPV2Page:({tabs})=>{const root=new Node('section');root.append(tabs[0].content);return {root};},el,window,location:{origin:'https://cp.dunerealms.com'},URL,AbortController,setTimeout,clearTimeout});
const dispose=mount(host,'shared-inbox',{fetcher:async()=>({ok:true,json:async()=>({authenticated:true,user:{permissions:[]}})})});
await new Promise(resolve=>setImmediate(resolve));
const workspace=host.children[0].children[0];assert.equal(workspace.children.length,3);const [status,frameBox,tools]=workspace.children;assert.equal(frameBox.className,'cp-v2-native-frame');assert.equal(tools.tag,'details');assert.equal(tools.open,undefined);assert.equal(tools.children[0].textContent,'+ Workspace details');assert.equal(tools.children.filter(n=>n.textContent==='Reload workspace').length,1);const reload=tools.children.find(n=>n.textContent==='Reload workspace');assert.equal(status.hidden,false);
frameBox.children[0].onload();assert.equal(status.hidden,true);assert.doesNotMatch(status.textContent,/Native workspace loaded|role checks remain authoritative/);
reload.onclick();assert.equal(status.hidden,false);await new Promise(resolve=>setImmediate(resolve));dispose();
});