格式化:Prettier统一代码风格(仅格式,无逻辑变化)
仅为 npm run format 的输出;黄金对照、旧库兼容、浏览器流程与单元测试全部通过且零差异。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3743d687e7
commit
28c64814f0
221 files changed
+116943
-26708
No files matched your search
@@ -1,116 +1,443 @@
|
||||
'use strict';
|
||||
const {test}=require('node:test'),assert=require('node:assert/strict'),fs=require('node:fs'),vm=require('node:vm'),{randomUUID}=require('node:crypto');
|
||||
const Calc=require('../public/calculations'),MaterialPools=require('../material-pools'),{Ledger}=require('../ledger');
|
||||
const D='2026-09-28',OTHER='2026-09-27',SALE='2026-09-29',actor={id:'preview-owner',name:'合成麦麦',role:'owner'};
|
||||
const usage=(sequence,mg,orderId='other-order',lineIndex=0,active=1)=>({fifo_sequence:sequence,created:'2026-09-29T00:00:00.000Z',mg,order_id:orderId,line_index:lineIndex,active});
|
||||
const pool=(extra={})=>({id:D,date:D,totalMg:15000,totalAmount:1360000,usedMg:0,remainingMg:15000,usages:[],...extra});
|
||||
const entries=quantities=>quantities.map((mg,lineIndex)=>({lineIndex,mg}));
|
||||
const line=(quantity='1',id=D)=>({name:'足金999',unit:'g',quantity,pocket:'',payer:'owner',sale:'1200',costParts:{material:'',labor:'0',accessories:'0',other:'0'},costMaterial:{mode:'pool',poolId:id}});
|
||||
function fixture(t){
|
||||
const l=new Ledger(':memory:');t.after(()=>l.close());
|
||||
const run=(input,fn)=>l.transact(randomUUID(),actor,input,fn);
|
||||
const buy=(grams='15',total='13600',date=D)=>{const input={date,grams,total,payer:'owner',note:'合成料款'};return run(input,()=>l.createMaterialPurchase(input,actor));};
|
||||
const issue=lines=>{const input={date:SALE,customer:'合成客户',salesperson:'合成开单人',payment_account:'助理微信',fee_items:[],lines};return run(input,()=>l.issueOrder(input,actor)).id;};
|
||||
const edit=(id,lines)=>{const info=l.orderEdit(id,actor),input={...info.inputs,lines,revision:info.revision,reason:'合成原单更正',verified:true};return run(input,()=>l.editOrder(id,input,actor));};
|
||||
return {l,buy,issue,edit};
|
||||
const { test } = require('node:test'),
|
||||
assert = require('node:assert/strict'),
|
||||
fs = require('node:fs'),
|
||||
vm = require('node:vm'),
|
||||
{ randomUUID } = require('node:crypto');
|
||||
const Calc = require('../public/calculations'),
|
||||
MaterialPools = require('../material-pools'),
|
||||
{ Ledger } = require('../ledger');
|
||||
const D = '2026-09-28',
|
||||
OTHER = '2026-09-27',
|
||||
SALE = '2026-09-29',
|
||||
actor = { id: 'preview-owner', name: '合成麦麦', role: 'owner' };
|
||||
const usage = (sequence, mg, orderId = 'other-order', lineIndex = 0, active = 1) => ({
|
||||
fifo_sequence: sequence,
|
||||
created: '2026-09-29T00:00:00.000Z',
|
||||
mg,
|
||||
order_id: orderId,
|
||||
line_index: lineIndex,
|
||||
active,
|
||||
});
|
||||
const pool = (extra = {}) => ({
|
||||
id: D,
|
||||
date: D,
|
||||
totalMg: 15000,
|
||||
totalAmount: 1360000,
|
||||
usedMg: 0,
|
||||
remainingMg: 15000,
|
||||
usages: [],
|
||||
...extra,
|
||||
});
|
||||
const entries = quantities => quantities.map((mg, lineIndex) => ({ lineIndex, mg }));
|
||||
const line = (quantity = '1', id = D) => ({
|
||||
name: '足金999',
|
||||
unit: 'g',
|
||||
quantity,
|
||||
pocket: '',
|
||||
payer: 'owner',
|
||||
sale: '1200',
|
||||
costParts: { material: '', labor: '0', accessories: '0', other: '0' },
|
||||
costMaterial: { mode: 'pool', poolId: id },
|
||||
});
|
||||
function fixture(t) {
|
||||
const l = new Ledger(':memory:');
|
||||
t.after(() => l.close());
|
||||
const run = (input, fn) => l.transact(randomUUID(), actor, input, fn);
|
||||
const buy = (grams = '15', total = '13600', date = D) => {
|
||||
const input = { date, grams, total, payer: 'owner', note: '合成料款' };
|
||||
return run(input, () => l.createMaterialPurchase(input, actor));
|
||||
};
|
||||
const issue = lines => {
|
||||
const input = {
|
||||
date: SALE,
|
||||
customer: '合成客户',
|
||||
salesperson: '合成开单人',
|
||||
payment_account: '助理微信',
|
||||
fee_items: [],
|
||||
lines,
|
||||
};
|
||||
return run(input, () => l.issueOrder(input, actor)).id;
|
||||
};
|
||||
const edit = (id, lines) => {
|
||||
const info = l.orderEdit(id, actor),
|
||||
input = { ...info.inputs, lines, revision: info.revision, reason: '合成原单更正', verified: true };
|
||||
return run(input, () => l.editOrder(id, input, actor));
|
||||
};
|
||||
return { l, buy, issue, edit };
|
||||
}
|
||||
// 前端预估与正式服务的 prepareLines 独立核对;全部使用内存账,预估前后不写任何表。
|
||||
function compareServer(f,specs,orderId=null){
|
||||
const pools=f.l.materialPools({from:'2000-01-01',date:SALE}).rows,allUsages=pools.flatMap(value=>value.usages);
|
||||
const lines=specs.map(({mg,poolId=D})=>({name:'足金999',unit:'g',quantity:mg,costParts:{material:0,labor:0,accessories:0,other:0},costMaterial:{mode:'pool',poolId}}));
|
||||
const contents=()=>JSON.stringify(f.l.rows("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name").map(({name})=>[name,f.l.rows('SELECT * FROM "'+name+'"')]));
|
||||
const before=contents(),snapshots=JSON.stringify(pools),expected=MaterialPools.prepareLines(f.l,structuredClone(lines),SALE,{day:value=>value,now:()=> '9999-01-01T00:00:00.000Z',fail:message=>{throw new Error(message);}},{orderId});
|
||||
const actual=Array(specs.length).fill(null);
|
||||
for(const id of new Set(lines.map(value=>value.costMaterial.poolId))){
|
||||
const current=pools.find(value=>value.id===id),candidates=specs.flatMap(({mg,poolId=D},lineIndex)=>poolId===id?[{mg,lineIndex}]:[]);
|
||||
const preview=Calc.poolMaterialPreview(current,candidates,{orderId,allUsages});
|
||||
for(const {lineIndex} of candidates)actual[lineIndex]=preview.amounts[lineIndex];
|
||||
function compareServer(f, specs, orderId = null) {
|
||||
const pools = f.l.materialPools({ from: '2000-01-01', date: SALE }).rows,
|
||||
allUsages = pools.flatMap(value => value.usages);
|
||||
const lines = specs.map(({ mg, poolId = D }) => ({
|
||||
name: '足金999',
|
||||
unit: 'g',
|
||||
quantity: mg,
|
||||
costParts: { material: 0, labor: 0, accessories: 0, other: 0 },
|
||||
costMaterial: { mode: 'pool', poolId },
|
||||
}));
|
||||
const contents = () =>
|
||||
JSON.stringify(
|
||||
f.l
|
||||
.rows("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name")
|
||||
.map(({ name }) => [name, f.l.rows('SELECT * FROM "' + name + '"')]),
|
||||
);
|
||||
const before = contents(),
|
||||
snapshots = JSON.stringify(pools),
|
||||
expected = MaterialPools.prepareLines(
|
||||
f.l,
|
||||
structuredClone(lines),
|
||||
SALE,
|
||||
{
|
||||
day: value => value,
|
||||
now: () => '9999-01-01T00:00:00.000Z',
|
||||
fail: message => {
|
||||
throw new Error(message);
|
||||
},
|
||||
},
|
||||
{ orderId },
|
||||
);
|
||||
const actual = Array(specs.length).fill(null);
|
||||
for (const id of new Set(lines.map(value => value.costMaterial.poolId))) {
|
||||
const current = pools.find(value => value.id === id),
|
||||
candidates = specs.flatMap(({ mg, poolId = D }, lineIndex) => (poolId === id ? [{ mg, lineIndex }] : []));
|
||||
const preview = Calc.poolMaterialPreview(current, candidates, { orderId, allUsages });
|
||||
for (const { lineIndex } of candidates) actual[lineIndex] = preview.amounts[lineIndex];
|
||||
}
|
||||
assert.deepEqual(actual,expected.map(value=>value.materialPool.amount));assert.equal(contents(),before);assert.equal(JSON.stringify(pools),snapshots);
|
||||
assert.deepEqual(
|
||||
actual,
|
||||
expected.map(value => value.materialPool.amount),
|
||||
);
|
||||
assert.equal(contents(), before);
|
||||
assert.equal(JSON.stringify(pools), snapshots);
|
||||
return actual;
|
||||
}
|
||||
test('前端同池多行按累计金额分币,保留既有耗用的尾差起点',()=>{
|
||||
assert.deepEqual(Calc.poolMaterialPreview(pool(),entries([1000,1000])).amounts,{0:90667,1:90666});
|
||||
const current=pool({usages:[usage(1,1000)],usedMg:1000});
|
||||
assert.deepEqual(Calc.poolMaterialPreview(current,entries([1000,1000])).amounts,{0:90666,1:90667});
|
||||
test('前端同池多行按累计金额分币,保留既有耗用的尾差起点', () => {
|
||||
assert.deepEqual(Calc.poolMaterialPreview(pool(), entries([1000, 1000])).amounts, { 0: 90667, 1: 90666 });
|
||||
const current = pool({ usages: [usage(1, 1000)], usedMg: 1000 });
|
||||
assert.deepEqual(Calc.poolMaterialPreview(current, entries([1000, 1000])).amounts, { 0: 90666, 1: 90667 });
|
||||
});
|
||||
test('一分钱料池的三行预估为0/1/0,不能每行独立取整',()=>{
|
||||
const preview=Calc.poolMaterialPreview(pool({totalMg:1000,totalAmount:1}),entries([1,499,500]));
|
||||
assert.deepEqual(preview.amounts,{0:0,1:1,2:0});assert.equal(Object.values(preview.amounts).reduce((sum,value)=>sum+value,0),1);
|
||||
test('一分钱料池的三行预估为0/1/0,不能每行独立取整', () => {
|
||||
const preview = Calc.poolMaterialPreview(pool({ totalMg: 1000, totalAmount: 1 }), entries([1, 499, 500]));
|
||||
assert.deepEqual(preview.amounts, { 0: 0, 1: 1, 2: 0 });
|
||||
assert.equal(
|
||||
Object.values(preview.amounts).reduce((sum, value) => sum + value, 0),
|
||||
1,
|
||||
);
|
||||
});
|
||||
test('编辑从原位置替换整单用量,不能把原单排在后开单之后',()=>{
|
||||
const current=pool({usages:[usage(1,1000,'editing'),usage(2,1000)],usedMg:2000});
|
||||
const preview=Calc.poolMaterialPreview(current,entries([1000]),{orderId:'editing'});
|
||||
assert.deepEqual(preview.amounts,{0:90667});assert.equal(preview.usedMg,1000);assert.equal(preview.wantedMg,1000);
|
||||
test('编辑从原位置替换整单用量,不能把原单排在后开单之后', () => {
|
||||
const current = pool({ usages: [usage(1, 1000, 'editing'), usage(2, 1000)], usedMg: 2000 });
|
||||
const preview = Calc.poolMaterialPreview(current, entries([1000]), { orderId: 'editing' });
|
||||
assert.deepEqual(preview.amounts, { 0: 90667 });
|
||||
assert.equal(preview.usedMg, 1000);
|
||||
assert.equal(preview.wantedMg, 1000);
|
||||
});
|
||||
test('同池整单超量时所有行均待补,空白料池也不能预估成零成本',()=>{
|
||||
const preview=Calc.poolMaterialPreview(pool({totalMg:2000,usages:[usage(1,1)]}),entries([1000,1000]));
|
||||
assert.equal(preview.covered,false);assert.deepEqual(preview.amounts,{0:null,1:null});
|
||||
assert.deepEqual(Calc.poolMaterialPreview(pool({totalMg:0,totalAmount:0}),entries([1])).amounts,{0:null});
|
||||
test('同池整单超量时所有行均待补,空白料池也不能预估成零成本', () => {
|
||||
const preview = Calc.poolMaterialPreview(pool({ totalMg: 2000, usages: [usage(1, 1)] }), entries([1000, 1000]));
|
||||
assert.equal(preview.covered, false);
|
||||
assert.deepEqual(preview.amounts, { 0: null, 1: null });
|
||||
assert.deepEqual(Calc.poolMaterialPreview(pool({ totalMg: 0, totalAmount: 0 }), entries([1])).amounts, { 0: null });
|
||||
});
|
||||
test('缺少稳定耗用顺序或重复商品行时拒绝伪造预估',()=>{
|
||||
assert.throws(()=>Calc.poolMaterialPreview(pool({usages:[{...usage(1,1000),fifo_sequence:undefined}]}),entries([1000])),/刷新料价/);
|
||||
assert.throws(()=>Calc.poolMaterialPreview(pool(),[{lineIndex:0,mg:1},{lineIndex:0,mg:2}]),/商品行无效/);
|
||||
test('缺少稳定耗用顺序或重复商品行时拒绝伪造预估', () => {
|
||||
assert.throws(
|
||||
() =>
|
||||
Calc.poolMaterialPreview(pool({ usages: [{ ...usage(1, 1000), fifo_sequence: undefined }] }), entries([1000])),
|
||||
/刷新料价/,
|
||||
);
|
||||
assert.throws(
|
||||
() =>
|
||||
Calc.poolMaterialPreview(pool(), [
|
||||
{ lineIndex: 0, mg: 1 },
|
||||
{ lineIndex: 0, mg: 2 },
|
||||
]),
|
||||
/商品行无效/,
|
||||
);
|
||||
});
|
||||
test('新增多行和已有先单的前端预估逐行等于后端,预估保持只读',t=>{
|
||||
const f=fixture(t);f.buy();assert.deepEqual(compareServer(f,[{mg:1000},{mg:1000}]),[90667,90666]);
|
||||
f.issue([line()]);assert.deepEqual(compareServer(f,[{mg:1000},{mg:1000}]),[90666,90667]);
|
||||
test('新增多行和已有先单的前端预估逐行等于后端,预估保持只读', t => {
|
||||
const f = fixture(t);
|
||||
f.buy();
|
||||
assert.deepEqual(compareServer(f, [{ mg: 1000 }, { mg: 1000 }]), [90667, 90666]);
|
||||
f.issue([line()]);
|
||||
assert.deepEqual(compareServer(f, [{ mg: 1000 }, { mg: 1000 }]), [90666, 90667]);
|
||||
});
|
||||
test('满池编辑和修改克重保留原usage身份,前端结果与后端一致',t=>{
|
||||
const f=fixture(t);f.buy('3','2720');const id=f.issue([line(),line()]);f.issue([line()]);
|
||||
assert.deepEqual(compareServer(f,[{mg:1000},{mg:1000}],id),[90667,90666]);
|
||||
assert.deepEqual(compareServer(f,[{mg:1500},{mg:500}],id),[136000,45333]);
|
||||
test('满池编辑和修改克重保留原usage身份,前端结果与后端一致', t => {
|
||||
const f = fixture(t);
|
||||
f.buy('3', '2720');
|
||||
const id = f.issue([line(), line()]);
|
||||
f.issue([line()]);
|
||||
assert.deepEqual(compareServer(f, [{ mg: 1000 }, { mg: 1000 }], id), [90667, 90666]);
|
||||
assert.deepEqual(compareServer(f, [{ mg: 1500 }, { mg: 500 }], id), [136000, 45333]);
|
||||
});
|
||||
test('删除中间行按当前line_index复用原位置,后开订单不抢占尾差',t=>{
|
||||
const f=fixture(t);f.buy();const id=f.issue([line(),line('2'),line('3')]);f.issue([line()]);
|
||||
assert.deepEqual(compareServer(f,[{mg:1000},{mg:3000}],id),[90667,272000]);
|
||||
test('删除中间行按当前line_index复用原位置,后开订单不抢占尾差', t => {
|
||||
const f = fixture(t);
|
||||
f.buy();
|
||||
const id = f.issue([line(), line('2'), line('3')]);
|
||||
f.issue([line()]);
|
||||
assert.deepEqual(compareServer(f, [{ mg: 1000 }, { mg: 3000 }], id), [90667, 272000]);
|
||||
});
|
||||
test('同毫秒跨池互换使用全局fifo_sequence,不能用各池数组下标代替',t=>{
|
||||
const f=fixture(t);f.buy();f.buy('15','13600.01',OTHER);const id=f.issue([line(),line('1',OTHER)]);f.issue([line()]);
|
||||
f.l.run('UPDATE material_pool_usages SET created=?','2026-09-29T00:00:00.000Z');
|
||||
assert.deepEqual(compareServer(f,[{mg:1000,poolId:OTHER},{mg:1000,poolId:D}],id),[90667,90667]);
|
||||
test('同毫秒跨池互换使用全局fifo_sequence,不能用各池数组下标代替', t => {
|
||||
const f = fixture(t);
|
||||
f.buy();
|
||||
f.buy('15', '13600.01', OTHER);
|
||||
const id = f.issue([line(), line('1', OTHER)]);
|
||||
f.issue([line()]);
|
||||
f.l.run('UPDATE material_pool_usages SET created=?', '2026-09-29T00:00:00.000Z');
|
||||
assert.deepEqual(
|
||||
compareServer(
|
||||
f,
|
||||
[
|
||||
{ mg: 1000, poolId: OTHER },
|
||||
{ mg: 1000, poolId: D },
|
||||
],
|
||||
id,
|
||||
),
|
||||
[90667, 90667],
|
||||
);
|
||||
});
|
||||
test('重新启用inactive原index保持位置,新index追加到后开单之后',t=>{
|
||||
const f=fixture(t);f.buy('1','0.01');const id=f.issue([line('0.001'),line('0.499')]);f.issue([line('0.250')]);
|
||||
const info=f.l.orderEdit(id,actor);f.edit(id,[info.inputs.lines[0]]);
|
||||
assert.equal(f.l.one('SELECT active FROM material_pool_usages WHERE order_id=? AND line_index=1',id).active,0);
|
||||
assert.deepEqual(compareServer(f,[{mg:1},{mg:499},{mg:250}],id),[0,1,0]);
|
||||
test('重新启用inactive原index保持位置,新index追加到后开单之后', t => {
|
||||
const f = fixture(t);
|
||||
f.buy('1', '0.01');
|
||||
const id = f.issue([line('0.001'), line('0.499')]);
|
||||
f.issue([line('0.250')]);
|
||||
const info = f.l.orderEdit(id, actor);
|
||||
f.edit(id, [info.inputs.lines[0]]);
|
||||
assert.equal(f.l.one('SELECT active FROM material_pool_usages WHERE order_id=? AND line_index=1', id).active, 0);
|
||||
assert.deepEqual(compareServer(f, [{ mg: 1 }, { mg: 499 }, { mg: 250 }], id), [0, 1, 0]);
|
||||
});
|
||||
function formHarness(current,rows,orderId=''){
|
||||
const makeRow=input=>{
|
||||
const controls={dataset:{prefix:'cost-',defaultDate:'false',allowed:'true',preferredSource:'pool',poolId:input.poolId||D,...input.dataset}};
|
||||
const fields={unit:{value:input.unit||'g'},nameType:{value:input.name||'足金999'},quantity:{value:input.quantity},'cost-materialSource':{value:input.source||'pool'},'cost-materialPoolId':{value:input.poolId||D}};
|
||||
return {controls,fields,querySelector(selector){if(selector==='.material-source-controls')return input.legacy?null:controls;return fields[selector.match(/\[name="([^"]+)"\]/)?.[1]]||null;}};
|
||||
function formHarness(current, rows, orderId = '') {
|
||||
const makeRow = input => {
|
||||
const controls = {
|
||||
dataset: {
|
||||
prefix: 'cost-',
|
||||
defaultDate: 'false',
|
||||
allowed: 'true',
|
||||
preferredSource: 'pool',
|
||||
poolId: input.poolId || D,
|
||||
...input.dataset,
|
||||
},
|
||||
};
|
||||
const fields = {
|
||||
unit: { value: input.unit || 'g' },
|
||||
nameType: { value: input.name || '足金999' },
|
||||
quantity: { value: input.quantity },
|
||||
'cost-materialSource': { value: input.source || 'pool' },
|
||||
'cost-materialPoolId': { value: input.poolId || D },
|
||||
};
|
||||
return {
|
||||
controls,
|
||||
fields,
|
||||
querySelector(selector) {
|
||||
if (selector === '.material-source-controls') return input.legacy ? null : controls;
|
||||
return fields[selector.match(/\[name="([^"]+)"\]/)?.[1]] || null;
|
||||
},
|
||||
};
|
||||
};
|
||||
const elements = rows.map(makeRow),
|
||||
member = { value: 'assistant' },
|
||||
form = {
|
||||
querySelectorAll: () => elements,
|
||||
querySelector(selector) {
|
||||
return selector === '[name="date"]'
|
||||
? { value: SALE }
|
||||
: selector === '[name="orderId"]'
|
||||
? { value: orderId }
|
||||
: selector === '[name="member"]'
|
||||
? member
|
||||
: null;
|
||||
},
|
||||
};
|
||||
const context = vm.createContext({
|
||||
LedgerCalc: Calc,
|
||||
teamMemberKeys: () => ['owner', 'assistant', 'assistant2'],
|
||||
setTimeout() {},
|
||||
clearTimeout() {},
|
||||
document: { addEventListener() {} },
|
||||
state: { user: { id: 'preview-user', role: 'owner', member: 'owner' } },
|
||||
currentMember: () => 'owner',
|
||||
$: selector => (selector === '#order-form' ? form : null),
|
||||
__pools: current,
|
||||
__rows: elements,
|
||||
__pool: current[0],
|
||||
__updated: 0,
|
||||
updateTotal() {
|
||||
context.__updated++;
|
||||
},
|
||||
});
|
||||
vm.runInContext(fs.readFileSync(require.resolve('../public/material-pools-ui.js'), 'utf8'), context);
|
||||
vm.runInContext('materialPoolsCache={rows:__pools};materialPoolsCacheUser=state.user.id;', context);
|
||||
return {
|
||||
context,
|
||||
form,
|
||||
member,
|
||||
run: code => vm.runInContext(code, context),
|
||||
rows: elements,
|
||||
preview: index => vm.runInContext('materialPoolFormPreview(__pool,__rows[' + index + '])', context),
|
||||
choices: () => vm.runInContext('materialPoolAvailableRows("' + SALE + '","' + OTHER + '")', context),
|
||||
};
|
||||
const elements=rows.map(makeRow),member={value:'assistant'},form={querySelectorAll:()=>elements,querySelector(selector){return selector==='[name="date"]'?{value:SALE}:selector==='[name="orderId"]'?{value:orderId}:selector==='[name="member"]'?member:null;}};
|
||||
const context=vm.createContext({LedgerCalc:Calc,teamMemberKeys:()=>['owner','assistant','assistant2'],setTimeout(){},clearTimeout(){},document:{addEventListener(){}},state:{user:{id:'preview-user',role:'owner',member:'owner'}},currentMember:()=> 'owner',$:selector=>selector==='#order-form'?form:null,__pools:current,__rows:elements,__pool:current[0],__updated:0,updateTotal(){context.__updated++;}});
|
||||
vm.runInContext(fs.readFileSync(require.resolve('../public/material-pools-ui.js'),'utf8'),context);
|
||||
vm.runInContext("materialPoolsCache={rows:__pools};materialPoolsCacheUser=state.user.id;",context);
|
||||
return {context,form,member,run:code=>vm.runInContext(code,context),rows:elements,preview:index=>vm.runInContext('materialPoolFormPreview(__pool,__rows['+index+'])',context),choices:()=>vm.runInContext('materialPoolAvailableRows("'+SALE+'","'+OTHER+'")',context)};
|
||||
}
|
||||
test('前端从整张表单现值计算,改前行或切手填会立即改变后行尾差',()=>{
|
||||
const h=formHarness([pool()],[{quantity:'1'},{quantity:'1'}]);
|
||||
assert.equal(h.preview(0).amount,90667);assert.equal(h.preview(1).amount,90666);
|
||||
h.rows[0].fields.quantity.value='1.001';assert.equal(h.preview(1).amount,90667);
|
||||
h.rows[0].fields['cost-materialSource'].value='manual';assert.equal(h.preview(1).amount,90667);assert.equal(h.preview(1).wantedMg,1000);
|
||||
h.rows[0].fields['cost-materialSource'].value='pool';h.rows[0].fields.quantity.value='14.001';assert.equal(h.preview(0).amount,null);assert.equal(h.preview(1).amount,null);
|
||||
test('前端从整张表单现值计算,改前行或切手填会立即改变后行尾差', () => {
|
||||
const h = formHarness([pool()], [{ quantity: '1' }, { quantity: '1' }]);
|
||||
assert.equal(h.preview(0).amount, 90667);
|
||||
assert.equal(h.preview(1).amount, 90666);
|
||||
h.rows[0].fields.quantity.value = '1.001';
|
||||
assert.equal(h.preview(1).amount, 90667);
|
||||
h.rows[0].fields['cost-materialSource'].value = 'manual';
|
||||
assert.equal(h.preview(1).amount, 90667);
|
||||
assert.equal(h.preview(1).wantedMg, 1000);
|
||||
h.rows[0].fields['cost-materialSource'].value = 'pool';
|
||||
h.rows[0].fields.quantity.value = '14.001';
|
||||
assert.equal(h.preview(0).amount, null);
|
||||
assert.equal(h.preview(1).amount, null);
|
||||
});
|
||||
test('表单保留混合商品行索引,排除手填银饰按件,并用原单号替换耗用',()=>{
|
||||
const current=pool({usages:[usage(1,1000,'editing',1),usage(2,1000)]});
|
||||
const h=formHarness([current],[{quantity:'1',source:'manual'},{quantity:'1'},{quantity:'5',name:'足银999'},{quantity:'5',unit:'件'},{quantity:'1',poolId:OTHER}], 'editing');
|
||||
assert.equal(h.preview(1).amount,90667);assert.equal(h.preview(1).usedMg,1000);assert.equal(h.preview(1).wantedMg,1000);
|
||||
h.rows.splice(0,1);assert.equal(h.preview(0).amount,90666);
|
||||
test('表单保留混合商品行索引,排除手填银饰按件,并用原单号替换耗用', () => {
|
||||
const current = pool({ usages: [usage(1, 1000, 'editing', 1), usage(2, 1000)] });
|
||||
const h = formHarness(
|
||||
[current],
|
||||
[
|
||||
{ quantity: '1', source: 'manual' },
|
||||
{ quantity: '1' },
|
||||
{ quantity: '5', name: '足银999' },
|
||||
{ quantity: '5', unit: '件' },
|
||||
{ quantity: '1', poolId: OTHER },
|
||||
],
|
||||
'editing',
|
||||
);
|
||||
assert.equal(h.preview(1).amount, 90667);
|
||||
assert.equal(h.preview(1).usedMg, 1000);
|
||||
assert.equal(h.preview(1).wantedMg, 1000);
|
||||
h.rows.splice(0, 1);
|
||||
assert.equal(h.preview(0).amount, 90666);
|
||||
});
|
||||
test('编辑本单满池时仍可选回原料池,新单和不占用该池的其他订单不开放满池',()=>{
|
||||
const current=pool({remainingMg:0,usedMg:15000,usages:[usage(1,15000,'editing')]});
|
||||
assert.equal(formHarness([current],[{quantity:'1',poolId:OTHER}],'editing').choices().find(value=>value.id===D).availableMg,15000);
|
||||
assert.ok(!formHarness([current],[{quantity:'1',poolId:OTHER}]).choices().some(value=>value.id===D));
|
||||
assert.ok(!formHarness([current],[{quantity:'1',poolId:OTHER}],'another-order').choices().some(value=>value.id===D));
|
||||
test('编辑本单满池时仍可选回原料池,新单和不占用该池的其他订单不开放满池', () => {
|
||||
const current = pool({ remainingMg: 0, usedMg: 15000, usages: [usage(1, 15000, 'editing')] });
|
||||
assert.equal(
|
||||
formHarness([current], [{ quantity: '1', poolId: OTHER }], 'editing')
|
||||
.choices()
|
||||
.find(value => value.id === D).availableMg,
|
||||
15000,
|
||||
);
|
||||
assert.ok(
|
||||
!formHarness([current], [{ quantity: '1', poolId: OTHER }])
|
||||
.choices()
|
||||
.some(value => value.id === D),
|
||||
);
|
||||
assert.ok(
|
||||
!formHarness([current], [{ quantity: '1', poolId: OTHER }], 'another-order')
|
||||
.choices()
|
||||
.some(value => value.id === D),
|
||||
);
|
||||
});
|
||||
function personalQuote(){const own={kind:'personal_recovery',member:'assistant',quantity:1000,amount:85000,allocations:[{payer:'owner',amount:85000}],averageRate:85000},common={kind:'public',member:null,quantity:500,amount:50000,allocations:[{payer:'assistant2',amount:50000}],averageRate:100000};return {date:SALE,member:'assistant',entries:[{lineIndex:0,poolId:D,mg:1500,amount:135000,status:'allocated',allocations:[...own.allocations,...common.allocations],materialSources:[own,common]}],pools:[{poolId:D,revision:'personal-v1',personalRemainingMg:0,personalRemainingAmount:0,publicRemainingMg:1500,publicRemainingAmount:150000,eligibleRemainingMg:1500,before:{personalRemainingMg:1000,personalRemainingAmount:85000,publicRemainingMg:2000,publicRemainingAmount:200000,eligibleRemainingMg:3000}}]};}
|
||||
test('个人回收料普通单与改单按归属走只读quote,两段成本相加而非公共均价乘整重',async()=>{
|
||||
for(const id of ['', 'editing']){const h=formHarness([pool({totalMg:2000,totalAmount:200000,averageRate:100000,remainingMg:2000,personalPools:{assistant:{remainingMg:1000}}})],[{quantity:'1.5'}],id),calls=[];h.context.__quote=personalQuote();h.context.api=async(url,input)=>{calls.push({url,input});return h.context.__quote;};assert.equal(h.preview(0).amount,null);await h.run('loadMaterialQuote()');const preview=h.preview(0);assert.equal(preview.amount,135000);assert.deepEqual(JSON.parse(JSON.stringify(preview.materialSources.map(source=>[source.kind,source.quantity,source.amount]))),[['personal_recovery',1000,85000],['public',500,50000]]);assert.equal(calls[0].url,'/api/material-pools/quote');assert.equal(calls[0].input.member,'assistant');assert.equal(calls[0].input.orderId,id||undefined);assert.equal(preview.balance.personalRemainingMg,0);assert.equal(preview.balance.publicRemainingMg,1500);assert.equal(h.choices().find(value=>value.id===D).availableMg,3000);assert.equal(h.context.__updated,1);}
|
||||
function personalQuote() {
|
||||
const own = {
|
||||
kind: 'personal_recovery',
|
||||
member: 'assistant',
|
||||
quantity: 1000,
|
||||
amount: 85000,
|
||||
allocations: [{ payer: 'owner', amount: 85000 }],
|
||||
averageRate: 85000,
|
||||
},
|
||||
common = {
|
||||
kind: 'public',
|
||||
member: null,
|
||||
quantity: 500,
|
||||
amount: 50000,
|
||||
allocations: [{ payer: 'assistant2', amount: 50000 }],
|
||||
averageRate: 100000,
|
||||
};
|
||||
return {
|
||||
date: SALE,
|
||||
member: 'assistant',
|
||||
entries: [
|
||||
{
|
||||
lineIndex: 0,
|
||||
poolId: D,
|
||||
mg: 1500,
|
||||
amount: 135000,
|
||||
status: 'allocated',
|
||||
allocations: [...own.allocations, ...common.allocations],
|
||||
materialSources: [own, common],
|
||||
},
|
||||
],
|
||||
pools: [
|
||||
{
|
||||
poolId: D,
|
||||
revision: 'personal-v1',
|
||||
personalRemainingMg: 0,
|
||||
personalRemainingAmount: 0,
|
||||
publicRemainingMg: 1500,
|
||||
publicRemainingAmount: 150000,
|
||||
eligibleRemainingMg: 1500,
|
||||
before: {
|
||||
personalRemainingMg: 1000,
|
||||
personalRemainingAmount: 85000,
|
||||
publicRemainingMg: 2000,
|
||||
publicRemainingAmount: 200000,
|
||||
eligibleRemainingMg: 3000,
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
test('个人回收料普通单与改单按归属走只读quote,两段成本相加而非公共均价乘整重', async () => {
|
||||
for (const id of ['', 'editing']) {
|
||||
const h = formHarness(
|
||||
[
|
||||
pool({
|
||||
totalMg: 2000,
|
||||
totalAmount: 200000,
|
||||
averageRate: 100000,
|
||||
remainingMg: 2000,
|
||||
personalPools: { assistant: { remainingMg: 1000 } },
|
||||
}),
|
||||
],
|
||||
[{ quantity: '1.5' }],
|
||||
id,
|
||||
),
|
||||
calls = [];
|
||||
h.context.__quote = personalQuote();
|
||||
h.context.api = async (url, input) => {
|
||||
calls.push({ url, input });
|
||||
return h.context.__quote;
|
||||
};
|
||||
assert.equal(h.preview(0).amount, null);
|
||||
await h.run('loadMaterialQuote()');
|
||||
const preview = h.preview(0);
|
||||
assert.equal(preview.amount, 135000);
|
||||
assert.deepEqual(
|
||||
JSON.parse(JSON.stringify(preview.materialSources.map(source => [source.kind, source.quantity, source.amount]))),
|
||||
[
|
||||
['personal_recovery', 1000, 85000],
|
||||
['public', 500, 50000],
|
||||
],
|
||||
);
|
||||
assert.equal(calls[0].url, '/api/material-pools/quote');
|
||||
assert.equal(calls[0].input.member, 'assistant');
|
||||
assert.equal(calls[0].input.orderId, id || undefined);
|
||||
assert.equal(preview.balance.personalRemainingMg, 0);
|
||||
assert.equal(preview.balance.publicRemainingMg, 1500);
|
||||
assert.equal(h.choices().find(value => value.id === D).availableMg, 3000);
|
||||
assert.equal(h.context.__updated, 1);
|
||||
}
|
||||
});
|
||||
test('个人池读数按当前成员隔离,换归属或离开后迟到quote不覆盖当前预览',async()=>{
|
||||
const h=formHarness([pool({personalPools:{assistant:{remainingMg:1000}}})],[{quantity:'1.5'}]);let resolve;h.context.api=()=>new Promise(yes=>{resolve=yes;});h.preview(0);const read=h.run('loadMaterialQuote()');h.member.value='assistant2';assert.equal(h.preview(0).amount,null);resolve(personalQuote());await read;assert.equal(h.context.__updated,0);assert.equal(h.preview(0).quote.result,null);h.context.__bad=personalQuote();h.context.__bad.entries[0].materialSources[0].member='assistant2';h.member.value='assistant';h.context.__input={date:SALE,member:'assistant',entries:[{lineIndex:0,poolId:D,mg:1500}]};assert.throws(()=>h.run('validateMaterialQuote(__bad,__input)'),/个人回收料/);
|
||||
test('个人池读数按当前成员隔离,换归属或离开后迟到quote不覆盖当前预览', async () => {
|
||||
const h = formHarness([pool({ personalPools: { assistant: { remainingMg: 1000 } } })], [{ quantity: '1.5' }]);
|
||||
let resolve;
|
||||
h.context.api = () =>
|
||||
new Promise(yes => {
|
||||
resolve = yes;
|
||||
});
|
||||
h.preview(0);
|
||||
const read = h.run('loadMaterialQuote()');
|
||||
h.member.value = 'assistant2';
|
||||
assert.equal(h.preview(0).amount, null);
|
||||
resolve(personalQuote());
|
||||
await read;
|
||||
assert.equal(h.context.__updated, 0);
|
||||
assert.equal(h.preview(0).quote.result, null);
|
||||
h.context.__bad = personalQuote();
|
||||
h.context.__bad.entries[0].materialSources[0].member = 'assistant2';
|
||||
h.member.value = 'assistant';
|
||||
h.context.__input = { date: SALE, member: 'assistant', entries: [{ lineIndex: 0, poolId: D, mg: 1500 }] };
|
||||
assert.throws(() => h.run('validateMaterialQuote(__bad,__input)'), /个人回收料/);
|
||||
});
|
||||
Reference in new issue
Block a user