MC/website/tests/organisation-workspace-shell.test.mjs
2026-11-27 23:43:05 +00:00

22 lines
2.4 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import {readFileSync} from 'node:fs';
import {runInNewContext} from 'node:vm';
function workspace(hash='#clan'){
const listeners={},tabs=['clan','company','politics'].map(kind=>({dataset:{organisationKind:kind},setAttribute(){}})),frame={src:'',style:{},contentWindow:{postMessage(){}},addEventListener(){}},location={href:'https://www.dunerealms.com/realm-hub.html'+hash,hash,replace(){}},writes=[];
const context={URL,Number,document:{querySelector:s=>s==='#organisation-frame'?frame:{},querySelectorAll:()=>tabs},location,history:{replaceState(a,b,url){writes.push(String(url));}},window:{addEventListener:(type,fn)=>listeners[type]=fn},addEventListener(){}};
runInNewContext(readFileSync(new URL('../web/organisation-workspace.js',import.meta.url),'utf8'),context);
return{frame,writes,message:data=>listeners.message({origin:'https://cp.dunerealms.com',source:frame.contentWindow,data}),spoof:event=>listeners.message(event)};
}
test('organisation choices open existing native management services without changing authority',()=>{
for(const[kind,path]of [['clan','/clan-cp'],['company','/company-cp'],['politics','/politics-cp']]){const app=workspace('#'+kind);assert.equal(new URL(app.frame.src).pathname,path);assert.equal(new URL(app.frame.src).origin,'https://cp.dunerealms.com');}
});
test('only the current authenticated service frame may preserve selected organisation and route',()=>{
const app=workspace('#company'),data={type:'dune-organisation-context',kind:'company',id:'company-123',page:'claims'};
app.spoof({origin:'https://attacker.invalid',source:app.frame.contentWindow,data});app.spoof({origin:'https://cp.dunerealms.com',source:{},data});app.message({...data,kind:'politics'});assert.equal(app.writes.length,0);
app.message(data);const saved=new URL(app.writes[0]);assert.equal(saved.searchParams.get('company'),'company-123');assert.equal(saved.hash,'#company/claims');
app.message({...data,page:'../staff'});assert.equal(app.writes.length,1);
});
test('native frame resizing is bounded and never accepts another window',()=>{
const app=workspace();app.message({type:'dune-embed-size',height:900});assert.equal(app.frame.style.height,'900px');app.message({type:'dune-embed-size',height:1e10});assert.equal(app.frame.style.height,'50000px');app.message({type:'dune-embed-size',height:NaN});assert.equal(app.frame.style.height,'50000px');
});