diff --git a/website/control-panel/public/cp-v2/cp-v2-home-relocation.mjs b/website/control-panel/public/cp-v2/cp-v2-home-relocation.mjs index 1d11fec8..b826e7d4 100644 --- a/website/control-panel/public/cp-v2/cp-v2-home-relocation.mjs +++ b/website/control-panel/public/cp-v2/cp-v2-home-relocation.mjs @@ -1,6 +1,7 @@ import {element as el} from './cp-v2-page-template.mjs'; const money=v=>{const n=BigInt(v);return(n/100n)+'.'+String(n%100n).padStart(2,'0')+' DM';}; -export function createHomeRelocationControl({request,kind,id,onApplied}){ +export function createHomeRelocationControl({request,kind,id:initialId,onApplied}){ + let id=initialId; const root=el('details',undefined,'claims-card claims-reference');root.hidden=true;root.append(el('summary','Move Home Block')); const text=el('p','Moving releases the old estate. Your charter, members, accounts and history stay with the organisation. The fee is 10% of its treasury; Basic uses the owner’s personal accounts.'); const world=el('select'),account=el('select'),worldLabel=el('label','Destination world'),accountLabel=el('label','Pay from'),review=el('button','Review relocation'),confirm=el('input'),consent=el('label'),submit=el('button','Confirm relocation'),status=el('p'); @@ -18,5 +19,5 @@ export function createHomeRelocationControl({request,kind,id,onApplied}){ submit.onclick=async()=>{if(busy||!quote||!confirm.checked)return;busy=true;submit.disabled=true;review.disabled=true;try{ const result=await request('/api/v1/claims/home-relocation',{kind,id,worldKey:quote.worldKey,requestId,quoteHash:quote.quoteHash,sourceAccount:account.value,confirmed:true});status.textContent=pending(result);quote=null;consent.hidden=true;accountLabel.hidden=true;submit.hidden=true;await onApplied?.(); }catch(e){status.textContent=e.message;}finally{busy=false;review.disabled=false;submit.disabled=!confirm.checked;}}; - return{root,setState(data){root.hidden=!id||!['clan','company'].includes(kind)||data.canUpgrade!==true;if(!world.options.length)for(const w of data.worlds||[]){const option=el('option',w.name);option.value=w.key;world.append(option);}if(kind==='clan')worldLabel.hidden=true;}}; + return{root,setState(data){const resolved=data[kind]?.id;if(resolved&&id!==resolved){id=resolved;reset();}root.hidden=!id||!['clan','company'].includes(kind)||data.canUpgrade!==true;if(!world.options.length)for(const w of data.worlds||[]){const option=el('option',w.name);option.value=w.key;world.append(option);}if(kind==='clan')worldLabel.hidden=true;}}; } diff --git a/website/node/game-native-state.mjs b/website/node/game-native-state.mjs index 40edaaa4..f267b548 100644 --- a/website/node/game-native-state.mjs +++ b/website/node/game-native-state.mjs @@ -1,6 +1,6 @@ import {createHash} from 'node:crypto'; import {gameAuthorized,json} from './game-link.mjs'; -import {readClanDeed} from './clan-deeds.mjs'; +import {readClanDeed,clanDeedAuthority} from './clan-deeds.mjs'; import {readCompanyDeed} from './company-deeds.mjs'; import {boardReadStore,createBoardDeedResolver} from './board-deed-source.mjs'; import {readPassiveOrder} from './political-passive-orders.mjs'; @@ -15,6 +15,7 @@ import {registeredClaimChunks} from './game-claim-expansion.mjs'; const digest=value=>createHash('sha256').update(JSON.stringify(value)).digest('hex'); const bounded=(rows,max)=>{if(rows.length>max)throw Error('Native inventory exceeds supported batch');return rows;}; +export function nativeClanControllerIds(deed,authority){return [...new Set([deed.owner?.id,...(deed.root===deed.clan.id?[deed.heir?.id]:[]),...(authority.parentControllerIds||[])].filter(Boolean))].sort();} // A consumer view of current deeds/keys, not a new organisation or permission store. // Initial creation and later edits/deletions use this same projection. export async function readGameNativeState(db,{clock=Date.now}={}){ @@ -31,7 +32,8 @@ export async function readGameNativeState(db,{clock=Date.now}={}){ try{ const deed=await readClanDeed(reader,row.id);if(!deed.owner&&!row.institution_key)throw Error('Unresolved owner'); const p=parent('clan',row.id)||(deed.clan.parent_clan_id?{kind:'clan',id:deed.clan.parent_clan_id,keyId:null}:null); - const record={id:row.id,name:deed.clan.name,tag:deed.clan.tag,description:deed.clan.description,lifecycle:'active',protected:!!row.institution_key,hidden:isClandestineInstitution('clan',row.id),version:deed.version,parent:p,members:roster(deed.candidates.filter(m=>m.clan_id===row.id)),source:{kind:'clan-deed',id:row.id}}; + const authority=await clanDeedAuthority(reader,deed.owner?.id||'',row.id),managers=nativeClanControllerIds(deed,authority).filter(id=>people.has(id)).map(id=>people.get(id)); + const record={id:row.id,name:deed.clan.name,tag:deed.clan.tag,description:deed.clan.description,lifecycle:'active',protected:!!row.institution_key,hidden:isClandestineInstitution('clan',row.id),version:deed.version,parent:p,members:roster(deed.candidates.filter(m=>m.clan_id===row.id)),managers,source:{kind:'clan-deed',id:row.id}}; record.homeServer=nativeServer(deed.clan.home_world);record.fingerprint=digest(record);clans.push(record); }catch{review.push({kind:'clan',id:row.id});} } diff --git a/website/tests/game-native-state.test.mjs b/website/tests/game-native-state.test.mjs index 046b25b1..3b8b90a1 100644 --- a/website/tests/game-native-state.test.mjs +++ b/website/tests/game-native-state.test.mjs @@ -1,6 +1,12 @@ import test from 'node:test';import assert from 'node:assert/strict'; -import {createGameNativeStateAPI} from '../node/game-native-state.mjs'; +import {createGameNativeStateAPI,nativeClanControllerIds} from '../node/game-native-state.mjs'; import {fixture} from './rebellion-fixture.mjs'; +test('native build controllers follow the current mother deed without changing the child roster',()=>{ + const child={clan:{id:'child'},root:'mother',owner:{id:'child-owner'},heir:{id:'child-heir'}}; + assert.deepEqual(nativeClanControllerIds(child,{parentControllerIds:['mother-owner','mother-heir','mother-owner']}),['child-owner','mother-heir','mother-owner']); + assert.deepEqual(nativeClanControllerIds({...child,root:'child'},{parentControllerIds:[]}),['child-heir','child-owner']); + assert.equal(child.heir.id,'child-heir'); +}); test('native state uses the existing private game credential, never browser fields or arbitrary commands',async()=>{ const secret='x'.repeat(40);let reads=0;const route=createGameNativeStateAPI({}, {GAME_LINK_SECRET:secret},{read:async()=>{reads++;return {ok:true};}}); const request=(body={action:'read'},auth='Bearer '+secret)=>new Request('https://cp.dunerealms.com/internal/game/native-state',{method:'POST',headers:{authorization:auth,'content-type':'application/json'},body:JSON.stringify(body)}); diff --git a/website/tests/home-relocation-control.test.mjs b/website/tests/home-relocation-control.test.mjs new file mode 100644 index 00000000..ec352c56 --- /dev/null +++ b/website/tests/home-relocation-control.test.mjs @@ -0,0 +1,18 @@ +import test from 'node:test';import assert from 'node:assert/strict'; +import {createHomeRelocationControl} from '../control-panel/public/cp-v2/cp-v2-home-relocation.mjs'; +class Element{ + constructor(tag){this.tagName=tag.toUpperCase();this.children=[];this.textContent='';} + append(...items){this.children.push(...items);}replaceChildren(...items){this.children=items;}setAttribute(){} + get options(){return this.children;}get value(){return this.selectedValue??(this.tagName==='SELECT'?this.children[0]?.value||'':'');}set value(value){this.selectedValue=value;} +} +test('own-clan relocation resolves the server-provided clan ID and discards a reviewed quote when context changes',async()=>{ + const previous=globalThis.document;globalThis.document={createElement:tag=>new Element(tag)}; + try{ + const calls=[],id='aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa',other='bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb'; + const control=createHomeRelocationControl({kind:'clan',id:null,request:async url=>{calls.push(url);return{worlds:[{key:'kaladam',name:'Kaladam'}],worldKey:'kaladam',fromWorld:'Arrakduin',worldName:'Kaladam',accounts:[{id:'1',name:'Current',number:'DR1',availableMinor:'1000'}],feeMinor:'100',funding:'Owner personal accounts',quoteHash:'x'};}}); + control.setState({clan:{id},canUpgrade:true,worlds:[]});assert.equal(control.root.hidden,false); + const buttons=control.root.children.filter(e=>e.tagName==='BUTTON');await buttons[0].onclick();assert.equal(new URL(calls[0],'https://cp.dunerealms.com').searchParams.get('id'),id); + assert.equal(buttons[1].hidden,false);control.setState({clan:{id:other},canUpgrade:true,worlds:[]});assert.equal(buttons[1].hidden,true);assert.equal(buttons[1].disabled,true); + await buttons[0].onclick();assert.equal(new URL(calls[1],'https://cp.dunerealms.com').searchParams.get('id'),other); + }finally{globalThis.document=previous;} +});