MC/website/tests/church-election-flow.test.mjs
2026-11-27 23:43:05 +00:00

15 lines
1.8 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import {startElectionClock} from '../control-panel/election-voting.mjs';
import {ELECTION_RULES,electionScope} from '../control-panel/public/election-rules.mjs';
import {applicationElectionId} from '../control-panel/public/election-applications-view.mjs';
import {createPanel} from '../control-panel/server.mjs';
import {LocalD1} from './d1-adapter.mjs';
test('the existing election clock advances every configured office, including the Church',async()=>{
const expected=5+Object.keys(ELECTION_RULES).filter(k=>k!=='house').length,calls=[],errors=[];let complete;
const done=new Promise(r=>complete=r),stop=startElectionClock({}, {clock:()=>Date.parse('2026-11-25T12:00:00Z'),onError:e=>errors.push(e),advance:async(_db,kind,faction)=>{calls.push({kind,faction});if(calls.length===expected)complete();}});
let timer;try{await Promise.race([done,new Promise((_,reject)=>timer=setTimeout(()=>reject(Error('A configured election was omitted from the clock')),1000))]);assert.equal(calls.length,expected);for(const kind of Object.keys(ELECTION_RULES).filter(k=>k!=='house'))assert.deepEqual(calls.filter(c=>c.kind===kind),[{kind,faction:electionScope(kind)}]);assert.equal(errors.length,0);}finally{clearTimeout(timer);stop();}
});
test('Church application cards retain their own faith election identifier',()=>{assert.equal(applicationElectionId({kind:'church',faction:'muad_dib',election_start:Date.parse('2026-12-13T15:00:00Z')}),'ELC-FTH-20261213');});
test('the real panel routes Church voting through normal election authentication',async()=>{const db=new LocalD1();try{const response=await createPanel(db)(new Request('https://cp.dunerealms.com/api/politics/ballot/church'));assert.equal(response.status,401);assert.equal((await response.json()).message,'Sign in first.');}finally{db.close();}});