cmvr-wms/mobile-app/tests/arrival-actions.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

96 lines
6.6 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import {readFileSync} from 'node:fs';
import {createRequire} from 'node:module';
import vm from 'node:vm';
import {arrivalActions} from '../src/core/arrival-actions.ts';
const admin=['*:*:*'];
const ready={id:10,historical:false,purchaseStatus:1,priceLocked:true,finalUnitPrice:12,
purchaseBatchId:2,supplierId:3,remainingQuantity:5,pendingArrivalQuantity:0};
const actions=(changes={},queue='arrival',permissions=admin)=>arrivalActions({...ready,...changes},permissions,queue);
test('a ready purchase exposes registration; zero is a valid locked quote',()=>{
for(const price of [0,'0',12,'12.50']){
const [a]=actions({finalUnitPrice:price});assert.equal(a.enabled,true);assert.equal(a.title,'登记到货');
}
});
test('unconfirmed purchases retain a disabled button and an actionable explanation',()=>{
for(const priceLocked of [false,0,'0','false',null,undefined]){
const [a]=actions({priceLocked});assert.equal(a.enabled,false);assert.equal(a.kind,'arrival');
assert.match(a.reason,/Web.*确认采购.*锁定报价/);
}
});
test('a locked flag alone cannot permit missing or invalid quotes',()=>{
for(const finalUnitPrice of [null,undefined,'',NaN,-1])assert.equal(actions({finalUnitPrice})[0].enabled,false);
});
test('registration requires the batch and its supplier, with a clear explanation',()=>{
for(const changes of [{purchaseBatchId:null},{supplierId:null},{supplierId:''},{purchaseBatchId:0}]){
assert.equal(actions(changes)[0].enabled,false);assert.match(actions(changes)[0].reason,/采购批次和供应商/);
}
});
test('pending arrivals expose acceptance even when all ordered quantity is registered',()=>{
const a=actions({remainingQuantity:0,pendingArrivalQuantity:5});
assert.equal(a[0].enabled,false);assert.match(a[0].reason,/办理验收/);
assert.equal(a[1].kind,'accept');assert.equal(a[1].enabled,true);
assert.equal(actions({remainingQuantity:0,pendingArrivalQuantity:5},'accept')[0].enabled,true);
// Acceptance does not invent a supplier requirement absent from the acceptance service.
assert.equal(actions({supplierId:null,pendingArrivalQuantity:2},'accept')[0].enabled,true);
});
test('pending records still require locked quotes and missing quantities fail safely',()=>{
assert.equal(actions({pendingArrivalQuantity:3,priceLocked:false},'accept')[0].enabled,false);
assert.equal(actions({pendingArrivalQuantity:undefined},'accept')[0].enabled,false);
assert.match(actions({pendingArrivalQuantity:0},'accept')[0].reason,/先登记到货/);
assert.match(actions({remainingQuantity:undefined})[0].reason,/刷新/);
});
test('historical and cancelled purchases can never enter either operation',()=>{
for(const changes of [{historical:true},{historical:1},{historical:'1'},{historical:'true'},{purchaseStatus:5},{purchaseStatus:'5'}]){
for(const a of actions({...changes,pendingArrivalQuantity:2}))assert.equal(a.enabled,false);
}
for(const historical of [false,0,'0','false'])assert.equal(actions({historical})[0].enabled,true);
});
test('both read and write permission are required; read-only accounts cannot operate',()=>{
for(const permissions of [[],['warehouse:purchase:list'],['warehouse:purchase:edit']]){
for(const a of actions({pendingArrivalQuantity:2},'accept',permissions)){
assert.equal(a.enabled,false);assert.match(a.reason,/采购负责人协助/);
}
}
for(const permissions of [['warehouse:purchase:manage'],['warehouse:purchase:list','warehouse:purchase:edit']]){
assert.equal(actions({},'arrival',permissions)[0].enabled,true);
}
});
const require=createRequire(import.meta.url),React=require('react'),ts=require('typescript');
const compiled=ts.transpileModule(readFileSync(new URL('../src/screens/ArrivalActions.tsx',import.meta.url),'utf8'),
{compilerOptions:{target:ts.ScriptTarget.ES2022,module:ts.ModuleKind.CommonJS,jsx:ts.JsxEmit.ReactJSX,esModuleInterop:true}}).outputText;
const imports={react:React,'react/jsx-runtime':require('react/jsx-runtime'),'react-native':{View:'View'},
'../ui':{Button:'Button',Note:'Note',s:{row:{}}},'../core/arrival-actions':{arrivalActions}};
const component={exports:{}};
vm.runInNewContext(compiled,{module:component,exports:component.exports,require:name=>imports[name]||require(name)});
function render(changes={},queue='arrival',onHistory){
const opened=[];
const tree=component.exports.ArrivalActions({row:{...ready,...changes},permissions:admin,queue,onOpen:kind=>opened.push(kind),onHistory});
const nodes=[];
function walk(node){if(!node||typeof node!=='object')return;nodes.push(node);React.Children.toArray(node.props?.children).forEach(walk);}
walk(tree);return {tree,nodes,opened,buttons:nodes.filter(n=>n.type==='Button'),notes:nodes.filter(n=>n.type==='Note')};
}
test('real rendered controls remain visible when unavailable; even direct handler calls cannot open them',()=>{
const s=render({priceLocked:false,pendingArrivalQuantity:3});assert.equal(s.buttons.length,2);
for(const b of s.buttons){assert.equal(b.props.disabled,true);b.props.onPress();}
assert.deepEqual(s.opened,[]);assert.equal(s.notes.length,1);assert.match(s.notes[0].props.children,/确认采购/);
});
test('real rendered controls open the correct form only; they never submit a request',()=>{
const s=render({pendingArrivalQuantity:2});assert.deepEqual(s.buttons.map(b=>b.props.title),['登记到货','验收到货']);
s.buttons.forEach(b=>{assert.equal(b.props.disabled,false);b.props.onPress();});
assert.deepEqual(s.opened,['arrival','accept']);assert.equal(s.notes.length,0);
const accept=render({remainingQuantity:0,pendingArrivalQuantity:2},'accept');
assert.equal(accept.buttons.length,1);assert.equal(accept.buttons[0].props.title,'验收到货');
});
test('history shares the same row as registration and acceptance with short labels and flexible widths',()=>{
let history=0;const s=render({pendingArrivalQuantity:2},'arrival',()=>history++);assert.deepEqual(s.buttons.map(b=>b.props.title),['登记到货','验收到货','到货记录']);
const row=s.nodes.find(n=>n.type==='View'&&n.props.style?.flexDirection==='row');assert.equal(row.props.style.flexWrap,undefined);assert.equal(React.Children.toArray(row.props.children).length,3);assert.equal(s.tree.props.style.width,'100%');
for(const button of s.buttons){assert.equal(button.props.style.flex,1);assert.equal(button.props.style.minWidth,0);assert.equal(button.props.style.paddingHorizontal,8);}
s.buttons[2].props.onPress();assert.equal(history,1);assert.deepEqual(s.opened,[]);
const accept=render({pendingArrivalQuantity:2,remainingQuantity:0},'accept',()=>{});assert.deepEqual(accept.buttons.map(b=>b.props.title),['验收到货','到货记录']);
});