2026-10-20 10:04:17 +08:00
|
|
|
|
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:()=>{}};
|
2026-10-21 15:13:39 +08:00
|
|
|
|
const native={Modal:'Modal',ScrollView:'ScrollView',View:'View',KeyboardAvoidingView:'KeyboardAvoidingView',Platform:{OS:'android'}};
|
2026-10-20 10:04:17 +08:00
|
|
|
|
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);
|
|
|
|
|
|
});
|