import test from 'node:test'; import assert from 'node:assert/strict'; import {createHash} from 'node:crypto'; import {downloadLegacyPackage} from '../src/core/legacy-package-download.ts'; import {productionAppSigner,getOfficialPackage,officialAppInternalOrigin} from '../src/core/official-app-update.ts'; const bytes=Buffer.alloc(600000,7),hash=createHash('sha256').update(bytes).digest('hex'); const release={environment:'prod',packageName:'cn.cmvr.wms',versionCode:15,versionName:'1.0.3',sha256:hash,size:bytes.length,signerSha256:productionAppSigner,downloadPath:`/static/mobile/cmvr-wms-prod-v15-${hash}.apk`,notes:[]}; function files(initial){let content=initial;const calls=[];return {calls,cacheDirectory:'file:///cache/',async getInfoAsync(){return {exists:!!content,size:content?.length};},async deleteAsync(){content=undefined;},async readAsStringAsync(uri,options){return content.subarray(options.position,options.position+options.length).toString('base64');},createDownloadResumable(url,uri,options,progress){calls.push(url);return {async downloadAsync(){content=bytes;progress({totalBytesWritten:bytes.length});return {status:200,uri};},async pauseAsync(){}};}};} test('legacy updates download inside the app using the selected company network and verify all bytes',async()=>{ const fs=files(),events=[],uri=await downloadLegacyPackage(release,officialAppInternalOrigin,fs,new AbortController().signal,(...event)=>events.push(event)); assert.equal(fs.calls[0],officialAppInternalOrigin+release.downloadPath);assert.match(uri,/^file:\/\/\/cache\/.*\.apk$/);assert.deepEqual(events.at(-1),['verifying',100]); await downloadLegacyPackage(release,officialAppInternalOrigin,fs,new AbortController().signal,()=>{});assert.equal(fs.calls.length,1); }); test('corrupt or cancelled compatibility downloads never return an installable file',async()=>{ await assert.rejects(downloadLegacyPackage(release,officialAppInternalOrigin,files(Buffer.alloc(bytes.length,8)),new AbortController().signal,()=>{}),/校验失败/); const controller=new AbortController();controller.abort();const fs=files();await assert.rejects(downloadLegacyPackage(release,officialAppInternalOrigin,fs,controller.signal,()=>{}),/取消/);assert.equal(fs.calls.length,0); await assert.rejects(downloadLegacyPackage(release,'https://other.example',files(),new AbortController().signal,()=>{}),/地址无效/); }); test('official manifest requests can use the same internal connection without external Internet access',async()=>{ let requested;await getOfficialPackage(new AbortController().signal,undefined,async url=>{requested=url;return new Response(JSON.stringify(release));},officialAppInternalOrigin);assert.ok(requested.startsWith(officialAppInternalOrigin+'/static/mobile/')); });