增加库存流水分页筛选与每日自动快照

This commit is contained in:
summer committed 2026-10-26 22:46:55 +08:00
1 parent b3b67c8003
commit aa7f7cad27
12 files changed
+659 -55

No files matched your search

@@ -0,0 +1,71 @@
'use strict';
const test=require('node:test'),assert=require('node:assert/strict'),fs=require('node:fs'),vm=require('node:vm');
const D='2026-10-26',Y='2026-10-25',pockets=['现货','金料','找金','回收旧料','君宝汇'];
const inventory=(mg=1234)=>pockets.map((pocket,index)=>({pocket,mg:index?0:mg,reserved:0,available:index?0:mg}));
const snapshot=(extra={})=>({date:D,source:'automatic',actor:'系统自动',created:D+'T12:00:00Z',updated:D+'T15:51:00Z',scheduled_for:D+'T23:50:00+08:00',stale:false,inventory:inventory(),revision:'saved-revision',...extra});
const schedule=(extra={})=>({enabled:true,time:'23:50',timeZone:'Asia/Shanghai',date:D,status:'waiting',lastSuccess:null,lastError:null,nextAttemptAt:D+'T15:50:00Z',...extra});
const page=(extra={})=>({date:D,yesterdayDate:Y,revision:'current-revision',canReview:true,current:inventory(9876),todaySnapshot:null,yesterday:null,legacyInventory:[],automaticSnapshot:schedule(),...extra});
function node(extra={}){return {innerHTML:'',textContent:'',value:'',isConnected:true,setAttribute(name,value){this[name]=value;},...extra};}
function harness(value=page(),role='owner'){
const nodes=new Map(),handlers={},calls=[],toasts=[];
for(const id of ['inventory-history-list','inventory-history-count','inventory-history-caption','inventory-history-date'])nodes.set('#'+id,node());
const context=vm.createContext({console,URLSearchParams,setInterval(){},setTimeout(){},clearTimeout(){},window:{scrollTo(){}},document:{querySelector:query=>nodes.get(query)||null,querySelectorAll(){return [];},addEventListener(type,handler){(handlers[type]??=[]).push(handler);}},__page:value,__role:role,__calls:calls,__toasts:toasts});
const run=code=>vm.runInContext(code,context);
run(fs.readFileSync(require.resolve('../public/app.js'),'utf8').replace(/\ninit\(\);\s*$/,''));
run(`state={user:{id:'synthetic-user',role:__role},today:'${D}'};session={user:state.user};tab='stock';inventoryPage=__page;inventoryHistoryOpen=true;inventoryHistoryMode='all';toast=(message,error=false)=>__toasts.push({message,error});api=async(url)=>{__calls.push({url});return __result;};`);
return {run,context,nodes,calls,toasts,async history(snapshots){context.__result={snapshots};await run('loadInventorySnapshots()');return nodes.get('#inventory-history-list').innerHTML;},async review(){const button={disabled:false,dataset:{action:'inventory-review'},closest(){return null;}};for(const handler of handlers.click||[])await handler({target:{closest:selector=>selector==='button'?button:null}});}};
}
test('自动快照只显示系统自动保存,实际保存时间不冒用计划时间或首次保存时间',()=>{
const h=harness(page({todaySnapshot:snapshot({actor:'麦麦'}),automaticSnapshot:schedule({status:'saved',lastSuccess:{date:D,scheduledFor:D+'T23:50:00+08:00',savedAt:D+'T15:51:00Z'}})})),html=h.run('stockReviewStatus(__page)');
assert.match(html,/系统自动保存/);assert.match(html,/实际保存 [^<]*23:51/);assert.match(html,/北京时间每日 23:50 自动保存/);assert.doesNotMatch(html,/已复核|今天尚未保存|20:00/);
assert.doesNotMatch(h.run('stockHeading(__page)'),/麦麦核对后保存/);
});
test('手动复核与旧快照返回兼容,旧接口缺少自动计划时不承诺已启用自动保存',()=>{
const manual=snapshot({source:'manual',actor:'合成麦麦'}),h=harness(page({todaySnapshot:manual}));let html=h.run('stockReviewStatus(__page)');
assert.match(html,/麦麦复核/);assert.match(html,/合成麦麦/);assert.match(html,/今天已复核/);
delete manual.source;delete manual.updated;delete h.context.__page.automaticSnapshot;
html=h.run('stockReviewStatus(__page)');assert.match(html,/麦麦复核/);assert.match(html,/今天已复核/);assert.match(html,/实际保存 [^<]*20:00/);assert.doesNotMatch(html,/北京时间每日|系统自动保存/);
});
test('保存后补记时保留来源与时间,自动和人工快照都不显示当前已复核',()=>{
const h=harness(page({todaySnapshot:snapshot({stale:true}),yesterday:snapshot({date:Y,stale:true})}));let html=h.run('stockReviewStatus(__page)');
assert.match(html,/系统自动保存/);assert.match(html,/实际保存 [^<]*23:51/);assert.match(html,/保存后有库存变动,保留当时保存值/);assert.match(html,/卡片仍显示昨天当时保存的数量/);assert.doesNotMatch(html,/已复核|需重新复核/);
h.context.__page.todaySnapshot.source='manual';html=h.run('stockReviewStatus(__page)');assert.match(html,/麦麦复核/);assert.match(html,/需重新复核/);assert.doesNotMatch(html,/今天已复核/);
});
test('昨天卡片保留过期快照数量和零库存,当前数量与保存来源单独展示',()=>{
const h=harness(page({yesterday:snapshot({date:Y,stale:true,inventory:inventory(1234)})})),html=h.run('stockCards(__page)');
assert.match(html,/当前数量<\/div><strong>9\.876/);assert.match(html,/昨天快照/);assert.match(html,/<b class="">1\.234g<\/b>/);assert.match(html,/<b class="">0\.000g<\/b>/);assert.match(html,/系统自动保存 · 后有补记/);assert.doesNotMatch(html,/昨天复核|麦麦复核|已复核/);
h.context.__page.yesterday=null;assert.match(h.run('stockCards(__page)'),/未保存快照/);
});
test('历史表按每个日期一行显示自动、人工和旧快照,使用各自实际保存值及时间',async()=>{
const automatic=snapshot({date:Y,stale:true,inventory:[...inventory(1234),{pocket:'历史货盘',mg:100,reserved:0}]}),manual=snapshot({source:'manual',date:D,actor:'<b>合成麦麦</b>',inventory:inventory(2500)}),old=snapshot({date:'2026-10-24',actor:'旧合成麦麦'});delete old.source;delete old.updated;
const h=harness(),html=await h.history([manual,automatic,old]);
assert.match(html,/保存来源/时间/);assert.equal((html.match(/<tr><td>/g)||[]).length,3);assert.equal(h.nodes.get('#inventory-history-count').textContent,'3 天');assert.match(html,/系统自动保存/);assert.match(html,/麦麦复核/);assert.match(html,/旧合成麦麦/);assert.match(html,/实际保存 [^<]*23:51/);assert.match(html,/实际保存 [^<]*20:00/);assert.match(html,/1\.234/);assert.match(html,/2\.500/);assert.match(html,/保存后有库存补记,保留当时保存值/);assert.match(html,/历史货盘 0\.100g/);assert.doesNotMatch(html,/已复核|<b>合成麦麦<\/b>/);assert.match(html,/&lt;b&gt;合成麦麦&lt;\/b&gt;/);
assert.match(h.run('stockHistoryPanel()'),/每日库存快照/);assert.doesNotMatch(h.run('stockHistoryPanel()'),/每日复核快照/);
});
test('无快照与未启动状态清楚显示,不把预定时间冒充保存成功',async()=>{
const h=harness(page({automaticSnapshot:schedule({enabled:false,status:'stopped',nextAttemptAt:null})})),html=h.run('stockReviewStatus(__page)');
assert.match(html,/今天尚未保存快照/);assert.match(html,/计划:北京时间每日 23:50 自动保存,当前未启动/);assert.doesNotMatch(html,/今天已自动保存|今天已复核/);assert.match(await h.history([]),/尚未保存每日库存快照/);
});
test('自动保存失败只显示有界且转义的错误和本日重试时间',()=>{
const message='<img src=x onerror=alert(1)> '+ '合成故障'.repeat(60),h=harness(page({automaticSnapshot:schedule({status:'failed',lastError:{date:D,attemptedAt:D+'T15:50:00Z',message},nextAttemptAt:D+'T15:52:00Z'})})),html=h.run('stockReviewStatus(__page)');
assert.match(html,/本次自动保存未成功/);assert.match(html,/23:50/);assert.match(html,/下次重试 [^<]*23:52/);assert.match(html,/&lt;img src=x/);assert.match(html,/…/);assert.doesNotMatch(html,/<img|今天已自动保存|今天已复核/);assert.ok(html.length<900,'长错误不得撑满页面');assert.ok(!html.includes('合成故障'.repeat(35)));
h.context.__page.automaticSnapshot=schedule();const waiting=h.run('stockReviewStatus(__page)');assert.match(waiting,/北京时间每日 23:50 自动保存/);assert.doesNotMatch(waiting,/未成功|下次重试|今天已自动保存/);
});
test('未知来源保守显示为已保存,不标成麦麦复核',()=>{
const h=harness(page({todaySnapshot:snapshot({source:'unrecognized'})})),html=h.run('stockReviewStatus(__page)');assert.match(html,/>已保存<\/span>/);assert.doesNotMatch(html,/麦麦复核|今天已复核|系统自动保存/);
});
test('手动复核仍仅麦麦可操作,自动快照不改变权限与版本提交',async()=>{
const h=harness(page({todaySnapshot:snapshot()}));assert.match(h.run('stockHeading(__page)'),/data-action="inventory-review"/);
h.run('mutate=async(url,data)=>{__calls.push({url,data});return {};};refresh=async()=>{};');await h.review();assert.equal(h.calls.length,1);assert.equal(h.calls[0].url,'/api/inventory/review');assert.deepEqual(JSON.parse(JSON.stringify(h.calls[0].data)),{date:D,verified:true,revision:'current-revision'});
h.context.__page.canReview=false;assert.doesNotMatch(h.run('stockHeading(__page)'),/data-action="inventory-review"/);await h.review();assert.equal(h.calls.length,1);assert.match(h.toasts.at(-1).message,/手动复核库存由麦麦操作/);
const assistant=harness(page({canReview:true,todaySnapshot:snapshot()}),'assistant');assistant.run('mutate=async(url,data)=>{__calls.push({url,data});return {};};');assert.doesNotMatch(assistant.run('stockHeading(__page)'),/data-action="inventory-review"/);await assistant.review();assert.equal(assistant.calls.length,0);assert.match(assistant.toasts.at(-1).message,/手动复核库存由麦麦操作/);
});