Files
order-notification-automation/tests/app/automatic-pdf-ledger.test.cjs
T

12 lines
4.0 KiB
JavaScript

'use strict';
const test=require('node:test'),assert=require('node:assert/strict');
const {queryPdfLedger}=require('../../src/modules/dashboard/pdf-ledger.cjs');
const admin={username:'admin',role:'admin'},workspace={recordSources:()=>[{id:'a',owner:'alice'},{id:'b',owner:'bob'}]};
const order=(id,connectionId='a')=>({id,categoryId:'overseas',sample:{connectionId},event:{orderNo:id,serialNum:'SN-'+id,processName:'流程',nodeName:'通知'},ruleSnapshot:{name:'类别'},archive:{archivedAt:'2026-10-27T01:00:00Z',files:[{name:id+'.pdf',path:'C:\\private\\'+id+'.pdf',kind:'pdf',size:123,sha256:'a'.repeat(64)}]}});
function context(orders){return {jobs:[{jobId:'manual',status:'error',updatedAt:'2026-10-26T01:00:00Z',metadata:{order:'MANUAL'}}],outputDir:'.',orderRules:{all:k=>k==='orders'?orders:[]},workspace,user:admin};}
test('PDF ledger combines automatic archives and manual exports before search sorting and pagination',async()=>{const c=context(Array.from({length:76},(_,i)=>order('ORDER-'+i)));const p=await queryPdfLedger(c,{page:4});assert.equal(p.total,77);assert.equal(p.jobs.length,2);assert.equal(p.jobs.at(-1).jobId,'manual');const found=await queryPdfLedger(c,{q:'SN-ORDER-2',status:'success'});assert.equal(found.total,11);const auto=found.jobs[0];assert.equal(auto.sourceType,'order-archive');assert.equal(auto.fileStatus,'archived');assert.match(auto.downloadUrl,/^\/api\/order-rules\/orders\/ORDER-/);assert.equal(auto.archiveReceipts[0].status,'success');assert.doesNotMatch(JSON.stringify(found),/private|sha256/);assert.equal((await queryPdfLedger(c,{page:100})).page,4);});
test('automatic PDF rows obey connection access and removal while retaining archived sent orders',async()=>{const a=order('A'),b=order('B','b'),removed={...order('OLD'),removedAt:'now'},unarchived={...order('NONE'),archive:null};a.status='sent';const c=context([a,b,removed,unarchived]);c.user={username:'alice',role:'viewer'};const r=await queryPdfLedger(c);assert.deepEqual(r.jobs.filter(j=>j.sourceType==='order-archive').map(j=>j.metadata.order),['A']);c.user={username:'nobody',role:'operator'};assert.equal((await queryPdfLedger(c,{status:'success'})).total,0);});
test('dashboard ledger route uses merged records even when manual SQL pagination is available',async()=>{const {dashboardApi}=require('../../src/modules/dashboard/api.cjs');let response;const c=context([order('A')]);await dashboardApi({...c,store:{jobs:()=>c.jobs,jobPage:()=>{throw Error('must not paginate manual records first');}},route:'/api/jobs',request:{method:'GET',url:'/api/jobs?view=ledger&status=success'},reply:(status,body)=>response={status,body}});assert.equal(response.status,200);assert.equal(response.body.total,1);assert.equal(response.body.jobs[0].metadata.order,'A');});
test('PDF ledger renders automatic source and uses the order archive download route in list and detail',async t=>{const f=await require('../fixtures/records-app.cjs').setup(t),page=await f.page('#records-pdf');const payload=await queryPdfLedger(context([order('AUTO-TEST')]));await page.route('**/api/jobs?*',route=>route.fulfill({json:payload}));await page.reload();const row=page.locator('[data-pdf-row]').filter({hasText:'AUTO-TEST'});await row.waitFor();assert.match(await row.innerText(),/BPM 订单自动归档/);assert.equal(await row.getByRole('link',{name:'下载 PDF'}).getAttribute('href'),'/api/order-rules/orders/AUTO-TEST/files/0');await row.getByRole('button',{name:'查看详情'}).click();const detail=page.getByRole('dialog');await detail.waitFor();assert.match(await detail.innerText(),/BPM 订单自动归档/);assert.equal(await detail.getByRole('link',{name:'下载 PDF'}).getAttribute('href'),'/api/order-rules/orders/AUTO-TEST/files/0');});
test('automatic PDFs preserve actual project and model for display and search',async()=>{const o=order('A');o.event.project='PROJECT-EXACT';o.event.model='MODEL-EXACT';const r=await queryPdfLedger(context([o]),{q:'PROJECT-EXACT'});assert.equal(r.total,1);assert.equal(r.jobs[0].metadata.project,'PROJECT-EXACT');assert.equal(r.jobs[0].metadata.model,'MODEL-EXACT');});