MC/website/tests/dmpaint-templates.test.mjs
2026-11-27 23:43:05 +00:00

57 lines
5.1 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import {createDMPaint} from '../control-panel/dmpaint.mjs';
import {templateCatalogue,templateModes,renderPaintTemplate} from '../control-panel/dmpaint-templates.mjs';
import {mountPaintTemplates} from '../control-panel/public/dmpaint/templates.mjs';
const request=path=>new Request('https://cp.dunerealms.com'+path);
const route=(staff=false,user={id:'person'})=>createDMPaint({}, {authenticate:async()=>user,authorise:async()=>staff});
test('five curated designs serve clans and companies equally, with fixed POL artwork excluded',()=>{
const ids=new Set(templateModes.flatMap(mode=>templateCatalogue(mode).map(d=>d.id)));
assert.equal(ids.size,5);assert.deepEqual(templateCatalogue('clan'),templateCatalogue('company'));
assert.deepEqual(templateCatalogue('politics'),[]);
for(const mode of templateModes)for(const design of templateCatalogue(mode)){
const svg=renderPaintTemplate({mode,id:design.id,sealData:'data:image/png;base64,AAAA'});
assert.match(svg,/width="512" height="512"/);assert.doesNotMatch(svg,/<(?:script|foreignObject|style)\b|\bon\w+=|(?:https?:)?\/\/[^w]/i);
const tags=[...svg.matchAll(/<\/?([a-zA-Z]+)/g)].map(m=>m[1]);assert.ok(tags.every(t=>['svg','title','g','path','circle','image','text'].includes(t)));
}
});
test('editable labels escape markup and colours cannot inject SVG attributes',()=>{
const options={mode:'clan',id:'desert-crest',title:'<script>& "Clan"'};
const svg=renderPaintTemplate(options);assert.match(svg,/&lt;script&gt;&amp; &quot;Clan&quot;/);assert.doesNotMatch(svg,/<script>/);
for(const invalid of [{primary:'red" onclick="x'},{title:'x'.repeat(41)},{outfit:'javascript:'},{mode:'politics'}])assert.throws(()=>renderPaintTemplate({...options,...invalid}));
const a=renderPaintTemplate({mode:'character',id:'desert-traveller',outfit:'hood'}),b=renderPaintTemplate({mode:'character',id:'desert-traveller',outfit:'headscarf'}),c=renderPaintTemplate({mode:'character',id:'desert-traveller',outfit:'armour'});assert.notEqual(a,b);assert.notEqual(b,c);
});
test('template previews and downloads enforce the current session and staff modes on every read',async()=>{
const citizen=route(),staff=route(true);
for(const path of ['/dmpaint/templates.json?mode=seal','/dmpaint/template.svg?mode=award&design=service-award']){
assert.equal((await citizen(request(path))).status,403);assert.equal((await route(false,null)(request(path))).status,401);
}
assert.equal((await citizen(request('/dmpaint/template.svg?mode=clan&design=service-seal'))).status,400);
assert.equal((await citizen(request('/dmpaint/template.svg?mode=politics&design=desert-crest'))).status,400);
for(const mode of ['clan','company','character']){
const catalogue=await (await citizen(request('/dmpaint/templates.json?mode='+mode))).json();assert.ok(catalogue.templates.length);
for(const design of catalogue.templates){const response=await citizen(request('/dmpaint/template.svg?mode='+mode+'&design='+design.id));assert.equal(response.status,200);assert.match(response.headers.get('cache-control'),/private, no-store/);assert.match(await response.text(),/<svg/);}
}
for(const [mode,id]of [['seal','service-seal'],['award','service-award']]){const response=await staff(request('/dmpaint/template.svg?mode='+mode+'&design='+id));assert.equal(response.status,200);const svg=await response.text();assert.match(svg,/data:image\/png;base64,iVBOR/);assert.doesNotMatch(svg,/<image href="https?:/);}
});
class Element{
constructor(tag){this.tag=tag;this.children=[];this.textContent='';this.attributes={};this.listeners={};}
append(...items){this.children.push(...items);}
setAttribute(k,v){this.attributes[k]=v;}
addEventListener(k,fn){this.listeners[k]=fn;}
}
const tree=node=>[node,...node.children.flatMap(tree)];
test('loading a design preserves a dirty canvas when cancelled and uses the chosen editable fields when confirmed',async()=>{
let inserted,current='<svg>my existing drawing</svg>',confirmations=0,accept=false,writes=0;
const document={createElement:tag=>new Element(tag),querySelector:()=>({before:panel=>{inserted=panel;}})};
const editor={svgCanvas:{getSvgString:()=>current,setSvgString:svg=>{writes++;current=svg;return true;}}};
const handle=route(),status={};
await mountPaintTemplates({editor,status,mode:'clan',cleanCanvas:'<svg/>',document,fetcher:(url)=>handle(request(url)),confirmReplace:()=>{confirmations++;return accept;}});
const nodes=tree(inserted),apply=nodes.find(n=>n.textContent==='Load design');
await apply.onclick();assert.equal(writes,0);assert.equal(confirmations,1);assert.match(current,/my existing drawing/);
const title=nodes.find(n=>n.tag==='input'&&n.type==='text');title.value='MY CLAN';
const colour=nodes.find(n=>n.tag==='input'&&n.type==='color');colour.value='#AABBCC';
accept=true;await apply.onclick();assert.equal(writes,1);assert.match(current,/MY CLAN/);assert.match(current,/#AABBCC/);assert.equal(apply.disabled,false);assert.equal(inserted.open,false);
// A loaded template remains a drawing: subsequent replacement still needs consent.
accept=false;await apply.onclick();assert.equal(writes,1);assert.equal(confirmations,3);
});