web/website/tests/exchange-public-control.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

14 lines
2.9 KiB
JavaScript

import test from 'node:test';import assert from 'node:assert/strict';
import {createPublicExchange} from '../node/exchange-public.mjs';import {LocalD1} from './d1-adapter.mjs';
import {exchangeRegister} from '../control-panel/public/cp-v2/cp-v2-exchange-authority.mjs';
test('paused public listings never call the live bridge or return fictional offers',async()=>{const db=new LocalD1('CREATE TABLE website_settings(setting_key TEXT,setting_value TEXT);');try{db.sqlite.exec("INSERT INTO website_settings VALUES('exchange_public_enabled','false')");let calls=0;const app=createPublicExchange(db,{}, {gateway:async()=>{calls++;return Response.json({listings:[{id:1}]});}});const response=await app(new Request('https://www.dunerealms.com/api/exchange/listings'));assert.equal(response.status,503);assert.equal((await response.json()).error,'market_paused');assert.equal(calls,0);}finally{db.close();}});
test('enabled public listings retain the existing sanitized bridge and reject writes',async()=>{const db=new LocalD1('CREATE TABLE website_settings(setting_key TEXT,setting_value TEXT);');try{let calls=0;const app=createPublicExchange(db,{}, {gateway:async request=>{calls++;assert.equal(new URL(request.url).pathname,'/api/exchange/listings');return Response.json({listings:[]});}});assert.deepEqual(await(await app(new Request('https://www.dunerealms.com/api/exchange/listings'))).json(),{listings:[]});assert.equal((await app(new Request('https://www.dunerealms.com/api/exchange/listings',{method:'POST'}))).status,405);assert.equal(calls,1);}finally{db.close();}});
test('staff exchange projection preserves decimal text and distinguishes disconnected sources',()=>{const data={enabled:true,connected:false,listings:[]};assert.equal(exchangeRegister(data).connected,false);const row={material:'SPICE',seller_name:'Trader',quantity:2,price:'1234.56'};assert.equal(exchangeRegister({...data,connected:true,listings:[row]}).listings[0].price,'1234.56');assert.throws(()=>exchangeRegister({...data,enabled:'yes'}));assert.throws(()=>exchangeRegister({...data,listings:[{...row,price:'invalid'}]}));});
// Exercise the production SQL through the MySQL adapter, including driver field names.
import {STAFF_EXCHANGE_QUERY} from '../control-panel/community-admin.mjs';
import {openGameMysqlPool} from '../node/game-mysql-pool.mjs';
test('staff exchange SQL retains named id and decimal price through MySQL conversion',async()=>{
const pool=await openGameMysqlPool('mysql://fixture:fixture@localhost/fixture',{mysql:{createPool(){return{async query({sql}){assert.match(sql,/CAST\(id AS CHAR\) AS id/);assert.match(sql,/CAST\(price AS CHAR\) AS price/);return [[{id:'1',material:'DIAMOND',seller_name:'Trader',quantity:1,price:'100.50'}]];},async end(){}};}}});
const {rows}=await pool.query({text:STAFF_EXCHANGE_QUERY});assert.equal(exchangeRegister({enabled:true,connected:true,listings:rows}).listings[0].price,'100.50');await pool.end();
});