سامانه صدور فاکتور و حسابداری
test(stage3): compare actual UI source JSON to server SQL-v1 amounts and fail-closed negatives
davoodmehraban89 committed
Oct 23, 2026 at 02:45 UTC
f40556fdd98f4cd237772fb115123455c1bb9feb
1 file changed
+61
scripts/stage3-ui-sql-v1-check.mjs
new
+61
@@ -0,0 +1,61 @@
1
+#!/usr/bin/env node
2
+// Stage 3: independent SQL validator for actual, synthetic Chromium form output.
3
+// Runs ONLY against disposable localhost Supabase CLI / PostgreSQL. Never hosted.
4
+import fs from 'node:fs';
5
+import {spawnSync} from 'node:child_process';
6
+
7
+const TARGET='127.0.0.1', PORT='54322';
8
+for(const key of ['SUPABASE_ACCESS_TOKEN','SUPABASE_DB_PASSWORD','SUPABASE_SERVICE_ROLE_KEY']){
9
+ if(process.env[key])throw Error('FINORA_TEST_TARGET_PRODUCTION_FORBIDDEN '+key);
10
+}
11
+if(process.env.PGHOST && !['localhost',TARGET].includes(process.env.PGHOST))
12
+ throw Error('FINORA_TEST_TARGET_PRODUCTION_FORBIDDEN PGHOST');
13
+const sourceFile=process.argv[2]||'stage3-ui-fixtures.json';
14
+const fixture=JSON.parse(fs.readFileSync(sourceFile,'utf8'));
15
+const quote=s=>"'"+String(s).replaceAll("'","''")+"'";
16
+function check(value,kind){
17
+ const sql='SELECT private.finora_phase1a_document_amounts('+
18
+ quote(JSON.stringify(value))+'::jsonb,'+quote(kind)+')::text;';
19
+ const out=spawnSync('psql',['-h',TARGET,'-p',PORT,'-U','postgres','-d','postgres',
20
+ '-X','-qAt','-v','ON_ERROR_STOP=1','-c',sql],
21
+ {encoding:'utf8',env:{...process.env,PGHOST:TARGET,PGPORT:PORT,PGPASSWORD:'postgres'},
22
+ timeout:20000,maxBuffer:1024*1024});
23
+ if(out.error)throw out.error;
24
+ return {ok:out.status===0,value:out.status===0?JSON.parse((out.stdout||'').trim()):null,
25
+ error:(out.stderr||'').slice(0,1600)};
26
+}
27
+function expectReject(kind,value,reason){
28
+ const r=check(value,kind);
29
+ if(r.ok||!r.error.includes(reason))
30
+ throw Error('FINORA_STAGE3_NEGATIVE_FAILED '+kind+' '+reason+' '+JSON.stringify(r).slice(0,500));
31
+}
32
+function copy(v){return JSON.parse(JSON.stringify(v))}
33
+let positives=0,negatives=0;
34
+for(const kind of ['sale','purchase']){
35
+ const src=fixture[kind];
36
+ if(!src||!Array.isArray(src.items)||src.items.length!==1||
37
+ src.currencyCode!=='IRR'||src.accountingVersion!==1||
38
+ src.vatMode!=='with_vat'||src.vatType!=='percent'||src.discountType!=='fixed')
39
+ throw Error('FINORA_STAGE3_UI_FIXTURE_MISSING_FIELDS '+kind);
40
+ const pos=check(src,kind);
41
+ const expected={net:src.subtotal-src.discount,vat:src.vat,total:src.grandTotal};
42
+ if(!pos.ok||!pos.value||Object.keys(expected).some(k=>Number(pos.value[k])!==expected[k]))
43
+ throw Error('FINORA_STAGE3_UI_SERVER_AMOUNT_MISMATCH '+kind+' '+JSON.stringify(pos).slice(0,500));
44
+ positives++;
45
+ const variants=[
46
+ {name:'discount',reason:'FINORA_SOURCE_DISCOUNT_MISMATCH',edit:x=>{x.discountInput+=1}},
47
+ {name:'vat',reason:'FINORA_SOURCE_VAT_MISMATCH',edit:x=>{x.vat+=1}},
48
+ {name:'currency',reason:'FINORA_SOURCE_METHOD_OR_CURRENCY_UNSUPPORTED',edit:x=>{x.currencyCode='UNSUPPORTED_NON_RIAL'}},
49
+ {name:'quantity',reason:'FINORA_SOURCE_ITEM_QUANTITY_INVALID',edit:x=>{x.items[0].qty=1.5}},
50
+ {name:'line-total',reason:'FINORA_SOURCE_ITEM_TOTAL_MISMATCH',edit:x=>{x.items[0].lineTotal+=1}},
51
+ {name:'payment',reason:'FINORA_SOURCE_METHOD_OR_CURRENCY_UNSUPPORTED',edit:x=>{x.paymentMethod='card'}}
52
+ ];
53
+ for(const v of variants){
54
+ const altered=copy(src);v.edit(altered);
55
+ expectReject(kind,altered,v.reason);negatives++;
56
+ }
57
+}
58
+console.log('FINORA_STAGE3_UI_SQL_V1_PASS '+JSON.stringify({
59
+ validation:'actual Playwright DOM source against real local SQL-v1 function',
60
+ positives,negatives,server:'localhost only',historicalMutation:false
61
+}));