import test from 'node:test';import assert from 'node:assert/strict';import {randomUUID} from 'node:crypto'; import {FAITH_POL,faithAppointmentRole} from '../control-panel/public/faith-pol-policy.mjs'; import {electionDates,electionScope,validateCandidature} from '../control-panel/public/election-rules.mjs'; import {PUBLIC_OFFICES,projectGovernment} from '../node/public-government.mjs'; import {UNIVERSAL_REBELLION_QUESTS,rebellionQuest} from '../node/rebellion-quest-policy.mjs'; import {planOfficeTerm,officeLedgerSchema} from '../control-panel/election-office-ledger.mjs'; import {createPoliticalAppointments,POLITICAL_APPOINTMENT_SCHEMA,activePoliticalAppointments} from '../node/political-appointments.mjs'; import {BankFixture} from './postbank-fixture.mjs'; import {fixture as rebellionFixture} from './rebellion-fixture.mjs'; test('parallel faith offices use the same election calendar, independent scopes, and distinct lore roles',()=>{ const now=Date.parse('2026-11-22T00:00:00Z');assert.deepEqual(electionDates('church',now),electionDates('bene',now)); assert.equal(electionScope('church'),'muad_dib');assert.equal(electionScope('bene'),'veiled_concord'); const candidate=validateCandidature({kind:'church',accepted:true,start:electionDates('church',now,1)[0],motivation:'I will serve the people of this faith.'},{linked:true},now); assert.equal(candidate.faction,'muad_dib');assert.deepEqual(FAITH_POL.muad_dib.roles,{officeholder:'Doci',officer:'Prior',member:'Worshipper'}); assert.equal(faithAppointmentRole('muad_dib','Prior'),'officer');assert.equal(faithAppointmentRole('veiled_concord','Sister'),'member');assert.equal(faithAppointmentRole('muad_dib','Doci'),null); }); test('Doci is created by the ordinary election ledger and projects an independent active office',()=>{ const start=Date.parse('2026-11-15T15:00:00Z'),winner=randomUUID(),user=randomUUID(),player=randomUUID(),round={kind:'church',faction:'muad_dib',status:'winner',election_start:start,closes_at:start+10800000,winner_id:winner},app={id:winner,kind:'church',faction:'muad_dib',election_start:start,user_id:user,player_uuid:player}; const term=planOfficeTerm(round,app,null,start+10800000);assert.equal(term.office,'doci');assert.equal(term.expiresAt,start+28*86400000); const offices=projectGovernment([{...round,office:term.office,outcome:'winner',starts_at:term.startsAt,expires_at:term.expiresAt,username:'faith_leader',display_name:'Faith Leader'}],term.startsAt+1); assert.equal(offices.find(o=>o.key==='muad_dib').holder.username,'faith_leader');assert.equal(offices.find(o=>o.key==='veiled_concord').holder,null); }); test('both faith offices deny direct rebellion and never appear in universal war quests',async()=>{ for(const key of ['veiled_concord','muad_dib']){ assert.equal(PUBLIC_OFFICES.find(o=>o.key===key).challengeable,false);assert.throws(()=>rebellionQuest(key),/cannot be taken/);assert.equal(UNIVERSAL_REBELLION_QUESTS.some(q=>q.officeKey===key),false); const f=await rebellionFixture();try{await assert.rejects(f.service.declare(f.winner,{...f.input,officeKey:key}),/cannot be taken/);assert.equal((await f.db.first('SELECT COUNT(*) n FROM jarvis_generated_instructions')).n,0);}finally{f.db.close();} } }); test('Prior and Worshipper reuse ordinary appointment deeds and expire with the Doci term',async()=>{ const db=new BankFixture(),owner={id:randomUUID()},member={id:randomUUID()},term=randomUUID(); try{ for(const sql of [...officeLedgerSchema,...POLITICAL_APPOINTMENT_SCHEMA])await db.query(sql.replace(/ ENGINE=InnoDB(?: DEFAULT CHARSET=utf8mb4)?/g,'').replace(/UNIQUE KEY \w+\(/g,'UNIQUE(')); db.sqlite.exec('CREATE TABLE site_accounts(id TEXT PRIMARY KEY,username TEXT,display_name TEXT,password_hash TEXT)'); for(const person of [owner,member])await db.query("INSERT INTO site_accounts VALUES(?,?,?,'hash')",[person.id,person.id,person.id]); await db.query("INSERT INTO political_office_terms(id,kind,faction,election_start,round_id,user_id,office,starts_at,expires_at,outcome,streak) VALUES(?,'church','muad_dib',0,?,?,'doci',0,10000,'winner',1)",[term,randomUUID(),owner.id]); const service=createPoliticalAppointments(db,{clock:()=>1000}); for(const title of ['Prior','Worshipper'])await service.appoint(owner,{officeKey:'muad_dib',userId:member.id,requestId:randomUUID(),title,type:'job',expiresAt:20000}); const roles=await activePoliticalAppointments(db,{clock:()=>1000});assert.deepEqual(new Set(roles.map(r=>r.role_key)),new Set(['officer','member']));assert(roles.every(r=>r.expires_at===10000)); await db.query("UPDATE political_office_terms SET outcome='resigned' WHERE id=?",[term]);assert.equal((await activePoliticalAppointments(db,{clock:()=>1000})).length,0); }finally{db.close();} });