web/website/tests/cp-v2-user-receipts.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

26 lines
2.7 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import {readFile} from 'node:fs/promises';
import {privateKitCodes,redemptionRequest} from '../control-panel/public/cp-v2/cp-v2-rewards.mjs';
import {ledgerRows} from '../control-panel/public/cp-v2/cp-v2-user-commerce.mjs';
import {NATIVE_SERVICES,nativeServiceAccess} from '../control-panel/public/cp-v2/cp-v2-native-services.mjs';
const kit='DRK-'+'A'.repeat(32),discovery='DRQ-'+'A'.repeat(40);
test('private codes are restricted to unclaimed purchases, never seller receipts or confirmed deliveries',()=>{
const orders=[{side:'buyer',status:'purchased',code:kit},{side:'seller',status:'pending',code:kit},{side:'buyer',status:'delivered',code:kit},{side:'buyer',status:'queued',code:kit},{side:'buyer',status:'unclaimed',code:'invalid'}];
assert.deepEqual(privateKitCodes(orders),[orders[0]]);assert.throws(()=>privateKitCodes({}));
});
test('redemption routes only exact supported private codes and requires deliberate consent',()=>{
assert.deepEqual(redemptionRequest(kit.toLowerCase(),true),{path:'/api/kits/redeem',body:{code:kit},kind:'kit'});
assert.deepEqual(redemptionRequest(discovery,true),{path:'/api/quest/rewards/claim',body:{code:discovery},kind:'discovery'});
assert.throws(()=>redemptionRequest(kit,false));assert.throws(()=>redemptionRequest('DRK-123',true));assert.throws(()=>redemptionRequest('DRU-'+'A'.repeat(48),true),/Imperial Tailor/);
});
test('ledger filters references, counterparties and directions while preserving original balances',()=>{
const ledger=Array.from({length:44},(_,i)=>({memo:i%2?'Personal transfer':'alpha launch',action:'credit',amountDunemarks:i%2?'-5.00':'5.00',balanceDunemarks:'100.00',createdAt:i,fromName:'Source',toName:'Citizen'}));
const model=ledgerRows({linked:true,bank:{ledger}},{direction:'credit',query:'alpha'});assert.equal(model.rows.length,20);assert.equal(model.total,22);assert.equal(model.pages,2);assert.equal(model.rows[0].createdAt,42);
assert.equal(ledger[0].balanceDunemarks,'100.00');assert.equal(ledgerRows({linked:false}).linked,false);assert.throws(()=>ledgerRows({linked:true}));
});
test('restored personal bank uses the production API and leaves the disabled staging adapter untouched',async()=>{
const client=await readFile(new URL('../control-panel/public/banking.mjs',import.meta.url),'utf8');assert.match(client,/staff\?'\/api\/bank\/staff':'\/api\/bank\/personal'/);assert.doesNotMatch(client,/\/api\/v1\/me\/bank/);
const spec=NATIVE_SERVICES['personal-bank'];assert.equal(nativeServiceAccess({permissions:[]},spec),false);assert.equal(nativeServiceAccess({permissions:['web.bank.personal.view']},spec),true);assert.equal(spec.url,'https://cp.dunerealms.com/user-service.html?service=bank#personal-bank');
});