52 lines
6.6 KiB
JavaScript
52 lines
6.6 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import {readFile} from 'node:fs/promises';
|
|
import {Script,runInNewContext} from 'node:vm';
|
|
import {LocalD1} from './d1-adapter.mjs';
|
|
import {createPanel} from '../control-panel/server.mjs';
|
|
import {POLITICAL_TABS,politicalRoute,politicsDestination} from '../control-panel/public/cp-v2/cp-v2-politics-navigation.mjs';
|
|
import {governmentRoute,politicalFrameMessage} from '../web/government-cp.js';
|
|
import {workspacesForSurface} from '../control-panel/public/cp-v2/cp-v2-surface.mjs';
|
|
import {politicsCPDocument} from '../control-panel/politics-cp-shell.mjs';
|
|
|
|
test('Political CP exposes central Elections, Voting Directory, Offices and Tools menus with every existing chamber',()=>{
|
|
assert.deepEqual(POLITICAL_TABS.map(t=>t.label),['Elections','Voting Directory','Offices','Decisions & Schemes','Tools']);
|
|
const pages=POLITICAL_TABS.flatMap(t=>t.pages.map(([id])=>id));assert.equal(new Set(pages).size,pages.length);
|
|
for(const tab of POLITICAL_TABS)for(const [page]of tab.pages){const route=tab.id+'/'+page;assert.deepEqual(politicalRoute('#'+route),{tab:tab.id,page});assert.equal(governmentRoute(route),route);assert.equal(governmentRoute('politics/'+page),route);assert.equal(politicsDestination('politics',page),'https://www.dunerealms.com/government.html#'+route);assert.equal(politicsDestination(page),'https://www.dunerealms.com/government.html#'+route);}
|
|
assert.equal(politicalRoute('elections').page,'house-elections');assert.equal(politicalRoute('offices').page,'office-register');assert.equal(politicalRoute('tools').page,'council-motd');
|
|
assert.equal(politicsDestination('bank','bank-transfers'),null);assert.equal(politicsDestination('realm','game'),null);assert.equal(politicsDestination('realm','landsraad'),'https://www.dunerealms.com/government.html#offices/landsraad');assert.equal(governmentRoute('https://evil.invalid'),'elections/house-elections');
|
|
});
|
|
|
|
test('Politics is removed from the User and Clan CP surfaces and legacy routing redirects to Government',async()=>{
|
|
const workspaces=['realm','clan','politics','bank','events'].map(id=>({id}));assert.deepEqual(workspacesForSurface(workspaces,false).map(w=>w.id),['realm','bank','events']);assert.deepEqual(workspacesForSurface(workspaces,true).map(w=>w.id),['clan','events']);
|
|
const source=await readFile(new URL('../control-panel/public/cp-v2/v2.js',import.meta.url),'utf8');assert.match(source,/if\(requestedPolitics\)location\.replace\(requestedPolitics\)/);assert.match(source,/const politicalTarget=politicsDestination\(wid,pid\);if\(politicalTarget\)\{location\.assign\(politicalTarget\)/);
|
|
});
|
|
|
|
test('the real server serves an independent Political CP and styles without the normal CP router',async()=>{
|
|
const db=new LocalD1(),app=createPanel(db);try{
|
|
const response=await app(new Request('https://cp.dunerealms.com/politics-cp'));assert.equal(response.status,200);assert.match(response.headers.get('content-type'),/text\/html/);const html=await response.text();assert.match(html,/data-cp-mode="politics"/);assert.match(html,/cp-v2-politics-shell.mjs/);assert.doesNotMatch(html,/class="command-banner"/);assert.match(html,/clan-cp-shell/);assert.match(html,/id="sidebar-right"/);assert.doesNotMatch(html,/src="\/v2\/v2.js|class="dune-site-header"/);
|
|
assert.equal((await app(new Request('https://cp.dunerealms.com/politics-cp',{method:'HEAD'}))).status,200);assert.equal((await app(new Request('https://cp.dunerealms.com/politics-cp',{method:'POST'}))).status,405);
|
|
for(const file of ['cp-v2-politics-shell.mjs','cp-v2-politics-shell.css','cp-v2-politics-navigation.mjs','cp-v2-politics-native.css'])assert.equal((await app(new Request('https://cp.dunerealms.com/v2/'+file))).status,200,file);
|
|
assert.equal((await app(new Request('https://cp.dunerealms.com/political-catalog.mjs'))).status,200);
|
|
assert.equal((await app(new Request('https://cp.dunerealms.com/api/public/government?user=other'))).status,400);assert.equal((await app(new Request('https://cp.dunerealms.com/api/public/government',{method:'POST'}))).status,405);
|
|
}finally{db.close();}
|
|
});
|
|
|
|
test('Political CP reuses the authored CP layout and normal CP remains a valid classic script',async()=>{
|
|
const template=await readFile(new URL('../control-panel/public/cp-v2/index.html',import.meta.url),'utf8'),html=politicsCPDocument(template);
|
|
const layout=template.slice(template.indexOf('<div class="layout"'),template.indexOf('<footer'));assert(html.includes(layout));assert.match(html,/class="portals" aria-label="Workspace portals"/);assert.match(html,/type="module" src="\/v2\/cp-v2-politics-shell.mjs"/);
|
|
const source=await readFile(new URL('../control-panel/public/cp-v2/v2.js',import.meta.url),'utf8');assert.doesNotThrow(()=>new Script(source));
|
|
const redirect=runInNewContext(source.slice(source.indexOf('function politicsDestination('),source.indexOf('const requestedPolitics='))+';politicsDestination');
|
|
for(const page of ['house-elections','imperial-elections','landsraad','council-motd'])assert.equal(redirect('politics',page),politicsDestination('politics',page));
|
|
});
|
|
|
|
test('Government hosts the Political CP while retaining the public office directory',async()=>{
|
|
const html=await readFile(new URL('../web/government.html',import.meta.url),'utf8');assert.match(html,/id="political-workspace"/);assert.match(html,/src="https:\/\/cp.dunerealms.com\/politics-cp#household\/overview"/);assert.match(html,/id="public-register"/);assert.match(html,/id="imperial-offices"/);assert.doesNotMatch(html,/cp.dunerealms.com\/#politics\//);assert.match(html,/government-cp.css/);assert.match(html,/government-cp.js/);
|
|
const entry=await readFile(new URL('../control-panel/public/user-service-entry.mjs',import.meta.url),'utf8');assert.match(entry,/v2\/cp-v2-politics-native.css/);assert.match(entry,/politicsDestination\('politics',page\)/);
|
|
});
|
|
|
|
test('Government rejects forged frame navigation, origins and unbounded height messages',()=>{
|
|
const frame={contentWindow:{}},message=data=>({source:frame.contentWindow,origin:'https://cp.dunerealms.com',data});
|
|
assert.deepEqual(politicalFrameMessage(message({type:'dune-embed-size',height:900.2}),frame),{height:901});assert.deepEqual(politicalFrameMessage(message({type:'dune-politics-route',tab:'tools',page:'council-motd'}),frame),{route:'tools/council-motd'});
|
|
for(const event of [{...message({type:'dune-embed-size',height:900}),origin:'https://evil.invalid'},{...message({type:'dune-embed-size',height:900}),source:{}},message({type:'dune-embed-size',height:Infinity}),message({type:'dune-embed-size',height:999999}),message({type:'dune-politics-route',tab:'tools',page:'https://evil.invalid'})])assert.equal(politicalFrameMessage(event,frame),null);
|
|
});
|

Staff