40 lines
6 KiB
JavaScript
40 lines
6 KiB
JavaScript
import {test} from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import {renderPayroll} from '../control-panel/public/cp-v2/cp-v2-postbank-payroll.mjs';
|
|
class Node{
|
|
constructor(tag){this.tagName=tag.toUpperCase();this.children=[];this.attrs={};this._text='';this.value='';this.checked=false;this.disabled=false;}
|
|
set textContent(value){this._text=String(value);this.children=[];}get textContent(){return this._text+this.children.map(n=>n.textContent).join('');}
|
|
append(...nodes){for(const node of nodes){node.parent=this;this.children.push(node);}}prepend(...nodes){for(const node of nodes)node.parent=this;this.children.unshift(...nodes);}
|
|
replaceChildren(...nodes){this._text='';this.children=[];this.append(...nodes);}setAttribute(key,value){this.attrs[key]=String(value);}focus(){}reportValidity(){return true;}
|
|
remove(){this.parent.children=this.parent.children.filter(n=>n!==this);}
|
|
}
|
|
const all=node=>node.children.flatMap(child=>[child,...all(child)]);
|
|
async function fixture(work,{canRun=true,enabled=true,state='not_started',post=async()=>({paid:1,failed:0}),active=()=>true,additional=false}={}){
|
|
const previous=globalThis.document;globalThis.document={createElement:tag=>new Node(tag)};
|
|
const host=new Node('main'),calls=[],data={policy:{version:1,value:{enabled:true,payDay:28}},ranks:[],eligibleCount:1,records:[],cycle:{period:'2026-11',previewKey:'a'.repeat(64),state,enabled,canRun,pendingCount:1,paidCount:0,netMinor:'280000',taxMinor:'302344',employerCostMinor:'582344',issuerBalanceMinor:'-582344',recipients:[{name:'Officer <QA>',netMinor:'280000'}]}};
|
|
if(additional)data.additionalCycle={...data.cycle,mode:'additional',state:'new_cycle',canRun:true};
|
|
const api={request:async(path,input)=>{calls.push({path,input});return input?post(input):data;}};
|
|
const nodes=()=>all(host),button=label=>nodes().find(n=>n.tagName==='BUTTON'&&n.textContent===label);
|
|
try{await renderPayroll(host,{api,isActive:active});await work({host,calls,nodes,button});}finally{globalThis.document=previous;}
|
|
}
|
|
const event={preventDefault(){}};
|
|
function confirm(nodes){const form=nodes.find(n=>n.tagName==='FORM'&&n.textContent.includes('Confirm wage cycle'));const controls=all(form);controls.find(n=>n.tagName==='INPUT'&&n.type!=='checkbox').value='Run confirmed monthly wages';controls.find(n=>n.type==='checkbox').checked=true;return form;}
|
|
|
|
test('P&O exposes Run Wage Cycle and pending totals even when there are no payroll records',()=>fixture(async({host,button,calls})=>{assert(button('Run Wage Cycle'));assert(host.textContent.includes('2800.00 DM'));assert(host.textContent.includes('5823.44 DM'));assert(host.textContent.includes('No payroll has been posted yet.'));assert.equal(calls.length,1);}));
|
|
test('opening and cancelling confirmation does not post wages',()=>fixture(async({button,host,calls})=>{button('Run Wage Cycle').onclick();assert(host.textContent.includes('Officer <QA>'));button('Cancel').onclick();assert.equal(button('Run Wage Cycle').disabled,false);assert.equal(calls.length,1);}));
|
|
test('confirmation requires an explicit checkbox and reason, then sends the reviewed period and preview',()=>fixture(async({button,nodes,calls})=>{
|
|
button('Run Wage Cycle').onclick();const form=nodes().find(n=>n.tagName==='FORM'&&n.textContent.includes('Confirm wage cycle'));await form.onsubmit(event);assert.equal(calls.length,1);
|
|
confirm(nodes());await form.onsubmit(event);const call=calls.find(c=>c.input);assert.equal(call.path,'/api/bank/admin/payroll/run');assert.equal(call.input.period,'2026-11');assert.equal(call.input.previewKey,'a'.repeat(64));assert.equal(call.input.confirmed,true);assert.match(call.input.requestId,/^[a-f0-9-]{36}$/);assert(nodes().some(n=>n.attrs.role==='status'&&n.textContent.includes('1 salaries paid')));
|
|
}));
|
|
test('double submission is disabled while the payment response is pending',()=>fixture(async({button,nodes,calls})=>{
|
|
button('Run Wage Cycle').onclick();const form=confirm(nodes()),first=form.onsubmit(event);await form.onsubmit(event);assert.equal(calls.filter(c=>c.input).length,1);assert.equal(button('Cancel').disabled,true);await first;
|
|
},{post:async()=>{await new Promise(resolve=>setImmediate(resolve));return {paid:1,failed:0};}}));
|
|
test('an uncertain response requires status reload and cannot silently resubmit',()=>fixture(async({button,nodes,calls,host})=>{
|
|
button('Run Wage Cycle').onclick();const form=confirm(nodes());await form.onsubmit(event);await form.onsubmit(event);assert.equal(calls.filter(c=>c.input).length,1);assert(button('Reload wage status'));assert(host.textContent.includes('check recorded payments'));await button('Reload wage status').onclick();assert.equal(calls.filter(c=>!c.input).length,2);
|
|
},{post:async()=>{throw Error('Result unavailable.');}}));
|
|
test('completed and disabled cycles show a disabled Run Wage Cycle button',async()=>{
|
|
await fixture(async({button,host})=>{assert.equal(button('Run Wage Cycle').disabled,true);assert(host.textContent.includes('cannot be paid again'));},{canRun:false,state:'completed'});
|
|
await fixture(async({button,host})=>{assert.equal(button('Run Wage Cycle').disabled,true);assert(host.textContent.includes('Enable and save payroll settings'));},{canRun:false,enabled:false});
|
|
});
|
|
|
|
test('completed monthly payroll offers an independent additional payment with explicit confirmation',()=>fixture(async({button,nodes,calls,host})=>{assert.equal(button('Run Wage Cycle').disabled,true);assert.equal(button('Run Additional Wage Cycle').disabled,false);button('Run Additional Wage Cycle').onclick();const form=nodes().find(n=>n.tagName==='FORM'&&n.textContent.includes('Confirm ADDITIONAL'));assert(host.textContent.includes('EXTRA full wage payment'));await form.onsubmit(event);assert.equal(calls.filter(c=>c.input).length,0);const controls=all(form);controls.find(n=>n.tagName==='INPUT'&&n.type!=='checkbox').value='Authorised extra wages';controls.find(n=>n.type==='checkbox').checked=true;await form.onsubmit(event);assert.equal(calls.filter(c=>c.input).length,1);assert.equal(calls.find(c=>c.input).input.mode,'additional');},{canRun:false,state:'completed',additional:true}));
|

Staff