MC/website/tests/item-lore-model.test.mjs
2026-11-27 23:43:05 +00:00

26 lines
2.8 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import {itemLoreModel,itemMaterialIcon,itemHref,itemPublicHref,itemIdentity,itemRoman,safeItemArt} from '../control-panel/public/item-lore-model.mjs';
test('native block icons and default lore artwork use the shared asset origin from every CP',()=>{
const item=itemLoreModel({material:'minecraft:COBBLESTONE'});assert.equal(item.material,'cobblestone');assert.equal(item.icon,'https://www.dunerealms.com/assets/minecraft-items/cobblestone.png');assert.equal(item.art,'https://www.dunerealms.com/assets/imperial-atelier-v1.webp');assert.equal(itemMaterialIcon('CHEST'),'https://www.dunerealms.com/assets/minecraft-items/chest.png');assert.equal(itemMaterialIcon('../../admin'),null);
});
test('listing identity is public and strict; preview records never produce checkout links',()=>{
assert.equal(itemHref({id:42,type:'items'}),'/item.html?kind=item&id=42');assert.equal(itemPublicHref({id:42,type:'items'}),'https://www.dunerealms.com/item.html?kind=item&id=42');
const id='12345678-1234-4234-9234-123456789012';assert.equal(itemHref({listingId:id,type:'kits'}),'/item.html?kind=kit&id='+id);
for(const id of ['preview-x','0','1&override=1','<script>',-1,Infinity])assert.equal(itemIdentity({id,type:'items'}),null);
assert.equal(itemIdentity({listingId:'not-a-uuid'}),null);
});
test('missing marks, rarity and stats stay unregistered instead of inheriting imaginary kit stats',()=>{
const item=itemLoreModel({id:1,material:'diamond_sword',quantity:4,price:12.5,seller:'Traveller'});
assert.equal(item.quality,null);assert.equal(item.mark,null);assert.deepEqual(item.enchants,[]);assert.deepEqual(item.stats,[]);assert.equal(item.price,12.5);assert.equal(item.owner,'Traveller');
});
test('real supplied lore and enchantments map to the same Mythic and Roman display as game',()=>{
const item=itemLoreModel({id:1,quality:'mythic',equipmentMark:14,season:'S1',lore:[{text:'The desert remembers.'}],enchantments:{protection:7,mending:1,invalid:999},stats:[{label:'Armour',value:'+3'}]});
assert.equal(item.markLabel,'MK XIV');assert.equal(item.qualityLabel,'Mythic');assert.equal(item.lore[0],'The desert remembers.');assert.deepEqual(item.enchants,[{name:'Protection',level:'VII'},{name:'Mending',level:'I'}]);assert.deepEqual(item.stats,[{name:'Armour',value:'+3'}]);
assert.equal(itemRoman(0),'0');assert.equal(itemRoman(14),'XIV');assert.equal(itemRoman(-1),'—');
});
test('artwork rejects executable, credential-bearing and third-party URLs',()=>{
for(const input of ['javascript:alert(1)','https://attacker.test/a.png','https://x:y@www.dunerealms.com/assets/a.png','https://www.dunerealms.com/admin','https://www.dunerealms.com/assets/../admin'])assert.equal(safeItemArt(input),'/assets/imperial-atelier-v1.webp');
assert.equal(safeItemArt('/assets/item-0.webp'),'https://www.dunerealms.com/assets/item-0.webp');
});