web/website/tests/roster-avatars.test.mjs
unimatrixgit 77cdf19faa Checkpoint Alpha 1.0.0.3 graphical menus and moderation evidence
Preserve the current Alpha source and artwork snapshot. See handoff/alpha-1.0.0.3 for actual deployed hashes, checks, staged follow-ups and the production pause. This checkpoint does not claim every source feature is deployed.
2026-11-02 00:05:34 +01:00

21 lines
2.7 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import {createAvatars} from '../node/avatar.mjs';
const origin='https://cp.dunerealms.com';
function database(accounts){return{prepare(sql){assert(!sql.includes('site_profiles'),'roster must ignore uploaded avatar overrides');return{bind(name){this.name=name;return this},async first(){return Object.hasOwn(accounts,this.name)?{minecraft_uuid:accounts[this.name]}:null;}}}};}
test('roster username resolves Minecraft skin without exposing IDs or using overrides',async()=>{
const uuid='99d2fe76-7977-4d49-bd39-e48fc8c648b8',png=Buffer.from([137,80,78,71,13,10,26,10,1]);const calls=[];
const fetcher=async value=>{const url=String(value);calls.push(url);if(url.startsWith('https://sessionserver.mojang.com/'))return Response.json({properties:[{name:'textures',value:Buffer.from(JSON.stringify({textures:{SKIN:{url:'http://textures.minecraft.net/texture/abcdef'}}})).toString('base64')}]});assert.equal(url,'https://textures.minecraft.net/texture/abcdef');return new Response(png);};
const avatar=createAvatars(database({staffmember:uuid}),{}, {fetcher});const response=await avatar(new Request(origin+'/api/avatars/roster/staffmember'));
assert.equal(response.status,200);assert.equal(response.headers.get('content-type'),'image/png');assert.deepEqual(Buffer.from(await response.arrayBuffer()),png);assert.equal(calls.length,2);assert(!response.headers.has('location'));
});
test('unlinked staff receive face fallback; failed linked skin receives 64px crop-compatible Steve',async()=>{
const avatar=createAvatars(database({unlinked:null,linked:'88d2fe76-7977-4d49-bd39-e48fc8c648b8'}),{}, {fetcher:async()=>new Response(null,{status:503})});
const unlinked=await avatar(new Request(origin+'/api/avatars/roster/unlinked'));assert.equal(unlinked.status,200);assert.match(await unlinked.text(),/viewBox="0 0 8 8"/);
const linked=await avatar(new Request(origin+'/api/avatars/roster/linked'));assert.equal(linked.status,200);assert.equal(linked.headers.get('content-type'),'image/svg+xml');const svg=await linked.text();assert.match(svg,/width="64" height="64" viewBox="0 0 64 64"/);assert.match(svg,/transform="translate\(8 8\)"/);
});
test('roster rejects malformed paths, unknown accounts and non-GET requests',async()=>{
const avatar=createAvatars(database({}),{}, {fetcher:async()=>{throw Error('must not fetch')}});
for(const path of ['/api/avatars/roster/UPPERCASE','/api/avatars/roster/ab','/api/avatars/roster/%27or1','/api/avatars/roster/name/extra','/api/avatars/roster/unknown'])assert.equal((await avatar(new Request(origin+path))).status,404);
assert.equal((await avatar(new Request(origin+'/api/avatars/roster/name',{method:'POST'}))).status,405);
});