← back to CelebritySignatures
test/wallpaper-pattern.test.mjs
29 lines
import test from 'node:test';
import assert from 'node:assert/strict';
import {normalize,geometry,placements} from '../public/assets/wallpaper-pattern.js';
test('untrusted saved settings are bounded and keep only supported colors and repeat modes',()=>{
const s=normalize({qid:'../admin',stripeWidth:Infinity,gapWidth:-500,rowGap:1e99,scale:9999,zoom:null,repeat:'script',stripeColor:'url(secret)',gapColor:'#AB12EF'});
assert.equal(s.qid,'Q91');assert.equal(s.gapWidth,0);assert.equal(s.rowGap,6);assert.equal(s.scale,150);
assert.equal(s.repeat,'straight');assert.equal(s.stripeColor,'#e0e6dc');assert.equal(s.gapColor,'#ab12ef');
for(const input of [null,{},[],{stripeWidth:'NaN',rowGap:''}]) assert.ok(Number.isFinite(geometry(input,4).width));
});
test('half-drop doubles horizontal period and wraps both halves of alternate signatures',()=>{
const settings={stripeWidth:2,gapWidth:1,rowGap:1,scale:100};
const a=geometry(settings,2),b=geometry({...settings,repeat:'half-drop'},2);
assert.equal(b.width,a.width*2);assert.equal(b.height,a.height);
const p=placements(b,'half-drop'),even=p.filter(x=>x.column===0),odd=p.filter(x=>x.column===1);
assert.equal(even.length,1);assert.equal(odd.length,2);
assert.ok(odd.some(x=>x.y<0));assert.ok(odd.some(x=>x.y+x.height>b.height));
assert.equal(odd[1].y-odd[0].y,b.height);
const visible=odd.reduce((n,x)=>n+Math.min(b.height,x.y+x.height)-Math.max(0,x.y),0);
assert.equal(visible,b.motifHeight,'whole signature survives the tile boundary');
});
test('view zoom does not change export geometry and scale keeps signatures inside each band',()=>{
assert.deepEqual(geometry({zoom:40},8,150),geometry({zoom:150},8,150));
for(const repeat of ['straight','half-drop']) for(const stripeWidth of [1,10]) for(const gapWidth of [0,6]) for(const scale of [25,150]) {
const g=geometry({repeat,stripeWidth,gapWidth,scale},8,150);
assert.ok(g.motifWidth<g.stripe);assert.ok(g.width>0&&g.height>0);
for(const p of placements(g,repeat)) assert.ok(p.x>=p.column*g.columnWidth && p.x+p.width<=(p.column*g.columnWidth+g.stripe));
}
});