forked from unimatrix/web
25 lines
5.5 KiB
JavaScript
25 lines
5.5 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import {randomUUID} from 'node:crypto';
|
|
import {BankFixture} from './postbank-fixture.mjs';
|
|
import {createOfficeAuctions,OFFICE_AUCTION_SCHEMA,OFFICE_AUCTION_RULES} from '../node/jarvis-office-auctions.mjs';
|
|
|
|
async function fixture(){
|
|
const bank=new BankFixture();for(const sql of OFFICE_AUCTION_SCHEMA)await bank.query(sql.replace(/ ENGINE=InnoDB DEFAULT CHARSET=utf8mb4/g,''));
|
|
bank.sqlite.exec('CREATE TABLE test_holds(id TEXT PRIMARY KEY,user_id TEXT,amount INT,state TEXT);CREATE TABLE test_awards(auction_id TEXT PRIMARY KEY,winner_id TEXT,method TEXT);CREATE TABLE test_tax(amount INT);');
|
|
const users=[{id:randomUUID()},{id:randomUUID()}],revoked=new Set();let now=1000,draws=0,failAward=false;
|
|
const service=createOfficeAuctions(bank,{clock:()=>now,draw:n=>{draws++;return n-1;},eligible:async(tx,actor)=>!revoked.has(actor.id),
|
|
reserve:async(tx,{actor,amountMinor})=>{if(amountMinor>50000)throw Error('Insufficient funds');const id=randomUUID();await tx.query("INSERT INTO test_holds VALUES(?,?,?,'reserved')",[id,actor.id,amountMinor]);return id;},
|
|
release:async(tx,{holdId})=>tx.query("UPDATE test_holds SET state='released' WHERE id=?",[holdId]),
|
|
settlePayment:async(tx,{holdId,amountMinor,recipient})=>{assert.equal(recipient,'sys_tax_treasury');await tx.query("UPDATE test_holds SET state='posted' WHERE id=?",[holdId]);await tx.query('INSERT INTO test_tax VALUES(?)',[amountMinor]);},
|
|
award:async(tx,{auctionId,winnerId,method})=>{if(failAward)throw Error('Transfer unavailable');await tx.query('INSERT INTO test_awards VALUES(?,?,?)',[auctionId,winnerId,method]);}
|
|
});
|
|
const input={sourceId:randomUUID(),officeKey:'emperor',candidateIds:users.map(u=>u.id),opensAt:now},auction=await service.open(input);
|
|
const bid=(who=0,amount=10000,revision=1)=>({actor:users[who],input:{auctionId:auction.id,requestId:randomUUID(),amountMinor:amount,expectedRevision:revision,sourceAccount:'1'}});
|
|
return {bank,users,revoked,service,input,auction,bid,closeTime:()=>now=1000+OFFICE_AUCTION_RULES.durationMs,draws:()=>draws,failAward:()=>failAward=true};
|
|
}
|
|
test('office auction source is idempotent and public cards omit candidate identities and reservation references',async()=>{const f=await fixture();try{assert.equal((await f.service.open(f.input)).replayed,true);await assert.rejects(f.service.open({...f.input,officeKey:'house_leader'}));const rows=await f.service.browse();assert.equal(rows.length,1);assert.equal(rows[0].closesAt,10801000);assert.equal(rows[0].minimumBidMinor,10000);for(const key of ['candidate_ids','hold_id','highest_user_id'])assert.equal(key in rows[0],false);}finally{f.bank.close();}});
|
|
test('only tied eligible candidates bid; stale, undersized and repeated mismatched bids never reserve funds',async()=>{const f=await fixture();try{let b=f.bid();await assert.rejects(f.service.bid({id:randomUUID()},b.input));const first=await f.service.bid(b.actor,b.input);assert.equal(first.revision,2);assert.equal((await f.service.bid(b.actor,b.input)).replayed,true);await assert.rejects(f.service.bid(b.actor,{...b.input,amountMinor:11000}));b=f.bid(1,10999,2);await assert.rejects(f.service.bid(b.actor,b.input));b=f.bid(1,11000,1);await assert.rejects(f.service.bid(b.actor,b.input));assert.equal((await f.bank.first('SELECT COUNT(*) n FROM test_holds')).n,1);f.revoked.add(f.users[1].id);b=f.bid(1,11000,2);await assert.rejects(f.service.bid(b.actor,b.input));}finally{f.bank.close();}});
|
|
test('competing bids serialize; an outbid hold is released and failed replacement rolls back',async()=>{const f=await fixture();try{const a=f.bid(),b=f.bid(1);const attempts=await Promise.allSettled([f.service.bid(a.actor,a.input),f.service.bid(b.actor,b.input)]);assert.equal(attempts.filter(x=>x.status==='fulfilled').length,1);let next=f.bid(1,11000,2);await f.service.bid(next.actor,next.input);next=f.bid(0,60000,3);await assert.rejects(f.service.bid(next.actor,next.input));assert.equal((await f.bank.first("SELECT COUNT(*) n FROM test_holds WHERE state='reserved'")).n,1);assert.equal((await f.bank.first("SELECT COUNT(*) n FROM test_holds WHERE state='released'")).n,1);f.closeTime();const result=await f.service.close(f.auction.id);assert.equal(result.winnerId,f.users[1].id);assert.equal((await f.bank.first('SELECT SUM(amount) total FROM test_tax')).total,11000);assert.equal((await f.service.close(f.auction.id)).replayed,true);assert.equal((await f.bank.first('SELECT COUNT(*) n FROM test_tax')).n,1);}finally{f.bank.close();}});
|
|
test('no-bid lottery is persisted once and never rerolled after success',async()=>{const f=await fixture();try{await assert.rejects(f.service.close(f.auction.id));f.closeTime();const result=await f.service.close(f.auction.id);assert.equal(result.method,'lottery');assert.equal(result.winnerId,f.users.map(u=>u.id).sort()[0]);assert.equal((await f.service.close(f.auction.id)).winnerId,result.winnerId);assert.equal(f.draws(),1);assert.equal((await f.bank.first('SELECT COUNT(*) n FROM test_tax')).n,0);const b=f.bid();await assert.rejects(f.service.bid(b.actor,b.input));}finally{f.bank.close();}});
|
|
test('deed transfer failure rolls back payment and finalization together',async()=>{const f=await fixture();try{const b=f.bid();await f.service.bid(b.actor,b.input);f.closeTime();f.failAward();await assert.rejects(f.service.close(f.auction.id));assert.equal((await f.bank.first('SELECT COUNT(*) n FROM test_tax')).n,0);assert.equal((await f.bank.first("SELECT state FROM test_holds")).state,'reserved');assert.equal((await f.bank.first('SELECT state FROM jarvis_office_auctions')).state,'open');}finally{f.bank.close();}});
|

Staff