@davoodmehraban89 / Factor-easy-2 Mirror · read-only / commits / f270999
Fork 0

سامانه صدور فاکتور و حسابداری

Mirrored from https://github.com/davoodmehraban89/Factor-easy.git

Synced Dec 4, 08:00 UTC

test(phase1a): use two actual Postgres connections for number allocation and competing reversals

davoodmehraban89 committed Oct 16, 2026 at 16:47 UTC f270999f896c198fb43a6e2cd2e13405249384a1
1 file changed +62
scripts/phase1a-stage1-concurrency.mjs new
+62
@@ -0,0 +1,62 @@
1 +#!/usr/bin/env node
2 +import {spawn} from 'node:child_process';
3 +(async function main(){
4 + const U='abcd7777-7777-4777-8777-777777777777';
5 + const org=`(SELECT organization_id FROM public.organization_members WHERE user_id='${U}')`;
6 + function run(sql){
7 + return new Promise((resolve,reject)=>{
8 + const p=spawn('psql',['-X','-qAt','-v','ON_ERROR_STOP=1','-h','127.0.0.1','-p','54322','-U','postgres','-d','postgres'],{env:{...process.env,PGPASSWORD:'postgres'},stdio:['pipe','pipe','pipe']});
9 + let stdout='',stderr='';
10 + p.stdout.on('data',x=>stdout+=x);p.stderr.on('data',x=>stderr+=x);
11 + p.on('error',reject);p.on('close',exit=>resolve({exit,stdout,stderr}));
12 + p.stdin.end(sql);
13 + });
14 + }
15 + function check(ok,name,diagnostic=''){
16 + console.log(JSON.stringify({name,pass:ok,diagnostic:String(diagnostic).slice(-180)}));
17 + if(!ok)throw Error(`FINORA_CONCURRENCY_${name}`);
18 + }
19 + const setup=`
20 +BEGIN;
21 +INSERT INTO auth.users(id,instance_id,aud,role,email,email_confirmed_at,raw_user_meta_data)
22 +VALUES('${U}','00000000-0000-0000-0000-000000000000','authenticated','authenticated','phase1a-concurrency@example.invalid',now(),'{}'::jsonb);
23 +INSERT INTO public.records(organization_id,owner_id,collection,id,data)
24 +SELECT organization_id,user_id,'companies','COMP_RACE','{"name":"Concurrent Test"}'::jsonb
25 +FROM public.organization_members WHERE user_id='${U}';
26 +INSERT INTO public.records(organization_id,owner_id,collection,id,data)
27 +SELECT organization_id,user_id,'fiscalYears','FY_RACE',
28 +'{"companyId":"COMP_RACE","startDate":"1405/01/01","endDate":"1405/12/29","status":"open"}'::jsonb
29 +FROM public.organization_members WHERE user_id='${U}';
30 +INSERT INTO public.records(organization_id,owner_id,collection,id,data)
31 +SELECT organization_id,user_id,'accounts','RACE_ACC_'||n,
32 +jsonb_build_object('companyId','COMP_RACE','postingAllowed',true,'active',true)
33 +FROM public.organization_members CROSS JOIN generate_series(1,2) n
34 +WHERE user_id='${U}';
35 +INSERT INTO private.finora_phase1a_guard_modes(organization_id,mode)
36 +SELECT organization_id,'enforce' FROM public.organization_members WHERE user_id='${U}';
37 +COMMIT;`;
38 + const x=await run(setup);check(x.exit===0,'fixture',x.stderr);
39 + const auth=`SET LOCAL ROLE authenticated; SELECT set_config('request.jwt.claim.sub','${U}',true);`;
40 + const trans=q=>`BEGIN;\n${auth}\n${q}\nCOMMIT;`;
41 + const header=JSON.stringify({companyId:'COMP_RACE',fiscalYearId:'FY_RACE',date:'1405/07/24'});
42 + const lines=JSON.stringify([{accountId:'RACE_ACC_1',debit:1000,credit:0},{accountId:'RACE_ACC_2',debit:0,credit:1000}]);
43 + const draft=id=>`SELECT public.finora_journal_create_draft(${org},'${id}','${header}'::jsonb,'${lines}'::jsonb);`;
44 + const seed=await run(trans(`${draft('RACE_00001')}\n${draft('RACE_00002')}`));check(seed.exit===0,'drafts',seed.stderr);
45 + const [a,b]=await Promise.all([
46 + run(trans(`SELECT public.finora_journal_post(${org},'RACE_00001',1);`)),
47 + run(trans(`SELECT public.finora_journal_post(${org},'RACE_00002',1);`))
48 + ]);
49 + check(a.exit===0&&b.exit===0,'two_posts',a.stderr+' '+b.stderr);
50 + const numbered=await run(`SELECT string_agg(data->>'number',',' ORDER BY (data->>'number')::int) FROM public.records WHERE organization_id=${org} AND collection='journalVouchers' AND id IN ('RACE_00001','RACE_00002');`);
51 + check(numbered.exit===0&&numbered.stdout.trim()==='1,2','gapless_numbers',numbered.stdout);
52 + const third=await run(trans(`${draft('RACE_ORIGINAL')}\nSELECT public.finora_journal_post(${org},'RACE_ORIGINAL',1);`));check(third.exit===0,'original',third.stderr);
53 + const [r1,r2]=await Promise.all([
54 + run(trans(`SELECT public.finora_journal_reverse(${org},'RACE_ORIGINAL','RACE_REV_A','1405/07/24','Two sessions reversal',2,NULL);`)),
55 + run(trans(`SELECT public.finora_journal_reverse(${org},'RACE_ORIGINAL','RACE_REV_B','1405/07/24','Two sessions reversal',2,NULL);`))
56 + ]);
57 + const winners=[r1,r2].filter(x=>x.exit===0).length,blocked=[r1,r2].filter(x=>x.exit!==0&&x.stderr.includes('FINORA_REVERSAL_OVER_ALLOCATION')).length;
58 + check(winners===1&&blocked===1,'two_reversals_one_winner',r1.stderr+' '+r2.stderr);
59 + const count=await run(`SELECT count(*) FROM public.records WHERE organization_id=${org} AND collection='journalVouchers' AND data->>'sourceType'='reversal' AND data->>'sourceId'='RACE_ORIGINAL' AND data->>'status'='posted';`);
60 + check(count.exit===0&&count.stdout.trim()==='1','no_duplicate_reversals',count.stdout);
61 + console.log('FINORA_STAGE1_TWO_CONNECTIONS_PASS');
62 +})().catch(err=>{console.error(err);process.exitCode=1});