سامانه صدور فاکتور و حسابداری
Mirrored from https://github.com/davoodmehraban89/Factor-easy.git
Synced Dec 3, 23:15 UTC
test(adr216): expose safe source identity aggregates in existing preflight CLI
davoodmehraban89 committed
Nov 22, 2026 at 09:44 UTC
1b4532c6ef2fa44e9cf69b9fe46b28b0631f7f1f
2 files changed
+55
-3
scripts/draft-conversion-inventory.test.mjs
+36
-1
@@ -3,7 +3,10 @@
3
import test from 'node:test';
4
import assert from 'node:assert/strict';
5
import {spawnSync} from 'node:child_process';
6
-import {readFileSync} from 'node:fs';
6
+import {readFileSync,writeFileSync,mkdtempSync,rmSync} from 'node:fs';
7
+import {tmpdir} from 'node:os';
8
+import {join} from 'node:path';
9
+import {fileURLToPath} from 'node:url';
10
import {planDraftConversion} from './lib/draft-conversion-plan.mjs';
11
12
test('source-derived inventory on isolated PostgreSQL preserves scope, exact amounts and read-only behavior', async()=>{
@@ -92,3 +95,35 @@ test('source-derived inventory on isolated PostgreSQL preserves scope, exact amo
95
assert.equal(inventory().sourceIdentityAudit.crossScopeGeneratedVouchers,1);
96
}finally{spawnSync('docker',['rm','--force',name],{encoding:'utf8',timeout:30000});}
97
});
98
+
99
+test('existing preflight CLI emits allowlisted source counts and fails closed on missing or unsafe evidence',()=>{
100
+ const dir=mkdtempSync(join(tmpdir(),'finora-adr216-source-audit-'));
101
+ const path=join(dir,'PRIVATE_INVENTORY.json');
102
+ const cmd=fileURLToPath(new URL('./draft-conversion-preflight.mjs',import.meta.url));
103
+ const fields=["sourcesConsidered","withoutGeneratedVoucher","unlinkedWithGeneratedVoucher","danglingSourceLinks","wrongIdentitySourceLinks","linkedDraftSources","linkedDraftProjectionMismatches","postedProjectionMismatches","duplicateDraftSourceIdentities","orphanGeneratedVouchers","crossScopeGeneratedVouchers"];
104
+ const empty=Object.fromEntries(fields.map(k=>[k,0]));
105
+ const run=sourceIdentityAudit=>{
106
+ writeFileSync(path,JSON.stringify({journals:[],snapshotAt:'2026-11-22T00:00:00Z',
107
+ ...(sourceIdentityAudit===undefined?{}:{sourceIdentityAudit})}));
108
+ return spawnSync(process.execPath,[cmd,path],{encoding:'utf8',timeout:10000});
109
+ };
110
+ try{
111
+ const clean=run({...empty,sourcesConsidered:3,PRIVATE_SOURCE:'must-never-echo'});
112
+ assert.equal(clean.status,0,clean.stderr);
113
+ assert.equal(clean.stdout.includes('must-never-echo'),false);
114
+ const report=JSON.parse(clean.stdout);
115
+ assert.equal(report.executable,false,'preflight is never authorization to convert');
116
+ assert.equal(report.sourceIdentityReady,true);
117
+ assert.equal(report.sourceIdentityAudit.sourcesConsidered,3);
118
+ assert.equal(Object.keys(report.sourceIdentityAudit).length,fields.length);
119
+ const problem=run({...empty,sourcesConsidered:3,duplicateDraftSourceIdentities:1});
120
+ assert.equal(problem.status,2,'source draft collision must block readiness');
121
+ assert.equal(JSON.parse(problem.stdout).sourceIdentityReady,false);
122
+ assert.equal(run(undefined).status,2,'old journal-only snapshots must refresh');
123
+ assert.equal(JSON.parse(run(undefined).stdout).sourceIdentityAudit,null);
124
+ assert.equal(run({...empty,orphanGeneratedVouchers:-1}).status,1,'negative counts are invalid');
125
+ assert.equal(run({...empty,withoutGeneratedVoucher:'2'}).status,1,'untyped strings are invalid');
126
+ assert.equal(run({...empty,crossScopeGeneratedVouchers:Number.MAX_SAFE_INTEGER+1}).status,1,
127
+ 'unsafe JSON counts are invalid');
128
+ }finally{rmSync(dir,{recursive:true,force:true});}
129
+});
scripts/draft-conversion-preflight.mjs
+19
-2
@@ -7,13 +7,30 @@ try{
7
const inventory=JSON.parse(readFileSync(inventoryPath,'utf8'));
8
const options=resolutionsPath?JSON.parse(readFileSync(resolutionsPath,'utf8')):{};
9
const result=planDraftConversion(inventory.journals,options);
10
+ const sourceCountFields=["sourcesConsidered","withoutGeneratedVoucher","unlinkedWithGeneratedVoucher","danglingSourceLinks","wrongIdentitySourceLinks","linkedDraftSources","linkedDraftProjectionMismatches","postedProjectionMismatches","duplicateDraftSourceIdentities","orphanGeneratedVouchers","crossScopeGeneratedVouchers"];
11
+ // Only emit hard-coded aggregate counts, never arbitrary private inventory data.
12
+ let sourceIdentityAudit=null;
13
+ if(inventory.sourceIdentityAudit!==undefined){
14
+ const input=inventory.sourceIdentityAudit;
15
+ if(!input||typeof input!=='object'||Array.isArray(input))
16
+ throw Error('invalid source identity aggregate');
17
+ sourceIdentityAudit=Object.fromEntries(sourceCountFields.map(key=>{
18
+ const value=input[key];
19
+ if(typeof value!=='number'||!Number.isSafeInteger(value)||value<0)
20
+ throw Error('invalid aggregate count: '+key);
21
+ return [key,value];
22
+ }));
23
+ }
24
+ const sourceIdentityReady=sourceIdentityAudit!==null &&
25
+ sourceCountFields.every(key=>key==='sourcesConsidered'||sourceIdentityAudit[key]===0);
26
console.log(JSON.stringify({
27
snapshotAt:inventory.snapshotAt, journalCount:inventory.journals.length,
28
integrityReady:result.integrityReady, executable:result.executable,
29
conversionCandidates:result.candidates.length, retainedCancelledChains:result.cancelledChains.length,
30
existingDrafts:result.existingDrafts.length,
31
+ sourceIdentityAudit, sourceIdentityReady,
32
blockers:result.blockers.reduce((counts,{code})=>({...counts,[code]:(counts[code]||0)+1}),{}),
16
- scope:'Journal graph only; source-only legacy documents and unlinked cheques need separate source-adapter reconciliation.'
33
+ scope:'Journal graph + aggregate sale/purchase financial-source identity; cheque and other source adapters require independent reconciliation.'
34
},null,2));
18
- if(!result.integrityReady)process.exitCode=2;
35
+ if(!result.integrityReady||!sourceIdentityReady)process.exitCode=2;
36
}catch(error){console.error(error.message);process.exitCode=1;}