cmvr-wms/mobile-app/tests/server-address.test.mjs
lixiaolong cd86623061 feat(app): 添加服务器连接管理和到货操作功能
- 集成 ServerConnection 组件用于开发环境服务器切换
- 实现多服务器令牌隔离存储机制
- 添加 ArrivalOperation 屏幕处理到货和验收流程
- 支持待登记到货和待验收状态切换
- 优化库存操作界面的字段显示逻辑
- 增强 Excel 文件分片上传支持产品文件识别
- 添加 AI 助手会话滚动跟踪功能
- 实现开发服务器地址持久化存储
- 优化登录界面集成服务器连接组件
- 添加领用出库功能支持项目物资使用
- 改进盘点确认生效的操作流程
- 增强采购到货登记和验收的权限控制
- 优化移动端文件上传的断点续传机制
- 添加服务器切换时的数据清理保护机制
- 实现紧凑金额显示模式减少界面冗余
- 增强库存操作的数量验证和错误提示
- 优化 AI 助手的会话状态同步机制
2026-10-20 10:04:17 +08:00

47 lines
3.9 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');
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\/\*\*"/);
});