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

31 lines
5.2 KiB
JavaScript

import test from 'node:test';import assert from 'node:assert/strict';import {randomUUID} from 'node:crypto';
import {companyFixture} from './company-deed-fixture.mjs';
import {STAFF_OFFICE_ACCOUNT_ID} from '../node/organisation-keys.mjs';
import {IMPERIAL_PASSAGE_SCHEMA,createImperialPassage,hasImperialPassage,createImperialPassageAPI} from '../node/imperial-passage.mjs';
import {IMPERIAL_PASSAGE_COOLDOWN_MS,passageHouses} from '../control-panel/public/imperial-passage-policy.mjs';
async function fixture(){const f=await companyFixture();await f.db.query(IMPERIAL_PASSAGE_SCHEMA);f.db.sqlite.exec('ALTER TABLE political_office_terms ADD COLUMN id TEXT;ALTER TABLE political_office_terms ADD COLUMN kind TEXT;CREATE TABLE political_office_charters(office_key TEXT PRIMARY KEY,charter_kind TEXT,staff_key_id TEXT);');
const grant=await f.keys.issue({accountId:STAFF_OFFICE_ACCOUNT_ID,keyType:'staff-office-royal',scopeId:'office:emperor',authorize:async()=>true});await f.keys.consume({accountId:STAFF_OFFICE_ACCOUNT_ID,keyType:'staff-office-royal',code:grant.code,targetId:randomUUID(),requestId:randomUUID(),apply:async(tx,{keyId})=>tx.query("INSERT INTO political_office_charters VALUES('emperor','royal',?)",[keyId])});
await f.db.query("INSERT INTO political_office_terms(id,user_id,kind,faction,office,outcome,starts_at,expires_at) VALUES(?,?,'imperial','imperium','emperor','winner',0,?)",[randomUUID(),f.people.Owner.id,IMPERIAL_PASSAGE_COOLDOWN_MS*10]);let now=1000;
return{...f,service:createImperialPassage(f.db,f.keys,{clock:()=>now}),setTime:v=>now=v};}
test('native House selection replaces prior grant, retries are idempotent and cooldown opens exactly after seven days',async()=>{const f=await fixture();try{
const input={requestId:randomUUID(),houses:['atreides']};await f.service.grant(f.people.Owner,input);assert.equal(await hasImperialPassage(f.db,'atreides'),true);assert.equal(await hasImperialPassage(f.db,'corrino'),false);assert.equal((await f.service.read(f.people.Owner)).canExecute,false);
await f.service.grant(f.people.Owner,input);assert.equal((await f.db.first('SELECT COUNT(*) n FROM jarvis_imperial_passage_orders')).n,1);
await assert.rejects(f.service.grant(f.people.Owner,{...input,houses:['ordos']}),/reference/);f.setTime(1000+IMPERIAL_PASSAGE_COOLDOWN_MS-1);await assert.rejects(f.service.grant(f.people.Owner,{requestId:randomUUID(),houses:['corrino']}),/seven days/);
f.setTime(1000+IMPERIAL_PASSAGE_COOLDOWN_MS);await f.service.grant(f.people.Owner,{requestId:randomUUID(),houses:['corrino']});assert.equal(await hasImperialPassage(f.db,'atreides'),false);assert.equal(await hasImperialPassage(f.db,'corrino'),true);
const state=await f.service.read(f.people.Owner);await f.db.query('UPDATE organisation_keys SET revoked_at=1 WHERE id=?',[state.keyId]);assert.equal(await hasImperialPassage(f.db,'corrino'),false);assert.equal(await hasImperialPassage(f.db,'atreides'),false);
}finally{f.db.close();}});
test('office authority cannot be replaced by staff status or organisation targets',async()=>{const f=await fixture();try{
await assert.rejects(f.service.grant({...f.people.Other,role:'owner'},{requestId:randomUUID(),houses:['atreides']}),/current Emperor/);
for(const houses of [['clan'],['spacing_guild'],[randomUUID()],['atreides','atreides'],['atreides','harkonnen']])await assert.rejects(f.service.grant(f.people.Owner,{requestId:randomUUID(),houses}),/native Houses/);
await f.db.query('UPDATE political_office_terms SET expires_at=500');await assert.rejects(f.service.grant(f.people.Owner,{requestId:randomUUID(),houses:[]}),/current Emperor/);assert.throws(()=>passageHouses(null),/native Houses/);
}finally{f.db.close();}});
test('a historical multi-House record stays archived but cannot authorize an arbitrary House',async()=>{const f=await fixture();try{
await f.service.grant(f.people.Owner,{requestId:randomUUID(),houses:['atreides']});
await f.db.query('UPDATE jarvis_imperial_passage_orders SET houses_json=?',[JSON.stringify(['atreides','harkonnen'])]);
const prior=await f.service.read(f.people.Owner);assert.equal(prior.requiresReview,true);assert.deepEqual(prior.houses,[]);assert.equal(prior.canExecute,false);
assert.equal(await hasImperialPassage(f.db,'atreides'),false);assert.equal(await hasImperialPassage(f.db,'harkonnen'),false);
assert.equal((await f.db.first('SELECT COUNT(*) n FROM jarvis_imperial_passage_orders')).n,1);
}finally{f.db.close();}});
test('web API rejects foreign origin, identity overrides and unauthenticated writes',async()=>{const f=await fixture();try{const api=createImperialPassageAPI(f.db,f.keys,{authenticate:async()=>f.people.Owner,access:async()=>({nodes:['web.menu.game']}),clock:()=>1000}),request=(origin,body)=>new Request('https://cp.dunerealms.com/api/politics/arrakis-passage',{method:'POST',headers:{origin,'content-type':'application/json'},body:JSON.stringify(body)});
assert.equal((await api(request('https://evil.invalid',{requestId:randomUUID(),houses:[]}))).status,403);assert.equal((await api(request('https://cp.dunerealms.com',{requestId:randomUUID(),houses:[],actorId:f.people.Owner.id}))).status,400);const guest=createImperialPassageAPI(f.db,f.keys,{authenticate:async()=>null});assert.equal((await guest(request('https://cp.dunerealms.com',{}))).status,401);
}finally{f.db.close();}});