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

40 lines
No EOL
7.5 KiB
JavaScript

import test from 'node:test';import assert from 'node:assert/strict';import {randomUUID} from 'node:crypto';
import {clanFaithFixture} from './clan-faith-fixture.mjs';
import {currentClanFaith,createClanFaithAPI} from '../node/clan-faith.mjs';
import {CLAN_FAITH_POLICY as POLICY} from '../control-panel/public/clan-faith-policy.mjs';
import {faithQuestEvidence} from '../node/faith-quest-evidence.mjs';
test('a sealed invitation cannot change clan faith; only the legal owner can consent, idempotently',async()=>{const f=await clanFaithFixture();try{
const offer=f.offer();await f.service.offer(f.people.Owner,offer);await f.service.offer(f.people.Owner,offer);assert.equal((await currentClanFaith(f.db,f.clan)).faith,null);assert.equal((await f.db.first('SELECT COUNT(*) n FROM jarvis_faith_offers')).n,1);await assert.rejects(f.service.reply({...f.people.Owner,role:'owner'},f.reply(offer.requestId)),/legal owner/);const reply=f.reply(offer.requestId);await f.service.reply(f.people.Other,reply);await f.service.reply(f.people.Other,reply);assert.equal((await currentClanFaith(f.db,f.clan)).faith,'muad_dib');assert.equal((await f.db.first('SELECT COUNT(*) n FROM jarvis_faith_consents')).n,1);assert.equal((await f.db.first('SELECT owner_id FROM site_clans WHERE id=?',[f.clan])).owner_id,f.people.Other.id);assert.equal((await f.db.first('SELECT COUNT(*) n FROM site_clan_members WHERE clan_id=?',[f.clan])).n,2);await assert.rejects(f.service.reply(f.people.Other,{...reply,choice:'decline'}),/reference/);
}finally{f.db.close();}});
test('parallel faiths cannot stack or bypass the clan cooldown, and revocation never revives a prior faith',async()=>{const f=await clanFaithFixture();try{
const one=f.offer(),two=f.offer('BG-CONVERT');await f.service.offer(f.people.Owner,one);await f.service.offer(f.people.Ceo,two);await f.service.reply(f.people.Other,f.reply(one.requestId));await assert.rejects(f.service.reply(f.people.Other,f.reply(two.requestId)),/seven-day/);f.setTime(1000+POLICY.clanCooldownMs);const later=f.offer('BG-CONVERT');await f.service.offer(f.people.Ceo,later);const reply=f.reply(later.requestId);await f.service.reply(f.people.Other,reply);assert.equal((await currentClanFaith(f.db,f.clan)).faith,'veiled_concord');const row=await f.db.first('SELECT key_id FROM jarvis_faith_consents WHERE request_id=?',[reply.requestId]);await f.db.query('UPDATE organisation_keys SET revoked_at=1 WHERE id=?',[row.key_id]);assert.equal((await currentClanFaith(f.db,f.clan)).faith,null);
}finally{f.db.close();}});
test('faith office cooldown survives succession, an expired invitation is inert, and outlawed conversions issue nothing',async()=>{const f=await clanFaithFixture();try{
f.outlaw(true);const count=(await f.db.first('SELECT COUNT(*) n FROM organisation_keys')).n;await assert.rejects(f.service.offer(f.people.Owner,f.offer()),/outlawed/);assert.equal((await f.db.first('SELECT COUNT(*) n FROM organisation_keys')).n,count);f.outlaw(false);const offer=f.offer();await f.service.offer(f.people.Owner,offer);await assert.rejects(f.service.offer(f.people.Owner,f.offer()),/cooldown/);f.outlaw(true);await assert.rejects(f.service.reply(f.people.Other,f.reply(offer.requestId)),/outlawed/);assert.equal((await currentClanFaith(f.db,f.clan)).faith,null);f.outlaw(false);f.setTime(1000+POLICY.offerDurationMs);await assert.rejects(f.service.reply(f.people.Other,f.reply(offer.requestId)),/expired/);
f.setTime(2000);await f.db.query('UPDATE political_office_terms SET user_id=? WHERE id=?',[f.people.Member.id,f.terms.muad_dib]);await assert.rejects(f.service.offer(f.people.Member,f.offer()),/cooldown/);
}finally{f.db.close();}});
test('revoked faith authority and consent journal failures leave no partial conversion or extra key',async()=>{const f=await clanFaithFixture();try{
const offer=f.offer();await f.service.offer(f.people.Owner,offer);const before=(await f.db.first('SELECT COUNT(*) n FROM organisation_keys')).n;f.db.sqlite.exec("CREATE TRIGGER consent_failure BEFORE INSERT ON jarvis_faith_consents BEGIN SELECT RAISE(ABORT,'consent journal failed');END");await assert.rejects(f.service.reply(f.people.Other,f.reply(offer.requestId)),/journal failed/);assert.equal((await currentClanFaith(f.db,f.clan)).faith,null);assert.equal((await f.db.first('SELECT state FROM jarvis_faith_offers')).state,'open');assert.equal((await f.db.first('SELECT COUNT(*) n FROM organisation_keys')).n,before);f.db.sqlite.exec('DROP TRIGGER consent_failure');await f.db.query("UPDATE political_office_terms SET outcome='resigned' WHERE id=?",[f.terms.muad_dib]);await assert.rejects(f.service.reply(f.people.Other,f.reply(offer.requestId)),/authority changed/);assert.equal((await currentClanFaith(f.db,f.clan)).faith,null);
}finally{f.db.close();}});
test('faith API rejects cross-origin consent, account overrides and unconfirmed replies',async()=>{const f=await clanFaithFixture();try{
const api=createClanFaithAPI(f.db,f.keys,{service:f.service,authenticate:async()=>f.people.Owner,access:async()=>({nodes:['web.menu.game']})}),request=(body,origin='https://cp.dunerealms.com')=>new Request('https://cp.dunerealms.com/api/politics/faith',{method:'POST',headers:{origin,'Content-Type':'application/json'},body:JSON.stringify(body)});assert.equal((await api(request({action:'offer',...f.offer()},'https://evil.example'))).status,403);assert.equal((await api(request({action:'offer',...f.offer(),actorId:f.people.Other.id}))).status,400);assert.equal((await api(request({action:'reply',...f.reply(randomUUID()),confirmed:false}))).status,400);
}finally{f.db.close();}});
test('faith missions count current signed owner consent, not invitations, revoked proofs or former officeholders',async()=>{const f=await clanFaithFixture();try{
const uuid=randomUUID();await f.db.query('UPDATE site_game_links SET minecraft_uuid=? WHERE user_id=?',[uuid,f.people.Owner.id]);
const evidence=()=>faithQuestEvidence(f.db,{accountId:f.people.Owner.id,minecraftUuid:uuid,startsAt:0,endsAt:9999999999,now:f.clock()}),metric='pol-faith-muad_dib-conversions',offer=f.offer();
await f.service.offer(f.people.Owner,offer);assert.equal((await evidence()).totals.get(metric),0);const reply=f.reply(offer.requestId);await f.service.reply(f.people.Other,reply);assert.equal((await evidence()).totals.get(metric),1);assert.equal((await evidence()).proofIds.length,1);
await assert.rejects(faithQuestEvidence(f.db,{accountId:f.people.Member.id,minecraftUuid:uuid,startsAt:0,endsAt:9999999999,now:f.clock()}),/identity/);
await f.db.query("UPDATE political_office_terms SET outcome='resigned' WHERE id=?",[f.terms.muad_dib]);assert.equal((await evidence()).totals.get(metric),0);await f.db.query("UPDATE political_office_terms SET outcome='winner' WHERE id=?",[f.terms.muad_dib]);
await f.db.query('UPDATE organisation_keys SET revoked_at=? WHERE id=(SELECT key_id FROM jarvis_faith_consents WHERE request_id=?)',[f.clock(),reply.requestId]);assert.equal((await evidence()).totals.get(metric),0);
}finally{f.db.close();}});
test('native selectable clans exclude an outlawed faith, a current follower, lost authority and cooldown',async()=>{const f=await clanFaithFixture();try{
const list=()=>f.service.eligibleClans(f.people.Owner,'muad_dib');
assert.deepEqual((await list()).map(c=>c.id),[f.clan]);
assert.deepEqual(await f.service.eligibleClans(f.people.Member,'muad_dib'),[]);
f.outlaw(true);assert.deepEqual(await list(),[]);f.outlaw(false);
const offer=f.offer();await f.service.offer(f.people.Owner,offer);
assert.deepEqual(await list(),[]);await f.service.reply(f.people.Other,f.reply(offer.requestId));
f.setTime(1000+POLICY.officeCooldownMs);assert.deepEqual(await list(),[]);
}finally{f.db.close();}});