web/website/tests/deed-purchase-form.test.mjs

16 lines
3.7 KiB
JavaScript

import test from 'node:test';import assert from 'node:assert/strict';import vm from 'node:vm';import {readFile} from 'node:fs/promises';
const listing='11111111-1111-4111-8111-111111111111',parent='22222222-2222-4222-8222-222222222222';
async function mount(request,search='?listing='+listing){
const nodes=[];function el(tag,text,cls){const n={tag,textContent:text||'',className:cls,children:[],style:{},classList:{add(){}},value:'',hidden:false,disabled:false,checked:false,append(...children){this.children.push(...children);if(tag==='select'&&!this.value&&children[0])this.value=children[0].value;},replaceChildren(...children){this.children=children;this.value='';this.append(...children);},setAttribute(){},reportValidity(){return true;},get options(){return this.children;}};nodes.push(n);return n;}
const frame={root:el('section'),content:el('div'),status:el('p')},surfaces=new Map();let ready;
const source=(await readFile(new URL('../control-panel/public/cp-v2/cp-v2-deed-purchase.mjs',import.meta.url),'utf8')).replace(/^import .*;\r?\n/gm,'').replaceAll('import.meta.url',JSON.stringify('https://cp.dunerealms.com/v2/cp-v2-deed-purchase.mjs'));
vm.runInNewContext(source,{URL,AbortController,fetch:()=>{throw Error("Unexpected transport");},crypto:{randomUUID:()=> '33333333-3333-4333-8333-333333333333'},location:{href:'https://cp.dunerealms.com/deed-purchase'+search},history:{replaceState(){}},window:{addEventListener(){}},document:{head:el('head'),querySelector(selector){if(!surfaces.has(selector))surfaces.set(selector,el('div'));return surfaces.get(selector);}},el,createUserAPI:()=>({request}),createCPV2ContentPage:()=>frame,startCPAuthentication:fn=>{ready=fn();}});await ready;return {nodes,frame};
}
const quote={revision:1,name:'Test Company',kind:'company',priceMinor:2500,terms:'Patronage terms',recipientName:'Seller',recipientNumber:'DM12',recipientAccount:'2',parents:[{id:parent,kind:'company',name:'Buyer',entityId:'company:'+parent}],accounts:[{id:'1',entityId:'company:'+parent,name:'Buyer account',number:'DM11',available:'100.00'}]};
test('purchase form confirms the displayed quote and retries an uncertain response using the identical intent',async()=>{
const writes=[];const {nodes,frame}=await mount(async(path,input)=>{if(!input){if(path.includes('/status'))return {purchaseId:'33333333-3333-4333-8333-333333333333',state:'funded',nextProcessingAt:1234,message:'Reserved'};return quote;}writes.push(JSON.stringify(input));if(writes.length===1)throw Object.assign(Error('Connection lost'),{uncertain:true});return {purchaseId:input.requestId};});
const form=nodes.find(n=>n.tag==='form'),check=nodes.find(n=>n.type==='checkbox'),send=nodes.find(n=>n.type==='submit');check.checked=true;await form.onsubmit({preventDefault(){}});assert.equal(send.disabled,false);assert.equal(send.textContent,'Check the same purchase');await form.onsubmit({preventDefault(){}});assert.equal(writes.length,2);assert.equal(writes[0],writes[1]);const input=JSON.parse(writes[0]);assert.equal(input.expectedPriceMinor,2500);assert.equal(input.sourceAccount,'1');assert.equal(input.parentId,parent);assert.ok(frame.content.children.some(n=>n.textContent==='Purchase reserved'));
});
test('a persisted purchase link reads status without sending another purchase or reloading an unavailable offer',async()=>{
const calls=[];const id='33333333-3333-4333-8333-333333333333';const {frame}=await mount(async(path,input)=>{calls.push({path,input});return {purchaseId:id,complete:true,state:'posted',message:'Completed'};},'?purchase='+id);assert.equal(calls.length,1);assert.equal(calls[0].path,'/api/jarvis/deed-purchase/status?id='+id);assert.equal(calls[0].input,undefined);assert.ok(frame.content.children.some(n=>n.textContent==='Acquisition completed'));
});