MC/website/tests/feed-api.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

10 lines
2.4 KiB
JavaScript

import test from 'node:test';import assert from 'node:assert/strict';import {gzipSync} from 'node:zlib';import {handleFeed,decodeSnapshot,sanitizeOffer,category} from '../worker/feed-api.mjs';
const publisher='123456789012345678';
function encoded(snapshot,id='test'){const b64=gzipSync(JSON.stringify(snapshot)).toString('base64'),parts=Math.ceil(b64.length/40);return Array.from({length:parts},(_,i)=>({id:String(i),content:`DUNE_EXCHANGE ${id} ${i+1}/${parts} ${b64.slice(i*40,(i+1)*40)}`,author:{id:publisher},timestamp:new Date().toISOString()})).reverse()}
const snapshot=()=>({type:'dune_exchange',updatedAt:new Date().toISOString(),offers:[{id:'a',type:'contract',owner:'Paul',item:'Spice',quantity:64,price:null,status:'open'},{id:'sell',type:'sell',owner:'Economy',item:'Spice',quantity:null,price:15,status:'standing'}]});
test('Unconfigured feeds return no fabricated data',async()=>{const response=await handleFeed(new Request('https://example.test/api/exchange'),{});const data=await response.json();assert.equal(data.status,'not_connected');assert.deepEqual(data.offers,[])});
test('multipart snapshots assemble in reverse Discord order with correct prices',async()=>{const result=await decodeSnapshot(encoded(snapshot()),publisher);assert.equal(result.offers.length,2);assert.equal(result.offers[0].price,null);assert.equal(result.offers[1].price,15);assert.equal(result.offers[1].quantity,null)});
test('Incomplete and forged snapshots are rejected',async()=>{const messages=encoded(snapshot());assert.equal(await decodeSnapshot(messages.slice(1),publisher),null);assert.equal(await decodeSnapshot(messages,'987654321098765432'),null)});
test('Stale snapshots do not masquerade as live offers',async()=>{const data=snapshot();data.updatedAt=new Date(Date.now()-600000).toISOString();assert.equal(await decodeSnapshot(encoded(data),publisher),null)});
test('Malformed offers cannot inject prices or unsupported types',()=>{assert.equal(sanitizeOffer({type:'admin',item:'Spice',owner:'Paul'}),null);assert.equal(sanitizeOffer({type:'sell',item:'Spice',owner:'Paul',quantity:-1,price:15}),null);assert.equal(sanitizeOffer({type:'sell',item:'Spice',owner:'Paul',quantity:1,price:Infinity}),null)});
test('Announcements are assigned concrete categories',()=>{assert.equal(category('[Vaerkring] All worlds'), 'Guild');assert.equal(category('[CHOAM] New contracts'),'CHOAM');assert.equal(category('IMPERIAL APPOINTMENT'),'Imperial')});