web/website/tests/jarvis-auction-payments.test.mjs

7 lines
3.9 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 {PostbankService} from '../node/postbank-service.mjs';import {BANK_VERSION} from '../node/postbank-catalogue.mjs';import {entityId} from '../node/postbank-authority.mjs';
import {createAuctionPayments,AUCTION_HOLD_SCHEMA} from '../node/jarvis-auction-payments.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,AUCTION_HOLD_SCHEMA])await bank.query(sql.replace(/ ENGINE=InnoDB DEFAULT CHARSET=utf8mb4/g,''));const actor={id:randomUUID(),username:'Bidder'},other={id:randomUUID()},entity={id:entityId('clan',randomUUID()),kind:'clan',key:'test-clan',name:'Test clan'};let allowed=true,now=1000;const authority=async u=>({entities:[{...entity,canView:u.id===actor.id,canActivate:u.id===actor.id,canPay:allowed&&u.id===actor.id}]});await new PostbankService(bank,authority).activate(actor,{entityId:entity.id,packageKey:'pb_connect',termsVersion:BANK_VERSION,accepted:true,requestId:randomUUID()});const source=await bank.first("SELECT * FROM pb_accounts WHERE type_code='105'");await bank.query('UPDATE pb_accounts SET balance_minor=30000 WHERE id=?',[source.id]);await bank.query("INSERT INTO pb_accounts(system_key,type_code,display_name,balance_minor,state,created_at) VALUES('sys_tax_treasury','979','Tax',0,'active',1)");const payments=createAuctionPayments(bank,authority,{clock:()=>now});const service=createOfficeAuctions(bank,{...payments,clock:()=>now,eligible:async()=>true,award:async()=>{}}),auction=await service.open({sourceId:randomUUID(),officeKey:'emperor',candidateIds:[actor.id,other.id],opensAt:now});return{bank,actor,other,source,service,auction,close:()=>now+=OFFICE_AUCTION_RULES.durationMs,revoke:()=>allowed=false,bid:(amount,revision)=>({auctionId:auction.id,requestId:randomUUID(),amountMinor:amount,expectedRevision:revision,sourceAccount:String(source.id)})};}
test('office bids reserve available money immediately; a same-account increase replaces the hold and final payment uses double-entry Tax posting once',async()=>{const f=await fixture();try{await f.service.bid(f.actor,f.bid(10000,1));let source=await f.bank.first('SELECT * FROM pb_accounts WHERE id=?',[f.source.id]);assert.equal(source.balance_minor,30000);assert.equal(source.reserved_minor,10000);await f.service.bid(f.actor,f.bid(25000,2));source=await f.bank.first('SELECT * FROM pb_accounts WHERE id=?',[f.source.id]);assert.equal(source.reserved_minor,25000);await assert.rejects(f.service.bid(f.actor,f.bid(31000,3)));assert.equal((await f.bank.first('SELECT reserved_minor FROM pb_accounts WHERE id=?',[f.source.id])).reserved_minor,25000);f.close();await f.service.close(f.auction.id);await f.service.close(f.auction.id);source=await f.bank.first('SELECT * FROM pb_accounts WHERE id=?',[f.source.id]);assert.equal(source.balance_minor,5000);assert.equal(source.reserved_minor,0);assert.equal((await f.bank.first("SELECT balance_minor FROM pb_accounts WHERE system_key='sys_tax_treasury'")).balance_minor,25000);assert.equal((await f.bank.first('SELECT SUM(amount_minor) total FROM pb_entries')).total,0);assert.equal((await f.bank.first('SELECT COUNT(*) n FROM pb_orders')).n,1);}finally{f.bank.close();}});
test('a tied candidate cannot bid from another entity account or after payment authority is revoked',async()=>{const f=await fixture();try{await assert.rejects(f.service.bid(f.other,f.bid(10000,1)),e=>e.status===403);f.revoke();await assert.rejects(f.service.bid(f.actor,f.bid(10000,1)),e=>e.status===403);assert.equal((await f.bank.first('SELECT reserved_minor FROM pb_accounts WHERE id=?',[f.source.id])).reserved_minor,0);assert.equal((await f.bank.first('SELECT COUNT(*) n FROM pb_auction_holds')).n,0);}finally{f.bank.close();}});