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

49 lines
5.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import test from 'node:test';
import assert from 'node:assert/strict';
import {readFile} from 'node:fs/promises';
import {createRequire} from 'node:module';
import vm from 'node:vm';
import {modules} from '../src/core/modules.ts';
import {allows} from '../src/core/network.ts';
const require=createRequire(import.meta.url),React=require('react'),ts=require('typescript');
async function screen({mode='arrival',failure=false}={}){
const source=await readFile(new URL('../src/screens/ArrivalOperation.tsx',import.meta.url),'utf8');
const compiled=ts.transpileModule(source,{compilerOptions:{module:ts.ModuleKind.CommonJS,jsx:ts.JsxEmit.ReactJSX,esModuleInterop:true}}).outputText;
const values=[],refs=[],calls=[];let cursor=0,refCursor=0,done=0;
const record={id:101,arrivalNo:'DH-101',arrivalQuantity:3,acceptanceStatus:0,label:'DH-101 · 3 个'};
const react={...React,useState:initial=>{const i=cursor++;if(!(i in values))values[i]=initial;return[values[i],v=>values[i]=typeof v==='function'?v(values[i]):v];},useRef:initial=>{const i=refCursor++;return refs[i]??= {current:initial};},useEffect:()=>{}};
const native={Modal:'Modal',ScrollView:'ScrollView',View:'View'};
const ui={useTheme:()=>({bg:'#fff'}),s:{},Button:'Button',Card:'Card',Choice:'Choice',Field:'Field',Heading:'Heading',Note:'Note'};
const imports={react,'react/jsx-runtime':require('react/jsx-runtime'),'react-native':native,'react-native-safe-area-context':{SafeAreaView:'SafeAreaView'},'expo-crypto':{randomUUID:()=> 'stable-test-key'},'../ui':ui,'../core/modules':{display:v=>v??'—',localDate:()=> '2026-10-19',validDate:v=>v==='2026-10-19'},'../dialogs':{Dialog:{alert(){}}}};
const module={exports:{}};vm.runInNewContext(compiled,{module,exports:module.exports,require:name=>imports[name]||require(name),AbortController});
const api={async request(path,options){calls.push({path,...options});if(failure)throw Error('timeout');return {data:{}};}};
const render=()=>{cursor=0;refCursor=0;return module.exports.ArrivalOperation({api,row:{id:10,materialName:'电机',remainingQuantity:2,unit:'个'},mode,close(){},done(){done++;}});};
let tree=render();if(mode==='accept'){values[3]=[record];values[4]=record;values[5]=false;tree=render();}
function find(type,prop,value,node=tree){if(!node||typeof node!=='object')return null;if(node.type===type&&node.props[prop]===value)return node;for(const child of React.Children.toArray(node.props?.children)){const match=find(type,prop,value,child);if(match)return match;}return null;}
return {calls,get done(){return done;},render(){tree=render();},button:title=>find('Button','title',title),field:label=>find('Field','label',label)};
}
const settle=()=>new Promise(resolve=>setImmediate(resolve));
test('five field operations are distinct entries; read-only accounts do not gain write entries',()=>{
for(const key of ['arrival','pending','use','move','count'])assert.ok(modules.some(m=>m.key===key));
const permissions=['warehouse:stock:list'];
assert.ok(modules.find(m=>m.key==='stock').permissions.every(p=>allows(permissions,p)));
for(const key of ['arrival','use','move','count'])assert.equal(modules.find(m=>m.key===key).permissions.every(p=>allows(permissions,p)),false);
});
test('registering arrival previews first, then submits one stable request through the existing API',async()=>{
const s=await screen();assert.equal(s.calls.length,0);s.button('本次全部到齐').props.onPress();s.render();assert.equal(s.field('本次到货数量').props.value,'2');s.button('核对预览').props.onPress();s.render();assert.equal(s.calls.length,0);s.button('确认提交').props.onPress();await settle();
assert.equal(s.calls.length,1);assert.equal(s.calls[0].path,'/warehouse/purchase/arrival');assert.equal(s.calls[0].body.requestKey,'stable-test-key');assert.equal(s.calls[0].body.arrivalQuantity,2);assert.equal(s.done,1);
});
test('arrival quantity above the remaining amount cannot reach confirmation',async()=>{
const s=await screen();s.field('本次到货数量').props.onChange('3');s.render();s.button('核对预览').props.onPress();s.render();assert.equal(s.button('确认提交'),null);assert.equal(s.calls.length,0);
});
test('arrival retry preserves the original idempotency key and body after a lost response',async()=>{
const s=await screen({failure:true});s.button('本次全部到齐').props.onPress();s.render();s.button('核对预览').props.onPress();s.render();s.button('确认提交').props.onPress();await settle();s.render();assert.equal(s.button('返回修改').props.disabled,true);s.button('核对后重试原请求').props.onPress();await settle();assert.equal(s.calls.length,2);assert.deepEqual(s.calls[0].body,s.calls[1].body);
});
test('acceptance sends defective quantity against the selected arrival, never registers another arrival',async()=>{
const s=await screen({mode:'accept'});s.field('不合格数量(0 表示全部合格)').props.onChange('1');s.render();s.button('核对预览').props.onPress();s.render();assert.equal(s.calls.length,0);s.button('确认提交').props.onPress();await settle();assert.equal(s.calls[0].path,'/warehouse/purchase/arrival/101/accept');assert.equal(s.calls[0].method,'PUT');assert.equal(s.calls[0].body.defectiveQuantity,1);assert.equal(s.calls[0].body.acceptanceStatus,1);
});
test('uncertain acceptance cannot be blindly resubmitted or edited',async()=>{
const s=await screen({mode:'accept',failure:true});s.button('核对预览').props.onPress();s.render();s.button('确认提交').props.onPress();await settle();s.render();assert.equal(s.button('核对后重试原请求').props.disabled,true);assert.equal(s.button('返回修改').props.disabled,true);assert.equal(s.calls.length,1);
});