Repair Church candidate identifiers, ballot routing and automatic election advancement
This commit is contained in:
parent
d5564d0db0
commit
05965ac941
6 changed files with 25 additions and 5 deletions
|
|
@ -21,7 +21,7 @@ export async function initializeApplications(db) {
|
|||
}
|
||||
export function applicationPrefix(kind,faction){return kind==='church'?'FTH':kind==='guild'?'GLD':kind==='clan_delegate'?'CLN':kind==='imperial'?'IMP':faction==='veiled_concord'?'CON':'HSE';}
|
||||
export function applicationNumber(prefix,year,value){
|
||||
if(!['IMP','CON','HSE','GLD','CLN'].includes(prefix)||!Number.isInteger(year)||year<2026||!Number.isInteger(value)||value<1||value>999999)throw Error('Application number range exhausted or invalid');
|
||||
if(!['IMP','CON','FTH','HSE','GLD','CLN'].includes(prefix)||!Number.isInteger(year)||year<2026||!Number.isInteger(value)||value<1||value>999999)throw Error('Application number range exhausted or invalid');
|
||||
return `APP-${prefix}-${year}-${String(value).padStart(6,'0')}`;
|
||||
}
|
||||
// Transactional repository, not a publicly mounted route. Call only with an
|
||||
|
|
|
|||
|
|
@ -19,9 +19,9 @@ export async function advanceElection(db,kind,faction,now=Date.now()){
|
|||
}
|
||||
throw Error('Election round reconciliation requires attention');
|
||||
}
|
||||
export function startElectionClock(db,{clock=Date.now,onError=()=>console.error('Election clock failed')}={}){
|
||||
export function startElectionClock(db,{clock=Date.now,onError=()=>console.error('Election clock failed'),advance=advanceElection}={}){
|
||||
let busy=false,stopped=false;
|
||||
const tick=async()=>{if(busy||stopped)return;busy=true;try{const now=clock();for(const faction of houses)await advanceElection(db,'house',faction,now);for(const kind of ['imperial','bene','guild','clan_delegate','naib_chair'])await advanceElection(db,kind,electionScope(kind),now);}catch(error){onError(error);}finally{busy=false;}};
|
||||
const tick=async()=>{if(busy||stopped)return;busy=true;try{const now=clock();for(const faction of houses)await advance(db,'house',faction,now);for(const kind of Object.keys(ELECTION_RULES).filter(kind=>kind!=='house'))await advance(db,kind,electionScope(kind),now);}catch(error){onError(error);}finally{busy=false;}};
|
||||
const timer=setInterval(tick,15000);timer.unref?.();void tick();return()=>{stopped=true;clearInterval(timer);};
|
||||
}
|
||||
export function createElectionVoting(db,settings={},deps={}){
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import {electionDates,electionWindow} from './election-rules.mjs';
|
||||
const node=(tag,text,cls)=>{const e=document.createElement(tag);if(text!==undefined)e.textContent=text;if(cls)e.className=cls;return e;};
|
||||
const date=value=>new Date(Number(value)).toLocaleString('en-GB',{timeZone:'UTC',dateStyle:'medium',timeStyle:'short'})+' GMT';
|
||||
export function applicationElectionId(row){const code=row.kind==='guild'?'GLD':row.kind==='clan_delegate'?'CLN':row.kind==='imperial'?'IMP':row.faction==='veiled_concord'?'CON':'HSE';return 'ELC-'+code+'-'+new Date(Number(row.election_start)).toISOString().slice(0,10).replaceAll('-','');}
|
||||
export function applicationElectionId(row){const code=row.kind==='church'?'FTH':row.kind==='guild'?'GLD':row.kind==='clan_delegate'?'CLN':row.kind==='imperial'?'IMP':row.faction==='veiled_concord'?'CON':'HSE';return 'ELC-'+code+'-'+new Date(Number(row.election_start)).toISOString().slice(0,10).replaceAll('-','');}
|
||||
export function ensureElectionRecordStyles(){if(!document.querySelector('link[data-election-records]')){const link=node('link');link.rel='stylesheet';link.href=new URL('./cp-v2/cp-v2-election-records.css',import.meta.url).href;link.dataset.electionRecords='';document.head.append(link);}}
|
||||
export function renderApplications({user,context,kind:onlyKind=null}){
|
||||
ensureElectionRecordStyles();
|
||||
|
|
|
|||
|
|
@ -382,7 +382,7 @@ export function createPanel(db,settings={},dependencies={}){
|
|||
if(settings.POSTBANK_ENABLED==='true'&&path.startsWith('/api/bank/'))return postbank(request);
|
||||
if(settings.POSTBANK_ENABLED==='true'&&path==='/api/v1/me/bank')return json({message:'The game-linked bank has been retired from this website.'},410);
|
||||
if(path==='/api/politics/directory'){const response=await electionDirectory(request);if(request.method==='GET'&&response.ok&&progressionRuntime){try{const visitor=await identity(request,db);if(visitor)await progressionRuntime.recordPoliticalVisit(visitor);}catch{}}return response;}
|
||||
if(['/api/politics/ballot/house','/api/politics/ballot/imperial','/api/politics/ballot/bene','/api/politics/ballot/guild','/api/politics/ballot/clan_delegate','/api/politics/ballot/naib_chair'].includes(path))return electionVoting(request);
|
||||
if(['/api/politics/ballot/house','/api/politics/ballot/imperial','/api/politics/ballot/bene','/api/politics/ballot/church','/api/politics/ballot/guild','/api/politics/ballot/clan_delegate','/api/politics/ballot/naib_chair'].includes(path))return electionVoting(request);
|
||||
if(path==='/api/account/character')return characterChoice(request);
|
||||
if(path==='/internal/game/house')return gameHouseSelection(request);
|
||||
if(path==='/internal/game/native-state')return gameNativeState(request);
|
||||
|
|
|
|||
15
website/tests/church-election-flow.test.mjs
Normal file
15
website/tests/church-election-flow.test.mjs
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
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();}});
|
||||
|
|
@ -94,6 +94,11 @@ test('chair follows the existing candidature, ballot, result and deed flow; losi
|
|||
assert.equal((await reconcileNaibChairs(f.central,{clock:()=>closed+1})).ended,1);assert.equal((await reconcileNaibChairs(f.central,{clock:()=>closed+2})).ended,0);assert.equal((await f.db.first('SELECT outcome FROM political_office_terms WHERE id=?',[term.id])).outcome,'ineligible');assert.equal((await f.db.first('SELECT COUNT(*) n FROM political_succession_flows')).n,1);
|
||||
}finally{f.close();}
|
||||
});
|
||||
|
||||
test('Church candidatures complete the shared application, ballot and Doci office deed flow',async()=>{const f=await fixture();try{
|
||||
const [candidate,voter]=f.people,start='2026-12-13T15:00:00.000Z',opens=Date.parse(start),before=opens-86400000,context={linked:true,playerUuid:candidate.player,faction:'atreides'},application=await submitApplication(f.central,{actor:candidate,context,input:{kind:'church',start,accepted:true,motivation:'Represent the faithful without changing their House or family.'},now:before,clock:()=>before});assert.match(application.publicId,/^APP-FTH-/);
|
||||
const round=await openFirstRound(f.central,{kind:'church',faction:'muad_dib',start,now:opens}),input={kind:'church',start,roundId:round.id,requestId:randomUUID(),confirmed:true,allocations:[{applicationId:application.id,votes:1}]};await castBallot(f.central,{actor:voter,context:{linked:true,playerUuid:voter.player,faction:'harkonnen'},input,now:opens+1,clock:()=>opens+1});const closed=opens+10800000;await finalizeRound(f.central,{roundId:round.id,now:closed});const term=await reconcileOfficeResult(f.central,{roundId:round.id,now:closed});assert.equal(term.office,'doci');assert.equal(term.kind,'church');assert.equal(term.faction,'muad_dib');assert.equal(term.userId,candidate.id);assert.equal((await f.db.first('SELECT COUNT(*) n FROM political_office_terms')).n,1);
|
||||
}finally{f.close();}});
|
||||
test('chair candidatures and vote capacity require a current Naib context without changing Fremen leadership',()=>{
|
||||
const now=Date.parse('2026-11-18T00:00:00Z'),input={kind:'naib_chair',start:electionDates('naib_chair',now,1)[0],motivation:'Represent every sietch fairly in the Council.',accepted:true};
|
||||
assert.throws(()=>validateCandidature(input,{linked:true,clanId:randomUUID()},now),/ownership/);
|
||||
|
|
|
|||
Loading…
Reference in a new issue

Staff