cmvr-wms/mobile-app/tests/ota.test.mjs
lixiaolong 149bde836f feat(assistant): 扩展业务助手功能支持更多文档类型和业务场景
- 新增 PDF 文档支持,包括格式校验和完整性检查
- 扩展 Word、TXT 和 ZIP 格式附件处理能力
- 添加 Excel 附件可选择仅作为业务文件不解析行数据
- 实现业务助手表格布局配置新增库存和预算字段
- 集成通知、反馈通知等新的业务助手动作类型
- 完善部门管理包含父级部门和排序字段支持
- 增强业务规则校验涵盖更多系统管理场景
- 优化附件上传限制提示信息更准确
- 添加生成文件下载的安全性校验机制
- 扩展测试覆盖确保各业务动作规则正确性
2026-10-18 08:49:30 +08:00

42 lines
3.9 KiB
JavaScript

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);
});