6 lines
3.7 KiB
JavaScript
6 lines
3.7 KiB
JavaScript
import test from 'node:test';import assert from 'node:assert/strict';import {randomUUID} from 'node:crypto';
|
|
import {BankFixture} from './postbank-fixture.mjs';import {createElectionResults} from '../node/election-results.mjs';
|
|
import {roundSchema} from '../control-panel/election-rounds.mjs';import {ballotSchema} from '../control-panel/election-ballots.mjs';import {applicationSchema} from '../control-panel/election-applications.mjs';import {officeLedgerSchema} from '../control-panel/election-office-ledger.mjs';
|
|
async function fixture(){const db=new BankFixture();for(const sql of [...roundSchema,...ballotSchema,...applicationSchema,...officeLedgerSchema])await db.query(sql.replace(/ ENGINE=InnoDB/g,'').replace(/UNIQUE KEY \w+\s*\(/g,'UNIQUE('));db.sqlite.exec('CREATE TABLE site_accounts(id TEXT PRIMARY KEY,username TEXT,display_name TEXT)');const user=randomUUID(),application=randomUUID(),round=randomUUID(),ballot=randomUUID(),term=randomUUID(),auction=randomUUID();await db.query('INSERT INTO site_accounts VALUES(?,?,?)',[user,'candidate','Candidate']);await db.query("INSERT INTO political_applications VALUES(?,?,?,?,'house','fremen',0,'Programme','submitted',0)",[application,'APP-HSE-2026-000001',user,randomUUID()]);await db.query("INSERT INTO political_election_rounds VALUES(?,'house','fremen',0,0,0,10800000,'winner',?)",[round,application]);await db.query('INSERT INTO political_round_candidates VALUES(?,?)',[round,application]);await db.query("INSERT INTO political_ballots VALUES(?,?,?,?, 'house',0,'private voter allocations',1)",[ballot,round,randomUUID(),randomUUID()]);await db.query('INSERT INTO political_ballot_allocations VALUES(?,?,1)',[ballot,application]);await db.query("INSERT INTO political_office_terms VALUES(?,'house','fremen',0,?,?,?,?, 'house_leader',10800000,2419200000,'resigned',1)",[term,round,application,user,randomUUID()]);await db.query("INSERT INTO political_auction_triggers VALUES(?,'house','fremen',0,'[]','[]',0,'awarded',?,0)",[round,auction]);const central={prepare(sql){return{bind(...args){return{all:async()=>({results:await db.query(sql,args)})};}};}};const api=createElectionResults(central,{}, {authenticate:async()=>({id:randomUUID()}),access:async()=>({nodes:['web.menu.game']}),preview:async()=>Response.json({factionStatus:'verified',faction:'fremen'})});return{db,central,api,round,term,auction,close:()=>db.close()};}
|
|
test('result register returns genuine candidate totals, auction reference and resignation history without private ballots',async()=>{const f=await fixture();try{const response=await f.api(new Request('https://cp.dunerealms.com/api/politics/results/house?round='+f.round)),body=await response.json();assert.equal(response.status,200);assert.equal(body.faction,'fremen');assert.equal(body.candidates[0].votes,1);assert.equal(body.rounds[0].auction_id,f.auction);assert.equal(body.terms[0].outcome,'resigned');assert.equal(body.terms[0].round_id,f.round);const json=JSON.stringify(body);for(const field of ['private voter allocations','player_uuid','user_id','request_id'])assert.ok(!json.includes(field));}finally{f.close();}});
|
|
test('result details cannot switch faction or read a foreign round',async()=>{const f=await fixture();try{assert.equal((await f.api(new Request('https://cp.dunerealms.com/api/politics/results/house?faction=atreides'))).status,400);assert.equal((await f.api(new Request('https://cp.dunerealms.com/api/politics/results/house?round='+randomUUID()))).status,404);assert.equal((await f.api(new Request('https://cp.dunerealms.com/api/politics/results/imperial?round='+f.round))).status,404);assert.equal((await f.api(new Request('https://cp.dunerealms.com/api/politics/results/house',{method:'POST'}))).status,405);}finally{f.close();}});
|

Staff