import test from 'node:test'; import assert from 'node:assert/strict'; import {createReadStream,statSync,readdirSync,readFileSync} from 'node:fs'; 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'); let count=0,prefix='',suffix='',carry=''; const required=new Set(['/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 createReadStream(path,{highWaterMark:65536})){ 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(':')));if(required.has(key))found.add(key);} 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(':')));if(required.has(key))found.add(key);} assert.match(prefix,/^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))?1:0);},0); assert.equal(count,expected(web),'Unexpected number of embedded public assets'); 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 createReadStream(path,{highWaterMark:65536})){ 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 createReadStream(path,{highWaterMark:65536})){ 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'); });