forked from unimatrix/web
79 lines
5.7 KiB
JavaScript
79 lines
5.7 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import {createReadStream,statSync,readdirSync,readFileSync} from 'node:fs';
|
|
import {recipeCatalogue,recipeIcons} from '../scripts/recipe-catalogue.mjs';
|
|
import {MAIN_MENU} from '../scripts/main-menu.mjs';
|
|
import {fileURLToPath} from 'node:url';
|
|
import {scanAssetChunk,ASSET_ENTRY} from './build-artifact-scan.mjs';
|
|
|
|
// Validate the production bundle without importing hundreds of megabytes into a test worker.
|
|
test('built Worker embeds complete public assets and runtime without loading the bundle',async()=>{
|
|
const path=fileURLToPath(new URL('../dist/server/index.js',import.meta.url));
|
|
assert.ok(statSync(path).size>1000,'Build output is missing or empty');
|
|
const entrySource=readFileSync(path,'utf8'),assetFiles=[...entrySource.matchAll(/import DATA\d+ from ["']\.\/(assets-[a-f0-9]+\.mjs)["']/g)].map(m=>fileURLToPath(new URL('../dist/server/'+m[1],import.meta.url)));async function* builtStream(){for(const file of assetFiles.length?[...assetFiles,path]:[path])yield* createReadStream(file,{highWaterMark:65536});}
|
|
let count=0,prefix='',suffix='',carry='',hasBuildStamp=false;
|
|
const nativeOnly=new Set(['organisation-deed-keys-v1.png','organisation-deeds-clan-tiers-v1.png','organisation-deeds-company-tiers-v1.png','organisation-deeds-protected-v1.png'].map(name=>'/assets/'+name));
|
|
const generatedRecipes=['/recipe-catalogue.json',...recipeIcons(recipeCatalogue()).map(([url])=>url)];
|
|
const required=new Set([...generatedRecipes,'/lets-play.html','/imperial-recipe-book.html','/index.html','/kits.html','/manual-data.json','/assets/item-0.webp','/kits.js','/kits.css']);
|
|
const found=new Set();
|
|
const entry=ASSET_ENTRY;
|
|
for await(const chunk of builtStream()){
|
|
const text=chunk.toString('latin1');
|
|
if(prefix.length<256)prefix+=text.slice(0,256-prefix.length);
|
|
suffix=(suffix+text).slice(-4096);
|
|
const combined=carry+text;
|
|
const scanned=scanAssetChunk(combined),entries=scanned.entries;count+=entries.length;
|
|
for(const match of entries){const key=JSON.parse(match.slice(0,match.indexOf(':')));assert.ok(!nativeOnly.has(key),'CP-only artwork must stay out of the public bundle');if(required.has(key))found.add(key);if(key==='/ci-build.json')hasBuildStamp=true;}
|
|
carry=scanned.carry;
|
|
}
|
|
const remaining=carry.match(entry)||[];count+=remaining.length;
|
|
for(const match of remaining){const key=JSON.parse(match.slice(0,match.indexOf(':')));assert.ok(!nativeOnly.has(key),'CP-only artwork must stay out of the public bundle');if(required.has(key))found.add(key);if(key==='/ci-build.json')hasBuildStamp=true;}
|
|
assert.match(prefix,/^(?:export default |const ASSET_DATA=)\{"\/[^"\n]+":\[/);
|
|
const web=fileURLToPath(new URL('../web/',import.meta.url));
|
|
const expected=(dir)=>readdirSync(dir,{withFileTypes:true}).reduce((n,entry)=>{const name=dir+'/'+entry.name;if(entry.isDirectory())return n+expected(name);return n+(!/^assets\/seals\/[^/]+-provenance\.txt$/.test(name.slice(web.length+1))&&!nativeOnly.has('/'+name.slice(web.length+1))?1:0);},0);
|
|
for(const asset of nativeOnly){assert.ok(statSync(web+asset).size>0);assert.ok(readFileSync(new URL('../control-panel/server.mjs',import.meta.url),'utf8').includes(asset.split('/').pop()),'CP artwork route must retain '+asset);}
|
|
assert.equal(count,expected(web)+generatedRecipes.length+Number(hasBuildStamp),'Unexpected number of embedded public assets');
|
|
if(/^[0-9a-f]{40}$/.test(process.env.GITHUB_SHA||''))assert.ok(hasBuildStamp,'CI bundle must identify the exact publication');
|
|
assert.deepEqual(found,required,'Essential pages or assets missing from built Worker');
|
|
// A bounded rolling window extracts the navigation entry even when it crosses chunks.
|
|
const marker='"/portal-navigation.js":["text/javascript; charset=utf-8","';
|
|
let encoded='',pending='',collecting=false;
|
|
for await(const chunk of builtStream()){
|
|
const text=chunk.toString('latin1');
|
|
if(!collecting){
|
|
const joined=pending+text;
|
|
const start=joined.indexOf(marker);
|
|
if(start<0){pending=joined.slice(-marker.length);continue;}
|
|
collecting=true;pending=joined.slice(start+marker.length);
|
|
}else pending+=text;
|
|
const end=pending.indexOf('"]');
|
|
if(end>=0){encoded=pending.slice(0,end);break;}
|
|
assert.ok(pending.length<2_000_000,'Navigation asset unexpectedly large');
|
|
}
|
|
assert.ok(encoded,'Packaged navigation is missing');
|
|
assert.match(encoded,/^[A-Za-z0-9+/]+={0,2}$/,'Navigation payload is not valid base64');
|
|
const navigation=Buffer.from(encoded,'base64').toString('utf8');
|
|
for(const [name,url] of MAIN_MENU)assert.ok(navigation.includes(url),`Missing ${name} destination in bundled navigation`);
|
|
// Detect stale bundles and accidental modifications to authored kit assets.
|
|
for(const [asset,mime] of [['kits.js','text/javascript; charset=utf-8'],['kits.css','text/css; charset=utf-8']]){
|
|
const token=JSON.stringify('/'+asset)+':['+JSON.stringify(mime)+',"';
|
|
let payload='',tail='',started=false;
|
|
for await(const chunk of builtStream()){
|
|
const data=chunk.toString('latin1');
|
|
if(!started){
|
|
const merged=tail+data;const index=merged.indexOf(token);
|
|
if(index<0){tail=merged.slice(-token.length);continue;}
|
|
started=true;tail=merged.slice(index+token.length);
|
|
}else tail+=data;
|
|
const end=tail.indexOf('"]');
|
|
if(end>=0){payload=tail.slice(0,end);break;}
|
|
assert.ok(tail.length<4_000_000,asset+' exceeds safe inspection limit');
|
|
}
|
|
assert.ok(payload,'Missing packaged '+asset);
|
|
assert.deepEqual(Buffer.from(payload,'base64'),readFileSync(new URL('../web/'+asset,import.meta.url)),'Stale or modified '+asset+' in Worker bundle');
|
|
}
|
|
assert.match(suffix,/export default \{async fetch\(/);
|
|
assert.match(suffix,/Cache-Control/);
|
|
assert.match(suffix,/Page not found/,'Worker must retain 404 handling');
|
|
assert.match(suffix,/Method not allowed/,'Worker must reject unsupported methods');
|
|
});
|

Staff