import test from 'node:test';import assert from 'node:assert/strict'; import { mkdtemp, mkdir, writeFile, readFile } from 'node:fs/promises';import path from 'node:path';import os from 'node:os'; import { createHash, randomUUID, generateKeyPairSync, verify } from 'node:crypto'; import { createOtaServer } from '../scripts/ota-server.mjs'; test('internal OTA feed isolates dev/prod, platform/runtime and only serves immutable hashed assets',async t=>{ const root=await mkdtemp(path.join(os.tmpdir(),'cmvr-ota-'));const server=createOtaServer({root}); await new Promise(r=>server.listen(0,'127.0.0.1',r));t.after(()=>new Promise(r=>server.close(r))); const base=`http://127.0.0.1:${server.address().port}`,runtime='test-native-1',id=randomUUID(); const {privateKey,publicKey}=generateKeyPairSync('rsa',{modulusLength:2048}); const manifest=JSON.stringify({id,runtimeVersion:runtime,metadata:{environment:'dev'}}); const {sign}=await import('node:crypto');const signature=sign('RSA-SHA256',Buffer.from(manifest),privateKey).toString('base64'); for(const dir of [path.join(root,'dev/android',runtime),path.join(root,'releases',id),path.join(root,'assets')])await mkdir(dir,{recursive:true}); await writeFile(path.join(root,'dev/android',runtime,'current.json'),JSON.stringify({id})); await writeFile(path.join(root,'releases',id,'manifest.json'),manifest);await writeFile(path.join(root,'releases',id,'signature.txt'),`sig="${signature}", keyid="dev"`); const headers={'expo-platform':'android','expo-runtime-version':runtime};const response=await fetch(base+'/updates/dev',{headers}); assert.equal(response.status,200);assert.equal(response.headers.get('expo-protocol-version'),'1');assert.match(response.headers.get('expo-signature'),/keyid="dev"/); assert.equal(verify('RSA-SHA256',Buffer.from(await response.text()),publicKey,Buffer.from(signature,'base64')),true); for(const [channel,runtimeValue] of [['prod',runtime],['dev','other-native']]){ const empty=await fetch(base+`/updates/${channel}`,{headers:{...headers,'expo-runtime-version':runtimeValue}});assert.equal(empty.status,200);assert.match(empty.headers.get('content-type'),/multipart\/mixed/);assert.equal(await empty.text(),'--empty--\r\n'); } assert.equal((await fetch(base+'/updates/dev',{headers:{...headers,'expo-platform':'ios'}})).status,400); assert.equal((await fetch(base+'/updates/dev',{method:'POST'})).status,405); const content=Buffer.from('test-code'),hash=createHash('sha256').update(content).digest('hex');await writeFile(path.join(root,'assets',hash),content);await writeFile(path.join(root,'assets',hash+'.type'),'application/javascript'); const asset=await fetch(base+'/assets/'+hash);assert.equal(await asset.text(),'test-code');assert.match(asset.headers.get('cache-control'),/immutable/); assert.equal((await fetch(base+'/assets/not-a-hash')).status,404); assert.equal((await fetch(base+'/keys/private-key.pem')).status,404); }); test('production service rejects the developer channel, unsupported protocol and invalid configuration',async t=>{ const root=await mkdtemp(path.join(os.tmpdir(),'cmvr-prod-ota-')); assert.throws(()=>createOtaServer({root,environments:['other']}),/allowlist/); const server=createOtaServer({root,environments:['prod']}); await new Promise(resolve=>server.listen(0,'127.0.0.1',resolve));t.after(()=>new Promise(resolve=>server.close(resolve))); const base=`http://127.0.0.1:${server.address().port}`; const headers={'expo-platform':'android','expo-runtime-version':'native-prod'}; assert.equal((await fetch(base+'/updates/dev',{headers})).status,404); assert.equal((await fetch(base+'/updates/prod',{headers:{...headers,'expo-protocol-version':'2'}})).status,406); const empty=await fetch(base+'/updates/prod',{headers});assert.equal(empty.status,200); assert.equal(empty.headers.get('expo-server-defined-headers'),''); assert.equal(await empty.text(),'--empty--\r\n'); assert.equal((await fetch(base+'/health')).status,200); });