web/website/tests/postbank-payroll-accounts.test.mjs

41 lines
2.6 KiB
JavaScript

import {test} from 'node:test';
import assert from 'node:assert/strict';
import {ensurePayrollCurrentAccount} from '../node/postbank-payroll-accounts.mjs';
const recipient={id:'f3ffda37-01e4-41ea-b89d-fbeda82e453e',username:'unimatrix999'};
function fixture(accounts=[],hasClient=true){
const writes=[];return {writes,
async first(sql){return sql.includes('FROM pb_entities')?{id:'personal',kind:'person',active:1}:hasClient?{client_number:'123456789012'}:null;},
async query(sql,args){if(sql.startsWith('SELECT'))return accounts;writes.push({sql,args});return {insertId:77};}
};
}
test('payroll reuses an existing personal current account without opening a package',async()=>{
const tx=fixture([{id:19,type_code:'102',state:'active',package_state:'active',system_key:null}]);
const result=await ensurePayrollCurrentAccount(tx,recipient);
assert.equal(result.accountId,'19');assert.equal(result.created,false);
assert.equal(tx.writes.some(w=>w.sql.includes('INSERT INTO pb_packages')),false);
});
test('a missing personal account receives free Start 108 with zero opening funds',async()=>{
const tx=fixture([],false),result=await ensurePayrollCurrentAccount(tx,recipient);
assert.equal(result.created,true);
const pkg=tx.writes.find(w=>w.sql.includes('INSERT INTO pb_packages'));
assert.match(pkg.sql,/'pb_start',0/);assert.match(pkg.sql,/payroll-provisioned-basic/);
const account=tx.writes.find(w=>w.sql.includes('INSERT INTO pb_accounts'));
assert.match(account.sql,/'108'/);assert.match(account.sql,/,0,0,'active'/);
assert.equal(tx.writes.some(w=>w.sql.includes('pb_entries')||w.sql.includes('pb_orders')),false);
assert.equal(tx.writes.some(w=>w.sql.includes('979')),false);
});
test('blocked personal accounts require review rather than a replacement',async()=>{
const tx=fixture([{id:19,type_code:'108',state:'blocked',system_key:null}]);
await assert.rejects(ensurePayrollCurrentAccount(tx,recipient),/needs review/);
assert.equal(tx.writes.some(w=>w.sql.includes('INSERT INTO pb_packages')),false);
});
test('an official account is not selected as the personal salary destination',async()=>{
const tx=fixture([{id:19,type_code:'979',state:'active',system_key:null}]);
const result=await ensurePayrollCurrentAccount(tx,recipient);
assert.equal(result.created,true);assert.equal(result.accountId,'77');
});
test('an inactive package cannot be bypassed by reusing its otherwise active account',async()=>{
const tx=fixture([{id:19,type_code:'108',state:'active',package_state:'closed',system_key:null}]);
await assert.rejects(ensurePayrollCurrentAccount(tx,recipient),/needs review/);
assert.equal(tx.writes.some(w=>w.sql.includes('INSERT INTO pb_packages')),false);
});