@davoodmehraban89 / Factor-easy / commits / 034dd60
Fork 0

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

test: model atomic source linking and local-only retry freeze

davoodmehraban89 committed Nov 17, 2026 at 14:17 UTC 034dd609a965f3b906305e8042eb3feff633d30e
1 file changed +22 -4
scripts/journal-rpc-adapter.test.mjs
+22 -4
@@ -144,24 +144,38 @@ function sourceHarness(kind='sale',{journalDirty=false,failSourceSync=false,lose
144 const rows={[collection]:[source]};
145 h.context.getMyInvoices=()=>rows.invoices||[];
146 h.context.getMyPurchases=()=>rows.purchases||[];
147 - h.context.stripOwner=item=>{const x={...item};delete x.ownerUserId;delete x._serverVersion;return x;};
147 + h.context.stripOwner=item=>{const x={...item};delete x.ownerUserId;delete x._serverVersion;delete x.accountingPostingPending;delete x.accountingPostingPendingAt;return x;};
148 h.context.syncSnap=new Map();
149 h.context.syncRunning=false;h.context.syncPending=false;h.context.syncFailCount=0;
150 h.context.computeDiff=()=>journalDirty?{up:[{coll:'journalLines',key:'journalLines|LOCAL'}],del:[]}:{up:[],del:[]};
151 h.context.saveDatastore=()=>{h.context.syncPending=true;};
152 + let serverSource={...source},serverVoucher=null;
153 h.context.runSync=async()=>{
154 if(failSourceSync){h.context.syncFailCount=1;h.context.syncPending=true;return;}
154 - h.context.syncSnap.set(collection+'|'+source.id,JSON.stringify(h.context.stripOwner(source)));
155 + const key=collection+'|'+source.id,payload=JSON.stringify(h.context.stripOwner(source));
156 + if(h.context.syncSnap.get(key)!==payload)serverSource={...h.context.stripOwner(source),ownerUserId:source.ownerUserId};
157 + h.context.syncSnap.set(key,payload);
158 h.context.syncPending=false;
159 };
157 - h.context.pullAll=async()=>{};
160 + h.context.pullAll=async()=>{
161 + delete source.accountingPostingPending;delete source.accountingPostingPendingAt;
162 + Object.assign(source,serverSource);
163 + if(serverVoucher&&!h.records.journalVouchers.some(v=>v.id===serverVoucher.id))h.records.journalVouchers.push(serverVoucher);
164 + };
165 let first=true;
166 h.context.sb.rpc=async(name,args)=>{
167 h.calls.push({name,args});
168 if(name==='finora_journal_autopost'){
169 if(rejectWithCode)return {data:null,error:{message:'synthetic posting validation failure',code:'23514'}};
170 + const voucher=serverVoucher||{id:'JV_SERVER_'+kind,status:'posted',version:2,postedAt:'1405-07-19T10:00:00Z',postedBy:'USER_A'};
171 + if(!serverVoucher){
172 + serverVoucher=voucher;
173 + serverSource={...serverSource,journalVoucherId:voucher.id,accountingStatus:'posted',
174 + accountingPostedAt:voucher.postedAt,documentStatus:'posted',documentPostedAt:voucher.postedAt,
175 + documentPostedBy:voucher.postedBy,documentVersion:1};
176 + }
177 if(loseFirstResponse&&first){first=false;throw new Error('network response lost after server commit');}
164 - return {data:{voucher:{id:'JV_SERVER_'+kind,status:'posted',version:2,postedAt:'1405-07-19T10:00:00Z'}},error:null};
178 + return {data:{voucher},error:null};
179 }
180 return {data:null,error:null};
181 };
@@ -184,6 +198,7 @@ test('sale auto-post persists source first and delegates authoritative lines to
198 assert.equal(h.source.journalVoucherId,v.id);
199 assert.equal(h.source.accountingStatus,'posted');
200 assert.equal(h.source.documentStatus,'posted');
201 + assert.equal(h.source.accountingPostingPending,undefined);
202 assert.equal(h.calls.some(x=>x.name==='finora_sync_records'),false);
203 });
204
@@ -198,6 +213,7 @@ test('purchase auto-post uses the same server-owned source contract with purchas
213 assert.equal(h.source.journalVoucherId,v.id);
214 assert.equal(h.source.accountingStatus,'posted');
215 assert.equal(h.source.documentStatus,'posted');
216 + assert.equal(h.source.accountingPostingPending,undefined);
217 });
218
219 test('auto-post fails closed when local generic sync contains dirty journal rows',async()=>{
@@ -222,6 +238,7 @@ test('ambiguous auto-post retry reuses source identity and accepts server idempo
238 assert.equal(calls.length,2);
239 assert.equal(calls[0].args.p_source_id,calls[1].args.p_source_id);
240 assert.equal(calls[0].args.p_source_collection,calls[1].args.p_source_collection);
241 + assert.notEqual(calls[0].args.p_voucher_id,calls[1].args.p_voucher_id);
242 assert.equal(v.id,'JV_SERVER_sale');
243 });
244
@@ -253,6 +270,7 @@ test('sync hydration reads database row version required by journal CAS operatio
270 assert.match(syncSource,/select\('collection,id,data,version'\)/);
271 assert.match(syncSource,/obj\._serverVersion=Number\(r\.version\)/);
272 assert.match(syncSource,/p_organization_id:currentUser\.organizationId\|\|null/);
273 + assert.match(syncSource,/delete c\.accountingPostingPending/);
274 });
275
276