cmvr-wms/mobile-app/tests/field-operations.test.mjs
lixiaolong d37b4ff379 refactor(mobile): 重构移动端应用结构和配置
- 将 Account 组件中的 Text 导入移动到顶部
- 移除 App.tsx 中的 expo-updates 相关代码
- 添加 PackageUpdates 相关组件和 hooks
- 更新应用名称从 "CMVR 物资管理" 为 "车研物资"
- 添加应用图标和自适应图标配置
- 更新 Android 权限配置,添加 REQUEST_INSTALL_PACKAGES 权限
- 修改堆栈导航数据结构,支持路由参数传递
- 添加阅读尺寸偏好设置功能
- 重构登录界面为独立组件 LoginView
- 添加文件库界面组件 FileLibrary
- 更新状态栏样式逻辑
- 调整 Tab 栏样式和尺寸
- 集成包更新和应用更新对话框
- 修复导入语句顺序和类型引用
- 优化业务列表组件的路由参数处理
- 添加作用域参数用于 API 请求区分
2026-10-28 17:30:35 +08:00

50 lines
6.0 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';
import * as dateDisplay from '../src/core/date-display.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',KeyboardAvoidingView:'KeyboardAvoidingView',Platform:{OS:'android'}};
const ui={useTheme:()=>({bg:'#fff'}),s:{},Button:'Button',Card:'Card',Choice:'Choice',DateField:'DateField',Field:'Field',Heading:'Heading',Note:'Note'};
const imports={'./ArrivalPhotos':{ArrivalPhotos:'ArrivalPhotos'},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/date-display':dateDisplay,'../core/modules':{display:v=>v??'—',localDate:()=> '2026-10-19',validDate:v=>v==='2026-10-19'},'../dialogs':{Dialog:{alert(){}}},'../hooks/useOperationDraft':{useOperationDraft:()=>({ready:true,restored:false,error:'',saveNow:async()=>{},clear:async()=>{}})}};
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);
});