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),['验收到货','到货记录']); });