打印状态移至备注后并在点击打印时自动记录
This commit is contained in:
1 parent
846a30cded
commit
c12f2157b4
6 files changed
+80
-40
No files matched your search
@@ -0,0 +1,26 @@
|
||||
'use strict';
|
||||
const test=require('node:test'),assert=require('node:assert/strict'),fs=require('node:fs'),path=require('node:path'),vm=require('node:vm');
|
||||
const script=fs.readFileSync(path.join(__dirname,'../public/print.js'),'utf8').split('// 以点击页面“打印”为已打印依据;')[1];
|
||||
function fixture(fetcher,{printed=false}={}){
|
||||
const state={kind:'order',id:'synthetic-order',user:'original-user',printed,revision:0,sourceRevision:5},handlers={},calls=[],messages=[];
|
||||
const label={dataset:{state:JSON.stringify(state)},textContent:printed?'✓ 已打印':'未打印'},retry={hidden:true,disabled:false,addEventListener:(key,fn)=>handlers[key]=fn},error={textContent:''};
|
||||
const context=vm.createContext({window:{},document:{getElementById:id=>({'document-print-state':label,'retry-document-print':retry,'document-print-error':error})[id]},crypto:{randomUUID:()=> 'same-operation'},AbortSignal,localStorage:{setItem:(key,value)=>messages.push(JSON.parse(value))},fetch:async(url,options)=>{calls.push({url,...options});return fetcher(url,options,calls.length);}});
|
||||
vm.runInContext('// 以点击页面“打印”为已打印依据;'+script,context);
|
||||
return {calls,messages,label,retry,error,click:()=>context.window.recordDocumentPrint(),retrySave:()=>handlers.click()};
|
||||
}
|
||||
const response=(status,body)=>({ok:status===200,status,json:async()=>body});
|
||||
const success=()=>response(200,{printed:true,revision:1,sourceRevision:5,kind:'order',id:'synthetic-order'});
|
||||
test('仅打开预览不写入;打印后自动记录,多次点击不反转状态',async()=>{
|
||||
const f=fixture(success);assert.equal(f.calls.length,0);await f.click();assert.equal(f.calls.length,1);assert.equal(JSON.parse(f.calls[0].body).printed,true);assert.equal(f.calls[0].headers['X-Ledger-User'],'original-user');assert.equal(f.label.textContent,'✓ 已打印');assert.equal(f.messages[0].user,'original-user');await f.click();assert.equal(f.calls.length,1);
|
||||
});
|
||||
test('响应丢失复用操作号自动重试,不重复记录',async()=>{
|
||||
const f=fixture((url,options,n)=>{if(n===1)throw Error('网络断开');return success();});await f.click();assert.equal(f.calls.length,2);assert.equal(f.calls[0].body,f.calls[1].body);assert.equal(f.calls[0].headers['Idempotency-Key'],f.calls[1].headers['Idempotency-Key']);assert.equal(f.retry.hidden,true);assert.equal(f.label.textContent,'✓ 已打印');
|
||||
});
|
||||
test('持续中断显示失败与重试入口,不伪造已打印;恢复仍用原操作',async()=>{
|
||||
const f=fixture((url,options,n)=>{if(n<3)throw Error('连接失败');return success();});await f.click();assert.equal(f.label.textContent,'未打印');assert.equal(f.retry.hidden,false);await f.click();assert.equal(f.label.textContent,'✓ 已打印');assert.equal(f.calls[0].headers['Idempotency-Key'],f.calls[2].headers['Idempotency-Key']);
|
||||
});
|
||||
test('其他预览已记录时读取最新状态,单据变动或切换账号则要求刷新',async()=>{
|
||||
const f=fixture((url,options)=>options.method?response(409,{error:'状态已变化'}):success());await f.click();assert.equal(f.label.textContent,'✓ 已打印');assert.equal(f.calls.length,2);
|
||||
const stale=fixture((url,options)=>options.method?response(409,{error:'单据已变化'}):response(200,{printed:true,sourceRevision:6}));await stale.click();assert.equal(stale.label.textContent,'未打印');assert.match(stale.error.textContent,/刷新/);assert.equal(stale.retry.hidden,true);
|
||||
const switched=fixture(()=>response(409,{code:'ACCOUNT_CHANGED',error:'账号已切换'}));await switched.click();assert.equal(switched.calls.length,1);assert.match(switched.error.textContent,/刷新/);assert.equal(switched.messages.length,0);
|
||||
});
|
||||
@@ -10,7 +10,7 @@ function fixture(t){
|
||||
return {l,actors,order,run,mark};
|
||||
}
|
||||
const snapshot=l=>JSON.stringify(['orders','payments','expenses','movements','journal','price_locks','material_purchases','recovery_material_batches'].filter(name=>l.one("SELECT name FROM sqlite_master WHERE type='table' AND name=?",name)).map(name=>[name,l.rows('SELECT * FROM '+name)]));
|
||||
test('打开预览不是已打印;人工确认和撤销不改变订单账目、复核与库存',t=>{
|
||||
test('打开预览不是已打印;记录打印和撤销不改变订单账目、复核与库存',t=>{
|
||||
const f=fixture(t),{l,order,actors}=f;
|
||||
l.audit(order,'打开打印页',actors.assistant,{notice:'不代表纸张已输出'});
|
||||
assert.equal(Print.read(l,'order',order,actors.assistant).printed,false);
|
||||
@@ -21,7 +21,7 @@ test('打开预览不是已打印;人工确认和撤销不改变订单账目
|
||||
assert.equal(f.mark('order',order,false).printed,false);
|
||||
assert.equal(snapshot(l),before);assert.deepEqual(l.orderBook({date:today()}).rows,financialRows);assert.equal(l.detail(order).revision,detail.revision);
|
||||
assert.equal(l.detail(order).reviewed_revision,detail.reviewed_revision);
|
||||
assert.equal(l.rows("SELECT * FROM audit WHERE entity=? AND action IN ('确认已打印','改为未打印')",order).length,2);
|
||||
assert.equal(l.rows("SELECT * FROM audit WHERE entity=? AND action IN ('记录打印操作','改为未打印')",order).length,2);
|
||||
assert.ok(!Object.hasOwn(l.publicOrder(order),'printStatus'),'客户单据数据不带内部打印登记');
|
||||
});
|
||||
test('回收、置换与关联销售单共用打印状态;店员不能操作别人的单',t=>{
|
||||
@@ -49,14 +49,14 @@ test('旧状态和旧打印页不能覆盖新记录,重试幂等,失败事
|
||||
assert.throws(()=>f.run({},()=>{Print.update(l,'order',order,{...Print.read(l,'order',order,actors.assistant),printed:false},actors.assistant);throw new Error('rollback');}));
|
||||
assert.equal(Print.read(l,'order',order,actors.assistant).printed,true);
|
||||
});
|
||||
test('HTTP打印页含人工确认,接口鉴权、账号切换和重试正常',async t=>{
|
||||
test('HTTP打印页自动登记且无人工确认入口,接口鉴权、账号切换和重试正常',async t=>{
|
||||
const f=fixture(t),dir=fs.mkdtempSync(path.join(os.tmpdir(),'print-status-http-')),app=createApp({mode:'trial',ledger:f.l,dataDir:dir,networkAccess:{header:'',trustedProxies:[]},feishuConfig:{enabled:false}});
|
||||
await new Promise(done=>app.server.listen(0,'127.0.0.1',done));t.after(async()=>{await new Promise(done=>app.server.close(done));fs.rmSync(dir,{recursive:true,force:true});});
|
||||
const base='http://127.0.0.1:'+app.server.address().port;
|
||||
const login=await fetch(base+'/api/login',{method:'POST',headers:{'Content-Type':'application/json','X-Requested-With':'maimai-ledger',Origin:base},body:JSON.stringify({name:f.actors.assistant.name,password:'synthetic-print-password'})});assert.equal(login.status,200);
|
||||
const headers={Cookie:login.headers.get('set-cookie').split(';')[0],'X-Ledger-User':f.actors.assistant.id},url=base+'/api/print-status/order/'+f.order;
|
||||
assert.equal((await fetch(url)).status,401);
|
||||
const html=await (await fetch(base+'/print/'+f.order,{headers})).text();assert.match(html,/id="confirm-document-print"/);assert.match(html,/未打印(未登记)/);assert.match(html,/data-state=/);
|
||||
const html=await (await fetch(base+'/print/'+f.order,{headers})).text();assert.doesNotMatch(html,/id="confirm-document-print"/);assert.match(html,/id="retry-document-print" type="button" hidden/);assert.match(html,/未打印/);assert.match(html,/data-state=/);
|
||||
const current=await (await fetch(url,{headers})).json(),body=JSON.stringify({printed:true,revision:current.revision,sourceRevision:current.sourceRevision}),key=randomUUID(),options={method:'POST',headers:{...headers,'Content-Type':'application/json','X-Requested-With':'maimai-ledger',Origin:base,'Idempotency-Key':key},body};
|
||||
assert.equal((await fetch(url,{...options,headers:{...options.headers,'X-Ledger-User':f.actors.owner.id}})).status,409);
|
||||
assert.equal((await fetch(url,options)).status,200);assert.equal((await fetch(url,options)).status,200);
|
||||
|
||||
Reference in new issue
Block a user