| 12345678910111213141516171819202122232425262728293031323334 |
- import test from 'node:test';
- import assert from 'node:assert/strict';
- import {checkPublication,unwrap,needsProductEnrichment,checkHistoricalPlan} from '../scripts/lib/research-enrichment.mjs';
- const receipt={archiveSha256:'a'.repeat(64)};
- const result=()=>({ok:true,applied:true,allRowsVerified:true,archiveSha256:receipt.archiveSha256,dataset:'research',businessRowsChanged:0,
- sourceArchivePreserved:true,delta:{changed:{},mergeReview:{conflictRecords:0}}});
- test('a successful HTTP wrapper cannot hide an unverified publication',()=>{
- assert.equal(unwrap({body:{result:{ok:false,error:'REJECTED'}}}).ok,false);
- assert.throws(()=>checkPublication({...result(),allRowsVerified:false},receipt),/PUBLICATION_NOT_VERIFIED/);
- assert.throws(()=>checkPublication({...result(),archiveSha256:'b'.repeat(64)},receipt),/PUBLICATION_NOT_VERIFIED/);
- });
- test('research enrichment rejects business writes and unexpected field changes',()=>{
- assert.doesNotThrow(()=>checkPublication(result(),receipt));
- assert.throws(()=>checkPublication({...result(),businessRowsChanged:1},receipt),/PUBLICATION_NOT_VERIFIED/);
- assert.throws(()=>checkPublication({...result(),delta:{changed:{reviews:1}}},receipt),/UNEXPECTED_CHANGES/);
- assert.throws(()=>checkPublication({...result(),delta:{mergeReview:{conflictRecords:1}}},receipt),/UNEXPECTED_CHANGES/);
- });
- test('publishing historical specifications cannot hide still-missing reviews',()=>{
- const product={missingReviews:true,missingVerifiedSpec:true,missingPrice:false};
- assert.equal(needsProductEnrichment(product,{specs:1,reviews:0,prices:0}),true);
- assert.equal(needsProductEnrichment(product,{specs:1,reviews:10,prices:0}),false);
- assert.equal(needsProductEnrichment({...product,missingPrice:true},{specs:1,reviews:10,prices:0}),true);
- assert.equal(needsProductEnrichment(product,undefined),true);
- });
- test('historical archive publication rejects count drift, overwritten facts and new products',()=>{
- const receipt={expectedProductionAddedSpecs:160,expectedProductionAddedReviews:0};
- const plan={ok:true,mergePlan:{ok:true,dataset:'research',businessRowsChanged:0,delta:{added:{specs:160},changed:{},mergeReview:{conflictRecords:0}}}};
- assert.doesNotThrow(()=>checkHistoricalPlan(plan,receipt));
- for(const delta of [{added:{specs:320}},{added:{specs:160,products:1}},{added:{specs:160,prices:1}},
- {added:{specs:160},changed:{products:1}},{added:{specs:160},mergeReview:{conflictRecords:1}}]){
- assert.throws(()=>checkHistoricalPlan({...plan,mergePlan:{...plan.mergePlan,delta}},receipt),/REQUIRES_REVIEW/);
- }
- });
|