/** PR18 #197: no silently omitted names, bounded workers, real cancellation. */ import {executeTool} from './dsh_tool_probe.mjs'; import assert from 'node:assert/strict'; import {mkdir,mkdtemp,writeFile,readdir,readFile} from 'node:fs/promises'; import {spawnSync} from 'node:child_process'; import {createRequire,syncBuiltinESMExports} from 'node:module'; import path from 'node:path'; import {bootRuntime} from '../integration/dsh/start.mjs'; const output=path.resolve('build/dsh-zip-review-verification');await mkdir(output,{recursive:true}); const root=await mkdtemp(path.join(output,'run-')); const fixture=spawnSync('python',['-c', `import sys,zipfile,struct\nfrom pathlib import Path\np=Path(sys.argv[1])\nwith zipfile.ZipFile(p/'proto.zip','w',zipfile.ZIP_STORED) as z:z.writestr('__proto__','ORIGINAL_CONTENT')\ndata=bytearray((p/'proto.zip').read_bytes());n,e=struct.unpack_from('active--); if(abortOnWorker){const cancel=abortOnWorker;abortOnWorker=undefined;queueMicrotask(cancel);} return worker; }}); syncBuiltinESMExports(); for(const key of ['DSH_API_BASE','DSH_MODEL','DSH_API_KEY','DX_DSH_MODELS'])delete process.env[key]; process.env.DX_DSH_HOME=path.join(root,'home');process.env.DX_DSH_CONTEXT_FILE=path.join(root,'context.json'); await writeFile(process.env.DX_DSH_CONTEXT_FILE,'{"task_id":"zip-review","revision":"zip-review","status":"observe"}'); const {ctx,shutdown}=await bootRuntime({announce:false}); try{ const call=(file,signal=AbortSignal.timeout(30000))=>executeTool(ctx, {name:'extract_zip', arguments:{file_path:path.join(root,file),output_directory:root},callId:crypto.randomUUID(),signal}); const before=await readdir(root); for(const file of ['proto.zip','proto-bad.zip']){ const result=await call(file); assert.equal(result.isError,true,'unsupported __proto__ entry must be explicitly rejected, including corrupt payload'); assert.deepEqual(await readdir(root),before,'rejected archive must not publish an empty success directory'); } peak=0;created=0; const results=await Promise.all(Array.from({length:4},()=>call('many.zip'))); for(const result of results){ assert.equal(result.isError,false,JSON.stringify(result)); assert.equal(result.value.total_entries,20); for(let i=0;i<20;i++)assert.equal((await readFile(path.join(result.value.directory,i+'.txt'))).length,600*1024); } assert.equal(created,4,'each archive must use one worker, with no per-entry worker fan-out'); assert.ok(peak<=2,'ZIP decompression must use at most two workers across concurrent calls, actual '+peak); assert.equal(active,0,'completed calls must release all workers'); const cancel=new AbortController(),prior=await readdir(root); abortOnWorker=()=>cancel.abort(new Error('owned cancellation fixture')); const cancelled=await call('many.zip',cancel.signal).catch(error=>({isError:true,error})); assert.equal(cancelled.isError,true); assert.equal(active,0,'cancelled call must settle only after its worker exits'); assert.deepEqual(await readdir(root),prior,'cancel must not publish a partial directory'); const report={status:'passed',realHarness:true,protoEntryExplicitlyRejected:true,corruptProtoRejected:true, parallelArchives:4,filesPerArchive:20,peakWorkers:peak,cancelledWorkerStopped:true}; await writeFile(path.join(root,'report.json'),JSON.stringify(report,null,2));console.log(JSON.stringify(report)); }finally{threads.Worker=NativeWorker;syncBuiltinESMExports();await shutdown.shutdown(0);}