فینورا پرو - سامانه جامع حسابداری و صدور فاکتور

test(A01): enforce endorsement contact, ledger dimension and duplicate-key atomic rollback

davoodmehraban89 committed Oct 22, 2026 at 23:16 UTC 9029867ada426868c7aaf073833a958be04acbcc
1 file changed +107
tests/retail-phase2.test.mjs
+107
@@ -922,3 +922,110 @@ test('A01: outbound bounced cheque in later open period reverses without cash po
922 const trial=await accounts.trialBalance();
923 assert.equal(trial.totals.debit,trial.totals.credit);
924 });
925 +
926 +test('A01: cheque endorsement requires third-party identity and explicit confirmation before posting',async()=>{
927 + await setup();
928 + const id='a01-endorsement-identity';
929 + await db.put('contacts',{id:'endorsee-1',companyId:company,name:'تأمین‌کننده دریافت‌کننده'});
930 + await db.put('contacts',{id:'other-company-contact',companyId:'other-company',name:'خارج از شرکت'});
931 + const cheque=await retail.postCheque({
932 + id,direction:'inbound',status:'pending',issueDate:date,dueDate:date,
933 + contactId:'contact-1',amount:370,chequeNumber:'E370'
934 + });
935 + const snapshot=async()=>({
936 + cheque:await db.get('cheques',id),
937 + journals:(await db.getAll('journal_entries')).length,
938 + lines:(await db.getAll('journal_lines')).length,
939 + audit:(await db.getAll('audit_events')).length,
940 + corrections:(await db.getAll('document_corrections')).length,
941 + cash:(await db.getAll('treasury')).filter(x=>x.refChequeId===id).length
942 + });
943 + const baseline=await snapshot();
944 + for(const options of [
945 + {date}, {date,endorseeContactId:''},
946 + {date,endorseeContactId:'contact-1',explicitlyApproved:true},
947 + {date,endorseeContactId:'not-existent',explicitlyApproved:true},
948 + {date,endorseeContactId:'other-company-contact',explicitlyApproved:true},
949 + {date,endorseeContactId:'endorsee-1'},
950 + {date,endorseeContactId:'endorsee-1',explicitlyApproved:'true'},
951 + {date,endorseeContactId:'endorsee-1',explicitlyApproved:false}
952 + ]){
953 + await assert.rejects(retail.changeChequeStatus(id,'spent',options),
954 + /دریافت‌کننده|واگذاری|تأیید/);
955 + assert.deepEqual(await snapshot(),baseline,'Rejected endorsement mutated financial data');
956 + }
957 + const approved=await retail.changeChequeStatus(id,'spent',{
958 + date,endorseeContactId:'endorsee-1',explicitlyApproved:true
959 + });
960 + assert.equal(approved.status,'spent');
961 + assert.equal(approved.endorseeContactId,'endorsee-1');
962 + assert.equal(approved.endorseeContactName,'تأمین‌کننده دریافت‌کننده');
963 + assert.equal(approved.endorsementApprovedBy,'phase2-admin');
964 + assert.equal(approved.statusBusinessDate,date);
965 + const statusJournal=await db.get('journal_entries',approved.statusJournalId);
966 + const journalLines=(await db.getAll('journal_lines'))
967 + .filter(x=>x.journalId===statusJournal.id);
968 + const payable=(await db.getAll('account_chart')).find(x=>x.companyId===company&&x.code==='201');
969 + const incoming=(await db.getAll('account_chart')).find(x=>x.companyId===company&&x.code==='105');
970 + assert.ok(payable&&incoming);
971 + assert.deepEqual(journalLines.map(x=>({
972 + accountId:x.accountId,debit:x.debit,credit:x.credit,contactId:x.contactId
973 + })),[
974 + {accountId:payable.id,debit:370,credit:0,contactId:'endorsee-1'},
975 + {accountId:incoming.id,debit:0,credit:370,contactId:'contact-1'}
976 + ]);
977 + const correction=(await db.getAll('document_corrections'))
978 + .find(x=>x.sourceType==='cheque'&&x.sourceId===id);
979 + assert.equal(correction.correctionType,'endorsement');
980 + assert.equal(correction.endorseeContactId,'endorsee-1');
981 + assert.equal(correction.originalDrawerContactId,'contact-1');
982 + assert.equal(correction.correlationId,statusJournal.id);
983 + assert.equal(correction.approvedBy,'phase2-admin');
984 + assert.equal(correction.amount,370);
985 + assert.equal((await db.getAll('treasury')).filter(x=>x.refChequeId===id).length,0);
986 + const audit=(await db.getAll('audit_events')).filter(x=>x.correlationId===statusJournal.id);
987 + assert.equal(audit.length,1);
988 + const once=await snapshot();
989 + await assert.rejects(retail.changeChequeStatus(id,'spent',{
990 + date,endorseeContactId:'endorsee-1',explicitlyApproved:true
991 + }),/در جریان/);
992 + assert.deepEqual(await snapshot(),once);
993 + const trial=await accounts.trialBalance();
994 + assert.equal(trial.totals.debit,trial.totals.credit);
995 +});
996 +
997 +test('A01: duplicate endorsement correction key rolls back GL, status and audit atomically',async()=>{
998 + await setup();
999 + await db.put('contacts',{id:'endorsee-1',companyId:company,name:'تأمین‌کننده'});
1000 + const id='a01-endorsement-rollback';
1001 + await retail.postCheque({
1002 + id,direction:'inbound',status:'pending',issueDate:date,dueDate:date,
1003 + contactId:'contact-1',amount:120,chequeNumber:'E120'
1004 + });
1005 + const idem=JSON.stringify([company,'cheque_endorsement',id,2]);
1006 + await db.add('document_corrections',{
1007 + id:'collision-endorsement',companyId:company,sourceType:'cheque',sourceId:'fixture',
1008 + correctionType:'endorsement',idempotencyKey:idem
1009 + });
1010 + const before={
1011 + cheque:await db.get('cheques',id),
1012 + journals:(await db.getAll('journal_entries')).length,
1013 + lines:(await db.getAll('journal_lines')).length,
1014 + audit:(await db.getAll('audit_events')).length,
1015 + corrections:(await db.getAll('document_corrections')).length,
1016 + cash:(await db.getAll('treasury')).length
1017 + };
1018 + await assert.rejects(retail.changeChequeStatus(id,'spent',{
1019 + date,endorseeContactId:'endorsee-1',explicitlyApproved:true
1020 + }));
1021 + assert.deepEqual({
1022 + cheque:await db.get('cheques',id),
1023 + journals:(await db.getAll('journal_entries')).length,
1024 + lines:(await db.getAll('journal_lines')).length,
1025 + audit:(await db.getAll('audit_events')).length,
1026 + corrections:(await db.getAll('document_corrections')).length,
1027 + cash:(await db.getAll('treasury')).length
1028 + },before);
1029 + const trial=await accounts.trialBalance();
1030 + assert.equal(trial.totals.debit,trial.totals.credit);
1031 +});