web/website/tests/share-issuance.test.mjs
Dev 6deb4a2e7d DuneRealms V2 clean baseline
Owner-authorised history reset; current source tree preserved exactly.
2026-11-13 00:05:14 +01:00

11 lines
2.7 KiB
JavaScript

import test from 'node:test';import assert from 'node:assert/strict';import {issueShares} from '../node/share-ledger.mjs';
const classId='11111111-1111-1111-1111-111111111111',accountId='22222222-2222-2222-2222-222222222222',actorId='33333333-3333-3333-3333-333333333333';
const input={shareClassId:classId,accountId,actorId,referenceId:'issue:alpha-1',quantity:5,authorized:true};
function fake({supply='10',issued='3',previous=null,account=true}={}){const calls=[];return {calls,transaction:async fn=>fn({first:async(sql)=>{calls.push(sql);if(sql.includes('corrino_share_events'))return previous;if(sql.includes('corrino_share_classes'))return {authorized_shares:supply,issued_shares:issued};if(sql.includes('site_accounts'))return account?{id:accountId}:null;return null;},query:async(sql)=>{calls.push(sql);return {affectedRows:1};}})};}
test('authorized issue locks class and writes supply position and audit',async()=>{const db=fake();const result=await issueShares(db,input);assert.equal(result.created,true);assert.ok(db.calls.some(s=>s.includes('FOR UPDATE')));assert.equal(db.calls.filter(s=>s.startsWith('INSERT')).length,2);});
test('duplicate matching reference is idempotent',async()=>{const db=fake({previous:{id:'prior',share_class_id:classId,account_id:accountId,quantity:5,action:'issue'}});assert.deepEqual(await issueShares(db,input),{eventId:'prior',created:false});assert.equal(db.calls.length,1);});
test('conflicting reference is rejected',async()=>{const db=fake({previous:{id:'prior',share_class_id:classId,account_id:accountId,quantity:6,action:'issue'}});await assert.rejects(issueShares(db,input),/already used/);});
test('oversupply and missing account reject before writes',async()=>{for(const opts of [{supply:'5',issued:'3'},{account:false}]){const db=fake(opts);await assert.rejects(issueShares(db,input));assert.ok(!db.calls.some(s=>s.startsWith('INSERT')||s.startsWith('UPDATE')));}});
test('authorization required before transaction',async()=>{const db=fake();await assert.rejects(issueShares(db,{...input,authorized:false}),/not authorized/);assert.equal(db.calls.length,0);});
test('concurrent retry detected after class lock does not write again',async()=>{let checks=0;const calls=[];const db={transaction:async fn=>fn({first:async(sql)=>{calls.push(sql);if(sql.includes('corrino_share_events'))return ++checks===1?null:{id:'prior',share_class_id:classId,account_id:accountId,quantity:5,action:'issue'};if(sql.includes('corrino_share_classes'))return {authorized_shares:'10',issued_shares:'8'};throw Error('Should not query account');},query:async()=>{throw Error('Should not write');}})};assert.deepEqual(await issueShares(db,input),{eventId:'prior',created:false});assert.equal(checks,2);});