سامانه صدور فاکتور و حسابداری
Mirrored from https://github.com/davoodmehraban89/Factor-easy.git
Synced Dec 4, 08:00 UTC
test(phase1a): add real authenticated contract regression for normal journal draft/post workflow
davoodmehraban89 committed
Oct 16, 2026 at 14:31 UTC
74d29f60fa8d9d19999a8acef2693ddc4e306d1f
1 file changed
+37
scripts/phase1a-positive-client-contract.mjs
new
+37
@@ -0,0 +1,37 @@
1
+#!/usr/bin/env node
2
+// POSITIVE legacy JS client contract -- must pass before any 1A deployment.
3
+// Existing js/journal-engine.js creates draft with status/number, then posts
4
+// by mutating status/postedAt. Authenticated/RLS path must remain operable
5
+// through an approved server-controlled action contract, not raw field bypass.
6
+import {spawnSync} from 'node:child_process';
7
+const uid='99999999-9999-4999-8999-999999999999';
8
+const org="(SELECT organization_id FROM public.organization_members WHERE user_id='"+uid+"' LIMIT 1)";
9
+const q=s=>"'"+JSON.stringify(s).replace(/'/g,"''")+"'::jsonb";
10
+const voucher={id:'POSITIVE_JV_1',companyId:'CO',fiscalYearId:'FY',date:'1405/07/24',status:'draft',number:'1',totalDebit:100,totalCredit:100,createdBy:uid};
11
+const line=(id,debit,credit)=>({collection:'journalLines',id,data:{voucherId:voucher.id,companyId:'CO',accountId:id,debit,credit},expected_version:0});
12
+const v={collection:'journalVouchers',id:voucher.id,data:voucher,expected_version:0};
13
+const head=["BEGIN;","INSERT INTO auth.users(id,instance_id,aud,role,email,email_confirmed_at,raw_user_meta_data) VALUES('"+uid+"','00000000-0000-0000-0000-000000000000','authenticated','authenticated','phase1a-positive@example.invalid',now(),'{}'::jsonb);"].join('\n');
14
+const rpc=(up)=>"SELECT public.finora_sync_records_v2("+q(up)+",'[]'::jsonb,"+org+");";
15
+const scenarios=[
16
+ {name:'existing-client-draft-with-status-and-number',body:[rpc([v])]},
17
+ {name:'existing-client-batched-draft-and-lines',body:[rpc([v,line('JLA',100,0),line('JLB',0,100)])]},
18
+ {name:'existing-client-post-status-transition',body:[
19
+ "INSERT INTO public.records(organization_id,owner_id,collection,id,data) VALUES("+org+",'"+uid+"','journalVouchers','"+voucher.id+"',"+q(voucher)+");",
20
+ "INSERT INTO public.records(organization_id,owner_id,collection,id,data) VALUES("+org+",'"+uid+"','journalLines','JLA',"+q(line('JLA',100,0).data)+");",
21
+ "INSERT INTO public.records(organization_id,owner_id,collection,id,data) VALUES("+org+",'"+uid+"','journalLines','JLB',"+q(line('JLB',0,100).data)+");",
22
+ "SET LOCAL ROLE authenticated;",
23
+ "SELECT set_config('request.jwt.claim.sub','"+uid+"',true);",
24
+ "SELECT public.finora_sync_records_v2("+q([{collection:'journalVouchers',id:voucher.id,expected_version:1,data:{...voucher,status:'posted',postedAt:'2026-10-16T00:00:00Z'}}])+",'[]'::jsonb,"+org+");"
25
+ ]}
26
+];
27
+let pass=0;
28
+for(const s of scenarios){
29
+ const role=s.name==='existing-client-post-status-transition'?[]:["SET LOCAL ROLE authenticated;","SELECT set_config('request.jwt.claim.sub','"+uid+"',true);"];
30
+ const input=[head,...role,...s.body,'ROLLBACK;'].join('\n')+'\n';
31
+ const p=spawnSync('psql',['-X','-qAt','-v','ON_ERROR_STOP=1','-h','127.0.0.1','-p','54322','-U','postgres','-d','postgres'],{input,encoding:'utf8',env:{...process.env,PGPASSWORD:'postgres'},timeout:25000});
32
+ const ok=p.status===0;
33
+ if(ok)pass++;
34
+ console.log(JSON.stringify({scenario:s.name,result:ok?'PASS':'RED compatibility failure',exit:p.status,diagnostic:(p.stderr||'').split('\n').find(x=>/ERROR:/.test(x))?.slice(0,180)||''}));
35
+}
36
+console.log(JSON.stringify({clientContractTotal:scenarios.length,passed:pass,failed:scenarios.length-pass}));
37
+if(pass!==scenarios.length)process.exitCode=1;