cmvr-wms/mobile-app/tests/server-address.test.mjs
lixiaolong 95effeeff2 feat(app): 优化移动端应用配置和功能实现
- 添加生产环境服务器地址配置支持内外网切换
- 实现采购列表只看我创建的功能开关
- 更新构建脚本中的默认生产环境API地址
- 优化采购单表格单元格编辑体验改用自动补全组件
- 调整应用更新检查逻辑增强生产环境兼容性
- 修复开发服务器切换时的一些验证问题
- 增强应用启动时的服务地址配置灵活性
2026-10-29 17:27:46 +08:00

49 lines
4.1 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import {readFileSync} from 'node:fs';
import {developmentServer,serverTokenKey,connectionClient,checkDevelopmentServer} from '../src/core/server-address.ts';
const response=value=>new Response(JSON.stringify(value),{headers:{'Content-Type':'application/json'}});
test('development address accepts private IP, port and API prefix with optional scheme',()=>{
assert.equal(developmentServer(' 192.168.28.20:8080 '),'http://192.168.28.20:8080');
assert.equal(developmentServer('192.168.28.20:8088/dev-api/'),'http://192.168.28.20:8088/dev-api');
assert.equal(developmentServer('https://10.0.0.2/api/'),'https://10.0.0.2/api');
assert.equal(developmentServer('127.0.0.1:8080'),'http://127.0.0.1:8080');
for(const value of ['', 'example.com','http://8.8.8.8','192.168.28.1.evil.test','http://user:secret@10.0.0.2','10.0.0.2?token=x','10.0.0.2/#a','file:///tmp','10.0.0.2/%2e%2e/private','10.0.0.2/'+ 'a'.repeat(240)])assert.throws(()=>developmentServer(value));
});
test('saved native credentials are isolated by exact server address and environment',()=>{
assert.notEqual(serverTokenKey('dev','http://10.0.0.1:8080'),serverTokenKey('dev','http://10.0.0.2:8080'));
assert.notEqual(serverTokenKey('dev','http://10.0.0.1/a'),serverTokenKey('dev','http://10.0.0.1/b'));
assert.equal(serverTokenKey('prod','http://192.168.28.10/prod-api'),'cmvr.prod.token.internal');
assert.notEqual(serverTokenKey('prod','http://192.168.28.10/prod-api'),serverTokenKey('prod','https://wms.svc.cmvr-iot.icu/prod-api'));
assert.throws(()=>serverTokenKey('prod','https://evil.example/prod-api'));
assert.match(serverTokenKey('dev','http://10.0.0.1:8080'),/^[a-z\d._-]+$/);
});
test('connection handshake is anonymous, read-only, validates system response and never retries writes',async()=>{
const calls=[];const transport=async(url,options)=>{calls.push({url,options});return response({code:200,captchaEnabled:false});};
assert.equal(await checkDevelopmentServer('10.0.0.2:8080',undefined,transport),'http://10.0.0.2:8080');
assert.equal(calls[0].url,'http://10.0.0.2:8080/captchaImage');assert.equal(calls[0].options.method,'GET');
assert.equal(calls[0].options.headers.Authorization,undefined);assert.equal(calls[0].options.redirect,'error');
await assert.rejects(checkDevelopmentServer('10.0.0.2',undefined,async()=>response({code:200})),/系统登录信息/);
await assert.rejects(checkDevelopmentServer('10.0.0.2',undefined,async()=>{throw Error('offline');}),error=>error.code===503);
});
test('preview explicitly routes to selected service; native never uses a preview routing header',async()=>{
const calls=[];const transport=async(url,options)=>{calls.push({url,options});return response({code:200,captchaEnabled:false});};
await checkDevelopmentServer('192.168.28.20:8080','http://localhost:8091',transport);
assert.equal(calls[0].url,'http://localhost:8091/native-api/captchaImage');
assert.equal(calls[0].options.headers['X-CMVR-Dev-Server'],'http://192.168.28.20:8080');
const api=connectionClient('http://10.0.0.2',()=>{},undefined,transport);await api.request('/getInfo');
assert.equal(calls[1].options.headers['X-CMVR-Dev-Server'],undefined);
});
test('both login and settings expose the editor only for development; old in-flight sessions invalidated',()=>{
const app=readFileSync(new URL('../src/App.tsx',import.meta.url),'utf8');
assert.match(app,/variant==='dev'\?<ServerConnection/);assert.match(app,/<Root key=\{server\}/);
assert.match(app,/<Login[^>]*connection=\{connection\}/);assert.match(app,/<Settings[^>]*connection=\{connection\}/);
assert.match(app,/epoch\.current\+\+;api\.clear\(\)/);assert.match(app,/await checkDevelopmentServer/);
assert.match(app,/await SecureStore\.deleteItemAsync\(serverTokenKey\(variant,checked\)\)/);
const plugin=readFileSync(new URL('../plugins/with-internal-network.cjs',import.meta.url),'utf8');
assert.match(plugin,/env\.variant==='dev'\?'true':'false'/);
const inputs=readFileSync(new URL('../plugins/with-private-build-inputs.cjs',import.meta.url),'utf8');
assert.match(inputs,/sources\.exclude\("keys", "keys\/\*\*"/);
});