import test from 'node:test'; import assert from 'node:assert/strict'; import { readExportFile } from '../src/transfer/files.js'; test('large media uses bounded streaming with hash verification and renewed authority', async () => { const bytes=new Uint8Array(16*1024*1024+1);bytes[bytes.length-1]=42; const hash=Array.from(new Uint8Array(await crypto.subtle.digest('SHA-256',bytes)),v=>v.toString(16).padStart(2,'0')).join(''); const path='/project.isap/assets/recording.wav'; const expected={id:'recording',revision:2,hash,size:bytes.length}; const link={status:'ok',source:'dashboard',authoritative:true,path,fileId:expected.id,syncRevision:2,contentHash:hash,fileSize:bytes.length,downloadUrl:'https://downloads.example/one-use'}; const controller=new AbortController(); const session={signal:controller.signal,assertCurrent(){this.signal.throwIfAborted();}}; const original=globalThis.fetch;let requests=0,mode='ok'; const bridge={async invoke(command,args){assert.equal(command,'filesystem_get_download_link_file');assert.equal(args.path,path);requests++;return mode==='revoked'&&requests===2?{status:'unavailable',authoritative:false}:link;}}; globalThis.fetch=async(url,options)=> { assert.equal(url.href,link.downloadUrl);assert.equal(options.signal,session.signal);assert.equal(options.credentials,'omit'); return new Response(new ReadableStream({start(stream){stream.enqueue(mode==='corrupt'?new Uint8Array(bytes.length):bytes);stream.close();}})); }; try { assert.deepEqual(await readExportFile(session,bridge,{workspaceId:'workspace',groupId:'group'},{path},expected,bytes.length),bytes); assert.equal(requests,2); mode='corrupt';requests=0; await assert.rejects(readExportFile(session,bridge,{}, {path},expected,bytes.length),/hash mismatch/); assert.equal(requests,1); mode='revoked';requests=0; await assert.rejects(readExportFile(session,bridge,{}, {path},expected,bytes.length),/unavailable/); await assert.rejects(readExportFile(session,bridge,{}, {path},expected,bytes.length-1),/transfer limit/); } finally {globalThis.fetch=original;} });