MC/tools/build-planet-texture-packs.test.mjs
2026-11-28 17:00:13 +01:00

252 lines
15 KiB
JavaScript

import {test} from 'node:test';
import assert from 'node:assert/strict';
import {inflateRawSync,crc32} from 'node:zlib';
import {createHash} from 'node:crypto';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import {build,profiles,textureEntries,protectedTextures,zip} from './build-planet-texture-packs.mjs';
import {sandPalettes} from './flat-sand.mjs';
import {NOTE_SERIES,COIN_SERIES} from '../website/control-panel/public/cp-v2/dunemark-catalogue.mjs';
import {centralPackEntries,mergePackAssets} from './build-central-resource-pack.mjs';
function chunks(png,type){const found=[];for(let i=8;i<png.length;){const n=png.readUInt32BE(i);if(png.subarray(i+4,i+8).toString()===type)found.push(png.subarray(i+8,i+8+n));i+=12+n;}return Buffer.concat(found);}
test('every sand palette has identical grain, darker spice, and zero shader relief',()=>{
for(const planet of Object.keys(sandPalettes)){
const entries=textureEntries(planet),get=n=>entries.get('assets/minecraft/textures/block/'+n+'.png');
const sand=get('sand'),spice=get('suspicious_sand_0');assert.deepEqual(chunks(sand,'IDAT'),chunks(spice,'IDAT'));
const a=chunks(sand,'PLTE'),b=chunks(spice,'PLTE');for(let i=0;i<a.length;i++)assert(b[i]<a[i]);
for(const name of ['sand','red_sand','suspicious_sand_0','suspicious_sand_1','suspicious_sand_2','suspicious_sand_3']){
const normal=chunks(get(name+'_n'),'PLTE');for(let i=0;i<normal.length;i+=3)assert.deepEqual([...normal.subarray(i,i+3)],[128,128,255]);
assert(chunks(get(name+'_s'),'PLTE').every(x=>x===0));
}
}
});
function readZip(bytes){
const result=new Map();let offset=0;
while(bytes.readUInt32LE(offset)===0x04034b50){
const length=bytes.readUInt32LE(offset+18),raw=bytes.readUInt32LE(offset+22),nameLength=bytes.readUInt16LE(offset+26),extra=bytes.readUInt16LE(offset+28);
const name=bytes.subarray(offset+30,offset+30+nameLength).toString(),start=offset+30+nameLength+extra;
assert.equal(result.has(name),false,'Duplicate archive entry');
const data=inflateRawSync(bytes.subarray(start,start+length));assert.equal(data.length,raw);assert.equal(crc32(data),bytes.readUInt32LE(offset+14));result.set(name,data);offset=start+length;
}
assert.equal(bytes.readUInt32LE(offset),0x02014b50);
assert.equal(bytes.readUInt32LE(bytes.length-22),0x06054b50);
assert.equal(bytes.readUInt16LE(bytes.length-12),result.size);
return result;
}
test('each planet has distinct stone, readable ordinary items, and no custom-art overrides',()=>{
const stones=[];
for(const key of Object.keys(profiles)){
const entries=textureEntries(key);
for(const name of entries.keys())if(name.startsWith('assets/'))assert.match(name,/^assets\/minecraft\/textures\/(block|item)\/[a-z0-9_.-]+$/);
assert(entries.has('assets/minecraft/textures/item/diamond_sword.png'));
for(const name of protectedTextures)assert.equal(entries.has('assets/minecraft/textures/block/'+name+'.png'),false,'Dune spice carrier must remain unchanged');
assert(entries.has('LICENSE-Pixel-Perfection-CE.md'));
assert.match(entries.get('CREDITS.txt').toString(),/28e38cab7c1f03f86364ef704f705bebcc13cb3d/);
if(key!=='guild'&&!key.endsWith('_nether')&&!key.endsWith('_the_end'))stones.push(createHash('sha256').update(entries.get('assets/minecraft/textures/block/stone.png')).digest('hex'));
for(const name of entries.keys())if(name.endsWith('.mcmeta')&&name!=='pack.mcmeta')assert(entries.has(name.slice(0,-7)));
}
assert.equal(new Set(stones).size,5,'All five planets must have visibly distinct stone textures');
});
test('release archives round-trip, rebuild identically, and match client download pins',()=>{
const dir=fs.mkdtempSync(path.join(os.tmpdir(),'dune-materials-'));
try{
const a=build(path.join(dir,'a')),b=build(path.join(dir,'b'));assert.deepEqual(a,b);
for(const p of Object.values(a.packs)){
const bytes=fs.readFileSync(path.join(dir,'a',p.filename)),entries=readZip(bytes);
assert.equal(entries.size,p.entries);assert.equal(createHash('sha1').update(bytes).digest('hex'),p.sha1);
assert.equal(createHash('sha256').update(bytes).digest('hex'),p.sha256);
assert.deepEqual(bytes,fs.readFileSync(path.join(dir,'b',p.filename)));
const meta=JSON.parse(entries.get('pack.mcmeta'));assert.equal(meta.pack.pack_format,64);assert.equal(meta.pack.max_format,97);
assert(fs.readFileSync(path.join(dir,'a','planet-textures.yml'),'utf8').includes(p.sha1));
}
}finally{fs.rmSync(dir,{recursive:true,force:true});}
});
test('the central pack registers its planet block sprites without replacing existing atlas sources',()=>{
const atlasPath='assets/minecraft/atlases/blocks.json';
const baseSource={type:'single',resource:'dunerealms:gui/imperial_menu_atlas_v2'};
const base=new Map([
['pack.mcmeta',Buffer.from(JSON.stringify({pack:{pack_format:64}}))],
[atlasPath,Buffer.from(JSON.stringify({sources:[baseSource]}))]
]);
const entries=centralPackEntries(base);
const atlas=JSON.parse(entries.get(atlasPath));
assert.deepEqual(atlas.sources[0],baseSource);
const sprites=new Set(atlas.sources.map(source=>source.sprite??source.resource));
for(const [name,bytes] of entries){
if(!/^assets\/dr_[a-z]+\/models\/item\//.test(name))continue;
const texture=JSON.parse(bytes).textures.all;
if(!texture.startsWith('dr_'))continue;
assert(sprites.has(texture),'Missing atlas sprite '+texture);
assert(entries.has('assets/'+texture.replace(':','/textures/')+'.png'),'Missing texture file '+texture);
}
for(const planet of ['arrakis','caladan'])assert(sprites.has('dr_'+planet+':block/stone'));
const canonical=JSON.parse(entries.get('assets/dr_arrakis/items/stone.json'));
assert.equal(canonical.model.model,'dr_arrakis:item/stone');
const legacy=JSON.parse(entries.get('assets/dunerealms/models/item/planets/arrakis/stone.json'));
assert.equal(legacy.parent,canonical.model.model);
assert.deepEqual(centralPackEntries(entries).get(atlasPath),entries.get(atlasPath));
});
test('central generation refuses to overwrite a different item definition or model',()=>{
const metadata=Buffer.from(JSON.stringify({pack:{pack_format:64}}));
const generated=centralPackEntries(new Map([['pack.mcmeta',metadata]]));
for(const prefix of ['items','models/item']){
const file='assets/dunerealms/'+prefix+'/planets/arrakis/stone.json';
assert(generated.has(file));
assert.throws(()=>centralPackEntries(new Map([['pack.mcmeta',metadata],[file,Buffer.from('{}')]])),
error=>error.message==='Conflicting pack entry: '+file);
}
});
test('the central builder preserves compatible common runtime assets and leaves native overlays outside the base',()=>{
const inherited=[
'pack.mcmeta',
'assets/dunerealms/items/ecology/water_of_life.json',
'assets/dunerealms/models/item/ecology/jerboa_egg.json',
'assets/dunerealms/textures/entity/npc/chani.png',
'assets/dunerealms/particles/fixture.json',
'assets/dunerealms/sounds.json',
'assets/dunerealms/sounds/fixture.ogg'
];
const base=new Map(inherited.map(name=>[name,Buffer.from(name==='pack.mcmeta'
?JSON.stringify({pack:{pack_format:64}}):name)]));
const entries=centralPackEntries(base);
for(const name of inherited)assert.deepEqual(entries.get(name),base.get(name),name);
assert(!entries.has('assets/minecraft/textures/block/stone.png'));
assert(entries.has('assets/dr_arrakis/textures/block/stone.png'));
});
test('central generated licence text has identical ZIP bytes for Windows and LF inputs',()=>{
const inherited=Buffer.from('Production attribution\r\nKeep verbatim\r\n');
const base=new Map([
['pack.mcmeta',Buffer.from(JSON.stringify({pack:{pack_format:64}}))],
['CREDITS-Sand.txt',inherited],
['LICENSE-Sand-Luanti.txt',inherited]
]);
const names=['CREDITS-Sand.txt','LICENSE-Sand-Luanti.txt'];
const text='Copyright © 2026\nTerms remain intact.\n';
const fixture=eol=>()=>new Map(names.map(name=>[name,Buffer.from(text.replaceAll('\n',eol))]));
const windows=centralPackEntries(base,fixture('\r\n'));
const linux=centralPackEntries(base,fixture('\n'));
assert.deepEqual(zip(windows),zip(linux));
for(const name of names){
assert.deepEqual(windows.get(name),inherited);
assert.equal(windows.get('licenses/planet-materials/'+name).toString('utf8'),text);
}
});
test('source assets combine only recognized additive JSON and report every collision',()=>{
const lang='assets/dunerealms/lang/en_us.json';
const font='assets/dunerealms/font/menu.json';
const sounds='assets/dunerealms/sounds.json';
const atlas='assets/minecraft/atlases/blocks.json';
const texture='assets/dunerealms/textures/gui/logo.png';
const inputs=rows=>new Map(Object.entries(rows).map(([name,value])=>[name,Buffer.from(JSON.stringify(value))]));
const first=inputs({[lang]:{dune:'Dune'},[font]:{providers:[{type:'space'}]},[sounds]:{spice:{sounds:['dust']}},
[atlas]:{sources:[{type:'single'}]},[texture]:'pixels'});
const second=inputs({[lang]:{spice:'Spice'},[font]:{providers:[{type:'bitmap'}]},[sounds]:{menu:{sounds:['open']}},
[atlas]:{sources:[{type:'directory'}]}});
const merged=mergePackAssets(first,[second]);
assert.deepEqual(JSON.parse(merged.get(lang)),{dune:'Dune',spice:'Spice'});
assert.deepEqual(JSON.parse(merged.get(font)).providers,[{type:'space'},{type:'bitmap'}]);
assert.deepEqual(JSON.parse(merged.get(atlas)).sources,[{type:'single'},{type:'directory'}]);
assert.deepEqual(Object.keys(JSON.parse(merged.get(sounds))),['spice','menu']);
assert.throws(()=>mergePackAssets(first,[inputs({[lang]:{dune:'Again'},[texture]:'pixels'})]),
error=>error.message.includes(lang+'#dune')&&error.message.includes(texture));
assert.throws(()=>mergePackAssets(first,[inputs({'assets/dr_arrakis/../logo.png':'bad'})]),/Invalid resource asset path/);
});
test('the supplied DuneMenus logo glyph points to compact shared artwork',()=>{
const originalSource=new URL('../network/resource-pack/shared/',import.meta.url);
const fontFile='assets/dunerealms/font/menu.json';
const font=JSON.parse(fs.readFileSync(new URL(fontFile,originalSource)));
const provider=font.providers.find(entry=>entry.chars.includes('\ue000'));
assert.equal(provider.file,'dunerealms:gui/menu-logo.png');
assert(provider.ascent<=provider.height);
const image=fs.readFileSync(new URL('assets/dunerealms/textures/gui/menu-logo.png',originalSource));
assert.equal(image.subarray(0,8).toString('hex'),'89504e470d0a1a0a');
assert(image.readUInt32BE(16)<=96);
assert(image.readUInt32BE(20)<=36);
assert(image.length<16*1024);
});
test('identical imperial atlas artwork shares a texture while retaining both sprite registrations',()=>{
const atlasPaths=['assets/minecraft/atlases/blocks.json','assets/minecraft/atlases/items.json'];
const legacy='dunerealms:gui/imperial_menu_atlas_v2', canonical='dunerealms:gui/imperial-menu-atlas-v2';
const oldFile='assets/dunerealms/textures/gui/imperial_menu_atlas_v2.png';
const newFile='assets/dunerealms/textures/gui/imperial-menu-atlas-v2.png';
const source=Buffer.from(JSON.stringify({sources:[{type:'single',resource:legacy}]}));
const fixture=new Map([['pack.mcmeta',Buffer.from('{"pack":{"pack_format":64}}')],
[oldFile,Buffer.from('identical')],[newFile,Buffer.from('identical')],...atlasPaths.map(name=>[name,source])]);
const entries=centralPackEntries(fixture,()=>new Map());
assert(!entries.has(oldFile));
assert(entries.has(newFile));
for(const name of atlasPaths){
assert.deepEqual(JSON.parse(entries.get(name)).sources[0],{type:'single',resource:canonical,sprite:legacy});
}
const different=new Map(fixture);
different.set(oldFile,Buffer.from('different'));
assert(centralPackEntries(different,()=>new Map()).has(oldFile));
const direct=new Map(fixture);
direct.set('assets/dunerealms/font/direct.json',Buffer.from(JSON.stringify({providers:[{file:legacy+'.png'}]})));
assert(centralPackEntries(direct,()=>new Map()).has(oldFile));
});
test('new JSON assets generate identical ZIP bytes across checkout line endings',()=>{
const font='assets/dunerealms/font/menu.json';
const inherited='assets/dunerealms/font/production.json';
const texture='assets/dunerealms/textures/item/coin.png';
const base=new Map([[inherited,Buffer.from('{\r\n "providers": []\r\n}\r\n')]]);
const png=Buffer.from([137,80,78,71,13,10]);
const source=eol=>new Map([[font,Buffer.from(['{',' "providers": []','}', ''].join(eol))],[texture,png]]);
const windows=mergePackAssets(base,[source('\r\n')]);
const linux=mergePackAssets(base,[source('\n')]);
assert.deepEqual(zip(windows),zip(linux));
assert.deepEqual(windows.get(inherited),base.get(inherited));
assert.deepEqual(windows.get(texture),png);
});
test('every official Dunemark piece has a complete central-pack model and texture',()=>{
const root=new URL('../network/resource-pack/shared/assets/dunerealms/',import.meta.url);
const ids=[...NOTE_SERIES.map(([value])=>'note_'+value),...COIN_SERIES.map(([id])=>'coin_'+id.replaceAll('-','_')),
'note_0_leto','note_0_house'];
assert.equal(ids.length,21);
for(const id of ids){
const definition=JSON.parse(fs.readFileSync(new URL('items/currency/'+id+'.json',root)));
assert.equal(definition.model.model,'dunerealms:item/currency/'+id);
const model=JSON.parse(fs.readFileSync(new URL('models/item/currency/'+id+'.json',root)));
assert.equal(model.textures.layer0,'dunerealms:item/currency/'+id);
const png=fs.readFileSync(new URL('textures/item/currency/'+id+'.png',root));
assert.equal(png.subarray(0,8).toString('hex'),'89504e470d0a1a0a');
assert.equal(png.readUInt32BE(16),64);
assert.equal(png.readUInt32BE(20),64);
}
});
test('item-card atlas cells and the runtime glyph index agree',()=>{
const assets=new URL('../network/resource-pack/shared/assets/dunerealms/',import.meta.url);
const providers=JSON.parse(fs.readFileSync(new URL('font/card_art.json',assets))).providers;
const index=JSON.parse(fs.readFileSync(new URL('../JarvisConnector/src/main/resources/item-card-glyphs.json',import.meta.url)));
assert.equal(index.generic,0xe000);
const codepoints=new Set();
for(const {file,chars,height} of providers){
assert.equal(height,24);
const png=fs.readFileSync(new URL('textures/'+file.split(':')[1],assets));
assert.equal(png.readUInt32BE(16),480*chars[0].length);
assert.equal(png.readUInt32BE(20),72*chars.length);
for(const glyph of chars.join(''))if(glyph!=='\0'){
const point=glyph.codePointAt(0);
assert(!codepoints.has(point));
codepoints.add(point);
}
}
assert.deepEqual(new Set(Object.values(index)),codepoints);
assert(index['dunerealms:spy_module']>=0xe100);
assert(index['minecraft:diamond_shovel/mythic']>=0xe100);
const approved=fs.readFileSync(new URL('../docs/design/item-card/banner-art-source.png',import.meta.url));
assert.deepEqual(fs.readFileSync(new URL('../website/web/assets/item-card-banner.png',import.meta.url)),approved);
});