@davoodmehraban89 / Factor-easy / commits / 8aec35b
Fork 0

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

Test atomic restore and posted-history protection

davoodmehraban89 committed Sep 29, 2026 at 22:19 UTC 8aec35b8cfdc9ecf7f68cfeefdeb24b4475a0f3b
1 file changed +7
scripts/phase5-behavior-check.mjs
+7
@@ -11,3 +11,10 @@ let rejected=false;try{D.validateMovement({kind:'transfer',productId:'P1',wareho
11 D.validateRestore({formatVersion:5,warehouses:[]},['warehouses']);rejected=false;try{D.validateRestore({evil:[]},['warehouses'])}catch{rejected=true}if(!rejected)throw new Error('unknown restore collection accepted');
12 const app=fs.readFileSync('js/phase5-enterprise.js','utf8');for(const x of ['p5PostMovement','p5SaveCount','p5RunDepreciation','p5SaveRate','p5SaveCostCenter','p5PreviewRestore','p5QueueEvent','status:\'posted\''])if(!app.includes(x))throw new Error('Phase5 application invariant missing '+x);
13 console.log('Phase 5 behavioral/domain scenarios passed.');
14 +const syncSrc=fs.readFileSync('js/sync.js','utf8');const syncCtx={console,currentUser:{id:'u1'},COLLS:[],neutralize:x=>JSON.parse(JSON.stringify(x)),safeId:(id,p)=>String(id||p),document:{getElementById:()=>null},setTimeout:()=>0,clearTimeout:()=>{},localStorage:{getItem:()=>null,setItem:()=>{}},confirm:()=>true,alert:()=>{},refreshAllSurfaces:()=>{},requireWrite:()=>true};vm.createContext(syncCtx);vm.runInContext(syncSrc,syncCtx);
15 +vm.runInContext("datastore.products.push({id:'P1',ownerUserId:'u1',name:'Before'});datastore.journalVouchers.push({id:'JV1',ownerUserId:'u1',status:'posted'});datastore.journalLines.push({id:'JL1',ownerUserId:'u1',voucherId:'JV1',debit:100,credit:0});",syncCtx);
16 +let restoreRejected=false;try{vm.runInContext("absorbRecords({products:[{id:'P1',name:'After'}],journalLines:[{id:'JL1',voucherId:'JV1',debit:999,credit:0}]})",syncCtx)}catch{restoreRejected=true}if(!restoreRejected)throw new Error('restore overwrote posted journal history');
17 +const atomicState=vm.runInContext("({product:datastore.products.find(x=>x.id==='P1').name,debit:datastore.journalLines.find(x=>x.id==='JL1').debit})",syncCtx);if(atomicState.product!=='Before'||atomicState.debit!==100)throw new Error('failed restore was not atomic');
18 +vm.runInContext("absorbRecords({products:[{id:'P1',name:'After'}]})",syncCtx);if(vm.runInContext("datastore.products.find(x=>x.id==='P1').name",syncCtx)!=='After')throw new Error('safe mutable restore merge failed');
19 +console.log('Phase 5 restore atomicity and posted-history protection passed.');
20 +