Files
SGF/scripts/sgf-accounting-reports-http.mjs
2026-11-11 09:00:17 +02:00

5 lines
4.1 KiB
JavaScript

import fs from 'node:fs';import assert from 'node:assert/strict';import {session} from './sgf-single-http.mjs';import {ledgerReportQuery,checkedReport,reportBasis,reportPageCsv,minorAmount} from '../resources/js/ledger-report-view.js';
const s=await session('sgf:read_only','accounting'),checks=[],ok=(v,n)=>{assert(v,n);checks.push(n);},form={kind:'trial-balance',from_on:'2026-11-10',to_on:'2026-11-10',per_page:'25',account_id:''};
const get=async(q,basis=null)=>{const r=await s.get('/api/general-ledger/'+q.kind+'?'+new URLSearchParams(q.params));assert.equal(r.status,200);return checkedReport(await r.json(),q,basis);};const query=ledgerReportQuery(form),trial=await get(query);ok(trial.basis.journals===27,'existing27 journal basis preserved');ok(trial.rows.length>0&&trial.rows.length<=25,'bounded real account rows');ok(trial.totals.debit_minor===trial.totals.credit_minor,'actual filtered debit and credit balanced');const row=trial.rows.find(r=>r.code==='QA-CAIXA');assert(row);ok(minorAmount(row.closing_net_minor)==='950,00','cash actual ledger closing950');const ledgerQuery=ledgerReportQuery({...form,kind:'account-ledger',account_id:String(row.account_id)}),ledger=await get(ledgerQuery);ok(ledger.account.code==='QA-CAIXA'&&ledger.closing_net_minor===row.closing_net_minor,'drilldown explicit same account same closing');ok(ledger.rows.length>0&&ledger.rows.every(r=>r.review_id&&r.proposal_id&&r.reason),'original proposal review evidence present');ok(ledger.rows.at(-1).balance_minor===ledger.closing_net_minor,'running balance matches closing on complete known page');ok(reportBasis(ledger)===reportBasis(trial),'same native basis through same date');const larger=await get(ledgerReportQuery({...form,per_page:'100'}));ok(JSON.stringify(larger.rows)===JSON.stringify(trial.rows)&&JSON.stringify(larger.totals)===JSON.stringify(trial.totals),'page sizes25 and100 actual known accounts equivalent');const csv=reportPageCsv(trial);ok(csv.includes('Apenas a pagina consultada')&&csv.includes("'QA-CAIXA")&&csv.includes('950,00'),'CSV page only exact values and safe account text');const unsafe=structuredClone(ledger);unsafe.rows[0].reason=' =HYPERLINK("bad")';ok(reportPageCsv(unsafe).includes("' =HYPERLINK"),'CSV formula-like reason protected');assert.throws(()=>checkedReport({...trial,basis:{...trial.basis,last_journal_id:999999}},query,reportBasis(trial)));ok(true,'changed book marker demands new conference without DB mutation');assert.throws(()=>checkedReport(trial,{...query,params:{...query.params,to_on:'2026-11-09'}}));ok(true,'late response different dates refused');assert.throws(()=>ledgerReportQuery({...form,kind:'account-ledger'}));ok(true,'no account auto selection');assert.throws(()=>ledgerReportQuery({...form,from_on:''}));ok(true,'no dates assumed');assert.throws(()=>ledgerReportQuery({...form,account_id:'9007199254740993'}));ok(true,'unsafe identifier refused');assert.throws(()=>minorAmount(Number.MAX_SAFE_INTEGER+2));ok(true,'unsafe amount refused');if(!process.argv.includes('--before-install')){const missing=await s.get('/api/general-ledger/account-ledger?from_on=2026-11-10&to_on=2026-11-10&account_id=2147483647');ok(missing.status===403||missing.status===409,'foreign nonexistent account refused');const wrong=await session('sgf:read_only','finance'),denied=await wrong.get('/api/general-ledger/trial-balance?from_on=2026-11-10&to_on=2026-11-10');ok([403,409].includes(denied.status),'wrong module refused');}
const run=JSON.parse(fs.readFileSync('C:/laragon/data/sgf-tempo/run/modular-regression-current.json')),root=run.root+'/private/receipt-composition-cc50a276c10c',file=root+(process.argv.includes('--before-install')?'/accounting-reports-preinstall-20261110.json':'/accounting-reports-http-20261110.json');fs.writeFileSync(root+'/accounting-reports-samples-private.json',JSON.stringify({trial,ledger}));fs.writeFileSync(file,JSON.stringify({passed:true,checks,business_commands:0,new_grants:0,new_demo:false,server_read_engine_unchanged:true,http_error_mapping_corrected:!process.argv.includes('--before-install')},null,2));console.log(JSON.stringify({passed:true,checks:checks.length,proof:file}));