MC/tools/build-planet-texture-packs.test.mjs
2026-11-27 23:43:05 +00:00

64 lines
4.2 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} from './build-planet-texture-packs.mjs';
import {sandPalettes} from './flat-sand.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});}
});