2026-10-25 14:19:24 +08:00
'use strict' ;
const test = require ( 'node:test' ),
assert = require ( 'node:assert/strict' ),
{ randomUUID } = require ( 'node:crypto' );
2026-12-04 13:01:19 -08:00
const { Ledger , today } = require ( '../core/ledger' ),
2026-10-25 14:19:24 +08:00
{ createUser , createApp } = require ( '../server' );
const day = offset => new Date ( Date . parse ( today () + 'T00:00:00Z' ) + offset * 86400000 ). toISOString (). slice ( 0 , 10 );
const D0 = day ( - 3 ),
D1 = day ( - 2 ),
D2 = day ( - 1 ),
D3 = today ();
const amount = ( ledger , pocket = '现货' ) => ledger . inventory (). find ( row => row . pocket === pocket ). mg ;
function fixture ( t ) {
const l = new Ledger ( ':memory:' ),
actors = {};
t . after (() => l . close ());
for ( const member of [ 'owner' , 'assistant' , 'assistant2' ]) {
const role = member === 'owner' ? 'owner' : 'assistant' ,
name = '合成' + member ,
id = createUser ( l , name , role , 'synthetic-inventory-password' , member );
actors [ member ] = { id , name , role , member };
}
l . enableTeamMode ();
const run = ( payload , fn , member = 'owner' , key = randomUUID ()) =>
l . scope ( actors [ member ], () => l . transact ( key , actors [ member ], payload , fn ));
const stock = ( extra = {}, member = 'assistant' ) => {
const input = { kind : 'increase' , date : D0 , pocket : '现货' , grams : '5' , note : '合成手动库存' , ... extra },
result = run ( input , () => l . stock ( input , actors [ member ]), member );
return l . one ( 'SELECT id FROM movements WHERE group_id=? ORDER BY rowid' , result . group ). id ;
};
const detail = ( id , member = 'owner' ) => l . inventoryMovement ( id , actors [ member ]);
const update = ( id , extra = {}, member = 'owner' , key = randomUUID ()) => {
const current = detail ( id , member ),
input = { ... current . values , revision : current . revision , ... extra };
return run ( input , () => l . updateInventoryMovement ( id , input , actors [ member ]), member , key );
};
const remove = ( id , member = 'owner' , key = randomUUID ()) => {
const input = { revision : detail ( id , member ). revision , verified : true };
return run ( input , () => l . deleteInventoryMovement ( id , input , actors [ member ]), member , key );
};
const approve = ( id , extra = {}, key = randomUUID ()) => {
const current = l . inventoryChange ( id , actors . owner ),
input = { revision : current . revision , inventoryRevision : current . inventoryRevision , verified : true , ... extra };
return run ( input , () => l . approveInventoryChange ( id , input , actors . owner ), 'owner' , key );
};
const issue = ( grams = '1' , pocket = '现货' , date = D1 ) => {
const input = {
date ,
member : 'owner' ,
customer : '合成库存验收客户' ,
salesperson : '合成' ,
payment_account : '麦麦微信' ,
fee_items : [],
lines : [{ name : '合成足金' , unit : 'g' , quantity : grams , pocket , payer : 'owner' , cost : '0' , sale : '100' }],
};
return run ( input , () => l . issueOrder ( input , actors . owner )). id ;
};
const all = () =>
JSON . stringify (
2026-12-04 08:58:53 -08:00
l
2026-10-25 14:19:24 +08:00
. rows ( "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name" )
. map (({ name }) => [ name , l . rows ( 'SELECT * FROM "' + name + '" ORDER BY rowid' )]),
);
return { l , actors , run , stock , detail , update , remove , approve , issue , all };
}
test ( '麦麦更正足额入库不被瞬时冲正负数误拦,保原流水及利润并标记历史快照' , t => {
const f = fixture ( t ),
id = f . stock (),
original = f . l . one ( 'SELECT * FROM movements WHERE id=?' , id );
f . issue ( '3' );
f . l . saveInventorySnapshot ( D0 , f . actors . owner );
f . l . saveInventorySnapshot ( D2 , f . actors . owner );
const finance = () =>
JSON . stringify (
[ 'orders' , 'payments' , 'expenses' , 'journal' , 'team_fund_cash' ]. map ( table =>
f . l . rows ( 'SELECT * FROM ' + table + ' ORDER BY rowid' ),
2026-12-04 08:58:53 -08:00
),
),
2026-10-25 14:19:24 +08:00
before = finance ();
const result = f . update ( id , { note : '核对后只改备注' });
assert . equal ( result . applied , true );
assert . equal ( amount ( f . l ), 2000 );
assert . equal ( result . group . revision , 2 );
assert . equal ( result . group . status , 'modified' );
assert . deepEqual ( f . l . one ( 'SELECT * FROM movements WHERE id=?' , id ), original );
assert . equal ( finance (), before );
assert . equal ( f . l . inventorySnapshot ( D0 ). stale , true );
assert . equal ( f . l . inventorySnapshot ( D2 ). stale , true );
const current = result . group . movements [ 0 ];
assert . equal ( current . note , '核对后只改备注' );
assert . equal ( f . l . entityMember ( 'movement' , current . id ), 'assistant' );
const rows = f . l . inventoryMovements ({}, f . actors . owner ). movements ;
assert . equal ( rows . find ( row => row . id === id ). current , false );
assert . equal ( rows . find ( row => row . id === current . id ). canEdit , true );
assert . ok ( rows . filter ( row => row . reversal ). every ( row => ! row . manual && ! row . canDelete ));
assert . equal (
f . l . inventoryMovement ( current . id , f . actors . assistant ). canEdit ,
true ,
'归属及创建权限仍归原助理,不因老板批准而漂移' ,
);
});
2026-12-04 16:18:03 +08:00
test ( '助理跨天修改只生成申请,另一助理不可读取,麦麦批准后才改变库存' , t => {
const f = fixture ( t ),
id = f . stock ({ date : D2 }),
before = f . l . rows ( 'SELECT * FROM movements' ),
key = randomUUID ();
2026-10-25 14:19:24 +08:00
const payload = { ... f . detail ( id , 'assistant' ). values , revision : 1 , grams : '4' },
submit = () => f . run ( payload , () => f . l . updateInventoryMovement ( id , payload , f . actors . assistant ), 'assistant' , key );
const request = submit ();
assert . equal ( request . applied , false );
assert . equal ( request . pending , true );
assert . equal ( request . request . reason , '修改库存记录' );
assert . equal ( request . request . revision , 1 );
assert . deepEqual ( f . l . rows ( 'SELECT * FROM movements' ), before );
assert . equal ( amount ( f . l ), 5000 );
assert . equal ( JSON . stringify ( submit ()), JSON . stringify ( request ));
assert . equal ( f . l . inventoryChanges ({}, f . actors . assistant ). counts . pending , 1 );
assert . equal ( f . l . inventoryChanges ({}, f . actors . assistant2 ). counts . pending , 0 );
assert . throws (
() => f . l . inventoryChange ( request . requestId , f . actors . assistant2 ),
error => error . status === 404 ,
);
assert . throws (
() => f . detail ( id , 'assistant2' ),
error => error . status === 404 ,
);
assert . throws (
() =>
f . run (
{},
() =>
f . l . approveInventoryChange (
request . requestId ,
{ revision : 1 , inventoryRevision : request . request . inventoryRevision , verified : true },
f . actors . assistant ,
),
'assistant' ,
),
error => error . status === 403 ,
);
assert . throws (
() => f . update ( id , { grams : '3' }),
error => error . status === 409 ,
);
assert . throws (
() => f . remove ( id , 'assistant' ),
error => error . status === 409 ,
);
const result = f . approve ( request . requestId );
assert . equal ( result . request . status , 'approved' );
assert . equal ( result . request . revision , 2 );
assert . equal ( amount ( f . l ), 4000 );
assert . equal ( result . group . pending , null );
});
test ( '删除先申请,驳回不改库存;重申请批准后保留原行和删除状态' , t => {
const f = fixture ( t ),
id = f . stock (),
original = f . l . one ( 'SELECT * FROM movements WHERE id=?' , id ),
first = f . remove ( id , 'assistant' );
assert . equal ( amount ( f . l ), 5000 );
const rejected = f . run ({}, () =>
f . l . rejectInventoryChange ( first . requestId , { revision : 1 , note : '合成核对先保留' }, f . actors . owner ),
);
assert . equal ( rejected . request . status , 'rejected' );
assert . equal ( amount ( f . l ), 5000 );
assert . equal ( f . detail ( id ). canDelete , true );
assert . throws (
() => f . approve ( first . requestId ),
error => error . status === 409 ,
);
const second = f . remove ( id , 'assistant' ),
approved = f . approve ( second . requestId );
assert . equal ( amount ( f . l ), 0 );
assert . equal ( approved . group . status , 'deleted' );
assert . equal ( approved . group . canEdit , false );
assert . deepEqual ( f . l . one ( 'SELECT * FROM movements WHERE id=?' , id ), original );
const row = f . l . inventoryMovements ({}, f . actors . assistant ). movements . find ( row => row . id === id );
assert . equal ( row . status , 'deleted' );
assert . equal ( row . current , false );
assert . equal ( row . canDelete , false );
assert . throws (
() => f . remove ( id ),
error => error . status === 409 ,
);
});
test ( '平移双腿同组修改,最终余额有效即可;目标已消耗时删除整组原子失败' , t => {
const f = fixture ( t );
f . stock ({ grams : '10' }, 'owner' );
const id = f . stock ({ kind : 'transfer' , to : '金料' , grams : '5' }),
original = f . detail ( id ). movements ;
f . issue ( '4' , '金料' );
const target = original . find ( row => row . mg > 0 ). id ,
result = f . update ( target , { date : D1 , note : '合成平移整组更正' });
assert . equal ( result . group . movements . length , 2 );
assert . ok ( result . group . movements . every ( row => row . date === D1 && row . note === '合成平移整组更正' ));
assert . equal ( amount ( f . l ), 5000 );
assert . equal ( amount ( f . l , '金料' ), 1000 );
for ( const row of original ) assert . deepEqual ( f . l . one ( 'SELECT * FROM movements WHERE id=?' , row . id ), row );
const before = f . all ();
assert . throws (() => f . remove ( result . group . movements [ 0 ]. id ), /库存不足/ );
assert . equal ( f . all (), before );
const corrected = f . update ( result . group . movements [ 1 ]. id , { grams : '4' });
assert . equal ( amount ( f . l ), 6000 );
assert . equal ( amount ( f . l , '金料' ), 0 );
assert . equal (
corrected . group . movements . reduce (( sum , row ) => sum + row . mg , 0 ),
2026-12-04 08:58:53 -08:00
0 ,
2026-10-25 14:19:24 +08:00
);
});
test ( '改日期不能造成历史负库存,改克重不能挤占已确认订单预留' , t => {
const f = fixture ( t ),
id = f . stock ({ date : D1 , grams : '1' });
f . stock ({ date : D1 , kind : 'decrease' , grams : '1' }, 'owner' );
let before = f . all ();
assert . throws (() => f . update ( id , { date : D2 }), /在.*库存不足/ );
assert . equal ( f . all (), before );
const held = f . stock ({ date : D0 , grams : '5' }),
input = {
date : D1 ,
customer : '合成预留客户' ,
salesperson : '合成' ,
lines : [{ name : '足金' , unit : 'g' , quantity : '5' , pocket : '现货' , payer : 'owner' , cost : '0' , sale : '100' }],
fee_items : [],
};
const order = f . run ( input , () => f . l . saveOrder ( input , f . actors . owner )). id ;
f . run ({ order }, () => f . l . action ( order , 'confirm' , { date : D1 }, f . actors . owner ));
before = f . all ();
assert . throws (() => f . update ( held , { grams : '4' }), /可用库存不足.*占用5.000g/ );
assert . equal ( f . all (), before );
});
test ( '申请后销售仍足额可刷新批准,读取审批后再变化必须重新核对' , t => {
const f = fixture ( t ),
id = f . stock (),
request = f . update ( id , { grams : '4' }, 'assistant' ),
oldToken = request . request . inventoryRevision ;
f . issue ( '3' );
assert . throws (
() => f . approve ( request . requestId , { inventoryRevision : oldToken }),
error => error . status === 409 ,
);
const refreshed = f . l . inventoryChange ( request . requestId , f . actors . owner );
assert . equal ( refreshed . stale , false );
assert . equal ( refreshed . inventoryChanged , true );
assert . equal ( refreshed . canApprove , true );
assert . notEqual ( refreshed . inventoryRevision , oldToken );
assert . equal ( f . approve ( request . requestId ). request . status , 'approved' );
assert . equal ( amount ( f . l ), 1000 );
});
test ( '审批时最新库存不足全回滚,申请保持待批且无额外冲正或审计' , t => {
const f = fixture ( t ),
id = f . stock (),
request = f . update ( id , { grams : '4' }, 'assistant' );
f . issue ( '4.5' );
const before = f . all ();
assert . throws (() => f . approve ( request . requestId ), /库存不足/ );
assert . equal ( f . all (), before );
assert . equal ( amount ( f . l ), 500 );
assert . equal ( f . l . inventoryChange ( request . requestId , f . actors . owner ). status , 'pending' );
});
test ( '目标版本、唯一待批与审批幂等不允许重复更正删除' , t => {
const f = fixture ( t ),
id = f . stock (),
request = f . update ( id , { grams : '4' }, 'assistant' );
assert . throws (
() => f . update ( id , { revision : 0 }, 'assistant' ),
error => error . status === 409 ,
);
assert . throws (
() => f . update ( id , { grams : '3' }, 'assistant' ),
error => error . status === 409 ,
);
const current = f . l . inventoryChange ( request . requestId , f . actors . owner ),
input = { revision : 1 , inventoryRevision : current . inventoryRevision , verified : true },
key = randomUUID (),
call = () => f . run ( input , () => f . l . approveInventoryChange ( request . requestId , input , f . actors . owner ), 'owner' , key );
const result = call (),
rows = f . l . rows ( 'SELECT * FROM movements' );
assert . equal ( JSON . stringify ( call ()), JSON . stringify ( result ));
assert . deepEqual ( f . l . rows ( 'SELECT * FROM movements' ), rows );
assert . throws (
() => f . approve ( request . requestId ),
error => error . status === 409 ,
);
const removeKey = randomUUID (),
removeInput = { revision : 2 , verified : true },
remove = () =>
f . run ( removeInput , () => f . l . deleteInventoryMovement ( id , removeInput , f . actors . owner ), 'owner' , removeKey );
const deleted = remove ();
assert . deepEqual ( remove (), deleted );
assert . equal ( amount ( f . l ), 0 );
});
test ( '仅独立手动组可更正,历史采购成本只留档,未知来源与自动订单流水不开放' , t => {
const f = fixture ( t ),
opening = f . stock ({ kind : 'opening' }, 'owner' );
assert . equal ( f . detail ( opening ). manual , false );
assert . throws (
() => f . update ( opening ),
error => error . status === 403 ,
);
f . issue ( '1' );
const automatic = f . l . one ( 'SELECT id FROM movements WHERE order_id IS NOT NULL' ). id ;
assert . equal ( f . detail ( automatic ). canEdit , false );
assert . throws (
() => f . remove ( automatic ),
error => error . status === 403 ,
);
f . run ({}, () => {
f . l . move ({ date : D0 , pocket : '金料' , mg : 1000 , kind : 'allocation' , note : '合成自动来源' }, f . actors . owner );
return { ok : true };
});
const unknown = f . l . one ( "SELECT id FROM movements WHERE kind='allocation'" ). id ;
assert . equal ( f . detail ( unknown ). manual , false );
const purchase = f . stock ({ kind : 'purchase' , pocket : '找金' , grams : '1' , cost : '800' }, 'assistant2' ),
journal = JSON . stringify ( f . l . rows ( 'SELECT * FROM journal' )),
cash = JSON . stringify ( f . l . rows ( 'SELECT * FROM team_fund_cash' ));
const changed = f . update ( purchase , { note : '更正留档备注' });
assert . equal ( changed . group . movements [ 0 ]. purchase_cost , 80000 );
assert . equal ( JSON . stringify ( f . l . rows ( 'SELECT * FROM journal' )), journal );
assert . equal ( JSON . stringify ( f . l . rows ( 'SELECT * FROM team_fund_cash' )), cash );
assert . throws (
() => f . update ( purchase , {}, 'assistant' ),
error => error . status === 404 ,
);
});
test ( '写入平移新腿失败时流水、版本、申请和快照过期标记一起回滚' , t => {
const f = fixture ( t );
f . stock ({ grams : '10' }, 'owner' );
const id = f . stock ({ kind : 'transfer' , to : '金料' , grams : '5' }),
request = f . update ( id , { note : '合成待批平移' }, 'assistant' );
f . l . saveInventorySnapshot ( D3 , f . actors . owner );
f . l . db . exec (
"CREATE TRIGGER reject_manual_target BEFORE INSERT ON movements WHEN NEW.kind='transfer' AND NEW.pocket='金料' BEGIN SELECT RAISE(ABORT,'合成拒绝新目标'); END;" ,
);
const before = f . all ();
assert . throws (() => f . approve ( request . requestId ), /拒绝新目标/ );
assert . equal ( f . all (), before );
assert . equal ( f . l . inventorySnapshot ( D3 ). stale , false );
});
test ( '真实HTTP库存改删鉴权、CSRF、助理隔离和同操作号并发重试' , async t => {
2026-12-04 08:52:50 -08:00
const f = fixture ( t ),
id = f . stock ({ date : D2 }),
app = createApp ({ mode : 'trial' , port : 0 , ledger : f . l , feishuConfig : {} });
await new Promise ( resolve => app . server . listen ( 0 , '127.0.0.1' , resolve ));
t . after (
() =>
new Promise ( resolve => {
app . server . closeAllConnections ();
app . server . close ( resolve );
}),
);
2026-10-25 14:19:24 +08:00
const base = 'http://127.0.0.1:' + app . server . address (). port ;
const call = ( route , cookie = '' , body , key = randomUUID (), csrf = true ) =>
fetch ( base + route , {
method : body ? 'POST' : 'GET' ,
headers : {
...( cookie ? { Cookie : cookie } : {}),
...( body
2026-12-04 08:58:53 -08:00
? {
2026-10-25 14:19:24 +08:00
'Content-Type' : 'application/json' ,
'Idempotency-Key' : key ,
...( csrf ? { Origin : base , 'X-Requested-With' : 'maimai-ledger' } : { Origin : 'https://invalid.example' }),
2026-12-04 08:58:53 -08:00
}
2026-10-25 14:19:24 +08:00
: {}),
},
...( body ? { body : JSON . stringify ( body ) } : {}),
});
const login = async member => {
const res = await call ( '/api/login' , '' , { name : f . actors [ member ]. name , password : 'synthetic-inventory-password' });
assert . equal ( res . status , 200 );
return res . headers . get ( 'set-cookie' ). split ( ';' )[ 0 ];
};
const route = '/api/inventory/movements/' + id ;
assert . equal (( await call ( route )). status , 401 );
const a1 = await login ( 'assistant' ),
a2 = await login ( 'assistant2' ),
boss = await login ( 'owner' ),
input = { ... f . detail ( id ). values , revision : 1 , grams : '4' };
assert . equal (( await call ( route , a2 )). status , 404 );
assert . equal (( await call ( route + '/update' , a1 , input , randomUUID (), false )). status , 403 );
const key = randomUUID (),
responses = await Promise . all ([ call ( route + '/update' , a1 , input , key ), call ( route + '/update' , a1 , input , key )]);
assert . deepEqual (
responses . map ( res => res . status ),
[ 200 , 200 ],
);
const first = await responses [ 0 ]. json ();
assert . deepEqual ( await responses [ 1 ]. json (), first );
assert . equal ( amount ( f . l ), 5000 );
const list = await ( await call ( '/api/inventory/changes' , a2 )). json ();
assert . equal ( list . requests . length , 0 );
assert . equal (( await call ( '/api/inventory/changes/' + first . requestId , a2 )). status , 404 );
const request = await ( await call ( '/api/inventory/changes/' + first . requestId , boss )). json (),
approval = { revision : request . revision , inventoryRevision : request . inventoryRevision , verified : true };
assert . equal (( await call ( '/api/inventory/changes/' + first . requestId + '/approve' , a1 , approval )). status , 403 );
assert . equal (( await call ( '/api/inventory/changes/' + first . requestId + '/approve' , boss , approval )). status , 200 );
assert . equal ( amount ( f . l ), 4000 );
assert . equal (( await call ( '/api/inventory/changes/' + first . requestId + '/approve' , boss , approval )). status , 409 );
2026-12-04 16:18:03 +08:00
const currentId = f . stock ({ date : D3 }),
currentRoute = '/api/inventory/movements/' + currentId ;
const currentDetail = await ( await call ( currentRoute , a1 )). json ();
assert . equal ( currentDetail . requiresApproval , false );
assert . equal ( currentDetail . deleteRequiresApproval , true );
const currentInput = { ... currentDetail . values , revision : currentDetail . revision , grams : '4' },
currentKey = randomUUID ();
const saved = await call ( currentRoute + '/update' , a1 , currentInput , currentKey );
assert . equal ( saved . status , 200 );
const savedBody = await saved . json ();
assert . equal ( savedBody . applied , true );
assert . equal ( savedBody . pending , false );
assert . equal ( amount ( f . l ), 8000 );
assert . deepEqual ( await ( await call ( currentRoute + '/update' , a1 , currentInput , currentKey )). json (), savedBody );
assert . equal ( amount ( f . l ), 8000 );
});
test ( '员工当天改本人手动库存直接生效,原始流水、幂等与版本保护保留' , t => {
const f = fixture ( t ),
id = f . stock ({ date : D3 }),
original = f . l . one ( 'SELECT * FROM movements WHERE id=?' , id ),
key = randomUUID ();
assert . equal ( f . detail ( id , 'assistant' ). requiresApproval , false );
assert . equal ( f . detail ( id , 'assistant' ). deleteRequiresApproval , true );
const input = { ... f . detail ( id , 'assistant' ). values , revision : 1 , grams : '4' , reason : '核对实物后更正' },
submit = () => f . run ( input , () => f . l . updateInventoryMovement ( id , input , f . actors . assistant ), 'assistant' , key );
const result = submit ();
assert . equal ( result . applied , true );
assert . equal ( result . pending , false );
assert . equal ( result . group . revision , 2 );
assert . equal ( amount ( f . l ), 4000 );
assert . equal ( f . l . inventoryChanges ({}, f . actors . assistant ). counts . pending , 0 );
assert . equal ( JSON . stringify ( submit ()), JSON . stringify ( result ));
assert . equal ( amount ( f . l ), 4000 );
assert . deepEqual ( f . l . one ( 'SELECT * FROM movements WHERE id=?' , id ), original );
assert . throws (
() => f . run ( input , () => f . l . updateInventoryMovement ( id , input , f . actors . assistant ), 'assistant' ),
error => error . status === 409 ,
);
assert . throws (
() => f . update ( id , { grams : '3' }, 'assistant2' ),
error => error . status === 404 ,
);
const next = f . update ( id , { grams : '3' }, 'assistant' );
assert . equal ( next . applied , true );
assert . equal ( amount ( f . l ), 3000 );
assert . equal ( next . group . revision , 3 );
assert ( f . l . rows ( 'SELECT action FROM audit' ). some ( row => row . action === '修改手动库存记录' ));
});
test ( '当天改到旧日期、旧记录改到今天以及当天删除仍需审批' , t => {
const f = fixture ( t ),
current = f . stock ({ date : D3 });
const result = f . update ( current , { date : D2 , grams : '4' }, 'assistant' );
assert . equal ( result . pending , true );
assert . equal ( amount ( f . l ), 5000 );
const old = f . stock ({ date : D2 });
assert . equal ( f . update ( old , { date : D3 }, 'assistant' ). pending , true );
const deletion = f . stock ({ date : D3 });
assert . equal ( f . remove ( deletion , 'assistant' ). pending , true );
assert . equal ( amount ( f . l ), 15000 );
const changedByOwner = f . stock ({ date : D2 });
f . update ( changedByOwner , { date : D3 }, 'owner' );
assert . equal ( f . detail ( changedByOwner , 'assistant' ). requiresApproval , true );
assert . equal ( f . update ( changedByOwner , { grams : '4' }, 'assistant' ). pending , true );
});
test ( '当天平移和对数同步更正,负库存或不满足最新用量时整组回滚' , t => {
const f = fixture ( t );
f . run ({}, () =>
f . l . updatePocket (
'现货' ,
{ revision : f . l . pocket ( '现货' ). revision , countUnit : 'pair' , initialCount : 10 , countVerified : true },
f . actors . owner ,
2026-12-04 08:58:53 -08:00
),
2026-12-04 16:18:03 +08:00
);
const receipt = f . stock ({ date : D3 , stockCount : 2 });
const edited = f . update ( receipt , { grams : '4' , stockCount : 1 }, 'assistant' );
assert . equal ( edited . applied , true );
assert . equal ( f . l . inventory (). find ( row => row . pocket === '现货' ). count , 11 );
const transfer = f . stock ({ date : D3 , kind : 'transfer' , to : '金料' , grams : '2' , stockCount : 1 });
const result = f . update ( transfer , { grams : '1' }, 'assistant' );
assert . equal ( result . applied , true );
assert . equal ( result . group . movements . length , 2 );
assert . equal ( amount ( f . l ), 3000 );
assert . equal ( amount ( f . l , '金料' ), 1000 );
const before = f . all ();
assert . throws (() => f . update ( transfer , { grams : '5' }, 'assistant' ), /库存不足/ );
assert . equal ( f . all (), before );
});
test ( '页面在午夜前读取,跨到次日提交由服务端重新判定为审批' , t => {
const f = fixture ( t ),
id = f . stock ({ date : D3 });
assert . equal ( f . detail ( id , 'assistant' ). requiresApproval , false );
const nextDay = day ( 1 ),
input = { ... f . detail ( id ). values , revision : 1 , grams : '4' },
env = { ... f . l . businessEnv (), today : () => nextDay };
const result = f . run (
input ,
2026-12-04 13:01:19 -08:00
() => require ( '../domains/inventory/inventory-changes' ). change ( f . l , id , 'update' , input , f . actors . assistant , env ),
2026-12-04 16:18:03 +08:00
'assistant' ,
);
assert . equal ( result . pending , true );
assert . equal ( amount ( f . l ), 5000 );
2026-10-25 14:19:24 +08:00
});