MC/website/tests/shop-sales.test.mjs
2026-11-27 23:43:05 +00:00

6 lines
3.9 KiB
JavaScript

import test from 'node:test';import assert from 'node:assert/strict';import {readFileSync} from 'node:fs';import {BankFixture} from './postbank-fixture.mjs';import {createShopSales,projectShopSales} from '../node/shop-sales.mjs';
const product={id:76,name:'House sash',url:'https://shop.dunerealms.com/catalog/EUR/catalog/product?products_id=76',image:'/images/sash.png',regularPrice:'€100.00',salePrice:'€80.00',savingPercent:20};const feed=()=>({source:'oscommerce',currency:'EUR',items:[product]});const request=()=>new Request('https://www.dunerealms.com/api/shop-sales');
test('sales display only validated osCommerce products, prices and trusted shop links',()=>{const result=projectShopSales(feed());assert.equal(result.items[0].salePrice,'€80.00');assert.equal(result.items[0].image,'https://shop.dunerealms.com/images/sash.png');assert.throws(()=>projectShopSales({...feed(),source:'azuriom'}));for(const patch of [{url:'https://evil.example/product'},{url:'javascript:alert(1)'},{savingPercent:0}])assert.equal(projectShopSales({...feed(),items:[{...product,...patch}]}).items.length,0);assert.equal(projectShopSales({...feed(),items:[{...product,image:'https://evil.example/tracker'}]}).items[0].image,null);});
test('public sales reads share one bounded cache, refresh after a minute and never forward account credentials',async()=>{let count=0,now=0;const api=createShopSales({clock:()=>now,fetcher:async(url,options)=>{count++;assert.equal(url,'https://shop.dunerealms.com/catalog/dune-shop/sales-feed?currency=EUR');assert.equal(options.redirect,'error');assert.equal(options.headers.Cookie,undefined);return Response.json(feed());}});const responses=await Promise.all([api(request()),api(request()),api(request())]);assert.equal(count,1);assert.ok(responses.every(r=>r.status===200));await api(request());assert.equal(count,1);now=60001;await api(request());assert.equal(count,2);assert.equal((await api(new Request(request().url,{method:'POST'}))).status,405);assert.equal((await api(new Request(request().url+'?customer=other'))).status,400);});
test('unavailable or unrelated shop data produces an honest unavailable state rather than fabricated sales',async()=>{const api=createShopSales({fetcher:async()=>Response.json({source:'azuriom',items:[]})});assert.equal((await api(request())).status,503);const empty=createShopSales({fetcher:async()=>Response.json({...feed(),items:[]})});assert.deepEqual((await(await empty(request())).json()).items,[]);});
test('native osCommerce sale query excludes expired, future, disabled and other-platform offers',async()=>{const db=new BankFixture();try{db.sqlite.function('NOW',()=> '2026-11-20 20:00:00');await db.query('CREATE TABLE products(products_id INTEGER,products_price INTEGER,products_tax_class_id INTEGER,products_status INTEGER)');await db.query('CREATE TABLE platforms_products(products_id INTEGER,platform_id INTEGER)');await db.query('CREATE TABLE products_description(products_id INTEGER,products_name TEXT,language_id INTEGER,platform_id INTEGER)');await db.query('CREATE TABLE specials(products_id INTEGER,specials_new_products_price INTEGER,status INTEGER,start_date TEXT,expires_date TEXT)');for(let id=1;id<=6;id++){await db.query('INSERT INTO products VALUES(?,100,1,?)',[id,id===5?0:1]);await db.query('INSERT INTO platforms_products VALUES(?,?)',[id,id===6?8:7]);await db.query('INSERT INTO products_description VALUES(?,?,1,7)',[id,'Product '+id]);await db.query('INSERT INTO specials VALUES(?,80,?,?,?)',[id,id===4?0:1,id===2?'2026-11-21 00:00:00':null,id===3?'2026-11-19 00:00:00':null]);}const source=readFileSync(new URL('../ops/oscommerce/DuneShopController.php',import.meta.url),'utf8'),action=source.slice(source.indexOf('function actionSalesFeed'),source.indexOf('function actionIndex'));const sql=action.split('createCommand("')[1].split('")->queryAll')[0];assert.deepEqual((await db.query(sql)).map(r=>r.products_id),[1]);}finally{db.close();}});