forked from unimatrix/web
14 lines
3.3 KiB
JavaScript
14 lines
3.3 KiB
JavaScript
import test from 'node:test';import assert from 'node:assert/strict';import {randomUUID} from 'node:crypto';
|
|
import {mountItemPublication} from '../control-panel/public/cp-v2/cp-v2-item-publication.mjs';
|
|
class Element{constructor(tag){this.tagName=tag.toUpperCase();this.children=[];this.textContent='';this.attributes={}}append(...nodes){this.children.push(...nodes)}replaceChildren(...nodes){this.children=nodes;this._value=undefined}setAttribute(k,v){this.attributes[k]=v}get value(){return this._value??(this.tagName==='SELECT'?this.children[0]?.value:'')??''}set value(v){this._value=v}select(){}}
|
|
const all=node=>[node,...node.children.flatMap(all)],settle=()=>new Promise(r=>setImmediate(r));
|
|
test('restored staff publisher retries the same request, then locks published price/stock and exposes withdrawal',async()=>{
|
|
const previous=documentFixture(),host=new Element('section'),calls=[];let published=false;
|
|
try{const dispose=mountItemPublication(host,{fetcher:async(path,options)=>{if(options?.method==='POST'){calls.push(JSON.parse(options.body));if(calls.length===1)return Response.json({message:'retry'},{status:503});published=true;return Response.json({listingId:'9'});}return Response.json({definitions:[{id:'definition',keyId:randomUUID(),revision:3,design:{name:'Core'},state:published?'published':'draft',publication:published?{listingId:'9',state:'published'}:null}]});}});await settle();
|
|
const market=all(host).filter(n=>n.tagName==='FORM')[1],inputs=all(market).filter(n=>n.tagName==='INPUT'),button=all(market).find(n=>n.tagName==='BUTTON');inputs[0].value='2';inputs[1].value='500';inputs[2].checked=true;await market.onsubmit({preventDefault(){}});await market.onsubmit({preventDefault(){}});assert.equal(calls.length,2);assert.equal(calls[0].requestId,calls[1].requestId);assert.equal(calls[0].priceMinor,50000);assert.equal(calls[0].quantity,2);assert(button.disabled);assert(inputs[0].disabled&&inputs[1].disabled);inputs[2].checked=true;await market.onsubmit({preventDefault(){}});assert.equal(calls.length,2);assert(all(host).some(n=>n.textContent==='Withdraw official listing'));dispose();
|
|
}finally{globalThis.document=previous;}
|
|
});
|
|
test('an existing signed listing is connected instead of creating duplicate stock',async()=>{
|
|
const previous=documentFixture(),host=new Element('section'),calls=[],keyId=randomUUID();try{const dispose=mountItemPublication(host,{fetcher:async(path,options)=>{if(options?.method==='POST'){calls.push(JSON.parse(options.body));return Response.json({listingId:'9'});}return Response.json({definitions:[{id:randomUUID(),keyId,revision:3,design:{name:'Chopper'},state:'draft',publication:{listingId:'9',state:'unsealed',quantity:1,priceMinor:50000}}]});}});await settle();const market=all(host).filter(n=>n.tagName==='FORM')[1],inputs=all(market).filter(n=>n.tagName==='INPUT');assert.equal(all(market).find(n=>n.tagName==='BUTTON').textContent,'Connect existing official listing');assert(inputs[0].disabled&&inputs[1].disabled);inputs[2].checked=true;await market.onsubmit({preventDefault(){}});assert.deepEqual(calls,[{action:'seal-publication',keyId,listingId:'9',confirmed:true}]);dispose();}finally{globalThis.document=previous;}
|
|
});
|
|
function documentFixture(){const previous=globalThis.document;globalThis.document={createElement:tag=>new Element(tag)};return previous;}
|

Staff